From 494977b9d086c8e3de0a40c419c5a5b080a43187 Mon Sep 17 00:00:00 2001 From: Petr Michalec Date: Tue, 15 Dec 2020 08:38:59 +0100 Subject: [PATCH] helm values on inflator config - builtin Signed-off-by: Petr Michalec --- api/builtins/HelmChartInflationGenerator.go | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/api/builtins/HelmChartInflationGenerator.go b/api/builtins/HelmChartInflationGenerator.go index 78150aef4..b79b3e319 100644 --- a/api/builtins/HelmChartInflationGenerator.go +++ b/api/builtins/HelmChartInflationGenerator.go @@ -6,12 +6,15 @@ package builtins import ( "bytes" "fmt" + "io" + "io/ioutil" "os" "os/exec" "path" "regexp" "strings" + "github.com/imdario/mergo" "github.com/pkg/errors" "sigs.k8s.io/kustomize/api/filesys" "sigs.k8s.io/kustomize/api/resmap" @@ -62,6 +65,9 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf if p.Values == "" { p.Values = path.Join(p.ChartHome, p.ChartName, "values.yaml") } + if p.ValuesMerge == "" { + p.ValuesMerge = "override" + } // runHelmCommand will run `helm` command with args provided. Return stdout // and error if there is any. p.runHelmCommand = func(args []string) ([]byte, error) { @@ -88,6 +94,21 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf return nil } +// EncodeValues for writing +func (p *HelmChartInflationGeneratorPlugin) EncodeValues(w io.Writer) error { + d, err := yaml.Marshal(p.ValuesLocal) + if err != nil { + return err + } + _, err = w.Write(d) + if err != nil { + return err + } + return nil +} + +// + // Generate implements generator func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { // cleanup @@ -104,6 +125,50 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { return nil, err } } + + // values + if len(p.ValuesLocal) > 0 { + fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") + vf, err := os.Create(fn) + defer vf.Close() + if err != nil { + return nil, err + } + // override, merge, none + if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" { + p.Values = fn + } else { + pValues, err := ioutil.ReadFile(p.Values) + if err != nil { + return nil, err + } + chValues := make(map[string]interface{}) + err = yaml.Unmarshal(pValues, &chValues) + if err != nil { + return nil, err + } + if p.ValuesMerge == "override" { + err = mergo.Merge(&chValues, p.ValuesLocal, mergo.WithOverride) + if err != nil { + return nil, err + } + } + if p.ValuesMerge == "merge" { + err = mergo.Merge(&chValues, p.ValuesLocal) + if err != nil { + return nil, err + } + } + p.ValuesLocal = chValues + p.Values = fn + } + err = p.EncodeValues(vf) + if err != nil { + return nil, err + } + vf.Sync() + } + // render the charts stdout, err := p.runHelmCommand(p.getTemplateCommandArgs()) if err != nil {