diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 7615a029a54..6e6a08492a8 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -83,6 +83,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d *Affecting all Beats* +- Ensure that init containers are no longer tailed after they stop {pull}14394[14394] *Auditbeat* diff --git a/libbeat/autodiscover/providers/kubernetes/kubernetes.go b/libbeat/autodiscover/providers/kubernetes/kubernetes.go index e8c197634cf..4ee993eb7aa 100644 --- a/libbeat/autodiscover/providers/kubernetes/kubernetes.go +++ b/libbeat/autodiscover/providers/kubernetes/kubernetes.go @@ -198,9 +198,14 @@ func (p *Provider) emitEvents(pod *kubernetes.Pod, flag string, containers []kub containerIDs := map[string]string{} runtimes := map[string]string{} for _, c := range containerstatuses { - cid, runtime := kubernetes.ContainerIDWithRuntime(c) - containerIDs[c.Name] = cid - runtimes[c.Name] = runtime + // If the container is not being stopped then add the container only if it is in running state. + // This makes sure that we dont keep tailing init container logs after they have stopped. + // Emit the event in case that the pod is being stopped. + if flag == "stop" || c.State.Running != nil { + cid, runtime := kubernetes.ContainerIDWithRuntime(c) + containerIDs[c.Name] = cid + runtimes[c.Name] = runtime + } } // Emit container and port information diff --git a/libbeat/autodiscover/providers/kubernetes/kubernetes_test.go b/libbeat/autodiscover/providers/kubernetes/kubernetes_test.go index f2fffb17941..796c6d83efc 100644 --- a/libbeat/autodiscover/providers/kubernetes/kubernetes_test.go +++ b/libbeat/autodiscover/providers/kubernetes/kubernetes_test.go @@ -192,6 +192,9 @@ func TestEmitEvent(t *testing.T) { { Name: name, ContainerID: containerID, + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{}, + }, }, }, },