Skip to content

Commit cc35d3c

Browse files
committed
fix: fix while using local charts with version
fix #5163 Signed-off-by: Ardika Bagus <[email protected]>
1 parent a0f131c commit cc35d3c

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

api/internal/builtins/HelmChartInflationGenerator.go

Lines changed: 2 additions & 7 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: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,7 @@ func (p *plugin) validateArgs() (err error) {
9797
// be under the loader root (unless root restrictions are
9898
// disabled).
9999
if p.ValuesFile == "" {
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-
}
100+
p.ValuesFile = filepath.Join(p.absChartHome(), p.Name, "values.yaml")
106101
}
107102
for i, file := range p.AdditionalValuesFiles {
108103
// use Load() to enforce root restrictions
@@ -150,7 +145,7 @@ func (p *plugin) absChartHome() string {
150145
chartHome = filepath.Join(p.h.Loader().Root(), p.ChartHome)
151146
}
152147

153-
if p.Version != "" {
148+
if p.Version != "" && p.Repo != "" {
154149
return filepath.Join(chartHome, fmt.Sprintf("%s-%s", p.Name, p.Version))
155150
}
156151
return chartHome

plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,12 @@ func TestHelmChartInflationGeneratorWithSameChartMultipleVersions(t *testing.T)
670670
version: "1.1.2",
671671
releaseName: "terraform-1.1.2",
672672
},
673+
{
674+
name: "terraform chart with version 1.1.2 but without repo",
675+
chartName: "terraform",
676+
version: "1.1.2",
677+
releaseName: "terraform-1.1.2",
678+
},
673679
}
674680

675681
for _, tt := range tests {
@@ -689,12 +695,14 @@ releaseName: %s
689695
assert.True(t, len(rm.Resources()) > 0)
690696

691697
var chartDir string
692-
if tt.version != "" {
698+
if tt.version != "" && tt.repo != "" {
693699
chartDir = fmt.Sprintf("charts/%s-%s/%s", tt.chartName, tt.version, tt.chartName)
694700
} else {
695701
chartDir = fmt.Sprintf("charts/%s", tt.chartName)
696702
}
697703

704+
fmt.Printf("%s: %s\n", tt.name, chartDir)
705+
698706
d, err := th.GetFSys().ReadFile(filepath.Join(th.GetRoot(), chartDir, "Chart.yaml"))
699707
if err != nil {
700708
t.Fatal(err)
@@ -761,4 +769,56 @@ releaseName: podinfo2
761769
podinfo2ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo2ChartsDir, "Chart.yaml"))
762770
assert.NoError(t, err)
763771
assert.Contains(t, string(podinfo2ChartContents), "version: 6.1.8")
772+
773+
}
774+
775+
// Addressed: https://github.com/kubernetes-sigs/kustomize/issues/5163
776+
func TestHelmChartInflationGeneratorWithLocalChartWithVersion(t *testing.T) {
777+
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
778+
PrepBuiltin("HelmChartInflationGenerator")
779+
defer th.Reset()
780+
if err := th.ErrIfNoHelm(); err != nil {
781+
t.Skip("skipping: " + err.Error())
782+
}
783+
784+
th.GetFSys().MkdirAll(filepath.Join(th.GetRoot(), "charts/dummy/templates"))
785+
th.WriteF(filepath.Join(th.GetRoot(), "charts/dummy/Chart.yaml"), `
786+
apiVersion: v1
787+
appVersion: 1.0.0
788+
description: Dummy
789+
name: dummy
790+
version: 1.0.0
791+
`)
792+
793+
th.WriteF(filepath.Join(th.GetRoot(), "charts/dummy/values.yaml"), `
794+
foo: bar
795+
`)
796+
797+
th.WriteF(filepath.Join(th.GetRoot(), "charts/dummy/templates/cm.yaml"), `
798+
apiVersion: v1
799+
kind: ConfigMap
800+
metadata:
801+
name: {{ .Values.foo }}
802+
`)
803+
804+
dummyInlineHelmChart := th.LoadAndRunGenerator(`
805+
apiVersion: builtin
806+
kind: HelmChartInflationGenerator
807+
metadata:
808+
name: dummy
809+
name: dummy
810+
version: 1.0.0
811+
releaseName: dummy
812+
`)
813+
814+
dummyConfigmap, err := dummyInlineHelmChart.Resources()[0].GetFieldValue("metadata.name")
815+
assert.NoError(t, err)
816+
assert.Equal(t, "bar", dummyConfigmap)
817+
818+
dummyChartsDir := filepath.Join(th.GetRoot(), "charts/dummy")
819+
assert.True(t, th.GetFSys().Exists(dummyChartsDir))
820+
821+
dummyChartsContent, err := th.GetFSys().ReadFile(filepath.Join(dummyChartsDir, "Chart.yaml"))
822+
assert.NoError(t, err)
823+
assert.Contains(t, string(dummyChartsContent), "version: 1.0.0")
764824
}

0 commit comments

Comments
 (0)