Skip to content

Commit

Permalink
Fix Helm template parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Apr 9, 2024
1 parent 17faffd commit 5d84462
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,13 @@ func templateCommand(chart, repository, version, valuesFilePath, kubeVersion, ta
func parseChartContents(chartContents string) ([]map[string]any, error) {
var resources []map[string]any

for _, resource := range strings.Split(chartContents, "---") {
for _, resource := range strings.Split(chartContents, "---\n") {
if resource == "" {
continue
}

if !strings.HasPrefix(strings.TrimSpace(resource), "# Source") {
resource = strings.TrimSpace(resource)
if !strings.HasPrefix(resource, "# Source") {
continue
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,14 +559,15 @@ func TestTemplateCommand(t *testing.T) {

func TestParseChartContents_InvalidPayload(t *testing.T) {
contents := `---
# Source
# Source: some-invalid.yaml
invalid-resource
`

resources, err := parseChartContents(contents)
require.Error(t, err)

assert.ErrorContains(t, err, "yaml: unmarshal errors:\n line 2: cannot unmarshal !!str `invalid...` into map[string]interface {}")
assert.ErrorContains(t, err, "decoding resource from source '# Source: some-invalid.yaml'")
assert.ErrorContains(t, err, "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into map[string]interface {}")
assert.Nil(t, resources)
}

Expand Down

0 comments on commit 5d84462

Please sign in to comment.