Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Helm parse fix #310

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
}
Loading