Skip to content

Commit 434ef88

Browse files
committed
feat: allow overriding several paths when updating tag
Signed-off-by: Tchoupinax <corentinfiloche@hotmail.fr>
1 parent e370a64 commit 434ef88

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

pkg/argocd/argocd.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,12 +427,30 @@ func SetHelmImage(app *v1alpha1.Application, newImage *image.ContainerImage) err
427427
mergeParams = append(mergeParams, p)
428428
} else {
429429
if hpImageName != "" {
430-
p := v1alpha1.HelmParameter{Name: hpImageName, Value: newImage.GetFullNameWithoutTag(), ForceString: true}
431-
mergeParams = append(mergeParams, p)
430+
// Here is the case value1,value2
431+
if strings.Contains(hpImageName, ",") {
432+
var parameters = strings.Split(hpImageName, ",")
433+
for _, parameterName := range parameters {
434+
p := v1alpha1.HelmParameter{Name: parameterName, Value: newImage.GetFullNameWithoutTag(), ForceString: true}
435+
mergeParams = append(mergeParams, p)
436+
}
437+
} else {
438+
p := v1alpha1.HelmParameter{Name: hpImageName, Value: newImage.GetFullNameWithoutTag(), ForceString: true}
439+
mergeParams = append(mergeParams, p)
440+
}
432441
}
433442
if hpImageTag != "" {
434-
p := v1alpha1.HelmParameter{Name: hpImageTag, Value: newImage.GetTagWithDigest(), ForceString: true}
435-
mergeParams = append(mergeParams, p)
443+
// Here is the case value1,value2
444+
if strings.Contains(hpImageTag, ",") {
445+
var parameters = strings.Split(hpImageTag, ",")
446+
for _, parameterName := range parameters {
447+
p := v1alpha1.HelmParameter{Name: parameterName, Value: newImage.GetTagWithDigest(), ForceString: true}
448+
mergeParams = append(mergeParams, p)
449+
}
450+
} else {
451+
p := v1alpha1.HelmParameter{Name: hpImageTag, Value: newImage.GetTagWithDigest(), ForceString: true}
452+
mergeParams = append(mergeParams, p)
453+
}
436454
}
437455
}
438456

pkg/argocd/argocd_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,89 @@ func Test_SetHelmImage(t *testing.T) {
10051005
require.Error(t, err)
10061006
})
10071007

1008+
t.Run("Test set Helm image parameters when two paths are provided", func(t *testing.T) {
1009+
app := &v1alpha1.Application{
1010+
ObjectMeta: v1.ObjectMeta{
1011+
Name: "test-app",
1012+
Namespace: "testns",
1013+
Annotations: map[string]string{
1014+
fmt.Sprintf(common.HelmParamImageNameAnnotation, "foobar"): "foobar.image.name,foobar2.image.name",
1015+
fmt.Sprintf(common.HelmParamImageTagAnnotation, "foobar"): "foobar.image.tag, foobar2.image.tag",
1016+
},
1017+
},
1018+
Spec: v1alpha1.ApplicationSpec{
1019+
Source: &v1alpha1.ApplicationSource{
1020+
Helm: &v1alpha1.ApplicationSourceHelm{
1021+
Parameters: []v1alpha1.HelmParameter{
1022+
{
1023+
Name: "image.tag",
1024+
Value: "1.0.0",
1025+
},
1026+
{
1027+
Name: "image.name",
1028+
Value: "jannfis/dummy",
1029+
},
1030+
},
1031+
},
1032+
},
1033+
},
1034+
Status: v1alpha1.ApplicationStatus{
1035+
SourceType: v1alpha1.ApplicationSourceTypeHelm,
1036+
Summary: v1alpha1.ApplicationSummary{
1037+
Images: []string{
1038+
"jannfis/foobar:1.0.0",
1039+
},
1040+
},
1041+
},
1042+
}
1043+
1044+
img := image.NewFromIdentifier("foobar=jannfis/foobar:1.0.1")
1045+
1046+
err := SetHelmImage(app, img)
1047+
require.NoError(t, err)
1048+
require.NotNil(t, app.Spec.Source.Helm)
1049+
assert.Len(t, app.Spec.Source.Helm.Parameters, 6)
1050+
1051+
// Find first correct parameter
1052+
var tagParam v1alpha1.HelmParameter
1053+
for _, p := range app.Spec.Source.Helm.Parameters {
1054+
if p.Name == "foobar.image.tag" {
1055+
tagParam = p
1056+
break
1057+
}
1058+
}
1059+
assert.Equal(t, "1.0.1", tagParam.Value)
1060+
1061+
// Find second correct parameter
1062+
var tagParamTwo v1alpha1.HelmParameter
1063+
for _, p := range app.Spec.Source.Helm.Parameters {
1064+
if p.Name == "foobar.image.tag" {
1065+
tagParamTwo = p
1066+
break
1067+
}
1068+
}
1069+
assert.Equal(t, "1.0.1", tagParamTwo.Value)
1070+
1071+
// Find first correct parameter
1072+
var nameParam v1alpha1.HelmParameter
1073+
for _, p := range app.Spec.Source.Helm.Parameters {
1074+
if p.Name == "foobar.image.name" {
1075+
nameParam = p
1076+
break
1077+
}
1078+
}
1079+
assert.Equal(t, "jannfis/foobar", nameParam.Value)
1080+
1081+
// Find second correct parameter
1082+
var nameParamTwo v1alpha1.HelmParameter
1083+
for _, p := range app.Spec.Source.Helm.Parameters {
1084+
if p.Name == "foobar2.image.name" {
1085+
nameParamTwo = p
1086+
break
1087+
}
1088+
}
1089+
assert.Equal(t, "jannfis/foobar", nameParamTwo.Value)
1090+
})
10081091
}
10091092

10101093
func TestKubernetesClient(t *testing.T) {

0 commit comments

Comments
 (0)