Skip to content

Commit 83e7957

Browse files
authored
Fix k8s pod annotations tier in metadata (#16554)
1 parent da3584d commit 83e7957

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
6060
- Add `ssl.ca_sha256` option to the supported TLS option, this allow to check that a specific certificate is used as part of the verified chain. {issue}15717[15717]
6161
- Fix loading processors from annotation hints. {pull}16348[16348]
6262
- Fix k8s pods labels broken schema. {pull}16480[16480]
63+
- Fix k8s pods annotations broken schema. {pull}16554[16554]
6364
- Upgrade go-ucfg to latest v0.8.3. {pull}16450{16450}
6465

6566
*Auditbeat*

libbeat/common/kubernetes/metadata/pod.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (p *pod) Generate(obj kubernetes.Resource, opts ...FieldOptions) common.Map
5050

5151
out := p.resource.Generate("pod", obj, opts...)
5252
// TODO: remove this call when moving to 8.0
53-
out = p.exportPodLabels(out)
53+
out = p.exportPodLabelsAndAnnotations(out)
5454

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

95-
func (p *pod) exportPodLabels(in common.MapStr) common.MapStr {
95+
func (p *pod) exportPodLabelsAndAnnotations(in common.MapStr) common.MapStr {
9696
labels, err := in.GetValue("pod.labels")
9797
if err != nil {
9898
return in
9999
}
100100
in.Put("labels", labels)
101101
in.Delete("pod.labels")
102102

103+
annotations, err := in.GetValue("pod.annotations")
104+
if err != nil {
105+
return in
106+
}
107+
in.Put("annotations", annotations)
108+
in.Delete("pod.annotations")
109+
103110
return in
104111
}

libbeat/common/kubernetes/metadata/pod_test.go

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ func TestPod_Generate(t *testing.T) {
5353
Labels: map[string]string{
5454
"foo": "bar",
5555
},
56-
Annotations: map[string]string{},
56+
Annotations: map[string]string{
57+
"app": "production",
58+
},
5759
},
5860
TypeMeta: metav1.TypeMeta{
5961
Kind: "Pod",
@@ -71,6 +73,9 @@ func TestPod_Generate(t *testing.T) {
7173
"labels": common.MapStr{
7274
"foo": "bar",
7375
},
76+
"annotations": common.MapStr{
77+
"app": "production",
78+
},
7479
"namespace": "default",
7580
"node": common.MapStr{
7681
"name": "testnode",
@@ -87,7 +92,9 @@ func TestPod_Generate(t *testing.T) {
8792
Labels: map[string]string{
8893
"foo": "bar",
8994
},
90-
Annotations: map[string]string{},
95+
Annotations: map[string]string{
96+
"app": "production",
97+
},
9198
OwnerReferences: []metav1.OwnerReference{
9299
{
93100
APIVersion: "apps",
@@ -121,12 +128,19 @@ func TestPod_Generate(t *testing.T) {
121128
"labels": common.MapStr{
122129
"foo": "bar",
123130
},
131+
"annotations": common.MapStr{
132+
"app": "production",
133+
},
124134
},
125135
},
126136
}
127137

128-
cfg := common.NewConfig()
129-
metagen := NewPodMetadataGenerator(cfg, nil, nil, nil)
138+
config, err := common.NewConfigFrom(map[string]interface{}{
139+
"include_annotations": []string{"app"},
140+
})
141+
assert.Nil(t, err)
142+
143+
metagen := NewPodMetadataGenerator(config, nil, nil, nil)
130144
for _, test := range tests {
131145
t.Run(test.name, func(t *testing.T) {
132146
assert.Equal(t, test.output, metagen.Generate(test.input))
@@ -154,7 +168,9 @@ func TestPod_GenerateFromName(t *testing.T) {
154168
Labels: map[string]string{
155169
"foo": "bar",
156170
},
157-
Annotations: map[string]string{},
171+
Annotations: map[string]string{
172+
"app": "production",
173+
},
158174
},
159175
TypeMeta: metav1.TypeMeta{
160176
Kind: "Pod",
@@ -176,6 +192,9 @@ func TestPod_GenerateFromName(t *testing.T) {
176192
"labels": common.MapStr{
177193
"foo": "bar",
178194
},
195+
"annotations": common.MapStr{
196+
"app": "production",
197+
},
179198
},
180199
},
181200
{
@@ -188,7 +207,9 @@ func TestPod_GenerateFromName(t *testing.T) {
188207
Labels: map[string]string{
189208
"foo": "bar",
190209
},
191-
Annotations: map[string]string{},
210+
Annotations: map[string]string{
211+
"app": "production",
212+
},
192213
OwnerReferences: []metav1.OwnerReference{
193214
{
194215
APIVersion: "apps",
@@ -222,15 +243,21 @@ func TestPod_GenerateFromName(t *testing.T) {
222243
"labels": common.MapStr{
223244
"foo": "bar",
224245
},
246+
"annotations": common.MapStr{
247+
"app": "production",
248+
},
225249
},
226250
},
227251
}
228252

229253
for _, test := range tests {
230-
cfg := common.NewConfig()
254+
config, err := common.NewConfigFrom(map[string]interface{}{
255+
"include_annotations": []string{"app"},
256+
})
257+
assert.Nil(t, err)
231258
pods := cache.NewStore(cache.MetaNamespaceKeyFunc)
232259
pods.Add(test.input)
233-
metagen := NewPodMetadataGenerator(cfg, pods, nil, nil)
260+
metagen := NewPodMetadataGenerator(config, pods, nil, nil)
234261

235262
accessor, err := meta.Accessor(test.input)
236263
require.Nil(t, err)
@@ -262,7 +289,9 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) {
262289
Labels: map[string]string{
263290
"foo": "bar",
264291
},
265-
Annotations: map[string]string{},
292+
Annotations: map[string]string{
293+
"app": "production",
294+
},
266295
},
267296
TypeMeta: metav1.TypeMeta{
268297
Kind: "Pod",
@@ -320,24 +349,30 @@ func TestPod_GenerateWithNodeNamespace(t *testing.T) {
320349
"labels": common.MapStr{
321350
"foo": "bar",
322351
},
352+
"annotations": common.MapStr{
353+
"app": "production",
354+
},
323355
},
324356
},
325357
}
326358

327359
for _, test := range tests {
328-
cfg := common.NewConfig()
360+
config, err := common.NewConfigFrom(map[string]interface{}{
361+
"include_annotations": []string{"app"},
362+
})
363+
assert.Nil(t, err)
329364
pods := cache.NewStore(cache.MetaNamespaceKeyFunc)
330365
pods.Add(test.input)
331366

332367
nodes := cache.NewStore(cache.MetaNamespaceKeyFunc)
333368
nodes.Add(test.node)
334-
nodeMeta := NewNodeMetadataGenerator(cfg, nodes)
369+
nodeMeta := NewNodeMetadataGenerator(config, nodes)
335370

336371
namespaces := cache.NewStore(cache.MetaNamespaceKeyFunc)
337372
namespaces.Add(test.namespace)
338-
nsMeta := NewNamespaceMetadataGenerator(cfg, namespaces)
373+
nsMeta := NewNamespaceMetadataGenerator(config, namespaces)
339374

340-
metagen := NewPodMetadataGenerator(cfg, pods, nodeMeta, nsMeta)
375+
metagen := NewPodMetadataGenerator(config, pods, nodeMeta, nsMeta)
341376
t.Run(test.name, func(t *testing.T) {
342377
assert.Equal(t, test.output, metagen.Generate(test.input))
343378
})

libbeat/processors/add_kubernetes_metadata/indexers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func TestFilteredGenMeta(t *testing.T) {
257257
assert.Equal(t, ok, true)
258258
assert.Equal(t, len(labelMap), 2)
259259

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

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

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

0 commit comments

Comments
 (0)