Skip to content

Commit

Permalink
Breaking change for 8.0, namespace_annotations replaced by namespace.…
Browse files Browse the repository at this point in the history
…annotations (elastic#28230)

* Breaking change for 8.0, namespace_annotations replaced by namespace.annotations

* Take care of namespace being nil
  • Loading branch information
MichaelKatsoulis authored Oct 12, 2021
1 parent 764045e commit 058c405
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
22 changes: 10 additions & 12 deletions x-pack/elastic-agent/pkg/composable/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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",
},
Expand Down Expand Up @@ -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(),
Expand All @@ -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",
},
Expand All @@ -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{
Expand Down Expand Up @@ -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(),
Expand All @@ -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",
},
Expand All @@ -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{
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 058c405

Please sign in to comment.