From 837279a4b5c2b5e560bfc8fc3e6dd7d4c1cebd12 Mon Sep 17 00:00:00 2001 From: Chris Mark Date: Tue, 25 Feb 2020 12:13:06 +0200 Subject: [PATCH] Fix k8s pod labels tier in metadata (#16480) --- CHANGELOG.next.asciidoc | 1 + libbeat/common/kubernetes/metadata/pod.go | 13 ++++++++ .../common/kubernetes/metadata/pod_test.go | 30 +++++++++---------- .../add_kubernetes_metadata/indexers_test.go | 30 +++++++++---------- 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 6241ebba0a5..ef7c8514f3e 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -59,6 +59,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fix index names for indexing not always guaranteed to be lower case. {pull}16081[16081] - Add `ssl.ca_sha256` option to the supported TLS option, this allow to check that a specific certificate is used as part of the verified chain. {issue}15717[15717] - Fix loading processors from annotation hints. {pull}16348[16348] +- Fix k8s pods labels broken schema. {pull}16480[16480] - Upgrade go-ucfg to latest v0.8.3. {pull}16450{16450} *Auditbeat* diff --git a/libbeat/common/kubernetes/metadata/pod.go b/libbeat/common/kubernetes/metadata/pod.go index b42fa005cc0..7385d2abdf5 100644 --- a/libbeat/common/kubernetes/metadata/pod.go +++ b/libbeat/common/kubernetes/metadata/pod.go @@ -49,6 +49,8 @@ func (p *pod) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.Map } out := p.resource.Generate("pod", obj, opts...) + // TODO: remove this call when moving to 8.0 + out = p.exportPodLabels(out) if p.node != nil { meta := p.node.GenerateFromName(po.Spec.NodeName) @@ -89,3 +91,14 @@ func (p *pod) GenerateFromName(name string, opts ...FieldOptions) common.MapStr return nil } + +func (p *pod) exportPodLabels(in common.MapStr) common.MapStr { + labels, err := in.GetValue("pod.labels") + if err != nil { + return in + } + in.Put("labels", labels) + in.Delete("pod.labels") + + return in +} diff --git a/libbeat/common/kubernetes/metadata/pod_test.go b/libbeat/common/kubernetes/metadata/pod_test.go index 2c42958a759..c3838ad2c32 100644 --- a/libbeat/common/kubernetes/metadata/pod_test.go +++ b/libbeat/common/kubernetes/metadata/pod_test.go @@ -67,9 +67,9 @@ func TestPod_Generate(t *testing.T) { "pod": common.MapStr{ "name": "obj", "uid": uid, - "labels": common.MapStr{ - "foo": "bar", - }, + }, + "labels": common.MapStr{ + "foo": "bar", }, "namespace": "default", "node": common.MapStr{ @@ -110,9 +110,6 @@ func TestPod_Generate(t *testing.T) { "pod": common.MapStr{ "name": "obj", "uid": uid, - "labels": common.MapStr{ - "foo": "bar", - }, }, "namespace": "default", "deployment": common.MapStr{ @@ -121,6 +118,9 @@ func TestPod_Generate(t *testing.T) { "node": common.MapStr{ "name": "testnode", }, + "labels": common.MapStr{ + "foo": "bar", + }, }, }, } @@ -168,14 +168,14 @@ func TestPod_GenerateFromName(t *testing.T) { "pod": common.MapStr{ "name": "obj", "uid": uid, - "labels": common.MapStr{ - "foo": "bar", - }, }, "namespace": "default", "node": common.MapStr{ "name": "testnode", }, + "labels": common.MapStr{ + "foo": "bar", + }, }, }, { @@ -211,9 +211,6 @@ func TestPod_GenerateFromName(t *testing.T) { "pod": common.MapStr{ "name": "obj", "uid": uid, - "labels": common.MapStr{ - "foo": "bar", - }, }, "namespace": "default", "deployment": common.MapStr{ @@ -222,6 +219,9 @@ func TestPod_GenerateFromName(t *testing.T) { "node": common.MapStr{ "name": "testnode", }, + "labels": common.MapStr{ + "foo": "bar", + }, }, }, } @@ -304,9 +304,6 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) { "pod": common.MapStr{ "name": "obj", "uid": uid, - "labels": common.MapStr{ - "foo": "bar", - }, }, "namespace": "default", "namespace_uid": uid, @@ -320,6 +317,9 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) { "nodekey": "nodevalue", }, }, + "labels": common.MapStr{ + "foo": "bar", + }, }, }, } diff --git a/libbeat/processors/add_kubernetes_metadata/indexers_test.go b/libbeat/processors/add_kubernetes_metadata/indexers_test.go index 0d74cece012..acbe1bf51eb 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexers_test.go +++ b/libbeat/processors/add_kubernetes_metadata/indexers_test.go @@ -66,9 +66,9 @@ func TestPodIndexer(t *testing.T) { "pod": common.MapStr{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", - "labels": common.MapStr{ - "labelkey": "labelvalue", - }, + }, + "labels": common.MapStr{ + "labelkey": "labelvalue", }, "namespace": "testns", "node": common.MapStr{ @@ -117,14 +117,14 @@ func TestPodUIDIndexer(t *testing.T) { "pod": common.MapStr{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", - "labels": common.MapStr{ - "labelkey": "labelvalue", - }, }, "namespace": "testns", "node": common.MapStr{ "name": "testnode", }, + "labels": common.MapStr{ + "labelkey": "labelvalue", + }, } assert.Equal(t, expected.String(), indexers[0].Data.String()) @@ -173,14 +173,14 @@ func TestContainerIndexer(t *testing.T) { "pod": common.MapStr{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", - "labels": common.MapStr{ - "labelkey": "labelvalue", - }, }, "namespace": "testns", "node": common.MapStr{ "name": "testnode", }, + "labels": common.MapStr{ + "labelkey": "labelvalue", + }, } container1 := "docker://abcde" pod.Spec.NodeName = nodeName @@ -250,7 +250,7 @@ func TestFilteredGenMeta(t *testing.T) { indexers := podIndexer.GetMetadata(&pod) assert.Equal(t, len(indexers), 1) - rawLabels, _ := indexers[0].Data.GetValue("pod.labels") + rawLabels, _ := indexers[0].Data.GetValue("labels") assert.NotNil(t, rawLabels) labelMap, ok := rawLabels.(common.MapStr) @@ -274,7 +274,7 @@ func TestFilteredGenMeta(t *testing.T) { indexers = podIndexer.GetMetadata(&pod) assert.Equal(t, len(indexers), 1) - rawLabels, _ = indexers[0].Data.GetValue("pod.labels") + rawLabels, _ = indexers[0].Data.GetValue("labels") assert.NotNil(t, rawLabels) labelMap, ok = rawLabels.(common.MapStr) @@ -331,7 +331,7 @@ func TestFilteredGenMetaExclusion(t *testing.T) { indexers := podIndexer.GetMetadata(&pod) assert.Equal(t, len(indexers), 1) - rawLabels, _ := indexers[0].Data.GetValue("pod.labels") + rawLabels, _ := indexers[0].Data.GetValue("labels") assert.NotNil(t, rawLabels) labelMap, ok := rawLabels.(common.MapStr) @@ -392,14 +392,14 @@ func TestIpPortIndexer(t *testing.T) { "pod": common.MapStr{ "name": "testpod", "uid": "005f3b90-4b9d-12f8-acf0-31020a840133", - "labels": common.MapStr{ - "labelkey": "labelvalue", - }, }, "namespace": "testns", "node": common.MapStr{ "name": "testnode", }, + "labels": common.MapStr{ + "labelkey": "labelvalue", + }, } pod.Spec.Containers = []v1.Container{