Skip to content

Commit

Permalink
fix(helm): escape consecutive commas in cleanSetParameters (#19269) (#…
Browse files Browse the repository at this point in the history
…20113)



Signed-off-by: KangManJoo <[email protected]>
Signed-off-by: daengdaengLee <[email protected]>
Co-authored-by: daengdaengLee <[email protected]>
  • Loading branch information
eogns47 and daengdaengLee authored Oct 2, 2024
1 parent 1a84db5 commit 3070297
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 23 additions & 6 deletions util/helm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,19 +342,36 @@ type TemplateOpts struct {
SkipCrds bool
}

var (
re = regexp.MustCompile(`([^\\]),`)
apiVersionsRemover = regexp.MustCompile(`(--api-versions [^ ]+ )+`)
)

func cleanSetParameters(val string) string {
// `{}` equal helm list parameters format, so don't escape `,`.
if strings.HasPrefix(val, `{`) && strings.HasSuffix(val, `}`) {
return val
}
return re.ReplaceAllString(val, `$1\,`)

val = replaceAllWithLookbehind(val, ',', `\,`, '\\')
return val
}

func replaceAllWithLookbehind(val string, old rune, new string, lookbehind rune) string {
var result strings.Builder
var prevR rune
for _, r := range val {
if r == old {
if prevR != lookbehind {
result.WriteString(new)
} else {
result.WriteRune(old)
}
} else {
result.WriteRune(r)
}
prevR = r
}
return result.String()
}

var apiVersionsRemover = regexp.MustCompile(`(--api-versions [^ ]+ )+`)

func (c *Cmd) template(chartPath string, opts *TemplateOpts) (string, string, error) {
if callback, err := cleanupChartLockFile(filepath.Clean(path.Join(c.WorkDir, chartPath))); err == nil {
defer callback()
Expand Down
2 changes: 2 additions & 0 deletions util/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func TestHelmArgCleaner(t *testing.T) {
`not, clean`: `not\, clean`,
`a\,b,c`: `a\,b\,c`,
`{a,b,c}`: `{a,b,c}`,
`,,,,,\,`: `\,\,\,\,\,\,`,
`\,,\\,,`: `\,\,\\,\,`,
} {
cleaned := cleanSetParameters(input)
assert.Equal(t, expected, cleaned)
Expand Down

0 comments on commit 3070297

Please sign in to comment.