Skip to content

Commit

Permalink
fix relative path to values file
Browse files Browse the repository at this point in the history
  • Loading branch information
Shell32-Natsu committed Feb 1, 2021
1 parent 3892e3c commit 507244e
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 44 deletions.
76 changes: 56 additions & 20 deletions api/builtins/HelmChartInflationGenerator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -17,7 +17,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
path, err := p.writeValuesBytes(b)
if err != nil {
return err
}
vf.Sync()
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
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -105,15 +105,14 @@ 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
chartHome: %s
releaseName: test
releaseNamespace: testNamespace
values: %s
`, tempDir, tempDir, valuesPath))
valuesLocal:
resources:
limits:
Expand All @@ -122,6 +121,7 @@ valuesLocal:
requests:
memory: 512Mi
cpu: 200m
`, tempDir, tempDir, valuesPath))
th.AssertActualEqualsExpected(rm, `
apiVersion: v1
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 507244e

Please sign in to comment.