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) + } + }) + } +}