Skip to content

Commit 79de6d5

Browse files
authored
fix: failed to process a full image-spec annotation with git writeback to helmvalues (#843)
Signed-off-by: Cheng Fang <cfang@redhat.com>
1 parent 5990a9b commit 79de6d5

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

pkg/argocd/update.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,28 +451,34 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
451451
if helmAnnotationParamName == "" {
452452
return nil, fmt.Errorf("could not find an image-name annotation for image %s", c.ImageName)
453453
}
454+
// for image-spec annotation, helmAnnotationParamName holds image-spec annotation value,
455+
// and helmAnnotationParamVersion is empty
454456
if helmAnnotationParamVersion == "" {
455-
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
457+
if c.GetParameterHelmImageSpec(app.Annotations) == "" {
458+
// not a full image-spec, so image-tag is required
459+
return nil, fmt.Errorf("could not find an image-tag annotation for image %s", c.ImageName)
460+
}
461+
} else {
462+
// image-tag annotation is present, so continue to process image-tag
463+
helmParamVersion := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamVersion)
464+
if helmParamVersion == nil {
465+
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
466+
}
467+
err = setHelmValue(&helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
468+
if err != nil {
469+
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
470+
}
456471
}
457472

458473
helmParamName := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamName)
459474
if helmParamName == nil {
460475
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamName)
461476
}
462477

463-
helmParamVersion := getHelmParam(appSource.Helm.Parameters, helmAnnotationParamVersion)
464-
if helmParamVersion == nil {
465-
return nil, fmt.Errorf("%s parameter not found", helmAnnotationParamVersion)
466-
}
467-
468478
err = setHelmValue(&helmNewValues, helmAnnotationParamName, helmParamName.Value)
469479
if err != nil {
470480
return nil, fmt.Errorf("failed to set image parameter name value: %v", err)
471481
}
472-
err = setHelmValue(&helmNewValues, helmAnnotationParamVersion, helmParamVersion.Value)
473-
if err != nil {
474-
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
475-
}
476482
}
477483

478484
override, err = yaml.Marshal(helmNewValues)

pkg/argocd/update_test.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,56 @@ replicas: 1
13831383
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
13841384
})
13851385

1386+
t.Run("Valid Helm source with Helm values file and image-spec", func(t *testing.T) {
1387+
expected := `
1388+
image.spec.foo: nginx:v1.0.0
1389+
replicas: 1
1390+
`
1391+
app := v1alpha1.Application{
1392+
ObjectMeta: v1.ObjectMeta{
1393+
Name: "testapp",
1394+
Annotations: map[string]string{
1395+
"argocd-image-updater.argoproj.io/image-list": "nginx",
1396+
"argocd-image-updater.argoproj.io/write-back-method": "git",
1397+
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
1398+
"argocd-image-updater.argoproj.io/nginx.helm.image-spec": "image.spec.foo",
1399+
},
1400+
},
1401+
Spec: v1alpha1.ApplicationSpec{
1402+
Source: &v1alpha1.ApplicationSource{
1403+
RepoURL: "https://example.com/example",
1404+
TargetRevision: "main",
1405+
Helm: &v1alpha1.ApplicationSourceHelm{
1406+
Parameters: []v1alpha1.HelmParameter{
1407+
{
1408+
Name: "image.spec.foo",
1409+
Value: "nginx:v1.0.0",
1410+
ForceString: true,
1411+
},
1412+
},
1413+
},
1414+
},
1415+
},
1416+
Status: v1alpha1.ApplicationStatus{
1417+
SourceType: v1alpha1.ApplicationSourceTypeHelm,
1418+
Summary: v1alpha1.ApplicationSummary{
1419+
Images: []string{
1420+
"nginx:v0.0.0",
1421+
},
1422+
},
1423+
},
1424+
}
1425+
1426+
originalData := []byte(`
1427+
image.spec.foo: nginx:v0.0.0
1428+
replicas: 1
1429+
`)
1430+
yaml, err := marshalParamsOverride(&app, originalData)
1431+
require.NoError(t, err)
1432+
assert.NotEmpty(t, yaml)
1433+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
1434+
})
1435+
13861436
t.Run("Valid Helm source with Helm values file with multiple images", func(t *testing.T) {
13871437
expected := `
13881438
nginx.image.name: nginx
@@ -1834,7 +1884,6 @@ replicas: 1
18341884
originalData := []byte(`random: yaml`)
18351885
_, err := marshalParamsOverride(&app, originalData)
18361886
assert.Error(t, err)
1837-
assert.Equal(t, "wrongimage.name parameter not found", err.Error())
18381887
})
18391888

18401889
t.Run("Image-tag annotation value not found in Helm source parameters list", func(t *testing.T) {

0 commit comments

Comments
 (0)