Skip to content

Commit

Permalink
Fix duplicated events in kubernetes autodiscover for pods with init o…
Browse files Browse the repository at this point in the history
…r ephemeral containers (#22438) (#22492)

(cherry picked from commit 3dd6931)
  • Loading branch information
jsoriano committed Nov 9, 2020
1 parent b9298b7 commit b7331b3
Show file tree
Hide file tree
Showing 3 changed files with 376 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Orderly close processors when processing pipelines are not needed anymore to release their resources. {pull}16349[16349]
- Fix memory leak and events duplication in docker autodiscover and add_docker_metadata. {pull}21851[21851]
- Fix parsing of expired licences. {issue}21112[21112] {pull}22180[22180]
- Fix duplicated pod events in kubernetes autodiscover for pods with init or ephemeral containers. {pull}22438[22438]

*Auditbeat*

Expand Down
23 changes: 18 additions & 5 deletions libbeat/autodiscover/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,33 @@ func (p *pod) Stop() {
}

func (p *pod) emit(pod *kubernetes.Pod, flag string) {
containers, statuses := getContainersInPod(pod)
p.emitEvents(pod, flag, containers, statuses)
}

// getContainersInPod returns all the containers defined in a pod and their statuses.
// It includes init and ephemeral containers.
func getContainersInPod(pod *kubernetes.Pod) ([]kubernetes.Container, []kubernetes.PodContainerStatus) {
var containers []kubernetes.Container
var statuses []kubernetes.PodContainerStatus

// Emit events for all containers
p.emitEvents(pod, flag, pod.Spec.Containers, pod.Status.ContainerStatuses)
containers = append(containers, pod.Spec.Containers...)
statuses = append(statuses, pod.Status.ContainerStatuses...)

// Emit events for all initContainers
p.emitEvents(pod, flag, pod.Spec.InitContainers, pod.Status.InitContainerStatuses)
containers = append(containers, pod.Spec.InitContainers...)
statuses = append(statuses, pod.Status.InitContainerStatuses...)

// Emit events for all ephemeralContainers
// Ephemeral containers are alpha feature in k8s and this code may require some changes, if their
// api change in the future.
var mappedEphemeralsAsContainers []kubernetes.Container
for _, c := range pod.Spec.EphemeralContainers {
mappedEphemeralsAsContainers = append(mappedEphemeralsAsContainers, kubernetes.Container(c.EphemeralContainerCommon))
containers = append(containers, kubernetes.Container(c.EphemeralContainerCommon))
}
p.emitEvents(pod, flag, mappedEphemeralsAsContainers, pod.Status.EphemeralContainerStatuses)
statuses = append(statuses, pod.Status.EphemeralContainerStatuses...)

return containers, statuses
}

func (p *pod) emitEvents(pod *kubernetes.Pod, flag string, containers []kubernetes.Container,
Expand Down
Loading

0 comments on commit b7331b3

Please sign in to comment.