From 92b4e3dc27bea8344ec17acc2a4f15622573a328 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Mon, 9 Nov 2020 14:13:47 +0100 Subject: [PATCH] Add support for ephemeral containers in add_kubernetes_metadata (#22439) --- CHANGELOG.next.asciidoc | 2 +- .../add_kubernetes_metadata/indexers.go | 17 ++++++++-- .../add_kubernetes_metadata/indexers_test.go | 31 ++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 447d32f045a..670c73d2af3 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -26,7 +26,6 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Update to Golang 1.12.1. {pull}11330[11330] - Disable Alibaba Cloud and Tencent Cloud metadata providers by default. {pull}13812[12812] - API address is a required setting in `add_cloudfoundry_metadata`. {pull}21759[21759] -- Autodiscover kubernetes provider will find ephemeral containers. {pull}22389[22389] *Auditbeat* @@ -492,6 +491,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Add cloud.account.id for GCP into add_cloud_metadata processor. {pull}21776[21776] - Add proxy metricset for istio module. {pull}21751[21751] - Added Kafka version 2.2 to the list of supported versions. {pull}22328[22328] +- Add support for ephemeral containers in kubernetes autodiscover and `add_kubernetes_metadata`. {pull}22389[22389] {pull}22439[22439] *Auditbeat* diff --git a/libbeat/processors/add_kubernetes_metadata/indexers.go b/libbeat/processors/add_kubernetes_metadata/indexers.go index 017913771fc..84e3c3dd877 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexers.go +++ b/libbeat/processors/add_kubernetes_metadata/indexers.go @@ -182,7 +182,7 @@ func NewContainerIndexer(_ common.Config, metaGen metadata.MetaGen) (Indexer, er // GetMetadata returns the composed metadata list from all registered indexers func (c *ContainerIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex { var m []MetadataIndex - for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) { + for _, status := range getContainerStatusesInPod(pod) { cID, runtime := kubernetes.ContainerIDWithRuntime(status) if cID == "" { continue @@ -205,7 +205,7 @@ func (c *ContainerIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex { // GetIndexes returns the indexes for the given Pod func (c *ContainerIndexer) GetIndexes(pod *kubernetes.Pod) []string { var containers []string - for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) { + for _, status := range getContainerStatusesInPod(pod) { cID := kubernetes.ContainerID(status) if cID == "" { continue @@ -241,7 +241,7 @@ func (h *IPPortIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex { cIDs := make(map[string]string) runtimes := make(map[string]string) - for _, status := range append(pod.Status.ContainerStatuses, pod.Status.InitContainerStatuses...) { + for _, status := range getContainerStatusesInPod(pod) { cID, runtime := kubernetes.ContainerIDWithRuntime(status) if cID == "" { continue @@ -294,3 +294,14 @@ func (h *IPPortIndexer) GetIndexes(pod *kubernetes.Pod) []string { return hostPorts } + +func getContainerStatusesInPod(pod *kubernetes.Pod) []kubernetes.PodContainerStatus { + if pod == nil { + return nil + } + var statuses []kubernetes.PodContainerStatus + statuses = append(statuses, pod.Status.ContainerStatuses...) + statuses = append(statuses, pod.Status.InitContainerStatuses...) + statuses = append(statuses, pod.Status.EphemeralContainerStatuses...) + return statuses +} diff --git a/libbeat/processors/add_kubernetes_metadata/indexers_test.go b/libbeat/processors/add_kubernetes_metadata/indexers_test.go index f5144e2c9ee..e1d33f1a401 100644 --- a/libbeat/processors/add_kubernetes_metadata/indexers_test.go +++ b/libbeat/processors/add_kubernetes_metadata/indexers_test.go @@ -147,6 +147,8 @@ func TestContainerIndexer(t *testing.T) { containerImage := "containerimage" initContainerImage := "initcontainerimage" initContainer := "initcontainer" + ephemeralContainerImage := "ephemeralcontainerimage" + ephemeralContainer := "ephemeralcontainer" nodeName := "testnode" pod := kubernetes.Pod{ @@ -158,11 +160,8 @@ func TestContainerIndexer(t *testing.T) { "labelkey": "labelvalue", }, }, - Status: v1.PodStatus{ - ContainerStatuses: make([]kubernetes.PodContainerStatus, 0), - InitContainerStatuses: make([]kubernetes.PodContainerStatus, 0), - }, - Spec: v1.PodSpec{}, + Status: v1.PodStatus{}, + Spec: v1.PodSpec{}, } indexers := conIndexer.GetMetadata(&pod) @@ -199,16 +198,26 @@ func TestContainerIndexer(t *testing.T) { ContainerID: container2, }, } + container3 := "docker://klmno" + pod.Status.EphemeralContainerStatuses = []kubernetes.PodContainerStatus{ + { + Name: ephemeralContainer, + Image: ephemeralContainerImage, + ContainerID: container3, + }, + } indexers = conIndexer.GetMetadata(&pod) - assert.Equal(t, len(indexers), 2) + assert.Equal(t, len(indexers), 3) assert.Equal(t, indexers[0].Index, "abcde") assert.Equal(t, indexers[1].Index, "fghij") + assert.Equal(t, indexers[2].Index, "klmno") indices = conIndexer.GetIndexes(&pod) - assert.Equal(t, len(indices), 2) + assert.Equal(t, len(indices), 3) assert.Equal(t, indices[0], "abcde") assert.Equal(t, indices[1], "fghij") + assert.Equal(t, indices[2], "klmno") expected["container"] = common.MapStr{ "name": container, @@ -225,6 +234,14 @@ func TestContainerIndexer(t *testing.T) { "runtime": "docker", } assert.Equal(t, expected.String(), indexers[1].Data.String()) + + expected["container"] = common.MapStr{ + "name": ephemeralContainer, + "image": ephemeralContainerImage, + "id": "klmno", + "runtime": "docker", + } + assert.Equal(t, expected.String(), indexers[2].Data.String()) } func TestFilteredGenMeta(t *testing.T) {