Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update several path for image.tag #793

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,49 @@ 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 != "" {
p := v1alpha1.HelmParameter{Name: hpImageName, Value: newImage.GetFullNameWithoutTag(), ForceString: true}
mergeParams = append(mergeParams, p)
// Here is the case value1,value2
if strings.Contains(hpImageName, ",") {
var parameters = strings.Split(strings.ReplaceAll(hpImageName, " ", ""), ",")
for _, parameterName := range parameters {
if parameterName != "" {
p := v1alpha1.HelmParameter{Name: parameterName, Value: newImage.GetFullNameWithoutTag(), ForceString: true}
mergeParams = append(mergeParams, p)
}
}
} else {
p := v1alpha1.HelmParameter{Name: hpImageName, Value: newImage.GetFullNameWithoutTag(), ForceString: true}
mergeParams = append(mergeParams, p)
}
}
if hpImageTag != "" {
p := v1alpha1.HelmParameter{Name: hpImageTag, Value: newImage.GetTagWithDigest(), ForceString: true}
mergeParams = append(mergeParams, p)
// Here is the case value1,value2
if strings.Contains(hpImageTag, ",") {
var parameters = strings.Split(strings.ReplaceAll(hpImageTag, " ", ""), ",")
for _, parameterName := range parameters {
if parameterName != "" {
p := v1alpha1.HelmParameter{Name: parameterName, Value: newImage.GetTagWithDigest(), ForceString: true}
mergeParams = append(mergeParams, p)
}
}
} else {
p := v1alpha1.HelmParameter{Name: hpImageTag, Value: newImage.GetTagWithDigest(), ForceString: true}
mergeParams = append(mergeParams, p)
}
}
}

Expand Down
143 changes: 143 additions & 0 deletions pkg/argocd/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,149 @@ func Test_SetHelmImage(t *testing.T) {
require.Error(t, err)
})

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",
},
Tchoupinax marked this conversation as resolved.
Show resolved Hide resolved
},
Spec: v1alpha1.ApplicationSpec{
Source: &v1alpha1.ApplicationSource{
Helm: &v1alpha1.ApplicationSourceHelm{
Parameters: []v1alpha1.HelmParameter{
{
Name: "image.tag",
Value: "1.0.0",
},
{
Name: "image.name",
Value: "jannfis/dummy",
},
},
},
},
},
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, 6)

// Find first correct parameter
var tagParam v1alpha1.HelmParameter
for _, p := range app.Spec.Source.Helm.Parameters {
if p.Name == "foobar.image.tag" {
tagParam = p
break
}
}
assert.Equal(t, "1.0.1", tagParam.Value)

// Find second correct parameter
var tagParamTwo v1alpha1.HelmParameter
for _, p := range app.Spec.Source.Helm.Parameters {
if p.Name == "foobar2.image.tag" {
tagParamTwo = p
break
}
}
assert.Equal(t, "1.0.1", tagParamTwo.Value)

// Find first correct parameter
var nameParam v1alpha1.HelmParameter
for _, p := range app.Spec.Source.Helm.Parameters {
if p.Name == "foobar.image.name" {
nameParam = p
break
}
}
assert.Equal(t, "jannfis/foobar", nameParam.Value)

// Find second correct parameter
var nameParamTwo v1alpha1.HelmParameter
for _, p := range app.Spec.Source.Helm.Parameters {
if p.Name == "foobar2.image.name" {
nameParamTwo = p
break
}
}
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