Skip to content

Commit

Permalink
feat(ui): Recursive Helm Values files detection (#15935) (#15936)
Browse files Browse the repository at this point in the history
Signed-off-by: bagnaram <11695670+bagnaram@users.noreply.github.com>
  • Loading branch information
bagnaram authored Oct 19, 2023
1 parent 1fe6c89 commit 973565e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
32 changes: 16 additions & 16 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1997,12 +1997,13 @@ func (s *Service) createGetAppDetailsCacheHandler(res *apiclient.RepoAppDetailsR

func populateHelmAppDetails(res *apiclient.RepoAppDetailsResponse, appPath string, repoRoot string, q *apiclient.RepoServerAppDetailsQuery, gitRepoPaths io.TempPaths) error {
var selectedValueFiles []string
var availableValueFiles []string

if q.Source.Helm != nil {
selectedValueFiles = q.Source.Helm.ValueFiles
}

availableValueFiles, err := findHelmValueFilesInPath(appPath)
err := filepath.Walk(appPath, walkHelmValueFilesInPath(appPath, &availableValueFiles))
if err != nil {
return err
}
Expand Down Expand Up @@ -2079,26 +2080,25 @@ func loadFileIntoIfExists(path pathutil.ResolvedFilePath, destination *string) e
return nil
}

func findHelmValueFilesInPath(path string) ([]string, error) {
var result []string
func walkHelmValueFilesInPath(root string, valueFiles *[]string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {

files, err := os.ReadDir(path)
if err != nil {
return result, fmt.Errorf("error reading helm values file from %s: %w", path, err)
}

for _, f := range files {
if f.IsDir() {
continue
if err != nil {
return fmt.Errorf("error reading helm values file from %s: %w", path, err)
}
filename := f.Name()
fileNameExt := strings.ToLower(filepath.Ext(filename))

filename := info.Name()
fileNameExt := strings.ToLower(filepath.Ext(path))
if strings.Contains(filename, "values") && (fileNameExt == ".yaml" || fileNameExt == ".yml") {
result = append(result, filename)
relPath, err := filepath.Rel(root, path)
if err != nil {
return fmt.Errorf("error traversing path from %s to %s: %w", root, path, err)
}
*valueFiles = append(*valueFiles, relPath)
}
}

return result, nil
return nil
}
}

func populateKustomizeAppDetails(res *apiclient.RepoAppDetailsResponse, q *apiclient.RepoServerAppDetailsQuery, appPath string, reversion string, credsStore git.CredsStore) error {
Expand Down
21 changes: 16 additions & 5 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2663,16 +2663,27 @@ func runGit(t *testing.T, workDir string, args ...string) string {
return stringOut
}

func Test_findHelmValueFilesInPath(t *testing.T) {
func Test_walkHelmValueFilesInPath(t *testing.T) {
t.Run("does not exist", func(t *testing.T) {
files, err := findHelmValueFilesInPath("/obviously/does/not/exist")
var files []string
root := "/obviously/does/not/exist"
err := filepath.Walk(root, walkHelmValueFilesInPath(root, &files))
assert.Error(t, err)
assert.Empty(t, files)
})
t.Run("values files", func(t *testing.T) {
files, err := findHelmValueFilesInPath("./testdata/values-files")
var files []string
root := "./testdata/values-files"
err := filepath.Walk(root, walkHelmValueFilesInPath(root, &files))
assert.NoError(t, err)
assert.Len(t, files, 4)
assert.Len(t, files, 5)
})
t.Run("unrelated root", func(t *testing.T) {
var files []string
root := "./testdata/values-files"
unrelated_root := "/different/root/path"
err := filepath.Walk(root, walkHelmValueFilesInPath(unrelated_root, &files))
assert.Error(t, err)
})
}

Expand All @@ -2690,7 +2701,7 @@ func Test_populateHelmAppDetails(t *testing.T) {
err = populateHelmAppDetails(&res, appPath, appPath, &q, emptyTempPaths)
require.NoError(t, err)
assert.Len(t, res.Helm.Parameters, 3)
assert.Len(t, res.Helm.ValueFiles, 4)
assert.Len(t, res.Helm.ValueFiles, 5)
}

func Test_populateHelmAppDetails_values_symlinks(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
values: yaml

0 comments on commit 973565e

Please sign in to comment.