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 annotation enrichment #29605

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 @@ -30,6 +30,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- `include_matches` option of `journald` input no longer accepts a list of string. {pull}29294[29294]
- Add job.name in pods controlled by Jobs {pull}28954[28954]
- Change Docker base image from CentOS 7 to Ubuntu 20.04 {pull}29681[29681]
- Enrich kubernetes metadata with node annotations. {pull}29605[29605]

*Auditbeat*

Expand Down
11 changes: 8 additions & 3 deletions libbeat/common/kubernetes/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ func WithFields(key string, value interface{}) FieldOptions {
}
}

// WithLabels FieldOption allows adding labels under sub-resource(kind)
// WithMetadata FieldOption allows adding labels and annotations under sub-resource(kind)
// example if kind=namespace namespace.labels key will be added
func WithLabels(kind string) FieldOptions {
func WithMetadata(kind string) FieldOptions {
return func(meta common.MapStr) {
safemapstr.Put(meta, strings.ToLower(kind)+".labels", meta["labels"])
if meta["labels"] != nil {
safemapstr.Put(meta, strings.ToLower(kind)+".labels", meta["labels"])
}
if meta["annotations"] != nil {
safemapstr.Put(meta, strings.ToLower(kind)+".annotations", meta["annotations"])
}
}
}

Expand Down
23 changes: 19 additions & 4 deletions libbeat/common/kubernetes/metadata/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func TestNode_Generate(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"key1": "value1",
"key2": "value2",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Expand All @@ -71,11 +74,16 @@ func TestNode_Generate(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"key2": "value2",
},
}},
},
}

cfg := common.NewConfig()
cfg, _ := common.NewConfigFrom(Config{
IncludeAnnotations: []string{"key2"},
})
metagen := NewNodeMetadataGenerator(cfg, nil, client)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -102,7 +110,9 @@ func TestNode_GenerateFromName(t *testing.T) {
Labels: map[string]string{
"foo": "bar",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"key": "value",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Expand All @@ -121,12 +131,17 @@ func TestNode_GenerateFromName(t *testing.T) {
"labels": common.MapStr{
"foo": "bar",
},
"annotations": common.MapStr{
"key": "value",
},
},
},
}

for _, test := range tests {
cfg := common.NewConfig()
cfg, _ := common.NewConfigFrom(Config{
IncludeAnnotations: []string{"key"},
})
nodes := cache.NewStore(cache.MetaNamespaceKeyFunc)
nodes.Add(test.input)
metagen := NewNodeMetadataGenerator(cfg, nodes, client)
Expand Down
2 changes: 1 addition & 1 deletion libbeat/common/kubernetes/metadata/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p *pod) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) common.
}

if p.node != nil {
meta := p.node.GenerateFromName(po.Spec.NodeName, WithLabels("node"))
meta := p.node.GenerateFromName(po.Spec.NodeName, WithMetadata("node"))
if meta != nil {
out.Put("node", meta["node"])
} else {
Expand Down
20 changes: 16 additions & 4 deletions libbeat/common/kubernetes/metadata/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ func TestPod_GenerateWithNodeNamespaceWithAddResourceConfig(t *testing.T) {
"nodekey": "nodevalue",
"nodekey2": "nodevalue2",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"node.annotation": "node.value",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Node",
Expand All @@ -755,7 +757,9 @@ func TestPod_GenerateWithNodeNamespaceWithAddResourceConfig(t *testing.T) {
"app.kubernetes.io/name": "kube-state-metrics",
"nskey2": "nsvalue2",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"ns.annotation": "ns.value",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -773,13 +777,19 @@ func TestPod_GenerateWithNodeNamespaceWithAddResourceConfig(t *testing.T) {
"namespace_labels": common.MapStr{
"app_kubernetes_io/name": "kube-state-metrics",
},
"namespace_annotations": common.MapStr{
"ns_annotation": "ns.value",
},
"node": common.MapStr{
"name": "testnode",
"uid": uid,
"labels": common.MapStr{
"nodekey2": "nodevalue2",
},
"hostname": "node1",
"annotations": common.MapStr{
"node_annotation": "node.value",
},
},
"labels": common.MapStr{
"app_kubernetes_io/component": "exporter",
Expand All @@ -802,10 +812,12 @@ func TestPod_GenerateWithNodeNamespaceWithAddResourceConfig(t *testing.T) {
assert.NoError(t, err)

namespaceConfig, _ := common.NewConfigFrom(map[string]interface{}{
"include_labels": []string{"app.kubernetes.io/name"},
"include_labels": []string{"app.kubernetes.io/name"},
"include_annotations": []string{"ns.annotation"},
})
nodeConfig, _ := common.NewConfigFrom(map[string]interface{}{
"include_labels": []string{"nodekey2"},
"include_labels": []string{"nodekey2"},
"include_annotations": []string{"node.annotation"},
})
metaConfig := AddResourceMetadataConfig{
Namespace: namespaceConfig,
Expand Down
15 changes: 11 additions & 4 deletions libbeat/common/kubernetes/metadata/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ func TestService_GenerateWithNamespace(t *testing.T) {
Labels: map[string]string{
"nskey": "nsvalue",
},
Annotations: map[string]string{},
Annotations: map[string]string{
"ns.annotation": "value",
},
},
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
Expand All @@ -300,21 +302,26 @@ func TestService_GenerateWithNamespace(t *testing.T) {
"namespace_labels": common.MapStr{
"nskey": "nsvalue",
},
"namespace_annotations": common.MapStr{
"ns_annotation": "value",
},
},
},
},
}

for _, test := range tests {
cfg := common.NewConfig()
nsConfig, _ := common.NewConfigFrom(map[string]interface{}{
"include_annotations": []string{"ns.annotation"},
})
services := cache.NewStore(cache.MetaNamespaceKeyFunc)
services.Add(test.input)

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

metagen := NewServiceMetadataGenerator(cfg, services, nsMeta, client)
metagen := NewServiceMetadataGenerator(nsConfig, services, nsMeta, client)
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.output, metagen.Generate(test.input))
})
Expand Down