Skip to content

Commit 0f244a4

Browse files
author
Dustin Lish
authored
Fix using same helm chart with different versions (#4999)
* Fix using same helm chart with different versions * Fix p.ValuesFile when version is set * Updated: Fix using same helm chart with different versions * Add test for issue #4813 * Use if/else for readability, add version check to absChartHome
1 parent 2ce1c7c commit 0f244a4

File tree

3 files changed

+167
-6
lines changed

3 files changed

+167
-6
lines changed

api/internal/builtins/HelmChartInflationGenerator.go

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ func (p *plugin) validateArgs() (err error) {
9797
// be under the loader root (unless root restrictions are
9898
// disabled).
9999
if p.ValuesFile == "" {
100-
p.ValuesFile = filepath.Join(p.ChartHome, p.Name, "values.yaml")
100+
// If the version is specified, use the versioned values file.
101+
if p.Version != "" {
102+
p.ValuesFile = filepath.Join(p.ChartHome, fmt.Sprintf("%s-%s", p.Name, p.Version), p.Name, "values.yaml")
103+
} else {
104+
p.ValuesFile = filepath.Join(p.ChartHome, p.Name, "values.yaml")
105+
}
101106
}
102107
for i, file := range p.AdditionalValuesFiles {
103108
// use Load() to enforce root restrictions
@@ -138,10 +143,17 @@ func (p *plugin) errIfIllegalValuesMerge() error {
138143
}
139144

140145
func (p *plugin) absChartHome() string {
146+
var chartHome string
141147
if filepath.IsAbs(p.ChartHome) {
142-
return p.ChartHome
148+
chartHome = p.ChartHome
149+
} else {
150+
chartHome = filepath.Join(p.h.Loader().Root(), p.ChartHome)
151+
}
152+
153+
if p.Version != "" {
154+
return filepath.Join(chartHome, fmt.Sprintf("%s-%s", p.Name, p.Version))
143155
}
144-
return filepath.Join(p.h.Loader().Root(), p.ChartHome)
156+
return chartHome
145157
}
146158

147159
func (p *plugin) runHelmCommand(

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path/filepath"
1010
"testing"
1111

12+
"github.com/stretchr/testify/assert"
1213
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
1314
)
1415

@@ -579,3 +580,139 @@ valuesInline:
579580
`)
580581
th.AssertActualEqualsExpected(rm, "")
581582
}
583+
584+
func TestHelmChartInflationGeneratorWithSameChartMultipleVersions(t *testing.T) {
585+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
586+
PrepBuiltin("HelmChartInflationGenerator")
587+
defer th.Reset()
588+
if err := th.ErrIfNoHelm(); err != nil {
589+
t.Skip("skipping: " + err.Error())
590+
}
591+
592+
tests := []struct {
593+
name string
594+
chartName string
595+
repo string
596+
version string
597+
releaseName string
598+
}{
599+
{
600+
name: "terraform chart with no version grabs latest",
601+
chartName: "terraform",
602+
repo: "https://helm.releases.hashicorp.com",
603+
version: "",
604+
releaseName: "terraform-latest",
605+
},
606+
{
607+
name: "terraform chart with version 1.1.1",
608+
chartName: "terraform",
609+
repo: "https://helm.releases.hashicorp.com",
610+
version: "1.1.1",
611+
releaseName: "terraform-1.1.1",
612+
},
613+
{
614+
name: "terraform chart with version 1.1.1 again",
615+
chartName: "terraform",
616+
repo: "https://helm.releases.hashicorp.com",
617+
version: "1.1.1",
618+
releaseName: "terraform-1.1.1-1",
619+
},
620+
{
621+
name: "terraform chart with version 1.1.2",
622+
chartName: "terraform",
623+
repo: "https://helm.releases.hashicorp.com",
624+
version: "1.1.2",
625+
releaseName: "terraform-1.1.2",
626+
},
627+
}
628+
629+
for _, tt := range tests {
630+
t.Run(tt.name, func(t *testing.T) {
631+
config := fmt.Sprintf(`
632+
apiVersion: builtin
633+
kind: HelmChartInflationGenerator
634+
metadata:
635+
name: %s
636+
name: %s
637+
version: %s
638+
repo: %s
639+
releaseName: %s
640+
`, tt.chartName, tt.chartName, tt.version, tt.repo, tt.releaseName)
641+
642+
rm := th.LoadAndRunGenerator(config)
643+
assert.True(t, len(rm.Resources()) > 0)
644+
645+
var chartDir string
646+
if tt.version != "" {
647+
chartDir = fmt.Sprintf("charts/%s-%s/%s", tt.chartName, tt.version, tt.chartName)
648+
} else {
649+
chartDir = fmt.Sprintf("charts/%s", tt.chartName)
650+
}
651+
652+
d, err := th.GetFSys().ReadFile(filepath.Join(th.GetRoot(), chartDir, "Chart.yaml"))
653+
if err != nil {
654+
t.Fatal(err)
655+
}
656+
657+
assert.Contains(t, string(d), fmt.Sprintf("name: %s", tt.chartName))
658+
if tt.version != "" {
659+
assert.Contains(t, string(d), fmt.Sprintf("version: %s", tt.version))
660+
}
661+
})
662+
}
663+
}
664+
665+
// Test that verifies +1 instances of same chart with different versions
666+
// https://github.com/kubernetes-sigs/kustomize/issues/4813
667+
func TestHelmChartInflationGeneratorWithMultipleInstancesSameChartDifferentVersions(t *testing.T) {
668+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
669+
PrepBuiltin("HelmChartInflationGenerator")
670+
defer th.Reset()
671+
if err := th.ErrIfNoHelm(); err != nil {
672+
t.Skip("skipping: " + err.Error())
673+
}
674+
675+
podinfo1 := th.LoadAndRunGenerator(`
676+
apiVersion: builtin
677+
kind: HelmChartInflationGenerator
678+
metadata:
679+
name: podinfo
680+
name: podinfo
681+
version: 6.2.1
682+
repo: https://stefanprodan.github.io/podinfo
683+
releaseName: podinfo1
684+
`)
685+
686+
podinfo2 := th.LoadAndRunGenerator(`
687+
apiVersion: builtin
688+
kind: HelmChartInflationGenerator
689+
metadata:
690+
name: podinfo
691+
name: podinfo
692+
version: 6.1.8
693+
repo: https://stefanprodan.github.io/podinfo
694+
releaseName: podinfo2
695+
`)
696+
697+
podinfo1Img, err := podinfo1.Resources()[1].GetFieldValue("spec.template.spec.containers.0.image")
698+
assert.NoError(t, err)
699+
assert.Equal(t, "ghcr.io/stefanprodan/podinfo:6.2.1", podinfo1Img)
700+
701+
podinfo2Img, err := podinfo2.Resources()[1].GetFieldValue("spec.template.spec.containers.0.image")
702+
assert.NoError(t, err)
703+
assert.Equal(t, "ghcr.io/stefanprodan/podinfo:6.1.8", podinfo2Img)
704+
705+
podinfo1ChartsDir := filepath.Join(th.GetRoot(), "charts/podinfo-6.2.1/podinfo")
706+
assert.True(t, th.GetFSys().Exists(podinfo1ChartsDir))
707+
708+
podinfo2ChartsDir := filepath.Join(th.GetRoot(), "charts/podinfo-6.1.8/podinfo")
709+
assert.True(t, th.GetFSys().Exists(podinfo2ChartsDir))
710+
711+
podinfo1ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo1ChartsDir, "Chart.yaml"))
712+
assert.NoError(t, err)
713+
assert.Contains(t, string(podinfo1ChartContents), "version: 6.2.1")
714+
715+
podinfo2ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo2ChartsDir, "Chart.yaml"))
716+
assert.NoError(t, err)
717+
assert.Contains(t, string(podinfo2ChartContents), "version: 6.1.8")
718+
}

0 commit comments

Comments
 (0)