Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking change for 8.0, namespace_annotations replaced by namespace.annotations #28230

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If k8sMapping["namespace"] return nil it will panic. Perhaps you can check the value first or you can just add the namespace.annotations key at top level at kubemetaMap.(common.MapStr).Clone(). Sth like this:

ckMeta := kubemetaMap.(common.MapStr).Clone()
if len(namespaceAnnotations) != 0 {
    ckMeta.Put("kubernetes.annotations", namespaceAnnotations)
}
k8sMapping := map[string]interface{}(ckMeta)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I thought there is no way that this can be nil for a pod or a service

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it

}

// Pass annotations to all events so that it can be used in templating and by annotation builders.
Expand Down Expand Up @@ -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
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 @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
Expand Down Expand Up @@ -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(),
},
}
}

Expand Down