diff --git a/pkg/action/action.go b/pkg/action/action.go index 49d7316c0..183d5940b 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -26,6 +26,7 @@ import ( "os" "path" "path/filepath" + "regexp" "slices" "strings" "sync" @@ -62,6 +63,7 @@ import ( var Timestamper = time.Now var ( + compactDocSeparatorPattern = regexp.MustCompile(`(?m)^---(?=\S)`) // errMissingChart indicates that a chart was not provided. errMissingChart = errors.New("no chart provided") // errMissingRelease indicates that a release (name) was not provided. @@ -159,7 +161,9 @@ func annotateAndMerge(files map[string]string) (string, error) { continue } - manifests, err := kio.ParseAll(content) + // Go template whitespace trimming can produce lines like `---apiVersion: ...`. + // Normalize those into valid document separators before YAML parsing. + manifests, err := kio.ParseAll(normalizeDocSeparators(content)) if err != nil { return "", fmt.Errorf("parsing %s: %w", fname, err) } @@ -178,6 +182,10 @@ func annotateAndMerge(files map[string]string) (string, error) { return merged, nil } +func normalizeDocSeparators(content string) string { + return compactDocSeparatorPattern.ReplaceAllString(content, "---\n") +} + // splitAndDeannotate reconstructs individual files from a merged YAML stream, // removing filename annotations and grouping documents by their original filenames. func splitAndDeannotate(postrendered string) (map[string]string, error) { diff --git a/pkg/action/action_test.go b/pkg/action/action_test.go index 29a4885bd..da97b5477 100644 --- a/pkg/action/action_test.go +++ b/pkg/action/action_test.go @@ -504,6 +504,61 @@ metadata: postrenderer.helm.sh/postrender-filename: 'templates/multi.yaml' data: key: value2 +`, + }, + { + name: "normalizes glued document separator at file start", + files: map[string]string{ + "templates/glued.yaml": `---apiVersion: v1 +kind: ConfigMap +metadata: + name: test-cm +data: + key: value`, + }, + expected: `apiVersion: v1 +kind: ConfigMap +metadata: + name: test-cm + annotations: + postrenderer.helm.sh/postrender-filename: 'templates/glued.yaml' +data: + key: value +`, + }, + { + name: "normalizes glued document separator between manifests", + files: map[string]string{ + "templates/glued-multi.yaml": `apiVersion: v1 +kind: ConfigMap +metadata: + name: test-cm +data: + key: value +---apiVersion: v1 +kind: Secret +metadata: + name: test-secret +data: + password: dGVzdA==`, + }, + expected: `apiVersion: v1 +kind: ConfigMap +metadata: + name: test-cm + annotations: + postrenderer.helm.sh/postrender-filename: 'templates/glued-multi.yaml' +data: + key: value +--- +apiVersion: v1 +kind: Secret +metadata: + name: test-secret + annotations: + postrenderer.helm.sh/postrender-filename: 'templates/glued-multi.yaml' +data: + password: dGVzdA== `, }, {