From 43fec2d599df4132e18d7c0ed1c659cb1a77f13c Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Fri, 16 Feb 2024 08:08:28 +0800 Subject: [PATCH] fix: version path issue (#1344) Signed-off-by: yxxhero --- pkg/state/state.go | 2 +- pkg/state/util.go | 8 ++++++ pkg/state/util_test.go | 65 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/pkg/state/state.go b/pkg/state/state.go index 402cc46f..4adb8aca 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -3568,7 +3568,7 @@ func (st *HelmState) getOCIChart(release *ReleaseSpec, tempDir string, helm helm pathElems = append(pathElems, release.KubeContext) } - pathElems = append(pathElems, release.Name, chartName, chartVersion) + pathElems = append(pathElems, release.Name, chartName, safeVersionPath(chartVersion)) chartPath := filepath.Join(pathElems...) diff --git a/pkg/state/util.go b/pkg/state/util.go index de6aecc2..59cdb448 100644 --- a/pkg/state/util.go +++ b/pkg/state/util.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "strings" ) @@ -71,3 +72,10 @@ func getBuildDepsFlags(cpr *chartPrepareResult) []string { return flags } + +// safePath returns a clean path +func safeVersionPath(version string) string { + c := regexp.MustCompile(`=|>|<|!|\||~|\^| |,|\*`) + sp := c.ReplaceAll([]byte(version), []byte("_")) + return string(sp) +} diff --git a/pkg/state/util_test.go b/pkg/state/util_test.go index c4cceebb..cf2fbce4 100644 --- a/pkg/state/util_test.go +++ b/pkg/state/util_test.go @@ -1,6 +1,8 @@ package state -import "testing" +import ( + "testing" +) func TestIsLocalChart(t *testing.T) { testcases := []struct { @@ -151,3 +153,64 @@ func TestNormalizeChart(t *testing.T) { } } } +func TestSafeVersionPath(t *testing.T) { + testcases := []struct { + input string + expected string + }{ + { + input: "=1.2.3", + expected: "_1.2.3", + }, + { + input: ">1.0.0", + expected: "_1.0.0", + }, + { + input: "<=2.3.4", + expected: "__2.3.4", + }, + { + input: "!3.4.5", + expected: "_3.4.5", + }, + { + input: "|4.5.6", + expected: "_4.5.6", + }, + { + input: "~5.6.7", + expected: "_5.6.7", + }, + { + input: "^6.7.8", + expected: "_6.7.8", + }, + { + input: " 7.8.9", + expected: "_7.8.9", + }, + { + input: ",9.10.11", + expected: "_9.10.11", + }, + { + input: "<= 1.2.3, >= 1.4", + expected: "___1.2.3_____1.4", + }, + { + input: "*", + expected: "_", + }, + } + + for i := range testcases { + testcase := testcases[i] + + actual := safeVersionPath(testcase.input) + + if testcase.expected != actual { + t.Errorf("unexpected result: safeVersionPath(\"%s\"): expected=%v, got=%v", testcase.input, testcase.expected, actual) + } + } +}