Skip to content

Commit a0f131c

Browse files
dlishardikabs
authored andcommitted
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 f87942e commit a0f131c

File tree

3 files changed

+166
-6
lines changed

3 files changed

+166
-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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/stretchr/testify/require"
13+
"github.com/stretchr/testify/assert"
1314
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
1415
"sigs.k8s.io/kustomize/kyaml/copyutil"
1516
)
@@ -626,3 +627,138 @@ func copyTestChartsIntoHarness(t *testing.T, th *kusttest_test.HarnessEnhanced)
626627

627628
require.NoError(t, copyutil.CopyDir(th.GetFSys(), chartDir, thDir))
628629
}
630+
631+
func TestHelmChartInflationGeneratorWithSameChartMultipleVersions(t *testing.T) {
632+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
633+
PrepBuiltin("HelmChartInflationGenerator")
634+
defer th.Reset()
635+
if err := th.ErrIfNoHelm(); err != nil {
636+
t.Skip("skipping: " + err.Error())
637+
}
638+
tests := []struct {
639+
name string
640+
chartName string
641+
repo string
642+
version string
643+
releaseName string
644+
}{
645+
{
646+
name: "terraform chart with no version grabs latest",
647+
chartName: "terraform",
648+
repo: "https://helm.releases.hashicorp.com",
649+
version: "",
650+
releaseName: "terraform-latest",
651+
},
652+
{
653+
name: "terraform chart with version 1.1.1",
654+
chartName: "terraform",
655+
repo: "https://helm.releases.hashicorp.com",
656+
version: "1.1.1",
657+
releaseName: "terraform-1.1.1",
658+
},
659+
{
660+
name: "terraform chart with version 1.1.1 again",
661+
chartName: "terraform",
662+
repo: "https://helm.releases.hashicorp.com",
663+
version: "1.1.1",
664+
releaseName: "terraform-1.1.1-1",
665+
},
666+
{
667+
name: "terraform chart with version 1.1.2",
668+
chartName: "terraform",
669+
repo: "https://helm.releases.hashicorp.com",
670+
version: "1.1.2",
671+
releaseName: "terraform-1.1.2",
672+
},
673+
}
674+
675+
for _, tt := range tests {
676+
t.Run(tt.name, func(t *testing.T) {
677+
config := fmt.Sprintf(`
678+
apiVersion: builtin
679+
kind: HelmChartInflationGenerator
680+
metadata:
681+
name: %s
682+
name: %s
683+
version: %s
684+
repo: %s
685+
releaseName: %s
686+
`, tt.chartName, tt.chartName, tt.version, tt.repo, tt.releaseName)
687+
688+
rm := th.LoadAndRunGenerator(config)
689+
assert.True(t, len(rm.Resources()) > 0)
690+
691+
var chartDir string
692+
if tt.version != "" {
693+
chartDir = fmt.Sprintf("charts/%s-%s/%s", tt.chartName, tt.version, tt.chartName)
694+
} else {
695+
chartDir = fmt.Sprintf("charts/%s", tt.chartName)
696+
}
697+
698+
d, err := th.GetFSys().ReadFile(filepath.Join(th.GetRoot(), chartDir, "Chart.yaml"))
699+
if err != nil {
700+
t.Fatal(err)
701+
}
702+
703+
assert.Contains(t, string(d), fmt.Sprintf("name: %s", tt.chartName))
704+
if tt.version != "" {
705+
assert.Contains(t, string(d), fmt.Sprintf("version: %s", tt.version))
706+
}
707+
})
708+
}
709+
}
710+
711+
// Test that verifies +1 instances of same chart with different versions
712+
// https://github.com/kubernetes-sigs/kustomize/issues/4813
713+
func TestHelmChartInflationGeneratorWithMultipleInstancesSameChartDifferentVersions(t *testing.T) {
714+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
715+
PrepBuiltin("HelmChartInflationGenerator")
716+
defer th.Reset()
717+
if err := th.ErrIfNoHelm(); err != nil {
718+
t.Skip("skipping: " + err.Error())
719+
}
720+
721+
podinfo1 := th.LoadAndRunGenerator(`
722+
apiVersion: builtin
723+
kind: HelmChartInflationGenerator
724+
metadata:
725+
name: podinfo
726+
name: podinfo
727+
version: 6.2.1
728+
repo: https://stefanprodan.github.io/podinfo
729+
releaseName: podinfo1
730+
`)
731+
732+
podinfo2 := th.LoadAndRunGenerator(`
733+
apiVersion: builtin
734+
kind: HelmChartInflationGenerator
735+
metadata:
736+
name: podinfo
737+
name: podinfo
738+
version: 6.1.8
739+
repo: https://stefanprodan.github.io/podinfo
740+
releaseName: podinfo2
741+
`)
742+
743+
podinfo1Img, err := podinfo1.Resources()[1].GetFieldValue("spec.template.spec.containers.0.image")
744+
assert.NoError(t, err)
745+
assert.Equal(t, "ghcr.io/stefanprodan/podinfo:6.2.1", podinfo1Img)
746+
747+
podinfo2Img, err := podinfo2.Resources()[1].GetFieldValue("spec.template.spec.containers.0.image")
748+
assert.NoError(t, err)
749+
assert.Equal(t, "ghcr.io/stefanprodan/podinfo:6.1.8", podinfo2Img)
750+
751+
podinfo1ChartsDir := filepath.Join(th.GetRoot(), "charts/podinfo-6.2.1/podinfo")
752+
assert.True(t, th.GetFSys().Exists(podinfo1ChartsDir))
753+
754+
podinfo2ChartsDir := filepath.Join(th.GetRoot(), "charts/podinfo-6.1.8/podinfo")
755+
assert.True(t, th.GetFSys().Exists(podinfo2ChartsDir))
756+
757+
podinfo1ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo1ChartsDir, "Chart.yaml"))
758+
assert.NoError(t, err)
759+
assert.Contains(t, string(podinfo1ChartContents), "version: 6.2.1")
760+
761+
podinfo2ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo2ChartsDir, "Chart.yaml"))
762+
assert.NoError(t, err)
763+
assert.Contains(t, string(podinfo2ChartContents), "version: 6.1.8")
764+
}

0 commit comments

Comments
 (0)