From f1c4670f5a657bdb2d884bf149e64e13d0e45eb5 Mon Sep 17 00:00:00 2001 From: MichaelKatsoulis Date: Mon, 4 Oct 2021 14:32:46 +0300 Subject: [PATCH 1/2] Breaking change for 8.0, namespace_annotations replaced by namespace.annotations --- .../composable/providers/kubernetes/pod.go | 6 +-- .../providers/kubernetes/pod_test.go | 46 ++++++++++++------- .../providers/kubernetes/service.go | 3 +- .../providers/kubernetes/service_test.go | 13 +++++- 4 files changed, 44 insertions(+), 24 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 859f7f29dfd..0cf7348b52b 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go @@ -217,8 +217,7 @@ func generatePodData( k8sMapping := map[string]interface{}(kubemetaMap.(common.MapStr).Clone()) if len(namespaceAnnotations) != 0 { - // TODO: convert it to namespace.annotations for 8.0 - k8sMapping["namespace_annotations"] = namespaceAnnotations + k8sMapping["namespace"].(common.MapStr).Put("annotations", namespaceAnnotations) } // Pass annotations to all events so that it can be used in templating and by annotation builders. @@ -285,8 +284,7 @@ func generateContainerData( k8sMapping := map[string]interface{}(kubemetaMap.(common.MapStr).Clone()) if len(namespaceAnnotations) != 0 { - // TODO: convert it to namespace.annotations for 8.0 - k8sMapping["namespace_annotations"] = namespaceAnnotations + k8sMapping["namespace"].(common.MapStr).Put("annotations", namespaceAnnotations) } // add annotations to be discoverable by templates 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 03686ca7045..a91e99b7f4f 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go @@ -169,8 +169,7 @@ func generateServiceData( k8sMapping := map[string]interface{}(kubemetaMap.(common.MapStr).Clone()) if len(namespaceAnnotations) != 0 { - // TODO: convert it to namespace.annotations for 8.0 - k8sMapping["namespace_annotations"] = namespaceAnnotations + k8sMapping["namespace"].(common.MapStr).Put("annotations", namespaceAnnotations) } // Pass annotations to all events so that it can be used in templating and by annotation builders. 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..8df80af8fe8 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,11 @@ 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", + }, + "name": "testns", }, "annotations": common.MapStr{ "baz": "ban", @@ -83,6 +86,9 @@ func TestGenerateServiceData(t *testing.T) { "name": service.GetName(), "ip": "1.2.3.4", }, + "namespace": common.MapStr{ + "name": "testns", + }, "labels": common.MapStr{ "foo": "bar", }, @@ -148,6 +154,9 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt "annotations": common.MapStr{ "baz": "ban", }, + "namespace": common.MapStr{ + "name": k8sNode.GetNamespace(), + }, } } From 48e9885850905152ae76b57aeca4acd1b2debee6 Mon Sep 17 00:00:00 2001 From: MichaelKatsoulis Date: Mon, 4 Oct 2021 17:31:27 +0300 Subject: [PATCH 2/2] Take care of namespace being nil --- .../composable/providers/kubernetes/pod.go | 20 +++++++++---------- .../providers/kubernetes/service.go | 10 +++++----- .../providers/kubernetes/service_test.go | 7 ------- 3 files changed, 15 insertions(+), 22 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 0cf7348b52b..76959f34b9a 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go @@ -212,13 +212,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 { - k8sMapping["namespace"].(common.MapStr).Put("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{} @@ -279,13 +279,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 { - k8sMapping["namespace"].(common.MapStr).Put("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/service.go b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go index a91e99b7f4f..6fe156b337e 100644 --- a/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go +++ b/x-pack/elastic-agent/pkg/composable/providers/kubernetes/service.go @@ -164,13 +164,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 { - k8sMapping["namespace"].(common.MapStr).Put("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 8df80af8fe8..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 @@ -65,7 +65,6 @@ func TestGenerateServiceData(t *testing.T) { "annotations": common.MapStr{ "nsa": "nsb", }, - "name": "testns", }, "annotations": common.MapStr{ "baz": "ban", @@ -86,9 +85,6 @@ func TestGenerateServiceData(t *testing.T) { "name": service.GetName(), "ip": "1.2.3.4", }, - "namespace": common.MapStr{ - "name": "testns", - }, "labels": common.MapStr{ "foo": "bar", }, @@ -154,9 +150,6 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt "annotations": common.MapStr{ "baz": "ban", }, - "namespace": common.MapStr{ - "name": k8sNode.GetNamespace(), - }, } }