Skip to content

Commit

Permalink
feat: handle case when image-spec is chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
Tchoupinax committed Oct 23, 2024
1 parent 2c4170d commit 810a4a5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
15 changes: 13 additions & 2 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 62 additions & 2 deletions pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 810a4a5

Please sign in to comment.