Skip to content

Commit

Permalink
Preserve annotations in a kubernetes namespace metadata (#27045)
Browse files Browse the repository at this point in the history
  • Loading branch information
atercattus authored Jul 27, 2021
1 parent 877d8bc commit 4576058
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Omit full index template from errors that occur while loading the template. {pull}25743[25743]
- In the script processor, the `decode_xml` and `decode_xml_wineventlog` processors are now available as `DecodeXML` and `DecodeXMLWineventlog` respectively.
- Fix encoding errors when using the disk queue on nested data with multi-byte characters {pull}26484[26484]
- Preserve annotations in a kubernetes namespace metadata {pull}27045[27045]

*Auditbeat*

Expand Down
18 changes: 10 additions & 8 deletions libbeat/common/kubernetes/metadata/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ func flattenMetadata(in common.MapStr) common.MapStr {
}
}

rawLabels, err := in.GetValue("labels")
if err != nil {
return out
}
labels, ok := rawLabels.(common.MapStr)
if !ok {
return out
populateFromKeys := []string{"labels", "annotations"}
for _, key := range populateFromKeys {
rawValues, err := in.GetValue(key)
if err != nil {
continue
}
values, ok := rawValues.(common.MapStr)
if ok {
out[resource+"_"+key] = values
}
}
out[resource+"_labels"] = labels

return out
}
30 changes: 26 additions & 4 deletions libbeat/common/kubernetes/metadata/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func TestNamespace_Generate(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"spam": "baz",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -78,11 +80,20 @@ func TestNamespace_Generate(t *testing.T) {
"namespace_labels": common.MapStr{
"foo": "bar",
},
"namespace_annotations": common.MapStr{
"spam": "baz",
},
}},
},
}

cfg := common.NewConfig()
cfg, err := common.NewConfigFrom(Config{
IncludeAnnotations: []string{"spam"},
})
if err != nil {
t.Fatalf("Could not merge configs")
}

metagen := NewNamespaceMetadataGenerator(cfg, nil, client)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -109,7 +120,9 @@ func TestNamespace_GenerateFromName(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"spam": "baz",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -133,12 +146,21 @@ func TestNamespace_GenerateFromName(t *testing.T) {
"namespace_labels": common.MapStr{
"foo": "bar",
},
"namespace_annotations": common.MapStr{
"spam": "baz",
},
},
},
}

for _, test := range tests {
cfg := common.NewConfig()
cfg, err := common.NewConfigFrom(Config{
IncludeAnnotations: []string{"spam"},
})
if err != nil {
t.Fatalf("Could not merge configs")
}

namespaces := cache.NewStore(cache.MetaNamespaceKeyFunc)
namespaces.Add(test.input)
metagen := NewNamespaceMetadataGenerator(cfg, namespaces, client)
Expand Down

0 comments on commit 4576058

Please sign in to comment.