Skip to content

Commit

Permalink
fix: fix while using local charts with version
Browse files Browse the repository at this point in the history
fix kubernetes-sigs#5163

Signed-off-by: Ardika Bagus <me@ardikabs.com>
  • Loading branch information
ardikabs committed Aug 26, 2023
1 parent ca1e961 commit 9a928a6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
9 changes: 2 additions & 7 deletions api/internal/builtins/HelmChartInflationGenerator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ func (p *plugin) validateArgs() (err error) {
// be under the loader root (unless root restrictions are
// disabled).
if p.ValuesFile == "" {
// 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")
}
p.ValuesFile = filepath.Join(p.absChartHome(), p.Name, "values.yaml")
}
for i, file := range p.AdditionalValuesFiles {
// use Load() to enforce root restrictions
Expand Down Expand Up @@ -150,7 +145,7 @@ func (p *plugin) absChartHome() string {
chartHome = filepath.Join(p.h.Loader().Root(), p.ChartHome)
}

if p.Version != "" {
if p.Version != "" && p.Repo != "" {
return filepath.Join(chartHome, fmt.Sprintf("%s-%s", p.Name, p.Version))
}
return chartHome
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ func TestHelmChartInflationGeneratorWithSameChartMultipleVersions(t *testing.T)
version: "1.1.2",
releaseName: "terraform-1.1.2",
},
{
name: "terraform chart with version 1.1.2 but without repo",
chartName: "terraform",
version: "1.1.2",
releaseName: "terraform-1.1.2",
},
}

for _, tt := range tests {
Expand All @@ -643,12 +649,14 @@ releaseName: %s
assert.True(t, len(rm.Resources()) > 0)

var chartDir string
if tt.version != "" {
if tt.version != "" && tt.repo != "" {
chartDir = fmt.Sprintf("charts/%s-%s/%s", tt.chartName, tt.version, tt.chartName)
} else {
chartDir = fmt.Sprintf("charts/%s", tt.chartName)
}

fmt.Printf("%s: %s\n", tt.name, chartDir)

d, err := th.GetFSys().ReadFile(filepath.Join(th.GetRoot(), chartDir, "Chart.yaml"))
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -715,4 +723,56 @@ releaseName: podinfo2
podinfo2ChartContents, err := th.GetFSys().ReadFile(filepath.Join(podinfo2ChartsDir, "Chart.yaml"))
assert.NoError(t, err)
assert.Contains(t, string(podinfo2ChartContents), "version: 6.1.8")

}

// Addressed: https://github.com/kubernetes-sigs/kustomize/issues/5163
func TestHelmChartInflationGeneratorWithLocalChartWithVersion(t *testing.T) {
th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t).
PrepBuiltin("HelmChartInflationGenerator")
defer th.Reset()
if err := th.ErrIfNoHelm(); err != nil {
t.Skip("skipping: " + err.Error())
}

th.GetFSys().MkdirAll(filepath.Join(th.GetRoot(), "charts/dummy/templates"))
th.WriteF(filepath.Join(th.GetRoot(), "charts/dummy/Chart.yaml"), `
apiVersion: v1
appVersion: 1.0.0
description: Dummy
name: dummy
version: 1.0.0
`)

th.WriteF(filepath.Join(th.GetRoot(), "charts/dummy/values.yaml"), `
foo: bar
`)

th.WriteF(filepath.Join(th.GetRoot(), "charts/dummy/templates/cm.yaml"), `
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Values.foo }}
`)

dummyInlineHelmChart := th.LoadAndRunGenerator(`
apiVersion: builtin
kind: HelmChartInflationGenerator
metadata:
name: dummy
name: dummy
version: 1.0.0
releaseName: dummy
`)

dummyConfigmap, err := dummyInlineHelmChart.Resources()[0].GetFieldValue("metadata.name")
assert.NoError(t, err)
assert.Equal(t, "bar", dummyConfigmap)

dummyChartsDir := filepath.Join(th.GetRoot(), "charts/dummy")
assert.True(t, th.GetFSys().Exists(dummyChartsDir))

dummyChartsContent, err := th.GetFSys().ReadFile(filepath.Join(dummyChartsDir, "Chart.yaml"))
assert.NoError(t, err)
assert.Contains(t, string(dummyChartsContent), "version: 1.0.0")
}

0 comments on commit 9a928a6

Please sign in to comment.