diff --git a/pkg/policy/checker.go b/pkg/policy/checker.go index eea0a0cd..b493d513 100644 --- a/pkg/policy/checker.go +++ b/pkg/policy/checker.go @@ -8,6 +8,7 @@ import ( "regexp" "slices" "strings" + "unicode" "github.com/helmfile/helmfile/pkg/runtime" ) @@ -83,13 +84,15 @@ func TopKeys(helmfileContent []byte, hasSeparator bool) []string { clines := bytes.Split(helmfileContent, []byte("\n")) for _, line := range clines { - lineStr := strings.TrimSpace(string(line)) + lineStr := strings.TrimRightFunc(string(line), unicode.IsSpace) if lineStr == "" { continue // Skip empty lines } if hasSeparator && separatorRegex.MatchString(lineStr) { topKeys = append(topKeys, lineStr) - } else if topConfigKeysRegex.MatchString(lineStr) { + } + + if topConfigKeysRegex.MatchString(lineStr) { topKey := strings.SplitN(lineStr, ":", 2)[0] topKeys = append(topKeys, topKey) } diff --git a/pkg/policy/checker_test.go b/pkg/policy/checker_test.go index b867b4ad..7c28555c 100644 --- a/pkg/policy/checker_test.go +++ b/pkg/policy/checker_test.go @@ -230,6 +230,11 @@ func TestTopKeys(t *testing.T) { helmfileContent: []byte(""), want: nil, }, + { + name: "sub level contains top level key", + helmfileContent: []byte("bases:\n releases:\n - name: test\n namespace: test\n"), + want: []string{"bases"}, + }, } for _, tt := range tests {