Skip to content

Commit 58edbb4

Browse files
authored
Fix ordering and duplicate configs on autodiscover (elastic#19317)
elastic#18979 introduced a pod level event which is generated after all container events. The ordering is wrong in that pod events are sent last which would generate a valid event similar to container events. The ordering needs to be pod first and container events next so that pod events dont override valid container events. One other issue was that the pod level hint generates a single config with all hosts and it wont get over written by container hints causing more than one config to be spun up for the same hint (one with a container meta and one without).
1 parent 36e2978 commit 58edbb4

4 files changed

Lines changed: 538 additions & 103 deletions

File tree

libbeat/autodiscover/providers/kubernetes/pod.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
284284
var (
285285
annotations = common.MapStr{}
286286
nsAnn = common.MapStr{}
287+
events = make([]bus.Event, 0)
287288
)
288289
for k, v := range pod.GetObjectMeta().GetAnnotations() {
289290
safemapstr.Put(annotations, k, v)
@@ -299,7 +300,6 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
299300
}
300301
}
301302

302-
emitted := 0
303303
// Emit container and port information
304304
for _, c := range containers {
305305
// If it doesn't have an ID, container doesn't exist in
@@ -345,8 +345,7 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
345345
"kubernetes": meta,
346346
},
347347
}
348-
p.publish(event)
349-
emitted++
348+
events = append(events, event)
350349
}
351350

352351
for _, port := range c.Ports {
@@ -361,16 +360,16 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
361360
"kubernetes": meta,
362361
},
363362
}
364-
p.publish(event)
365-
emitted++
363+
events = append(events, event)
366364
}
367365
}
368366

369-
// Finally publish a pod level event so that hints that have no exposed ports can get processed.
367+
// Publish a pod level event so that hints that have no exposed ports can get processed.
370368
// Log hints would just ignore this event as there is no ${data.container.id}
371-
// Publish the pod level hint only if atleast one container level hint was emitted. This ensures that there is
369+
// Publish the pod level hint only if at least one container level hint was generated. This ensures that there is
372370
// no unnecessary pod level events emitted prematurely.
373-
if emitted != 0 {
371+
// We publish the pod level hint first so that it doesn't override a valid container level event.
372+
if len(events) != 0 {
374373
meta := p.metagen.Generate(pod)
375374

376375
// Information that can be used in discovering a workload
@@ -392,6 +391,10 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
392391
},
393392
}
394393
p.publish(event)
394+
}
395395

396+
// Publish the container level hints finally.
397+
for _, event := range events {
398+
p.publish(event)
396399
}
397400
}

0 commit comments

Comments
 (0)