From 822b37ae1dce23d75cc3fda3d8e261af3291f94e Mon Sep 17 00:00:00 2001 From: Tchoupinax Date: Wed, 23 Oct 2024 22:52:31 +0200 Subject: [PATCH] feat: handle case when image-spec is chosen Signed-off-by: Tchoupinax --- pkg/argocd/argocd.go | 15 +++++++-- pkg/argocd/argocd_test.go | 64 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/pkg/argocd/argocd.go b/pkg/argocd/argocd.go index 9c0f1feb..38740391 100644 --- a/pkg/argocd/argocd.go +++ b/pkg/argocd/argocd.go @@ -423,8 +423,19 @@ func SetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) err // we simply ignore any image-name and image-tag parameters that might be // there. if hpImageSpec != "" { - p := v1alpha1.HelmParameter{Name: hpImageSpec, Value: newImage.GetFullNameWithTag(), ForceString: true} - mergeParams = append(mergeParams, p) + // Here is the case value1,value2 + if strings.Contains(hpImageSpec, ",") { + var parameters = strings.Split(strings.ReplaceAll(hpImageSpec, " ", ""), ",") + for _, parameterName := range parameters { + if parameterName != "" { + p := v1alpha1.HelmParameter{Name: parameterName, Value: newImage.GetFullNameWithTag(), ForceString: true} + mergeParams = append(mergeParams, p) + } + } + } else { + p := v1alpha1.HelmParameter{Name: hpImageSpec, Value: newImage.GetFullNameWithTag(), ForceString: true} + mergeParams = append(mergeParams, p) + } } else { if hpImageName != "" { // Here is the case value1,value2 diff --git a/pkg/argocd/argocd_test.go b/pkg/argocd/argocd_test.go index d0a160a3..cba49eaf 100644 --- a/pkg/argocd/argocd_test.go +++ b/pkg/argocd/argocd_test.go @@ -1005,14 +1005,14 @@ func Test_SetHelmImage(t *testing.T) { require.Error(t, err) }) - t.Run("Test set Helm image parameters when two paths are provided", func(t *testing.T) { + t.Run("Test set Helm image parameters when two paths are provided with name and tag", func(t *testing.T) { app := &v1alpha1.Application{ ObjectMeta: v1.ObjectMeta{ Name: "test-app", Namespace: "testns", Annotations: map[string]string{ fmt.Sprintf(common.HelmParamImageNameAnnotation, "foobar"): "foobar.image.name,,, foobar2.image.name", - fmt.Sprintf(common.HelmParamImageTagAnnotation, "foobar"): "foobar.image.tag,,, foobar2.image.tag", // Space and comma are expected + fmt.Sprintf(common.HelmParamImageTagAnnotation, "foobar"): "foobar.image.tag,,, foobar2.image.tag", }, }, Spec: v1alpha1.ApplicationSpec{ @@ -1088,6 +1088,66 @@ func Test_SetHelmImage(t *testing.T) { } assert.Equal(t, "jannfis/foobar", nameParamTwo.Value) }) + + t.Run("Test set Helm image parameters when two paths are provided with image spec", func(t *testing.T) { + app := &v1alpha1.Application{ + ObjectMeta: v1.ObjectMeta{ + Name: "test-app", + Namespace: "testns", + Annotations: map[string]string{ + fmt.Sprintf(common.HelmParamImageSpecAnnotation, "foobar"): "foobar.image.spec,,, foobar2.image.spec", + }, + }, + Spec: v1alpha1.ApplicationSpec{ + Source: &v1alpha1.ApplicationSource{ + Helm: &v1alpha1.ApplicationSourceHelm{ + Parameters: []v1alpha1.HelmParameter{ + { + Name: "image.spec", + Value: "jannfis/dummy:1.0.0", + }, + }, + }, + }, + }, + Status: v1alpha1.ApplicationStatus{ + SourceType: v1alpha1.ApplicationSourceTypeHelm, + Summary: v1alpha1.ApplicationSummary{ + Images: []string{ + "jannfis/foobar:1.0.0", + }, + }, + }, + } + + img := image.NewFromIdentifier("foobar=jannfis/foobar:1.0.1") + + err := SetHelmImage(app, img) + require.NoError(t, err) + require.NotNil(t, app.Spec.Source.Helm) + assert.Len(t, app.Spec.Source.Helm.Parameters, 3) + + // Find first correct parameter + var specParam v1alpha1.HelmParameter + for _, p := range app.Spec.Source.Helm.Parameters { + fmt.Println(p.Name) + if p.Name == "foobar.image.spec" { + specParam = p + break + } + } + assert.Equal(t, "jannfis/foobar:1.0.1", specParam.Value) + + // Find second correct parameter + var specParamTwo v1alpha1.HelmParameter + for _, p := range app.Spec.Source.Helm.Parameters { + if p.Name == "foobar2.image.spec" { + specParamTwo = p + break + } + } + assert.Equal(t, "jannfis/foobar:1.0.1", specParamTwo.Value) + }) } func TestKubernetesClient(t *testing.T) {