Skip to content

Commit

Permalink
Helm parse fix (#310)
Browse files Browse the repository at this point in the history
* Update helm.go

* Update RELEASE_NOTES.md

* Update helm_test.go
  • Loading branch information
dbw7 authored Mar 13, 2024
1 parent 5f08c2a commit f39a327
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* [#242](https://github.com/suse-edge/edge-image-builder/issues/242) - Empty rpms directory triggers resolution
* [#283](https://github.com/suse-edge/edge-image-builder/issues/283) - Definition file argument to EIB is incorrect
* [#245](https://github.com/suse-edge/edge-image-builder/issues/245) - Pass additional arguments to Helm resolver
* [#307](https://github.com/suse-edge/edge-image-builder/issues/307) - Helm chart parsing logic breaks if "---" is present in the chart's resources
* [#272](https://github.com/suse-edge/edge-image-builder/issues/272) - Custom files should keep their permissions

---
Expand Down
4 changes: 4 additions & 0 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ func parseChartContents(chartContents string) ([]map[string]any, error) {
continue
}

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

source, content, found := strings.Cut(resource, "\n")
if !found {
return nil, fmt.Errorf("invalid resource: %s", resource)
Expand Down
82 changes: 60 additions & 22 deletions pkg/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,30 +460,51 @@ func TestTemplateCommand(t *testing.T) {
}

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

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

assert.ErrorContains(t, err, "invalid resource")
assert.ErrorContains(t, err, "yaml: unmarshal errors:\n line 2: cannot unmarshal !!str `invalid...` into map[string]interface {}")
assert.Nil(t, resources)
}

func TestParseChartContents(t *testing.T) {
contents := `
apiVersion: helm.cattle.io/v1
kind: HelmChart
# Source: cert-manager/templates/cainjector-serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
name: metallb
namespace: metallb-system
spec:
repo: https://suse-edge.github.io/charts
chart: metallb
name: cert-manager-cainjector
namespace: default
labels:
app: cainjector
app.kubernetes.io/name: cainjector
app.kubernetes.io/instance: cert-manager
app.kubernetes.io/component: "cainjector"
app.kubernetes.io/version: "v1.14.4"
app.kubernetes.io/managed-by: Helm
helm.sh/chart: cert-manager-v1.14.4
---
# Source: cert-manager/templates/serviceaccount.yaml
apiVersion: v1
kind: Namespace
kind: ServiceAccount
automountServiceAccountToken: true
metadata:
name: metallb-system
name: cert-manager
namespace: default
labels:
app: cert-manager
app.kubernetes.io/name: cert-manager
app.kubernetes.io/instance: cert-manager
app.kubernetes.io/component: "controller"
app.kubernetes.io/version: "v1.14.4"
app.kubernetes.io/managed-by: Helm
helm.sh/chart: cert-manager-v1.14.4
`

resources, err := parseChartContents(contents)
Expand All @@ -492,23 +513,40 @@ metadata:
require.Len(t, resources, 2)

assert.Equal(t, map[string]any{
"apiVersion": "helm.cattle.io/v1",
"kind": "HelmChart",
"apiVersion": "v1",
"kind": "ServiceAccount",
"automountServiceAccountToken": true,
"metadata": map[string]any{
"name": "metallb",
"namespace": "metallb-system",
},
"spec": map[string]any{
"repo": "https://suse-edge.github.io/charts",
"chart": "metallb",
"name": "cert-manager-cainjector",
"namespace": "default",
"labels": map[string]any{
"app": "cainjector",
"app.kubernetes.io/name": "cainjector",
"app.kubernetes.io/instance": "cert-manager",
"app.kubernetes.io/component": "cainjector",
"app.kubernetes.io/version": "v1.14.4",
"app.kubernetes.io/managed-by": "Helm",
"helm.sh/chart": "cert-manager-v1.14.4",
},
},
}, resources[0])

assert.Equal(t, map[string]any{
"apiVersion": "v1",
"kind": "Namespace",
"apiVersion": "v1",
"kind": "ServiceAccount",
"automountServiceAccountToken": true,
"metadata": map[string]any{
"name": "metallb-system",
"name": "cert-manager",
"namespace": "default",
"labels": map[string]any{
"app": "cert-manager",
"app.kubernetes.io/name": "cert-manager",
"app.kubernetes.io/instance": "cert-manager",
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/version": "v1.14.4",
"app.kubernetes.io/managed-by": "Helm",
"helm.sh/chart": "cert-manager-v1.14.4",
},
},
}, resources[1])
}

0 comments on commit f39a327

Please sign in to comment.