From c0224c050941e953a5d4afc9483f23398228ab33 Mon Sep 17 00:00:00 2001 From: Elliot Kennedy Date: Thu, 17 Dec 2020 04:03:05 +0000 Subject: [PATCH] [#9136] - processDependencyImportValues should only apply import values and not additional coalesced values Signed-off-by: Elliot Kennedy --- pkg/chartutil/dependencies.go | 43 +++++++++++++++++++++--------- pkg/chartutil/dependencies_test.go | 33 ++++++++++++----------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/pkg/chartutil/dependencies.go b/pkg/chartutil/dependencies.go index d2e7d6dc9..cce0fb4b5 100644 --- a/pkg/chartutil/dependencies.go +++ b/pkg/chartutil/dependencies.go @@ -16,6 +16,7 @@ limitations under the License. package chartutil import ( + "github.com/mitchellh/copystructure" "log" "strings" @@ -23,11 +24,28 @@ import ( ) // ProcessDependencies checks through this chart's dependencies, processing accordingly. -func ProcessDependencies(c *chart.Chart, v Values) error { - if err := processDependencyEnabled(c, v, ""); err != nil { +func ProcessDependencies(c *chart.Chart, vals map[string]interface{}) error { + v, err := copystructure.Copy(vals) + if err != nil { + return err + } + + valsCopy := v.(map[string]interface{}) + // if we have an empty map, make sure it is initialized + if valsCopy == nil { + valsCopy = make(map[string]interface{}) + } + return process(c, valsCopy) +} + +// process processes the dependencies +// +// This is a helper function for ProcessDependencies. +func process(c *chart.Chart, vals map[string]interface{}) error { + if err := processDependencyEnabled(c, vals, ""); err != nil { return err } - return processDependencyImportValues(c) + return processDependencyImportValues(c, vals) } // processDependencyConditions disables charts based on condition path value in values @@ -217,12 +235,11 @@ func set(path []string, data map[string]interface{}) map[string]interface{} { } // processImportValues merges values from child to parent based on the chart's dependencies' ImportValues field. -func processImportValues(c *chart.Chart) error { +func processImportValues(c *chart.Chart, v map[string]interface{}) error { if c.Metadata.Dependencies == nil { return nil } - // combine chart values and empty config to get Values - cvals, err := CoalesceValues(c, nil) + cvals, err := CoalesceValues(c, v) if err != nil { return err } @@ -247,8 +264,8 @@ func processImportValues(c *chart.Chart) error { log.Printf("Warning: ImportValues missing table from chart %s: %v", r.Name, err) continue } - // create value map from child to be merged into parent - b = CoalesceTables(cvals, pathToMap(parent, vv.AsMap())) + // create value map from child to be merged into parent values + b = CoalesceTables(b, pathToMap(parent, vv.AsMap())) case string: child := "exports." + iv outiv = append(outiv, map[string]string{ @@ -267,19 +284,19 @@ func processImportValues(c *chart.Chart) error { r.ImportValues = outiv } - // set the new values - c.Values = CoalesceTables(b, cvals) + // merge the import values into the chart config + c.Values = CoalesceTables(b, c.Values) return nil } // processDependencyImportValues imports specified chart values from child to parent. -func processDependencyImportValues(c *chart.Chart) error { +func processDependencyImportValues(c *chart.Chart, v map[string]interface{}) error { for _, d := range c.Dependencies() { // recurse - if err := processDependencyImportValues(d); err != nil { + if err := processDependencyImportValues(d, v); err != nil { return err } } - return processImportValues(c) + return processImportValues(c, v) } diff --git a/pkg/chartutil/dependencies_test.go b/pkg/chartutil/dependencies_test.go index bcb1d40e5..4f11fb012 100644 --- a/pkg/chartutil/dependencies_test.go +++ b/pkg/chartutil/dependencies_test.go @@ -181,10 +181,11 @@ func TestProcessDependencyImportValues(t *testing.T) { e["imported-chartA-B.SPextra5"] = "k8s" e["imported-chartA-B.SC1extra5"] = "tiller" - e["overridden-chart1.SC1bool"] = "false" - e["overridden-chart1.SC1float"] = "3.141592" - e["overridden-chart1.SC1int"] = "99" - e["overridden-chart1.SC1string"] = "pollywog" + // overridden values are not applied by processDependencyImportValues + e["overridden-chart1.SC1bool"] = "true" + e["overridden-chart1.SC1float"] = "3.14" + e["overridden-chart1.SC1int"] = "100" + e["overridden-chart1.SC1string"] = "dollywood" e["overridden-chart1.SPextra2"] = "42" e["overridden-chartA.SCAbool"] = "true" @@ -194,13 +195,13 @@ func TestProcessDependencyImportValues(t *testing.T) { e["overridden-chartA.SPextra4"] = "true" e["overridden-chartA-B.SCAbool"] = "true" - e["overridden-chartA-B.SCAfloat"] = "41.3" - e["overridden-chartA-B.SCAint"] = "808" - e["overridden-chartA-B.SCAstring"] = "jabberwocky" - e["overridden-chartA-B.SCBbool"] = "false" - e["overridden-chartA-B.SCBfloat"] = "1.99" - e["overridden-chartA-B.SCBint"] = "77" - e["overridden-chartA-B.SCBstring"] = "jango" + e["overridden-chartA-B.SCAfloat"] = "3.33" + e["overridden-chartA-B.SCAint"] = "555" + e["overridden-chartA-B.SCAstring"] = "wormwood" + e["overridden-chartA-B.SCBbool"] = "true" + e["overridden-chartA-B.SCBfloat"] = "0.25" + e["overridden-chartA-B.SCBint"] = "98" + e["overridden-chartA-B.SCBstring"] = "murkwood" e["overridden-chartA-B.SPextra6"] = "111" e["overridden-chartA-B.SCAextra1"] = "23" e["overridden-chartA-B.SCBextra1"] = "13" @@ -212,7 +213,7 @@ func TestProcessDependencyImportValues(t *testing.T) { e["SCBexported2A"] = "blaster" e["global.SC1exported2.all.SC1exported3"] = "SC1expstr" - if err := processDependencyImportValues(c); err != nil { + if err := processDependencyImportValues(c, make(map[string]interface{})); err != nil { t.Fatalf("processing import values dependencies %v", err) } cc := Values(c.Values) @@ -225,15 +226,15 @@ func TestProcessDependencyImportValues(t *testing.T) { switch pv := pv.(type) { case float64: if s := strconv.FormatFloat(pv, 'f', -1, 64); s != vv { - t.Errorf("failed to match imported float value %v with expected %v", s, vv) + t.Errorf("failed to match %s, imported float value %v, expected %v", kk, s, vv) } case bool: if b := strconv.FormatBool(pv); b != vv { - t.Errorf("failed to match imported bool value %v with expected %v", b, vv) + t.Errorf("failed to match %s, imported bool value %v, expected %v", kk, b, vv) } default: if pv != vv { - t.Errorf("failed to match imported string value %q with expected %q", pv, vv) + t.Errorf("failed to match %s, imported string value %q, expected %q", kk, pv, vv) } } } @@ -243,7 +244,7 @@ func TestProcessDependencyImportValuesForEnabledCharts(t *testing.T) { c := loadChart(t, "testdata/import-values-from-enabled-subchart/parent-chart") nameOverride := "parent-chart-prod" - if err := processDependencyImportValues(c); err != nil { + if err := processDependencyImportValues(c, make(map[string]interface{})); err != nil { t.Fatalf("processing import values dependencies %v", err) }