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

Fix k8s pod annotations tier in metadata #16554

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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