diff --git a/api/builtins/HelmChartInflationGenerator.go b/api/builtins/HelmChartInflationGenerator.go index ff1921500..e03d5abad 100644 --- a/api/builtins/HelmChartInflationGenerator.go +++ b/api/builtins/HelmChartInflationGenerator.go @@ -10,7 +10,7 @@ import ( "io/ioutil" "os" "os/exec" - "path" + "path/filepath" "regexp" "strings" @@ -51,7 +51,7 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf return fmt.Errorf("chartName cannot be empty") } if p.ChartHome == "" { - p.ChartHome = path.Join(p.tmpDir, "chart") + p.ChartHome = filepath.Join(p.tmpDir, "chart") } if p.ChartRepoName == "" { p.ChartRepoName = "stable" @@ -60,10 +60,10 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf p.HelmBin = "helm" } if p.HelmHome == "" { - p.HelmHome = path.Join(p.tmpDir, ".helm") + p.HelmHome = filepath.Join(p.tmpDir, ".helm") } if p.Values == "" { - p.Values = path.Join(p.ChartHome, p.ChartName, "values.yaml") + p.Values = filepath.Join(p.ChartHome, p.ChartName, "values.yaml") } if p.ValuesMerge == "" { p.ValuesMerge = "override" @@ -109,17 +109,16 @@ func (p *HelmChartInflationGeneratorPlugin) EncodeValues(w io.Writer) error { // useValuesLocal process (merge) inflator config provided values with chart default values.yaml func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error { - fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") - vf, err := os.Create(fn) - defer vf.Close() - if err != nil { - return err - } - // override, merge, none - if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" { - p.Values = fn - } else { - pValues, err := ioutil.ReadFile(p.Values) + // not override, merge, none + if !(p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false") { + var pValues []byte + var err error + + if filepath.IsAbs(p.Values) { + pValues, err = ioutil.ReadFile(p.Values) + } else { + pValues, err = p.h.Loader().Load(p.Values) + } if err != nil { return err } @@ -141,16 +140,48 @@ func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error { } } p.ValuesLocal = chValues - p.Values = fn } - err = p.EncodeValues(vf) + b, err := yaml.Marshal(p.ValuesLocal) if err != nil { return err } - vf.Sync() + path, err := p.writeValuesBytes(b) + if err != nil { + return err + } + p.Values = path return nil } +// copyValues will copy the relative values file into the temp directory +// to avoid messing up with CWD. +func (p *HelmChartInflationGeneratorPlugin) copyValues() error { + // only copy when the values path is not absolute + if filepath.IsAbs(p.Values) { + return nil + } + // we must use use loader to read values file + b, err := p.h.Loader().Load(p.Values) + if err != nil { + return err + } + path, err := p.writeValuesBytes(b) + if err != nil { + return err + } + p.Values = path + return nil +} + +func (p *HelmChartInflationGeneratorPlugin) writeValuesBytes(b []byte) (string, error) { + path := filepath.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") + err := ioutil.WriteFile(path, b, 0644) + if err != nil { + return "", err + } + return path, nil +} + // Generate implements generator func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { // cleanup @@ -174,6 +205,11 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { if err != nil { return nil, err } + } else { + err := p.copyValues() + if err != nil { + return nil, err + } } // render the charts @@ -190,7 +226,7 @@ func (p *HelmChartInflationGeneratorPlugin) getTemplateCommandArgs() []string { if p.ReleaseName != "" { args = append(args, p.ReleaseName) } - args = append(args, path.Join(p.ChartHome, p.ChartName)) + args = append(args, filepath.Join(p.ChartHome, p.ChartName)) if p.ReleaseNamespace != "" { args = append(args, "--namespace", p.ReleaseNamespace) } @@ -220,7 +256,7 @@ func (p *HelmChartInflationGeneratorPlugin) getPullCommandArgs() []string { // checkLocalChart will return true if the chart does exist in // local chart home. func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool { - path := path.Join(p.ChartHome, p.ChartName) + path := filepath.Join(p.ChartHome, p.ChartName) s, err := os.Stat(path) if err != nil { return false diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go index d297f0cf4..04cc7591c 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go @@ -17,7 +17,7 @@ import ( "io/ioutil" "os" "os/exec" - "path" + "path/filepath" "regexp" "strings" @@ -59,7 +59,7 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf return fmt.Errorf("chartName cannot be empty") } if p.ChartHome == "" { - p.ChartHome = path.Join(p.tmpDir, "chart") + p.ChartHome = filepath.Join(p.tmpDir, "chart") } if p.ChartRepoName == "" { p.ChartRepoName = "stable" @@ -68,10 +68,10 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf p.HelmBin = "helm" } if p.HelmHome == "" { - p.HelmHome = path.Join(p.tmpDir, ".helm") + p.HelmHome = filepath.Join(p.tmpDir, ".helm") } if p.Values == "" { - p.Values = path.Join(p.ChartHome, p.ChartName, "values.yaml") + p.Values = filepath.Join(p.ChartHome, p.ChartName, "values.yaml") } if p.ValuesMerge == "" { p.ValuesMerge = "override" @@ -117,17 +117,16 @@ func (p *HelmChartInflationGeneratorPlugin) EncodeValues(w io.Writer) error { // useValuesLocal process (merge) inflator config provided values with chart default values.yaml func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error { - fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") - vf, err := os.Create(fn) - defer vf.Close() - if err != nil { - return err - } - // override, merge, none - if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" { - p.Values = fn - } else { - pValues, err := ioutil.ReadFile(p.Values) + // not override, merge, none + if !(p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false") { + var pValues []byte + var err error + + if filepath.IsAbs(p.Values) { + pValues, err = ioutil.ReadFile(p.Values) + } else { + pValues, err = p.h.Loader().Load(p.Values) + } if err != nil { return err } @@ -149,16 +148,48 @@ func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error { } } p.ValuesLocal = chValues - p.Values = fn } - err = p.EncodeValues(vf) + b, err := yaml.Marshal(p.ValuesLocal) if err != nil { return err } - vf.Sync() + path, err := p.writeValuesBytes(b) + if err != nil { + return err + } + p.Values = path return nil } +// copyValues will copy the relative values file into the temp directory +// to avoid messing up with CWD. +func (p *HelmChartInflationGeneratorPlugin) copyValues() error { + // only copy when the values path is not absolute + if filepath.IsAbs(p.Values) { + return nil + } + // we must use use loader to read values file + b, err := p.h.Loader().Load(p.Values) + if err != nil { + return err + } + path, err := p.writeValuesBytes(b) + if err != nil { + return err + } + p.Values = path + return nil +} + +func (p *HelmChartInflationGeneratorPlugin) writeValuesBytes(b []byte) (string, error) { + path := filepath.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") + err := ioutil.WriteFile(path, b, 0644) + if err != nil { + return "", err + } + return path, nil +} + // Generate implements generator func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { // cleanup @@ -182,6 +213,11 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { if err != nil { return nil, err } + } else { + err := p.copyValues() + if err != nil { + return nil, err + } } // render the charts @@ -198,7 +234,7 @@ func (p *HelmChartInflationGeneratorPlugin) getTemplateCommandArgs() []string { if p.ReleaseName != "" { args = append(args, p.ReleaseName) } - args = append(args, path.Join(p.ChartHome, p.ChartName)) + args = append(args, filepath.Join(p.ChartHome, p.ChartName)) if p.ReleaseNamespace != "" { args = append(args, "--namespace", p.ReleaseNamespace) } @@ -228,7 +264,7 @@ func (p *HelmChartInflationGeneratorPlugin) getPullCommandArgs() []string { // checkLocalChart will return true if the chart does exist in // local chart home. func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool { - path := path.Join(p.ChartHome, p.ChartName) + path := filepath.Join(p.ChartHome, p.ChartName) s, err := os.Stat(path) if err != nil { return false diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go index b09160fec..efc54eb3e 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go @@ -23,7 +23,7 @@ kind: HelmChartInflationGenerator metadata: name: myMap chartName: minecraft -chartRepoUrl: https://kubernetes-charts.storage.googleapis.com +chartRepoUrl: https://charts.helm.sh/stable chartVersion: v1.2.0 releaseName: test releaseNamespace: testNamespace @@ -105,7 +105,7 @@ kind: HelmChartInflationGenerator metadata: name: myMap chartName: minecraft -chartRepoUrl: https://kubernetes-charts.storage.googleapis.com +chartRepoUrl: https://charts.helm.sh/stable chartVersion: v1.2.0 helmBin: helm helmHome: %s @@ -113,7 +113,6 @@ chartHome: %s releaseName: test releaseNamespace: testNamespace values: %s -`, tempDir, tempDir, valuesPath)) valuesLocal: resources: limits: @@ -122,6 +121,7 @@ valuesLocal: requests: memory: 512Mi cpu: 200m +`, tempDir, tempDir, valuesPath)) th.AssertActualEqualsExpected(rm, ` apiVersion: v1 @@ -290,8 +290,11 @@ spec: successThreshold: 1 timeoutSeconds: 1 resources: + limits: + cpu: 1000m + memory: 512Mi requests: - cpu: 500m + cpu: 200m memory: 512Mi volumeMounts: - mountPath: /data