Skip to content

Commit 09d08bf

Browse files
authored
fix: whitespace only values.yaml files fail parsing (#1228)
Signed-off-by: MenD32 <[email protected]>
1 parent 375346e commit 09d08bf

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

pkg/argocd/update.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"sync"
1212
"text/template"
1313
"time"
14+
"unicode"
15+
"unicode/utf8"
1416

1517
"golang.org/x/exp/slices"
1618

@@ -486,7 +488,7 @@ func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]by
486488
images := GetImagesAndAliasesFromApplication(app)
487489

488490
var helmNewValues yaml.Node
489-
if len(originalData) == 0 {
491+
if isOnlyWhitespace(originalData) {
490492
// allow non-exists target file
491493
helmNewValues = yaml.Node{
492494
Kind: yaml.DocumentNode,
@@ -874,3 +876,17 @@ func commitChanges(app *v1alpha1.Application, wbc *WriteBackConfig, changeList [
874876
}
875877
return nil
876878
}
879+
880+
func isOnlyWhitespace(data []byte) bool {
881+
if len(data) == 0 {
882+
return true
883+
}
884+
for i := 0; i < len(data); {
885+
r, size := utf8.DecodeRune(data[i:])
886+
if !unicode.IsSpace(r) {
887+
return false
888+
}
889+
i += size
890+
}
891+
return true
892+
}

pkg/argocd/update_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,72 @@ replicas: 1
22412241
_, err := marshalParamsOverride(&app, nil)
22422242
assert.Error(t, err)
22432243
})
2244+
2245+
t.Run("Whitespace only values file from helm source does not cause error", func(t *testing.T) {
2246+
expected := `
2247+
# auto generated by argocd image updater
2248+
2249+
nginx:
2250+
image:
2251+
tag: v1.0.0
2252+
name: nginx
2253+
`
2254+
app := v1alpha1.Application{
2255+
ObjectMeta: v1.ObjectMeta{
2256+
Name: "testapp",
2257+
Annotations: map[string]string{
2258+
"argocd-image-updater.argoproj.io/image-list": "nginx=nginx, redis=redis",
2259+
"argocd-image-updater.argoproj.io/write-back-method": "git",
2260+
"argocd-image-updater.argoproj.io/write-back-target": "helmvalues:./test-values.yaml",
2261+
"argocd-image-updater.argoproj.io/nginx.helm.image-name": "nginx.image.name",
2262+
"argocd-image-updater.argoproj.io/nginx.helm.image-tag": "nginx.image.tag",
2263+
},
2264+
},
2265+
Spec: v1alpha1.ApplicationSpec{
2266+
Sources: []v1alpha1.ApplicationSource{
2267+
{
2268+
Chart: "my-app",
2269+
Helm: &v1alpha1.ApplicationSourceHelm{
2270+
ReleaseName: "my-app",
2271+
ValueFiles: []string{"$values/some/dir/values.yaml"},
2272+
Parameters: []v1alpha1.HelmParameter{
2273+
{
2274+
Name: "nginx.image.name",
2275+
Value: "nginx",
2276+
ForceString: true,
2277+
},
2278+
{
2279+
Name: "nginx.image.tag",
2280+
Value: "v1.0.0",
2281+
ForceString: true,
2282+
},
2283+
},
2284+
},
2285+
RepoURL: "https://example.com/example",
2286+
TargetRevision: "main",
2287+
},
2288+
},
2289+
},
2290+
Status: v1alpha1.ApplicationStatus{
2291+
SourceTypes: []v1alpha1.ApplicationSourceType{
2292+
v1alpha1.ApplicationSourceTypeHelm,
2293+
"",
2294+
},
2295+
Summary: v1alpha1.ApplicationSummary{
2296+
Images: []string{
2297+
"nginx:v0.0.0",
2298+
},
2299+
},
2300+
},
2301+
}
2302+
2303+
originalData := []byte(`
2304+
`)
2305+
yaml, err := marshalParamsOverride(&app, originalData)
2306+
require.NoError(t, err)
2307+
assert.NotEmpty(t, yaml)
2308+
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
2309+
})
22442310
}
22452311

22462312
func Test_SetHelmValue(t *testing.T) {

0 commit comments

Comments
 (0)