diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 23647a64622..3f730e7cdc9 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -32,7 +32,6 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Remove redundant `cloudfoundry.*.timestamp` fields. This value is set in `@timestamp`. {pull}21175[21175] - Allow embedding of CAs, Certificate of private keys for anything that support TLS in ouputs and inputs https://github.com/elastic/beats/pull/21179 - API address is a required setting in `add_cloudfoundry_metadata`. {pull}21759[21759] -- Autodiscover kubernetes provider will find ephemeral containers. {pull}22389[22389] *Auditbeat* @@ -555,6 +554,7 @@ field. You can revert this change by configuring tags for the module and omittin - 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 6d584c395cb..d21bc953ef8 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) {