Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pod-uid support for add_kubernetes_metadata #7072

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ https://github.com/elastic/beats/compare/v6.2.3...master[Check the HEAD diff]
execve, execveat, fork, and vfork syscalls. A custom policy can be configured. {issue}5213[5213]
- Update Sarama to v1.16.0, adding better support for kafka 0.11, 1.0, and 1.1 {pull}7025[7025]
- Ship fields.yml as part of the binary {pull}4834[4834]
- Add Indexer indexing by pod uid. Enable pod uid metadata gathering in add_kubernetes_metadata. Extended Matcher log_path matching to support volume mounts {pull}7072[7072]

*Auditbeat*

Expand Down
10 changes: 10 additions & 0 deletions auditbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -3296,6 +3296,16 @@ type: keyword
Kubernetes pod name


--

*`kubernetes.pod.uid`*::
+
--
type: keyword

Kubernetes pod uid


--

*`kubernetes.namespace`*::
Expand Down
2 changes: 1 addition & 1 deletion auditbeat/include/fields.go

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions filebeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,16 @@ type: keyword
Kubernetes pod name


--

*`kubernetes.pod.uid`*::
+
--
type: keyword

Kubernetes pod uid


--

*`kubernetes.namespace`*::
Expand Down
2 changes: 1 addition & 1 deletion filebeat/include/fields.go

Large diffs are not rendered by default.

66 changes: 45 additions & 21 deletions filebeat/processor/add_kubernetes_metadata/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ func init() {
const LogPathMatcherName = "logs_path"

type LogPathMatcher struct {
LogsPath string
LogsPath string
ResourceType string
}

func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, error) {
config := struct {
LogsPath string `config:"logs_path"`
LogsPath string `config:"logs_path"`
ResourceType string `config:"resource_type"`
}{
LogsPath: "/var/lib/docker/containers/",
LogsPath: "/var/lib/docker/containers/",
ResourceType: "container",
}

err := cfg.Unpack(&config)
Expand All @@ -42,15 +45,20 @@ func newLogsPathMatcher(cfg common.Config) (add_kubernetes_metadata.Matcher, err
if logPath[len(logPath)-1:] != "/" {
logPath = logPath + "/"
}
resourceType := config.ResourceType

logp.Debug("kubernetes", "logs_path matcher log path: %s", logPath)
logp.Debug("kubernetes", "logs_path matcher resource type: %s", resourceType)

return &LogPathMatcher{LogsPath: logPath}, nil
return &LogPathMatcher{LogsPath: logPath, ResourceType: resourceType}, nil
}

// Docker container ID is a 64-character-long hexadecimal string
const containerIdLen = 64

// Pod UID is on the 5th index of the path directories
const podUIDPos = 5

func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string {
if value, ok := event["source"]; ok {
source := value.(string)
Expand All @@ -64,24 +72,40 @@ func (f *LogPathMatcher) MetadataIndex(event common.MapStr) string {
sourceLen := len(source)
logsPathLen := len(f.LogsPath)

// In case of the Kubernetes log path "/var/log/containers/",
// the container ID will be located right before the ".log" extension.
if strings.HasPrefix(f.LogsPath, "/var/log/containers/") && strings.HasSuffix(source, ".log") && sourceLen >= containerIdLen+4 {
containerIdEnd := sourceLen - 4
cid := source[containerIdEnd-containerIdLen : containerIdEnd]
logp.Debug("kubernetes", "Using container id: %s", cid)
return cid
}

// In any other case, we assume the container ID will follow right after the log path.
// However we need to check the length to prevent "slice bound out of range" runtime errors.
if sourceLen >= logsPathLen+containerIdLen {
cid := source[logsPathLen : logsPathLen+containerIdLen]
logp.Debug("kubernetes", "Using container id: %s", cid)
return cid
if f.ResourceType == "pod" {
// Specify a pod resource type when manually mounting log volumes and they end up under "/var/lib/kubelet/pods/"
// This will extract only the pod UID, which offers less granularity of metadata when compared to the container ID
if strings.HasPrefix(f.LogsPath, "/var/lib/kubelet/pods/") && strings.HasSuffix(source, ".log") {
pathDirs := strings.Split(source, "/")
if len(pathDirs) > podUIDPos {
podUID := strings.Split(source, "/")[podUIDPos]

logp.Debug("kubernetes", "Using pod uid: %s", podUID)
return podUID
}

logp.Debug("kubernetes", "Error extracting pod uid - source value contains matcher's logs_path, however it is too short to contain a Pod UID.")
}
} else {
// In case of the Kubernetes log path "/var/log/containers/",
// the container ID will be located right before the ".log" extension.
if strings.HasPrefix(f.LogsPath, "/var/log/containers/") && strings.HasSuffix(source, ".log") && sourceLen >= containerIdLen+4 {
containerIDEnd := sourceLen - 4
cid := source[containerIDEnd-containerIdLen : containerIDEnd]
logp.Debug("kubernetes", "Using container id: %s", cid)
return cid
}

// In any other case, we assume the container ID will follow right after the log path.
// However we need to check the length to prevent "slice bound out of range" runtime errors.
if sourceLen >= logsPathLen+containerIdLen {
cid := source[logsPathLen : logsPathLen+containerIdLen]
logp.Debug("kubernetes", "Using container id: %s", cid)
return cid
}

logp.Debug("kubernetes", "Error extracting container id - source value contains matcher's logs_path, however it is too short to contain a Docker container ID.")
}

logp.Debug("kubernetes", "Error extracting container id - source value contains matcher's logs_path, however it is too short to contain a Docker container ID.")
}

return ""
Expand Down
29 changes: 28 additions & 1 deletion filebeat/processor/add_kubernetes_metadata/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
// A random container ID that we use for our tests
const cid = "0069869de9adf97f574c62029aeba65d1ecd85a2a112e87fbc28afe4dec2b843"

// A random pod UID that we use for our tests
const puid = "005f3b90-4b9d-12f8-acf0-31020a840133"

func TestLogsPathMatcher_InvalidSource1(t *testing.T) {
cfgLogsPath := "" // use the default matcher configuration
source := "/var/log/messages"
Expand Down Expand Up @@ -54,18 +57,42 @@ func TestLogsPathMatcher_AnotherLogDir(t *testing.T) {
executeTest(t, cfgLogsPath, source, expectedResult)
}

func TestLogsPathMatcher_VarLibKubeletPods(t *testing.T) {
cfgLogsPath := "/var/lib/kubelet/pods/"
cfgResourceType := "pod"
source := fmt.Sprintf("/var/lib/kubelet/pods/%s/volumes/kubernetes.io~empty-dir/applogs/server.log", puid)
expectedResult := puid
executeTestWithResourceType(t, cfgLogsPath, cfgResourceType, source, expectedResult)
}

func TestLogsPathMatcher_InvalidSource4(t *testing.T) {
cfgLogsPath := "/var/lib/kubelet/pods/"
cfgResourceType := "pod"
source := fmt.Sprintf("/invalid/dir/%s/volumes/kubernetes.io~empty-dir/applogs/server.log", puid)
expectedResult := ""
executeTestWithResourceType(t, cfgLogsPath, cfgResourceType, source, expectedResult)
}

func executeTest(t *testing.T, cfgLogsPath string, source string, expectedResult string) {
executeTestWithResourceType(t, cfgLogsPath, "", source, expectedResult)
}

func executeTestWithResourceType(t *testing.T, cfgLogsPath string, cfgResourceType string, source string, expectedResult string) {
var testConfig = common.NewConfig()
if cfgLogsPath != "" {
testConfig.SetString("logs_path", -1, cfgLogsPath)
}

if cfgResourceType != "" {
testConfig.SetString("resource_type", -1, cfgResourceType)
}

logMatcher, err := newLogsPathMatcher(*testConfig)
assert.Nil(t, err)

input := common.MapStr{
"source": source,
}
output := logMatcher.MetadataIndex(input)
assert.Equal(t, output, expectedResult)
assert.Equal(t, expectedResult, output)
}
10 changes: 10 additions & 0 deletions heartbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,16 @@ type: keyword
Kubernetes pod name


--

*`kubernetes.pod.uid`*::
+
--
type: keyword

Kubernetes pod uid


--

*`kubernetes.namespace`*::
Expand Down
2 changes: 1 addition & 1 deletion heartbeat/include/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ func init() {

// Asset returns asset data
func Asset() string {
return "eJzsW99z27gRfvdfsZOndkbWNEmT6fih09RJe5q73Hls91mGgJWIMwgwAGhZN/3jO/hFgiKpH5ZydWfqlwtJcL8F9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7q9v8q6WWItlZafwRbIwG8jSom4psyeNcQa0QPrY4eTXTL+vBTixYeK0zBum2fXXmwMbpTgTjmmUZjdQOV8f2JGH/d1/zdgvqHd9KhWixJfgFgdfaKFuo2BPMec4MWwkw23GSbdYOTx0C+sDy+pznxWmgxmaR9vtv6NOY+jREXcqEnu+5FSmUrofi6Pin3o7Jylu2XOEfOv05/rUPurMx5GDrJ0fSW5x7xGNT3sK/1q47Ds0pjs82jYK7spYrFrv4TM3/n/JdN37Whz1nwAAAP//E8FTBQ=="
}
1 change: 1 addition & 0 deletions libbeat/autodiscover/providers/kubernetes/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
IncludeLabels []string `config:"include_labels"`
ExcludeLabels []string `config:"exclude_labels"`
IncludeAnnotations []string `config:"include_annotations"`
IncludePodUID bool `config:"include_pod_uid"`

Prefix string `config:"prefix"`
HintsEnabled bool `config:"hints.enabled"`
Expand Down
2 changes: 1 addition & 1 deletion libbeat/autodiscover/providers/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func AutodiscoverBuilder(bus bus.Bus, c *common.Config) (autodiscover.Provider,
return nil, err
}

metagen := kubernetes.NewMetaGenerator(config.IncludeAnnotations, config.IncludeLabels, config.ExcludeLabels)
metagen := kubernetes.NewMetaGenerator(config.IncludeAnnotations, config.IncludeLabels, config.ExcludeLabels, config.IncludePodUID)

config.Host = kubernetes.DiscoverKubernetesNode(config.Host, config.InCluster, client)

Expand Down
9 changes: 8 additions & 1 deletion libbeat/common/kubernetes/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ type metaGenerator struct {
annotations []string
labels []string
labelsExclude []string
poduid bool
}

// NewMetaGenerator initializes and returns a new kubernetes metadata generator
func NewMetaGenerator(annotations, labels, labelsExclude []string) MetaGenerator {
func NewMetaGenerator(annotations, labels, labelsExclude []string, includePodUID bool) MetaGenerator {
return &metaGenerator{
annotations: annotations,
labels: labels,
labelsExclude: labelsExclude,
poduid: includePodUID,
}
}

Expand Down Expand Up @@ -56,6 +58,11 @@ func (g *metaGenerator) PodMetadata(pod *Pod) common.MapStr {
"namespace": pod.Metadata.Namespace,
}

// Add Pod UID metadata if enabled
if g.poduid {
safemapstr.Put(meta, "pod.uid", pod.Metadata.UID)
}

if len(labelMap) != 0 {
meta["labels"] = labelMap
}
Expand Down
31 changes: 27 additions & 4 deletions libbeat/common/kubernetes/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,43 @@ import (

func TestPodMetadataDeDot(t *testing.T) {
tests := []struct {
pod *Pod
meta common.MapStr
pod *Pod
meta common.MapStr
metaGen MetaGenerator
}{
{
pod: &Pod{
Metadata: ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
UID: "005f3b90-4b9d-12f8-acf0-31020a840133",
},
},
meta: common.MapStr{"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}}},
meta: common.MapStr{
"pod": common.MapStr{"name": ""},
"namespace": "",
"node": common.MapStr{"name": ""},
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
},
metaGen: NewMetaGenerator(nil, nil, nil, false),
},
{
pod: &Pod{
Metadata: ObjectMeta{
Labels: map[string]string{"a.key": "foo", "a": "bar"},
UID: "005f3b90-4b9d-12f8-acf0-31020a840133",
},
},
meta: common.MapStr{
"pod": common.MapStr{"name": "", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133"},
"namespace": "",
"node": common.MapStr{"name": ""},
"labels": common.MapStr{"a": common.MapStr{"value": "bar", "key": "foo"}},
},
metaGen: NewMetaGenerator(nil, nil, nil, true),
},
}

for _, test := range tests {
assert.Equal(t, NewMetaGenerator(nil, nil, nil).PodMetadata(test.pod)["labels"], test.meta["labels"])
assert.Equal(t, test.metaGen.PodMetadata(test.pod), test.meta)
}
}
5 changes: 5 additions & 0 deletions libbeat/processors/add_kubernetes_metadata/_meta/fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
description: >
Kubernetes pod name

- name: pod.uid
type: keyword
description: >
Kubernetes pod uid

- name: namespace
type: keyword
description: >
Expand Down
1 change: 1 addition & 0 deletions libbeat/processors/add_kubernetes_metadata/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type kubeAnnotatorConfig struct {
IncludeLabels []string `config:"include_labels"`
ExcludeLabels []string `config:"exclude_labels"`
IncludeAnnotations []string `config:"include_annotations"`
IncludePodUID bool `config:"include_pod_uid"`
}

type Enabled struct {
Expand Down
27 changes: 27 additions & 0 deletions libbeat/processors/add_kubernetes_metadata/indexers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const (
ContainerIndexerName = "container"
PodNameIndexerName = "pod_name"
PodUIDIndexerName = "pod_uid"
IPPortIndexerName = "ip_port"
)

Expand Down Expand Up @@ -128,6 +129,32 @@ func (p *PodNameIndexer) GetIndexes(pod *kubernetes.Pod) []string {
return []string{fmt.Sprintf("%s/%s", pod.Metadata.Namespace, pod.Metadata.Name)}
}

// PodUIDIndexer indexes pods based on the pod UID
type PodUIDIndexer struct {
metaGen kubernetes.MetaGenerator
}

// NewPodUIDIndexer initializes and returns a PodUIDIndexer
func NewPodUIDIndexer(_ common.Config, metaGen kubernetes.MetaGenerator) (Indexer, error) {
return &PodUIDIndexer{metaGen: metaGen}, nil
}

// GetMetadata returns the composed metadata from PodNameIndexer and the pod UID
func (p *PodUIDIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
data := p.metaGen.PodMetadata(pod)
return []MetadataIndex{
{
Index: pod.Metadata.UID,
Data: data,
},
}
}

// GetIndexes returns the indexes for the given Pod
func (p *PodUIDIndexer) GetIndexes(pod *kubernetes.Pod) []string {
return []string{pod.Metadata.UID}
}

// ContainerIndexer indexes pods based on all their containers IDs
type ContainerIndexer struct {
metaGen kubernetes.MetaGenerator
Expand Down
Loading