From 89f64183cb90491bd62b9d887dbe21a6991f2b2b Mon Sep 17 00:00:00 2001 From: Withnale Date: Sat, 9 Dec 2017 17:54:41 +0000 Subject: [PATCH] Added support for export-values in requirements --- cmd/helm/template.go | 4 ++ pkg/chartutil/requirements.go | 67 +++++++++++++++++++ pkg/chartutil/requirements_test.go | 8 +++ .../testdata/subpop/requirements.yaml | 3 + 4 files changed, 82 insertions(+) diff --git a/cmd/helm/template.go b/cmd/helm/template.go index 3ca364cfb..f09d28a4c 100644 --- a/cmd/helm/template.go +++ b/cmd/helm/template.go @@ -186,6 +186,10 @@ func (t *templateCmd) run(cmd *cobra.Command, args []string) error { if err != nil { return err } + err = chartutil.ProcessRequirementsExportValues(c) + if err != nil { + return err + } // Set up engine. renderer := engine.New() diff --git a/pkg/chartutil/requirements.go b/pkg/chartutil/requirements.go index ce761a6fc..dcd13de0e 100644 --- a/pkg/chartutil/requirements.go +++ b/pkg/chartutil/requirements.go @@ -66,6 +66,9 @@ type Dependency struct { // ImportValues holds the mapping of source values to parent key to be imported. Each item can be a // string or pair of child/parent sublist items. ImportValues []interface{} `json:"import-values,omitempty"` + // ExportValues holds the mapping of source values to parent key to be exported. Each item can be a + // string or pair of child/parent sublist items. + ExportValues []interface{} `json:"export-values,omitempty"` // Alias usable alias to be used for the chart Alias string `json:"alias,omitempty"` } @@ -445,6 +448,60 @@ func processImportValues(c *chart.Chart) error { return nil } +// processImportValues merges values from child to parent based on the chart's dependencies' ImportValues field. +func processExportValues(c *chart.Chart) error { + reqs, err := LoadRequirements(c) + if err != nil { + return err + } + // combine chart values and empty config to get Values + cvals, err := CoalesceValues(c, &chart.Config{}) + if err != nil { + return err + } + b := make(map[string]interface{}, 0) + // import values from each dependency if specified in import-values + for _, r := range reqs.Dependencies { + if len(r.ExportValues) > 0 { + var outiv []interface{} + for _, riv := range r.ExportValues { + iv := riv.(map[string]interface{}) + nm := map[string]string{ + "child": iv["child"].(string), + "parent": iv["parent"].(string), + } + outiv = append(outiv, nm) + s := nm["parent"] + // get child table + vv, err := cvals.Table(s) + if err != nil { + log.Printf("Warning: ExportValues missing table: %v", err) + continue + } + name := r.Name + if r.Alias != "" { + name = r.Alias + } + // create value map from child to be merged into parent + vm := pathToMap(name+"."+nm["child"], vv.AsMap()) + b = coalesceTables(cvals, vm) + } + // set our formatted import values + r.ExportValues = outiv + } + } + b = coalesceTables(b, cvals) + y, err := yaml.Marshal(b) + if err != nil { + return err + } + + // set the new values + c.Values = &chart.Config{Raw: string(y)} + + return nil +} + // ProcessRequirementsImportValues imports specified chart values from child to parent. func ProcessRequirementsImportValues(c *chart.Chart) error { pc := getParents(c, nil) @@ -454,3 +511,13 @@ func ProcessRequirementsImportValues(c *chart.Chart) error { return nil } + +// ProcessRequirementsExportValues exports specified chart values from parent to child. +func ProcessRequirementsExportValues(c *chart.Chart) error { + pc := getParents(c, nil) + for i := len(pc) - 1; i >= 0; i-- { + processExportValues(pc[i]) + } + + return nil +} diff --git a/pkg/chartutil/requirements_test.go b/pkg/chartutil/requirements_test.go index 24388f86c..c1378d8d2 100644 --- a/pkg/chartutil/requirements_test.go +++ b/pkg/chartutil/requirements_test.go @@ -279,6 +279,10 @@ func TestProcessRequirementsImportValues(t *testing.T) { e["SCBexported2A"] = "blaster" e["global.SC1exported2.all.SC1exported3"] = "SC1expstr" + // `imports` + e["subchart1.mytags.back-end"] = "false" + e["subchart1.mytags.front-end"] = "true" + verifyRequirementsImportValues(t, c, v, e) } func verifyRequirementsImportValues(t *testing.T, c *chart.Chart, v *chart.Config, e map[string]string) { @@ -287,6 +291,10 @@ func verifyRequirementsImportValues(t *testing.T, c *chart.Chart, v *chart.Confi if err != nil { t.Errorf("Error processing import values requirements %v", err) } + err = ProcessRequirementsExportValues(c) + if err != nil { + t.Errorf("Error processing import values requirements %v", err) + } cv := c.GetValues() cc, err := ReadValues([]byte(cv.Raw)) if err != nil { diff --git a/pkg/chartutil/testdata/subpop/requirements.yaml b/pkg/chartutil/testdata/subpop/requirements.yaml index a8eb0aace..7104c7316 100644 --- a/pkg/chartutil/testdata/subpop/requirements.yaml +++ b/pkg/chartutil/testdata/subpop/requirements.yaml @@ -21,6 +21,9 @@ dependencies: parent: . - SCBexported2 - SC1exported1 + export-values: + - child: mytags + parent: tags - name: subchart2 repository: http://localhost:10191