Skip to content

Commit

Permalink
Fix k8s pod annotations tier in metadata (elastic#16554)
Browse files Browse the repository at this point in the history
(cherry picked from commit 83e7957)
  • Loading branch information
ChrsMark committed Feb 25, 2020
1 parent d828372 commit ca234b0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- 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]
- Fix k8s pods annotations broken schema. {pull}16554[16554]
- Upgrade go-ucfg to latest v0.8.3. {pull}16450{16450}

*Auditbeat*
Expand Down
11 changes: 9 additions & 2 deletions libbeat/common/kubernetes/metadata/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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)
out = p.exportPodLabelsAndAnnotations(out)

if p.node != nil {
meta := p.node.GenerateFromName(po.Spec.NodeName)
Expand Down Expand Up @@ -92,13 +92,20 @@ func (p *pod) GenerateFromName(name string, opts ...FieldOptions) common.MapStr
return nil
}

func (p *pod) exportPodLabels(in common.MapStr) common.MapStr {
func (p *pod) exportPodLabelsAndAnnotations(in common.MapStr) common.MapStr {
labels, err := in.GetValue("pod.labels")
if err != nil {
return in
}
in.Put("labels", labels)
in.Delete("pod.labels")

annotations, err := in.GetValue("pod.annotations")
if err != nil {
return in
}
in.Put("annotations", annotations)
in.Delete("pod.annotations")

return in
}
61 changes: 48 additions & 13 deletions libbeat/common/kubernetes/metadata/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func TestPod_Generate(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"app": "production",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
Expand All @@ -71,6 +73,9 @@ func TestPod_Generate(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"app": "production",
},
"namespace": "default",
"node": common.MapStr{
"name": "testnode",
Expand All @@ -87,7 +92,9 @@ func TestPod_Generate(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"app": "production",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps",
Expand Down Expand Up @@ -121,12 +128,19 @@ func TestPod_Generate(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"app": "production",
},
},
},
}

cfg := common.NewConfig()
metagen := NewPodMetadataGenerator(cfg, nil, nil, nil)
config, err := common.NewConfigFrom(map[string]interface{}{
"include_annotations": []string{"app"},
})
assert.Nil(t, err)

metagen := NewPodMetadataGenerator(config, nil, nil, nil)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.output, metagen.Generate(test.input))
Expand Down Expand Up @@ -154,7 +168,9 @@ func TestPod_GenerateFromName(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"app": "production",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
Expand All @@ -176,6 +192,9 @@ func TestPod_GenerateFromName(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"app": "production",
},
},
},
{
Expand All @@ -188,7 +207,9 @@ func TestPod_GenerateFromName(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"app": "production",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "apps",
Expand Down Expand Up @@ -222,15 +243,21 @@ func TestPod_GenerateFromName(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"app": "production",
},
},
},
}

for _, test := range tests {
cfg := common.NewConfig()
config, err := common.NewConfigFrom(map[string]interface{}{
"include_annotations": []string{"app"},
})
assert.Nil(t, err)
pods := cache.NewStore(cache.MetaNamespaceKeyFunc)
pods.Add(test.input)
metagen := NewPodMetadataGenerator(cfg, pods, nil, nil)
metagen := NewPodMetadataGenerator(config, pods, nil, nil)

accessor, err := meta.Accessor(test.input)
require.Nil(t, err)
Expand Down Expand Up @@ -262,7 +289,9 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"app": "production",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
Expand Down Expand Up @@ -320,24 +349,30 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"app": "production",
},
},
},
}

for _, test := range tests {
cfg := common.NewConfig()
config, err := common.NewConfigFrom(map[string]interface{}{
"include_annotations": []string{"app"},
})
assert.Nil(t, err)
pods := cache.NewStore(cache.MetaNamespaceKeyFunc)
pods.Add(test.input)

nodes := cache.NewStore(cache.MetaNamespaceKeyFunc)
nodes.Add(test.node)
nodeMeta := NewNodeMetadataGenerator(cfg, nodes)
nodeMeta := NewNodeMetadataGenerator(config, nodes)

namespaces := cache.NewStore(cache.MetaNamespaceKeyFunc)
namespaces.Add(test.namespace)
nsMeta := NewNamespaceMetadataGenerator(cfg, namespaces)
nsMeta := NewNamespaceMetadataGenerator(config, namespaces)

metagen := NewPodMetadataGenerator(cfg, pods, nodeMeta, nsMeta)
metagen := NewPodMetadataGenerator(config, pods, nodeMeta, nsMeta)
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.output, metagen.Generate(test.input))
})
Expand Down
4 changes: 2 additions & 2 deletions libbeat/processors/add_kubernetes_metadata/indexers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestFilteredGenMeta(t *testing.T) {
assert.Equal(t, ok, true)
assert.Equal(t, len(labelMap), 2)

rawAnnotations, _ := indexers[0].Data.GetValue("pod.annotations")
rawAnnotations, _ := indexers[0].Data.GetValue("annotations")
assert.Nil(t, rawAnnotations)

config, err := common.NewConfigFrom(map[string]interface{}{
Expand All @@ -284,7 +284,7 @@ func TestFilteredGenMeta(t *testing.T) {
ok, _ = labelMap.HasKey("foo")
assert.Equal(t, ok, true)

rawAnnotations, _ = indexers[0].Data.GetValue("pod.annotations")
rawAnnotations, _ = indexers[0].Data.GetValue("annotations")
assert.NotNil(t, rawAnnotations)
annotationsMap, ok := rawAnnotations.(common.MapStr)

Expand Down

0 comments on commit ca234b0

Please sign in to comment.