From d941fe4085cc10aea3cb79823222d336a6895540 Mon Sep 17 00:00:00 2001 From: Vijay Samuel Date: Tue, 5 Nov 2019 14:30:38 -0800 Subject: [PATCH] Ensure that init containers are no longer tailed after they stop --- CHANGELOG.next.asciidoc | 1 + .../autodiscover/providers/kubernetes/kubernetes.go | 11 ++++++++--- .../providers/kubernetes/kubernetes_test.go | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 660eae4558b..55c72cda649 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -319,6 +319,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - GA the `script` processor. {pull}14325[14325] - Add `fingerprint` processor. {issue}11173[11173] {pull}14205[14205] - Add support for API keys in Elasticsearch outputs. {pull}14324[14324] +- 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 569c3d23f5d..75476115a58 100644 --- a/libbeat/autodiscover/providers/kubernetes/kubernetes.go +++ b/libbeat/autodiscover/providers/kubernetes/kubernetes.go @@ -195,9 +195,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 fdd71e7991f..6e9e4dfc6aa 100644 --- a/libbeat/autodiscover/providers/kubernetes/kubernetes_test.go +++ b/libbeat/autodiscover/providers/kubernetes/kubernetes_test.go @@ -198,6 +198,9 @@ func TestEmitEvent(t *testing.T) { { Name: name, ContainerID: containerID, + State: v1.ContainerState{ + Running: &v1.ContainerStateRunning{}, + }, }, }, },