Skip to content

Commit

Permalink
feat: improve kustomization walk for helmCharts section (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greyeye authored Feb 14, 2025
1 parent 4676e7f commit b772f4e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/kustomize/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func processDir(sourceFS fs.FS, relBase string) (files []string, dirs []string,
var filesOrDirectories []string
filesOrDirectories = append(filesOrDirectories, kust.Bases...) // nolint:staticcheck // deprecated doesn't mean unused
filesOrDirectories = append(filesOrDirectories, kust.Resources...)

filesOrDirectories = append(filesOrDirectories, getValuesFromKustomizationHelm(&kust)...)
var directories []string
directories = append(directories, kust.Components...)

Expand Down Expand Up @@ -154,3 +154,11 @@ func isRemoteResource(resource string) bool {

return false
}

// getValuesFromKustomizationHelm will parse the helmCharts sections' valueFile field and return them as a slice of strings.
func getValuesFromKustomizationHelm(kust *types.Kustomization) (files []string) {
for _, helm := range kust.HelmCharts {
files = append(files, helm.ValuesFile)
}
return files
}
67 changes: 67 additions & 0 deletions pkg/kustomize/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/api/types"
_ "sigs.k8s.io/kustomize/api/types"
)

Expand Down Expand Up @@ -203,4 +204,70 @@ resources:
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to stat testdir/missing-resource.yaml")
})
t.Run("helmChart", func(t *testing.T) {
kustContent := `
helmCharts:
- name: dummy
repo: https://dummy.local/repo
version: 1.2.3
releaseName: dummy
namespace: dummy
includeCRDs: true
valuesFile: values-dummy.yaml
`
valueContent := `
dummy:
labels:
release: dummy
`
sourceFS := fstest.MapFS{
"testdir/kustomization.yaml": &fstest.MapFile{
Data: []byte(kustContent),
},
"testdir/values-dummy.yaml": &fstest.MapFile{
Data: []byte(valueContent),
},
}

files, _, err := processDir(sourceFS, "testdir")
assert.NoError(t, err)
assert.Contains(t, files, "testdir/values-dummy.yaml")
})
}

func Test_getValuesFromKustomizationHelm(t *testing.T) {
type args struct {
kust *types.Kustomization
}
tests := []struct {
name string
args args
wantFiles []string
}{
{
name: "normal",
args: args{
kust: &types.Kustomization{
HelmCharts: []types.HelmChart{
{Name: "dummy", ValuesFile: "values-dummy.yaml"},
},
},
},
wantFiles: []string{"values-dummy.yaml"},
},
{
name: "helmChart is nil.",
args: args{
kust: &types.Kustomization{
HelmCharts: nil,
},
},
wantFiles: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.wantFiles, getValuesFromKustomizationHelm(tt.args.kust), "getValuesFromKustomizationHelm(%v)", tt.args.kust)
})
}
}

0 comments on commit b772f4e

Please sign in to comment.