fix relative path to values file

This commit is contained in:
Donny Xia
2021-02-01 13:55:48 -08:00
parent 3892e3c910
commit 507244e6f8
3 changed files with 119 additions and 44 deletions

View File

@@ -10,7 +10,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@@ -51,7 +51,7 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
return fmt.Errorf("chartName cannot be empty") return fmt.Errorf("chartName cannot be empty")
} }
if p.ChartHome == "" { if p.ChartHome == "" {
p.ChartHome = path.Join(p.tmpDir, "chart") p.ChartHome = filepath.Join(p.tmpDir, "chart")
} }
if p.ChartRepoName == "" { if p.ChartRepoName == "" {
p.ChartRepoName = "stable" p.ChartRepoName = "stable"
@@ -60,10 +60,10 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
p.HelmBin = "helm" p.HelmBin = "helm"
} }
if p.HelmHome == "" { if p.HelmHome == "" {
p.HelmHome = path.Join(p.tmpDir, ".helm") p.HelmHome = filepath.Join(p.tmpDir, ".helm")
} }
if p.Values == "" { 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 == "" { if p.ValuesMerge == "" {
p.ValuesMerge = "override" 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 // useValuesLocal process (merge) inflator config provided values with chart default values.yaml
func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error { func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") // not override, merge, none
vf, err := os.Create(fn) if !(p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false") {
defer vf.Close() var pValues []byte
if err != nil { var err error
return err
} if filepath.IsAbs(p.Values) {
// override, merge, none pValues, err = ioutil.ReadFile(p.Values)
if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" { } else {
p.Values = fn pValues, err = p.h.Loader().Load(p.Values)
} else { }
pValues, err := ioutil.ReadFile(p.Values)
if err != nil { if err != nil {
return err return err
} }
@@ -141,16 +140,48 @@ func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
} }
} }
p.ValuesLocal = chValues p.ValuesLocal = chValues
p.Values = fn
} }
err = p.EncodeValues(vf) b, err := yaml.Marshal(p.ValuesLocal)
if err != nil { if err != nil {
return err return err
} }
vf.Sync() path, err := p.writeValuesBytes(b)
if err != nil {
return err
}
p.Values = path
return nil 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 // Generate implements generator
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
// cleanup // cleanup
@@ -174,6 +205,11 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
err := p.copyValues()
if err != nil {
return nil, err
}
} }
// render the charts // render the charts
@@ -190,7 +226,7 @@ func (p *HelmChartInflationGeneratorPlugin) getTemplateCommandArgs() []string {
if p.ReleaseName != "" { if p.ReleaseName != "" {
args = append(args, 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 != "" { if p.ReleaseNamespace != "" {
args = append(args, "--namespace", 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 // checkLocalChart will return true if the chart does exist in
// local chart home. // local chart home.
func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool { func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool {
path := path.Join(p.ChartHome, p.ChartName) path := filepath.Join(p.ChartHome, p.ChartName)
s, err := os.Stat(path) s, err := os.Stat(path)
if err != nil { if err != nil {
return false return false

View File

@@ -17,7 +17,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@@ -59,7 +59,7 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
return fmt.Errorf("chartName cannot be empty") return fmt.Errorf("chartName cannot be empty")
} }
if p.ChartHome == "" { if p.ChartHome == "" {
p.ChartHome = path.Join(p.tmpDir, "chart") p.ChartHome = filepath.Join(p.tmpDir, "chart")
} }
if p.ChartRepoName == "" { if p.ChartRepoName == "" {
p.ChartRepoName = "stable" p.ChartRepoName = "stable"
@@ -68,10 +68,10 @@ func (p *HelmChartInflationGeneratorPlugin) Config(h *resmap.PluginHelpers, conf
p.HelmBin = "helm" p.HelmBin = "helm"
} }
if p.HelmHome == "" { if p.HelmHome == "" {
p.HelmHome = path.Join(p.tmpDir, ".helm") p.HelmHome = filepath.Join(p.tmpDir, ".helm")
} }
if p.Values == "" { 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 == "" { if p.ValuesMerge == "" {
p.ValuesMerge = "override" 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 // useValuesLocal process (merge) inflator config provided values with chart default values.yaml
func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error { func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
fn := path.Join(p.ChartHome, p.ChartName, "kustomize-values.yaml") // not override, merge, none
vf, err := os.Create(fn) if !(p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false") {
defer vf.Close() var pValues []byte
if err != nil { var err error
return err
} if filepath.IsAbs(p.Values) {
// override, merge, none pValues, err = ioutil.ReadFile(p.Values)
if p.ValuesMerge == "none" || p.ValuesMerge == "no" || p.ValuesMerge == "false" { } else {
p.Values = fn pValues, err = p.h.Loader().Load(p.Values)
} else { }
pValues, err := ioutil.ReadFile(p.Values)
if err != nil { if err != nil {
return err return err
} }
@@ -149,16 +148,48 @@ func (p *HelmChartInflationGeneratorPlugin) useValuesLocal() error {
} }
} }
p.ValuesLocal = chValues p.ValuesLocal = chValues
p.Values = fn
} }
err = p.EncodeValues(vf) b, err := yaml.Marshal(p.ValuesLocal)
if err != nil { if err != nil {
return err return err
} }
vf.Sync() path, err := p.writeValuesBytes(b)
if err != nil {
return err
}
p.Values = path
return nil 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 // Generate implements generator
func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) { func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
// cleanup // cleanup
@@ -182,6 +213,11 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (resmap.ResMap, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else {
err := p.copyValues()
if err != nil {
return nil, err
}
} }
// render the charts // render the charts
@@ -198,7 +234,7 @@ func (p *HelmChartInflationGeneratorPlugin) getTemplateCommandArgs() []string {
if p.ReleaseName != "" { if p.ReleaseName != "" {
args = append(args, 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 != "" { if p.ReleaseNamespace != "" {
args = append(args, "--namespace", 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 // checkLocalChart will return true if the chart does exist in
// local chart home. // local chart home.
func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool { func (p *HelmChartInflationGeneratorPlugin) checkLocalChart() bool {
path := path.Join(p.ChartHome, p.ChartName) path := filepath.Join(p.ChartHome, p.ChartName)
s, err := os.Stat(path) s, err := os.Stat(path)
if err != nil { if err != nil {
return false return false

View File

@@ -23,7 +23,7 @@ kind: HelmChartInflationGenerator
metadata: metadata:
name: myMap name: myMap
chartName: minecraft chartName: minecraft
chartRepoUrl: https://kubernetes-charts.storage.googleapis.com chartRepoUrl: https://charts.helm.sh/stable
chartVersion: v1.2.0 chartVersion: v1.2.0
releaseName: test releaseName: test
releaseNamespace: testNamespace releaseNamespace: testNamespace
@@ -105,7 +105,7 @@ kind: HelmChartInflationGenerator
metadata: metadata:
name: myMap name: myMap
chartName: minecraft chartName: minecraft
chartRepoUrl: https://kubernetes-charts.storage.googleapis.com chartRepoUrl: https://charts.helm.sh/stable
chartVersion: v1.2.0 chartVersion: v1.2.0
helmBin: helm helmBin: helm
helmHome: %s helmHome: %s
@@ -113,7 +113,6 @@ chartHome: %s
releaseName: test releaseName: test
releaseNamespace: testNamespace releaseNamespace: testNamespace
values: %s values: %s
`, tempDir, tempDir, valuesPath))
valuesLocal: valuesLocal:
resources: resources:
limits: limits:
@@ -122,6 +121,7 @@ valuesLocal:
requests: requests:
memory: 512Mi memory: 512Mi
cpu: 200m cpu: 200m
`, tempDir, tempDir, valuesPath))
th.AssertActualEqualsExpected(rm, ` th.AssertActualEqualsExpected(rm, `
apiVersion: v1 apiVersion: v1
@@ -290,8 +290,11 @@ spec:
successThreshold: 1 successThreshold: 1
timeoutSeconds: 1 timeoutSeconds: 1
resources: resources:
limits:
cpu: 1000m
memory: 512Mi
requests: requests:
cpu: 500m cpu: 200m
memory: 512Mi memory: 512Mi
volumeMounts: volumeMounts:
- mountPath: /data - mountPath: /data