From 380abb9e79ace1b03d15985e885e4b9e42c78ac7 Mon Sep 17 00:00:00 2001 From: Dustin Lish Date: Fri, 31 Mar 2023 16:15:16 -0600 Subject: [PATCH] Use if/else for readability, add version check to absChartHome --- .../builtins/HelmChartInflationGenerator.go | 30 +++++++++---------- .../HelmChartInflationGenerator.go | 30 +++++++++---------- .../HelmChartInflationGenerator_test.go | 4 ++- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/api/internal/builtins/HelmChartInflationGenerator.go b/api/internal/builtins/HelmChartInflationGenerator.go index fefde9d060..2b0abc6bad 100644 --- a/api/internal/builtins/HelmChartInflationGenerator.go +++ b/api/internal/builtins/HelmChartInflationGenerator.go @@ -91,11 +91,11 @@ func (p *HelmChartInflationGeneratorPlugin) validateArgs() (err error) { // be under the loader root (unless root restrictions are // disabled). if p.ValuesFile == "" { - p.ValuesFile = filepath.Join(p.ChartHome, p.Name, "values.yaml") - // If the version is specified, use the versioned values file. if p.Version != "" { p.ValuesFile = filepath.Join(p.ChartHome, fmt.Sprintf("%s-%s", p.Name, p.Version), p.Name, "values.yaml") + } else { + p.ValuesFile = filepath.Join(p.ChartHome, p.Name, "values.yaml") } } for i, file := range p.AdditionalValuesFiles { @@ -137,10 +137,17 @@ func (p *HelmChartInflationGeneratorPlugin) errIfIllegalValuesMerge() error { } func (p *HelmChartInflationGeneratorPlugin) absChartHome() string { + var chartHome string if filepath.IsAbs(p.ChartHome) { - return p.ChartHome + chartHome = p.ChartHome + } else { + chartHome = filepath.Join(p.h.Loader().Root(), p.ChartHome) + } + + if p.Version != "" { + return filepath.Join(chartHome, fmt.Sprintf("%s-%s", p.Name, p.Version)) } - return filepath.Join(p.h.Loader().Root(), p.ChartHome) + return chartHome } func (p *HelmChartInflationGeneratorPlugin) runHelmCommand( @@ -254,7 +261,7 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (rm resmap.ResMap, err er return nil, err } var stdout []byte - stdout, err = p.runHelmCommand(p.AsHelmArgs(p.chartPath())) + stdout, err = p.runHelmCommand(p.AsHelmArgs(p.absChartHome())) if err != nil { return nil, err } @@ -285,7 +292,7 @@ func (p *HelmChartInflationGeneratorPlugin) pullCommand() []string { args := []string{ "pull", "--untar", - "--untardir", p.chartPath(), + "--untardir", p.absChartHome(), "--repo", p.Repo, p.Name} if p.Version != "" { @@ -297,7 +304,7 @@ func (p *HelmChartInflationGeneratorPlugin) pullCommand() []string { // chartExistsLocally will return true if the chart does exist in // local chart home. func (p *HelmChartInflationGeneratorPlugin) chartExistsLocally() (string, bool) { - path := filepath.Join(p.chartPath(), p.Name) + path := filepath.Join(p.absChartHome(), p.Name) s, err := os.Stat(path) if err != nil { return "", false @@ -329,15 +336,6 @@ func (p *HelmChartInflationGeneratorPlugin) checkHelmVersion() error { return nil } -// chartPath will return the path to the chart and handle the case where a version -// is specified -func (p *HelmChartInflationGeneratorPlugin) chartPath() string { - if p.Version != "" { - return filepath.Join(p.absChartHome(), fmt.Sprintf("%s-%s", p.Name, p.Version)) - } - return p.absChartHome() -} - func NewHelmChartInflationGeneratorPlugin() resmap.GeneratorPlugin { return &HelmChartInflationGeneratorPlugin{} } diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go index 040cae3fec..dbc6c3df72 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go @@ -97,11 +97,11 @@ func (p *plugin) validateArgs() (err error) { // be under the loader root (unless root restrictions are // disabled). if p.ValuesFile == "" { - p.ValuesFile = filepath.Join(p.ChartHome, p.Name, "values.yaml") - // If the version is specified, use the versioned values file. if p.Version != "" { p.ValuesFile = filepath.Join(p.ChartHome, fmt.Sprintf("%s-%s", p.Name, p.Version), p.Name, "values.yaml") + } else { + p.ValuesFile = filepath.Join(p.ChartHome, p.Name, "values.yaml") } } for i, file := range p.AdditionalValuesFiles { @@ -143,10 +143,17 @@ func (p *plugin) errIfIllegalValuesMerge() error { } func (p *plugin) absChartHome() string { + var chartHome string if filepath.IsAbs(p.ChartHome) { - return p.ChartHome + chartHome = p.ChartHome + } else { + chartHome = filepath.Join(p.h.Loader().Root(), p.ChartHome) + } + + if p.Version != "" { + return filepath.Join(chartHome, fmt.Sprintf("%s-%s", p.Name, p.Version)) } - return filepath.Join(p.h.Loader().Root(), p.ChartHome) + return chartHome } func (p *plugin) runHelmCommand( @@ -260,7 +267,7 @@ func (p *plugin) Generate() (rm resmap.ResMap, err error) { return nil, err } var stdout []byte - stdout, err = p.runHelmCommand(p.AsHelmArgs(p.chartPath())) + stdout, err = p.runHelmCommand(p.AsHelmArgs(p.absChartHome())) if err != nil { return nil, err } @@ -291,7 +298,7 @@ func (p *plugin) pullCommand() []string { args := []string{ "pull", "--untar", - "--untardir", p.chartPath(), + "--untardir", p.absChartHome(), "--repo", p.Repo, p.Name} if p.Version != "" { @@ -303,7 +310,7 @@ func (p *plugin) pullCommand() []string { // chartExistsLocally will return true if the chart does exist in // local chart home. func (p *plugin) chartExistsLocally() (string, bool) { - path := filepath.Join(p.chartPath(), p.Name) + path := filepath.Join(p.absChartHome(), p.Name) s, err := os.Stat(path) if err != nil { return "", false @@ -334,12 +341,3 @@ func (p *plugin) checkHelmVersion() error { } return nil } - -// chartPath will return the path to the chart and handle the case where a version -// is specified -func (p *plugin) chartPath() string { - if p.Version != "" { - return filepath.Join(p.absChartHome(), fmt.Sprintf("%s-%s", p.Name, p.Version)) - } - return p.absChartHome() -} diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go index c633d21bd6..e59eb97151 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go @@ -642,9 +642,11 @@ releaseName: %s rm := th.LoadAndRunGenerator(config) assert.True(t, len(rm.Resources()) > 0) - chartDir := fmt.Sprintf("charts/%s", tt.chartName) + var chartDir string if tt.version != "" { chartDir = fmt.Sprintf("charts/%s-%s/%s", tt.chartName, tt.version, tt.chartName) + } else { + chartDir = fmt.Sprintf("charts/%s", tt.chartName) } d, err := th.GetFSys().ReadFile(filepath.Join(th.GetRoot(), chartDir, "Chart.yaml"))