Skip to content

Commit 2a3d875

Browse files
authored
fix: don't write empty tag in helm values file (#1531)
Signed-off-by: Rapha <11071770+Resousse@users.noreply.github.com> Signed-off-by: Resousse <11071770+Resousse@users.noreply.github.com>
1 parent 8c75366 commit 2a3d875

File tree

2 files changed

+133
-3
lines changed

2 files changed

+133
-3
lines changed

pkg/argocd/update.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,12 @@ func marshalParamsOverride(ctx context.Context, applicationImages *ApplicationIm
436436
} else {
437437
tagValue = helmParamVer.Value
438438
}
439-
err = setHelmValue(&helmNewValues, helmParamVersion, tagValue)
440-
if err != nil {
441-
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
439+
//Write tag in helm value file only if tag is not empty
440+
if tagValue != "" {
441+
err = setHelmValue(&helmNewValues, helmParamVersion, tagValue)
442+
if err != nil {
443+
return nil, fmt.Errorf("failed to set image parameter version value: %v", err)
444+
}
442445
}
443446
}
444447

pkg/argocd/update_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,6 +3402,133 @@ kustomize:
34023402
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(yamlOutput)),
34033403
"Output should be valid Kustomize override YAML with updated image")
34043404
})
3405+
3406+
t.Run("Empty target tag should not override existing helm tag", func(t *testing.T) {
3407+
app := v1alpha1.Application{
3408+
ObjectMeta: v1.ObjectMeta{
3409+
Name: "testapp",
3410+
},
3411+
Spec: v1alpha1.ApplicationSpec{
3412+
Source: &v1alpha1.ApplicationSource{
3413+
RepoURL: "https://example.com/example",
3414+
TargetRevision: "main",
3415+
Helm: &v1alpha1.ApplicationSourceHelm{
3416+
Parameters: []v1alpha1.HelmParameter{
3417+
{
3418+
Name: "image.name",
3419+
Value: "nginx",
3420+
ForceString: true,
3421+
},
3422+
{
3423+
Name: "image.tag",
3424+
Value: "",
3425+
ForceString: true,
3426+
},
3427+
},
3428+
},
3429+
},
3430+
},
3431+
Status: v1alpha1.ApplicationStatus{
3432+
SourceType: v1alpha1.ApplicationSourceTypeHelm,
3433+
Summary: v1alpha1.ApplicationSummary{
3434+
Images: []string{
3435+
"nginx:",
3436+
},
3437+
},
3438+
},
3439+
}
3440+
3441+
originalData := []byte(`
3442+
image.name: nginx
3443+
image.tag: v1.0.0
3444+
replicas: 1
3445+
`)
3446+
im := NewImage(
3447+
image.NewFromIdentifier("nginx"))
3448+
im.HelmImageName = "image.name"
3449+
im.HelmImageTag = "image.tag"
3450+
applicationImages := &ApplicationImages{
3451+
Application: app,
3452+
Images: ImageList{im},
3453+
WriteBackConfig: &WriteBackConfig{
3454+
Target: "./test-values.yaml",
3455+
},
3456+
}
3457+
3458+
override, err := marshalParamsOverride(context.Background(), applicationImages, originalData)
3459+
require.NoError(t, err)
3460+
assert.NotEmpty(t, override)
3461+
3462+
var out map[string]interface{}
3463+
err = yaml.Unmarshal(override, &out)
3464+
require.NoError(t, err)
3465+
3466+
val, ok := out["image.tag"]
3467+
require.True(t, ok, "expected key %s to still exist", "image.tag")
3468+
assert.Equal(t, "v1.0.0", val)
3469+
})
3470+
3471+
t.Run("Empty target tag should not override a non existing helm tag", func(t *testing.T) {
3472+
app := v1alpha1.Application{
3473+
ObjectMeta: v1.ObjectMeta{
3474+
Name: "testapp",
3475+
},
3476+
Spec: v1alpha1.ApplicationSpec{
3477+
Source: &v1alpha1.ApplicationSource{
3478+
RepoURL: "https://example.com/example",
3479+
TargetRevision: "main",
3480+
Helm: &v1alpha1.ApplicationSourceHelm{
3481+
Parameters: []v1alpha1.HelmParameter{
3482+
{
3483+
Name: "image.name",
3484+
Value: "nginx",
3485+
ForceString: true,
3486+
},
3487+
{
3488+
Name: "image.tag",
3489+
Value: "",
3490+
ForceString: true,
3491+
},
3492+
},
3493+
},
3494+
},
3495+
},
3496+
Status: v1alpha1.ApplicationStatus{
3497+
SourceType: v1alpha1.ApplicationSourceTypeHelm,
3498+
Summary: v1alpha1.ApplicationSummary{
3499+
Images: []string{
3500+
"nginx:",
3501+
},
3502+
},
3503+
},
3504+
}
3505+
3506+
originalData := []byte(`
3507+
image.name: nginx
3508+
replicas: 1
3509+
`)
3510+
im := NewImage(
3511+
image.NewFromIdentifier("nginx"))
3512+
im.HelmImageName = "image.name"
3513+
im.HelmImageTag = "image.tag"
3514+
applicationImages := &ApplicationImages{
3515+
Application: app,
3516+
Images: ImageList{im},
3517+
WriteBackConfig: &WriteBackConfig{
3518+
Target: "./test-values.yaml",
3519+
},
3520+
}
3521+
3522+
override, err := marshalParamsOverride(context.Background(), applicationImages, originalData)
3523+
require.NoError(t, err)
3524+
assert.NotEmpty(t, override)
3525+
3526+
var out map[string]interface{}
3527+
err = yaml.Unmarshal(override, &out)
3528+
require.NoError(t, err)
3529+
_, ok := out["image.tag"]
3530+
require.False(t, ok, "expected key %s to be absent", "image.tag")
3531+
})
34053532
}
34063533

34073534
func Test_GetHelmValue(t *testing.T) {

0 commit comments

Comments
 (0)