From 058c4051b01a7ab1cb3ad2855a0628606ae695a3 Mon Sep 17 00:00:00 2001 From: Michael Katsoulis Date: Tue, 12 Oct 2021 18:36:17 +0300 Subject: [PATCH] Breaking change for 8.0, namespace_annotations replaced by namespace.annotations (#28230) * Breaking change for 8.0, namespace_annotations replaced by namespace.annotations * Take care of namespace being nil --- .../composable/providers/kubernetes/pod.go | 22 ++++----- .../providers/kubernetes/pod_test.go | 46 ++++++++++++------- .../providers/kubernetes/service.go | 11 ++--- .../providers/kubernetes/service_test.go | 6 ++- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go index 80b28fcab47..5a7a8c4a392 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go @@ -247,14 +247,13 @@ func generatePodData( return providerData{} } - // k8sMapping includes only the metadata that fall under kubernetes.* - // and these are available as dynamic vars through the provider - k8sMapping := map[string]interface{}(kubemetaMap.(common.MapStr).Clone()) - + ckMeta := kubemetaMap.(common.MapStr).Clone() if len(namespaceAnnotations) != 0 { - // TODO: convert it to namespace.annotations for 8.0 - k8sMapping["namespace_annotations"] = namespaceAnnotations + ckMeta.Put("namespace.annotations", namespaceAnnotations) } + // k8sMapping includes only the metadata that fall under kubernetes.* + // and these are available as dynamic vars through the provider + k8sMapping := map[string]interface{}(ckMeta) // Pass annotations to all events so that it can be used in templating and by annotation builders. annotations := common.MapStr{} @@ -315,14 +314,13 @@ func generateContainerData( continue } - // k8sMapping includes only the metadata that fall under kubernetes.* - // and these are available as dynamic vars through the provider - k8sMapping := map[string]interface{}(kubemetaMap.(common.MapStr).Clone()) - + ckMeta := kubemetaMap.(common.MapStr).Clone() if len(namespaceAnnotations) != 0 { - // TODO: convert it to namespace.annotations for 8.0 - k8sMapping["namespace_annotations"] = namespaceAnnotations + ckMeta.Put("namespace.annotations", namespaceAnnotations) } + // k8sMapping includes only the metadata that fall under kubernetes.* + // and these are available as dynamic vars through the provider + k8sMapping := map[string]interface{}(ckMeta) // add annotations to be discoverable by templates k8sMapping["annotations"] = annotations diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod_test.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod_test.go index 62d4a199892..5c26d50ea8f 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod_test.go @@ -50,15 +50,17 @@ func TestGeneratePodData(t *testing.T) { data := generatePodData(pod, &Config{}, &podMeta{}, namespaceAnnotations) mapping := map[string]interface{}{ - "namespace": pod.GetNamespace(), + "namespace": common.MapStr{ + "name": pod.GetNamespace(), + "annotations": common.MapStr{ + "nsa": "nsb", + }, + }, "pod": common.MapStr{ "uid": string(pod.GetUID()), "name": pod.GetName(), "ip": pod.Status.PodIP, }, - "namespace_annotations": common.MapStr{ - "nsa": "nsb", - }, "labels": common.MapStr{ "foo": "bar", }, @@ -73,7 +75,9 @@ func TestGeneratePodData(t *testing.T) { "name": "devcluster", "url": "8.8.8.8:9090"}, }, "kubernetes": common.MapStr{ - "namespace": "testns", + "namespace": common.MapStr{ + "name": "testns", + }, "labels": common.MapStr{ "foo": "bar", }, @@ -157,7 +161,12 @@ func TestGenerateContainerPodData(t *testing.T) { }) mapping := map[string]interface{}{ - "namespace": pod.GetNamespace(), + "namespace": common.MapStr{ + "name": pod.GetNamespace(), + "annotations": common.MapStr{ + "nsa": "nsb", + }, + }, "pod": common.MapStr{ "uid": string(pod.GetUID()), "name": pod.GetName(), @@ -171,9 +180,6 @@ func TestGenerateContainerPodData(t *testing.T) { "port": "80", "port_name": "http", }, - "namespace_annotations": common.MapStr{ - "nsa": "nsb", - }, "annotations": common.MapStr{ "app": "production", }, @@ -192,7 +198,9 @@ func TestGenerateContainerPodData(t *testing.T) { "name": "devcluster", "url": "8.8.8.8:9090"}, }, "kubernetes": common.MapStr{ - "namespace": "testns", + "namespace": common.MapStr{ + "name": "testns", + }, "annotations": common.MapStr{"app": "production"}, "labels": common.MapStr{"foo": "bar"}, "pod": common.MapStr{ @@ -272,7 +280,12 @@ func TestEphemeralContainers(t *testing.T) { }) mapping := map[string]interface{}{ - "namespace": pod.GetNamespace(), + "namespace": common.MapStr{ + "name": pod.GetNamespace(), + "annotations": common.MapStr{ + "nsa": "nsb", + }, + }, "pod": common.MapStr{ "uid": string(pod.GetUID()), "name": pod.GetName(), @@ -287,9 +300,6 @@ func TestEphemeralContainers(t *testing.T) { "image": "nginx:1.120", "runtime": "crio", }, - "namespace_annotations": common.MapStr{ - "nsa": "nsb", - }, "annotations": common.MapStr{ "app": "production", }, @@ -305,7 +315,9 @@ func TestEphemeralContainers(t *testing.T) { "name": "devcluster", "url": "8.8.8.8:9090"}, }, "kubernetes": common.MapStr{ - "namespace": "testns", + "namespace": common.MapStr{ + "name": "testns", + }, "labels": common.MapStr{"foo": "bar"}, "annotations": common.MapStr{"app": "production"}, "pod": common.MapStr{ @@ -381,7 +393,9 @@ func (p *podMeta) GenerateECS(obj kubernetes.Resource) common.MapStr { func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOptions) common.MapStr { k8sPod := obj.(*kubernetes.Pod) return common.MapStr{ - "namespace": k8sPod.GetNamespace(), + "namespace": common.MapStr{ + "name": k8sPod.GetNamespace(), + }, "pod": common.MapStr{ "uid": string(k8sPod.GetUID()), "name": k8sPod.GetName(), diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go index 0e3f0055af1..2bdf73380be 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go @@ -186,14 +186,13 @@ func generateServiceData( return &serviceData{} } - // k8sMapping includes only the metadata that fall under kubernetes.* - // and these are available as dynamic vars through the provider - k8sMapping := map[string]interface{}(kubemetaMap.(common.MapStr).Clone()) - + ckMeta := kubemetaMap.(common.MapStr).Clone() if len(namespaceAnnotations) != 0 { - // TODO: convert it to namespace.annotations for 8.0 - k8sMapping["namespace_annotations"] = namespaceAnnotations + ckMeta.Put("namespace.annotations", namespaceAnnotations) } + // k8sMapping includes only the metadata that fall under kubernetes.* + // and these are available as dynamic vars through the provider + k8sMapping := map[string]interface{}(ckMeta) // Pass annotations to all events so that it can be used in templating and by annotation builders. annotations := common.MapStr{} diff --git a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service_test.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service_test.go index c183541e6a7..c85abed7f07 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service_test.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service_test.go @@ -61,8 +61,10 @@ func TestGenerateServiceData(t *testing.T) { "name": service.GetName(), "ip": service.Spec.ClusterIP, }, - "namespace_annotations": common.MapStr{ - "nsa": "nsb", + "namespace": common.MapStr{ + "annotations": common.MapStr{ + "nsa": "nsb", + }, }, "annotations": common.MapStr{ "baz": "ban",