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

Preserve annotations in a kubernetes namespace metadata #27045

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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