Skip to content

Commit

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

Signed-off-by: KangManJoo <eogns47@konkuk.ac.kr>
Signed-off-by: daengdaengLee <gunho1020@gmail.com>
Co-authored-by: daengdaengLee <gunho1020@gmail.com>
Signed-off-by: austin5219 <3936059+austin5219@users.noreply.github.com>
  • Loading branch information
2 people authored and austin5219 committed Oct 16, 2024
1 parent 364e592 commit 4f57da9
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 4f57da9

Please sign in to comment.