From 5710329dd210408958254b880a21874024e7d40c Mon Sep 17 00:00:00 2001 From: RealAnna <89971034+RealAnna@users.noreply.github.com> Date: Fri, 28 Jul 2023 08:37:40 +0200 Subject: [PATCH] fix(operator): sanitize app name annotation from uppercase to lowercase (#1793) Signed-off-by: realanna Signed-off-by: Griffin --- operator/webhooks/pod_mutator/pod_mutating_webhook.go | 9 +++++++-- .../webhooks/pod_mutator/pod_mutating_webhook_test.go | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/operator/webhooks/pod_mutator/pod_mutating_webhook.go b/operator/webhooks/pod_mutator/pod_mutating_webhook.go index 04f2e34aec4..d7183acf8cd 100644 --- a/operator/webhooks/pod_mutator/pod_mutating_webhook.go +++ b/operator/webhooks/pod_mutator/pod_mutating_webhook.go @@ -357,8 +357,8 @@ func (a *PodMutatingWebhook) handleApp(ctx context.Context, logger logr.Logger, } func (a *PodMutatingWebhook) generateWorkload(ctx context.Context, pod *corev1.Pod, namespace string) *klcv1alpha3.KeptnWorkload { - version, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations) - applicationName, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.AppAnnotation, apicommon.K8sRecommendedAppAnnotations) + version := a.getVersion(pod) + applicationName := a.getAppName(pod) var preDeploymentTasks []string var postDeploymentTasks []string @@ -446,6 +446,11 @@ func (a *PodMutatingWebhook) getAppName(pod *corev1.Pod) string { return strings.ToLower(applicationName) } +func (a *PodMutatingWebhook) getVersion(pod *corev1.Pod) string { + version, _ := getLabelOrAnnotation(&pod.ObjectMeta, apicommon.VersionAnnotation, apicommon.K8sRecommendedVersionAnnotations) + return strings.ToLower(version) +} + func (a *PodMutatingWebhook) getOwnerReference(resource metav1.ObjectMeta) metav1.OwnerReference { reference := metav1.OwnerReference{} if len(resource.OwnerReferences) != 0 { diff --git a/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go b/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go index 764a79781cf..67971737bff 100644 --- a/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go +++ b/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go @@ -1364,8 +1364,8 @@ func TestPodMutatingWebhook_Handle_MultiService(t *testing.T) { Namespace: "default", Annotations: map[string]string{ apicommon.WorkloadAnnotation: "my-workload", - apicommon.VersionAnnotation: "0.1", - apicommon.AppAnnotation: "my-app", + apicommon.VersionAnnotation: "V0.1", + apicommon.AppAnnotation: "my-App", }, OwnerReferences: []metav1.OwnerReference{ { @@ -1417,7 +1417,7 @@ func TestPodMutatingWebhook_Handle_MultiService(t *testing.T) { require.Nil(t, err) - require.Equal(t, "my-app", kacr.Spec.AppName) + require.Equal(t, "my-app", kacr.Spec.AppName) // this makes sure that everything is lowercase // here we do not want a single-service annotation require.Empty(t, kacr.Annotations[apicommon.AppTypeAnnotation]) @@ -1432,7 +1432,7 @@ func TestPodMutatingWebhook_Handle_MultiService(t *testing.T) { require.Equal(t, klcv1alpha3.KeptnWorkloadSpec{ AppName: kacr.Spec.AppName, - Version: "0.1", + Version: "v0.1", ResourceReference: klcv1alpha3.ResourceReference{ UID: "1234", Kind: "Deployment",