From a119a280ae01621b6559e9252e9cf4c72eda0835 Mon Sep 17 00:00:00 2001 From: Sam McKendrick Date: Tue, 7 Apr 2020 18:54:21 +0200 Subject: [PATCH] fix(dependencies): Change early exit condition to accommodate charts with sub dependencies Signed-off-by: Sam McKendrick --- pkg/chartutil/dependencies.go | 2 +- pkg/chartutil/dependencies_test.go | 57 +++++++++++++++++++ .../parent-chart/Chart.yaml | 4 ++ .../charts/daughter-chart/Chart.yaml | 17 ++++++ .../daughter-chart/charts/dev/Chart.yaml | 4 ++ .../charts/dev/templates/secret.yaml | 7 +++ .../daughter-chart/charts/prod/Chart.yaml | 4 ++ .../charts/prod/templates/secret.yaml | 7 +++ .../daughter-chart/charts/staging/Chart.yaml | 4 ++ .../charts/staging/templates/secret.yaml | 7 +++ .../charts/daughter-chart/values.yaml | 7 +++ .../parent-chart/values.yaml | 6 ++ 12 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/Chart.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/Chart.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/Chart.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/templates/secret.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/Chart.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/templates/secret.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/Chart.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/templates/secret.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/values.yaml create mode 100644 pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/values.yaml diff --git a/pkg/chartutil/dependencies.go b/pkg/chartutil/dependencies.go index d2e7d6dc9..755c261ef 100644 --- a/pkg/chartutil/dependencies.go +++ b/pkg/chartutil/dependencies.go @@ -116,7 +116,7 @@ func getAliasDependency(charts []*chart.Chart, dep *chart.Dependency) *chart.Cha // processDependencyEnabled removes disabled charts from dependencies func processDependencyEnabled(c *chart.Chart, v map[string]interface{}, path string) error { - if c.Metadata.Dependencies == nil { + if c.Metadata.Dependencies == nil && len(c.Dependencies()) == 0 { return nil } diff --git a/pkg/chartutil/dependencies_test.go b/pkg/chartutil/dependencies_test.go index 342d7fe87..308c72210 100644 --- a/pkg/chartutil/dependencies_test.go +++ b/pkg/chartutil/dependencies_test.go @@ -269,6 +269,63 @@ func TestProcessDependencyImportValuesForEnabledCharts(t *testing.T) { } } +func TestProcessDependencyEnabledRespectsChildConditionsWhenParentLacksDependenciesBlock(t *testing.T) { + c := loadChart(t, "testdata/respect-conditions-without-dependencies-block/parent-chart") + + if err := processDependencyImportValues(c); err != nil { + t.Fatalf("processing import values dependencies %v", err) + } + + if len(c.Dependencies()) != 1 { + t.Fatalf("expected 1 dependency for this chart, but got %d", len(c.Dependencies())) + } + + if len(c.Metadata.Dependencies) != 0 { + t.Fatalf("expected 0 dependencies specified in parent chart's Chart.yaml, got %d", len(c.Metadata.Dependencies)) + } + + if len(c.Dependencies()[0].Dependencies()) != 3 { + t.Fatalf("expected 3 dependencies for the subchart, but got %d", len(c.Dependencies())) + } + + if len(c.Dependencies()[0].Metadata.Dependencies) != 3 { + t.Fatalf("expected 3 dependencies specified in subchart's Chart.yaml, got %d", len(c.Dependencies()[0].Metadata.Dependencies)) + } + + if err := processDependencyEnabled(c, c.Values, ""); err != nil { + t.Fatalf("expected no errors but got %q", err) + } + + if len(c.Dependencies()) != 1 { + t.Fatalf("expected 1 dependency for this chart, but got %d", len(c.Dependencies())) + } + + if len(c.Dependencies()[0].Dependencies()) != 2 { + t.Fatalf("expected 2 dependencies for this chart, but got %d", len(c.Dependencies()[0].Dependencies())) + } + + if len(c.Metadata.Dependencies) != 0 { + t.Fatalf("expected 0 dependencies specified in Chart.yaml, got %d", len(c.Metadata.Dependencies)) + } + + if len(c.Dependencies()[0].Metadata.Dependencies) != 2 { + t.Fatalf("expected 2 dependencies specified in subchart's Chart.yaml, got %d", len(c.Dependencies()[0].Metadata.Dependencies)) + } + + subChartDependencyChart0Name := c.Dependencies()[0].Dependencies()[0].Name() + subChartDependencyChart1Name := c.Dependencies()[0].Dependencies()[1].Name() + devChartName := "dev" + prodChartName := "prod" + + if subChartDependencyChart0Name != devChartName { + t.Fatalf("subchart's dependency chart name should be %s but got %s", devChartName, subChartDependencyChart1Name) + } + + if subChartDependencyChart1Name != prodChartName { + t.Fatalf("subchart's dependency chart name should be %s but got %s", prodChartName, subChartDependencyChart1Name) + } +} + func TestGetAliasDependency(t *testing.T) { c := loadChart(t, "testdata/frobnitz") req := c.Metadata.Dependencies diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/Chart.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/Chart.yaml new file mode 100644 index 000000000..5718cdc7a --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: parent-chart +version: v0.1.0 +appVersion: v0.1.0 \ No newline at end of file diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/Chart.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/Chart.yaml new file mode 100644 index 000000000..8676c715a --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/Chart.yaml @@ -0,0 +1,17 @@ +apiVersion: v2 +name: daughter-chart +version: v0.1.0 +appVersion: v0.1.0 +dependencies: + - name: dev + repository: "file://charts/dev" + version: ">= 0.0.1" + condition: global.dev.enabled,dev.enabled + - name: staging + repository: "file://charts/staging" + version: ">= 0.0.1" + condition: staging.enabled,global.staging.enabled + - name: prod + repository: "file://charts/prod" + version: ">= 0.0.1" + condition: global.prod.enabled,prod.enabled diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/Chart.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/Chart.yaml new file mode 100644 index 000000000..80a52f538 --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: dev +version: v0.1.0 +appVersion: v0.1.0 \ No newline at end of file diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/templates/secret.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/templates/secret.yaml new file mode 100644 index 000000000..b5af83dc4 --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/dev/templates/secret.yaml @@ -0,0 +1,7 @@ +--- +apiVersion: v1 +kind: Secret +metadata: + name: dev-test +stringData: + key: value diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/Chart.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/Chart.yaml new file mode 100644 index 000000000..bda4be458 --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: prod +version: v0.1.0 +appVersion: v0.1.0 \ No newline at end of file diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/templates/secret.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/templates/secret.yaml new file mode 100644 index 000000000..4852225d0 --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/prod/templates/secret.yaml @@ -0,0 +1,7 @@ +--- +apiVersion: v1 +kind: Secret +metadata: + name: prod-test +stringData: + key: value diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/Chart.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/Chart.yaml new file mode 100644 index 000000000..f9054a80d --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: staging +version: v0.1.0 +appVersion: v0.1.0 \ No newline at end of file diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/templates/secret.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/templates/secret.yaml new file mode 100644 index 000000000..475c543ca --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/charts/staging/templates/secret.yaml @@ -0,0 +1,7 @@ +--- +apiVersion: v1 +kind: Secret +metadata: + name: staging-test +stringData: + key: value diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/values.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/values.yaml new file mode 100644 index 000000000..951f93242 --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/charts/daughter-chart/values.yaml @@ -0,0 +1,7 @@ +# Default values for daughter-chart. +dev: + enabled: false +staging: + enabled: false +prod: + enabled: true diff --git a/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/values.yaml b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/values.yaml new file mode 100644 index 000000000..b616913cc --- /dev/null +++ b/pkg/chartutil/testdata/respect-conditions-without-dependencies-block/parent-chart/values.yaml @@ -0,0 +1,6 @@ +# Default values for parent-chart. +global: + dev: + enabled: true + staging: + enabled: true