From 936a14c0f8a43a3190595de7fa0d765a6ae7c9a7 Mon Sep 17 00:00:00 2001 From: Dustin Lish Date: Wed, 22 Mar 2023 13:19:40 -0600 Subject: [PATCH] Updated: Fix using same helm chart with different versions --- .../builtins/HelmChartInflationGenerator.go | 20 ++++++++++++++++--- .../HelmChartInflationGenerator.go | 20 ++++++++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/api/internal/builtins/HelmChartInflationGenerator.go b/api/internal/builtins/HelmChartInflationGenerator.go index 113f56ea7cf..fefde9d0609 100644 --- a/api/internal/builtins/HelmChartInflationGenerator.go +++ b/api/internal/builtins/HelmChartInflationGenerator.go @@ -92,6 +92,11 @@ func (p *HelmChartInflationGeneratorPlugin) validateArgs() (err error) { // 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") + } } for i, file := range p.AdditionalValuesFiles { // use Load() to enforce root restrictions @@ -249,7 +254,7 @@ func (p *HelmChartInflationGeneratorPlugin) Generate() (rm resmap.ResMap, err er return nil, err } var stdout []byte - stdout, err = p.runHelmCommand(p.AsHelmArgs(p.absChartHome())) + stdout, err = p.runHelmCommand(p.AsHelmArgs(p.chartPath())) if err != nil { return nil, err } @@ -280,7 +285,7 @@ func (p *HelmChartInflationGeneratorPlugin) pullCommand() []string { args := []string{ "pull", "--untar", - "--untardir", p.absChartHome(), + "--untardir", p.chartPath(), "--repo", p.Repo, p.Name} if p.Version != "" { @@ -292,7 +297,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.absChartHome(), p.Name) + path := filepath.Join(p.chartPath(), p.Name) s, err := os.Stat(path) if err != nil { return "", false @@ -324,6 +329,15 @@ 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 dd7d5b50837..040cae3fec8 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go @@ -97,10 +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 { @@ -259,7 +260,7 @@ func (p *plugin) Generate() (rm resmap.ResMap, err error) { return nil, err } var stdout []byte - stdout, err = p.runHelmCommand(p.AsHelmArgs(p.absChartHome())) + stdout, err = p.runHelmCommand(p.AsHelmArgs(p.chartPath())) if err != nil { return nil, err } @@ -290,7 +291,7 @@ func (p *plugin) pullCommand() []string { args := []string{ "pull", "--untar", - "--untardir", p.absChartHome(), + "--untardir", p.chartPath(), "--repo", p.Repo, p.Name} if p.Version != "" { @@ -302,7 +303,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.absChartHome(), p.Name) + path := filepath.Join(p.chartPath(), p.Name) s, err := os.Stat(path) if err != nil { return "", false @@ -333,3 +334,12 @@ 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() +}