Skip to content

Commit

Permalink
Fix ordering and duplicate configs on autodiscover (#19317)
Browse files Browse the repository at this point in the history
#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).
  • Loading branch information
vjsamuel authored Jul 2, 2020
1 parent 36e2978 commit 58edbb4
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 103 deletions.
19 changes: 11 additions & 8 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
var (
annotations = common.MapStr{}
nsAnn = common.MapStr{}
events = make([]bus.Event, 0)
)
for k, v := range pod.GetObjectMeta().GetAnnotations() {
safemapstr.Put(annotations, k, v)
Expand All @@ -299,7 +300,6 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
}
}

emitted := 0
// Emit container and port information
for _, c := range containers {
// If it doesn't have an ID, container doesn't exist in
Expand Down Expand Up @@ -345,8 +345,7 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
"kubernetes": meta,
},
}
p.publish(event)
emitted++
events = append(events, event)
}

for _, port := range c.Ports {
Expand All @@ -361,16 +360,16 @@ func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernet
"kubernetes": meta,
},
}
p.publish(event)
emitted++
events = append(events, event)
}
}

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

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

// Publish the container level hints finally.
for _, event := range events {
p.publish(event)
}
}
Loading

0 comments on commit 58edbb4

Please sign in to comment.