Skip to content

Commit

Permalink
Fix k8s pod labels tier in metadata (elastic#16480)
Browse files Browse the repository at this point in the history
(cherry picked from commit 837279a)
  • Loading branch information
ChrsMark committed Feb 25, 2020
1 parent b983d78 commit 68137b2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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]
- Upgrade go-ucfg to latest v0.8.1. {pull}15937{15937}
- 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*
Expand Down
13 changes: 13 additions & 0 deletions libbeat/common/kubernetes/metadata/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
30 changes: 15 additions & 15 deletions libbeat/common/kubernetes/metadata/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -121,6 +118,9 @@ func TestPod_Generate(t *testing.T) {
"node": common.MapStr{
"name": "testnode",
},
"labels": common.MapStr{
"foo": "bar",
},
},
},
}
Expand Down Expand Up @@ -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",
},
},
},
{
Expand Down Expand Up @@ -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{
Expand All @@ -222,6 +219,9 @@ func TestPod_GenerateFromName(t *testing.T) {
"node": common.MapStr{
"name": "testnode",
},
"labels": common.MapStr{
"foo": "bar",
},
},
},
}
Expand Down Expand Up @@ -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,
Expand All @@ -320,6 +317,9 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) {
"nodekey": "nodevalue",
},
},
"labels": common.MapStr{
"foo": "bar",
},
},
},
}
Expand Down
30 changes: 15 additions & 15 deletions libbeat/processors/add_kubernetes_metadata/indexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 68137b2

Please sign in to comment.