Skip to content

Commit

Permalink
fix: take last element in tag as Workload version number (#1726)
Browse files Browse the repository at this point in the history
Signed-off-by: realanna <anna.reale@dynatrace.com>
  • Loading branch information
RealAnna committed Jul 17, 2023
1 parent e3db89c commit dc3ade0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions operator/webhooks/pod_mutator/pod_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down
37 changes: 37 additions & 0 deletions operator/webhooks/pod_mutator/pod_mutating_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit dc3ade0

Please sign in to comment.