From dc3ade0af20e8ac424bfb860bd8d871d76b81119 Mon Sep 17 00:00:00 2001 From: RealAnna <89971034+RealAnna@users.noreply.github.com> Date: Mon, 17 Jul 2023 15:34:06 +0200 Subject: [PATCH] fix: take last element in tag as Workload version number (#1726) Signed-off-by: realanna --- .../pod_mutator/pod_mutating_webhook.go | 5 ++- .../pod_mutator/pod_mutating_webhook_test.go | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/operator/webhooks/pod_mutator/pod_mutating_webhook.go b/operator/webhooks/pod_mutator/pod_mutating_webhook.go index 2a9fb444ad..3a43b61368 100644 --- a/operator/webhooks/pod_mutator/pod_mutating_webhook.go +++ b/operator/webhooks/pod_mutator/pod_mutating_webhook.go @@ -241,8 +241,9 @@ func (a *PodMutatingWebhook) calculateVersion(pod *corev1.Pod) string { if len(pod.Spec.Containers) == 1 { image := strings.Split(pod.Spec.Containers[0].Image, ":") - if len(image) > 1 && image[1] != "" && image[1] != "latest" { - return image[1] + lenImg := len(image) - 1 + if lenImg >= 1 && image[lenImg] != "" && image[lenImg] != "latest" { + return image[lenImg] } } diff --git a/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go b/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go index 9f6add05e9..7562b1d18b 100644 --- a/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go +++ b/operator/webhooks/pod_mutator/pod_mutating_webhook_test.go @@ -1343,3 +1343,40 @@ func TestPodMutatingWebhook_Handle_MultiService(t *testing.T) { }, }, workload.Spec) } + +func TestPodMutatingWebhook_calculateVersion(t *testing.T) { + + tests := []struct { + name string + pod *corev1.Pod + want string + }{ + { + name: "simple tag", + pod: &corev1.Pod{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Image: "ciao:1.0.0"}, + }, + }}, + want: "1.0.0", + }, { + name: "local registry", + pod: &corev1.Pod{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + {Image: "localhost:5000/node-web-app:1.0.0"}, + }, + }}, + want: "1.0.0", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &PodMutatingWebhook{} + if got := a.calculateVersion(tt.pod); got != tt.want { + t.Errorf("calculateVersion() = %v, want %v", got, tt.want) + } + }) + } +}