fix: version path issue (#1344)

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2024-02-16 08:08:28 +08:00 committed by GitHub
parent 5da9bc2c1e
commit 43fec2d599
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 73 additions and 2 deletions

View File

@ -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...)

View File

@ -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)
}

View File

@ -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)
}
}
}