From 7e1e4539376cb57ce59727e1240c48a0aea0d729 Mon Sep 17 00:00:00 2001 From: Vijay Samuel Date: Wed, 26 Jul 2017 02:34:48 -0700 Subject: [PATCH] Adding support to exclude labels from kubernetes pod metadata (#4757) --- CHANGELOG.asciidoc | 2 + .../add_kubernetes_metadata/config.go | 1 + .../add_kubernetes_metadata/indexing.go | 10 +++- .../add_kubernetes_metadata/indexing_test.go | 46 +++++++++++++++++++ .../add_kubernetes_metadata/kubernetes.go | 5 +- 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 83ff4a470155..2f0d578f4383 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -46,6 +46,8 @@ https://github.com/elastic/beats/compare/v6.0.0-beta1...master[Check the HEAD di *Metricbeat* +- Add support to exclude labels from kubernetes pod metadata. {pull}4757[4757] + *Packetbeat* *Winlogbeat* diff --git a/libbeat/processors/add_kubernetes_metadata/config.go b/libbeat/processors/add_kubernetes_metadata/config.go index 6d53f63a0782..ec18bf58ca6d 100644 --- a/libbeat/processors/add_kubernetes_metadata/config.go +++ b/libbeat/processors/add_kubernetes_metadata/config.go @@ -17,6 +17,7 @@ type kubeAnnotatorConfig struct { DefaultMatchers Enabled `config:"default_matchers"` DefaultIndexers Enabled `config:"default_indexers"` IncludeLabels []string `config:"include_labels"` + ExcludeLabels []string `config:"exclude_labels"` IncludeAnnotations []string `config:"include_annotations"` } diff --git a/libbeat/processors/add_kubernetes_metadata/indexing.go b/libbeat/processors/add_kubernetes_metadata/indexing.go index 925ad9bbe248..f4471983e013 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexing.go +++ b/libbeat/processors/add_kubernetes_metadata/indexing.go @@ -172,8 +172,9 @@ func (m *Matchers) MetadataIndex(event common.MapStr) string { } type GenDefaultMeta struct { - annotations []string - labels []string + annotations []string + labels []string + labelsExclude []string } // GenerateMetaData generates default metadata for the given pod taking to account certain filters @@ -189,6 +190,11 @@ func (g *GenDefaultMeta) GenerateMetaData(pod *Pod) common.MapStr { labelMap = generateMapSubset(pod.Metadata.Labels, g.labels) } + // Exclude any labels that are present in the exclude_labels config + for _, label := range g.labelsExclude { + delete(labelMap, label) + } + annotationsMap = generateMapSubset(pod.Metadata.Annotations, g.annotations) meta := common.MapStr{ diff --git a/libbeat/processors/add_kubernetes_metadata/indexing_test.go b/libbeat/processors/add_kubernetes_metadata/indexing_test.go index 674f906b7846..5589c31a33c6 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexing_test.go +++ b/libbeat/processors/add_kubernetes_metadata/indexing_test.go @@ -209,3 +209,49 @@ func TestFilteredGenMeta(t *testing.T) { ok, _ = annotationsMap.HasKey("a") assert.Equal(t, ok, true) } + +func TestFilteredGenMetaExclusion(t *testing.T) { + var testConfig = common.NewConfig() + + filteredGen := &GenDefaultMeta{ + labelsExclude: []string{"x"}, + } + podIndexer, err := NewPodNameIndexer(*testConfig, filteredGen) + assert.Nil(t, err) + + podName := "testpod" + ns := "testns" + pod := Pod{ + Metadata: ObjectMeta{ + Name: podName, + Namespace: ns, + Labels: map[string]string{ + "foo": "bar", + "x": "y", + }, + Annotations: map[string]string{ + "a": "b", + "c": "d", + }, + }, + Spec: PodSpec{}, + } + + assert.Nil(t, err) + + indexers := podIndexer.GetMetadata(&pod) + assert.Equal(t, len(indexers), 1) + + rawLabels, _ := indexers[0].Data["labels"] + assert.NotNil(t, rawLabels) + + labelMap, ok := rawLabels.(common.MapStr) + assert.Equal(t, ok, true) + assert.Equal(t, len(labelMap), 1) + + ok, _ = labelMap.HasKey("foo") + assert.Equal(t, ok, true) + + ok, _ = labelMap.HasKey("x") + assert.Equal(t, ok, false) +} diff --git a/libbeat/processors/add_kubernetes_metadata/kubernetes.go b/libbeat/processors/add_kubernetes_metadata/kubernetes.go index 40fdd3a5a456..e4c70cd87401 100644 --- a/libbeat/processors/add_kubernetes_metadata/kubernetes.go +++ b/libbeat/processors/add_kubernetes_metadata/kubernetes.go @@ -74,8 +74,9 @@ func newKubernetesAnnotator(cfg *common.Config) (processors.Processor, error) { } metaGen := &GenDefaultMeta{ - labels: config.IncludeLabels, - annotations: config.IncludeAnnotations, + labels: config.IncludeLabels, + annotations: config.IncludeAnnotations, + labelsExclude: config.ExcludeLabels, } indexers := Indexers{