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 duplicated events in kubernetes autodiscover for pods with init or ephemeral containers #22438

Merged
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 @@ -192,6 +192,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix memory leak and events duplication in docker autodiscover and add_docker_metadata. {pull}21851[21851]
- Fixed documentation for commands in beats dev guide {pull}22194[22194]
- 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