Skip to content

Commit

Permalink
Fix helm template parsing (#363)
Browse files Browse the repository at this point in the history
* Fix Helm template parsing

Signed-off-by: Atanas Dinov <[email protected]>

* Update release notes

Signed-off-by: Atanas Dinov <[email protected]>

* Log warning when splitting Helm resources fails

Signed-off-by: Atanas Dinov <[email protected]>

---------

Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov authored Apr 9, 2024
1 parent 17faffd commit a48e537
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [#352](https://github.com/suse-edge/edge-image-builder/issues/352) - Resizing raw images results in dracut-pre-mount failure
* [#355](https://github.com/suse-edge/edge-image-builder/issues/355) - Helm fails getting charts stored in unauthenticated OCI registries
* [#359](https://github.com/suse-edge/edge-image-builder/issues/359) - Helm validation does not check if a chart uses an undefined repository
* [#362](https://github.com/suse-edge/edge-image-builder/issues/362) - Helm templating failure

---

Expand Down
8 changes: 5 additions & 3 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,20 @@ 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
}

source, content, found := strings.Cut(resource, "\n")
if !found {
return nil, fmt.Errorf("invalid resource: %s", resource)
zap.S().Warnf("Invalid Helm resource: %s", resource)
continue
}

var r map[string]any
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 a48e537

Please sign in to comment.