Fix: addon dependency package retrieving is not compatible to v-prefixed version (#6316)

This commit is contained in:
qiaozp
2023-09-02 21:00:41 +08:00
committed by GitHub
parent 08548968f0
commit 6cbc12f9bb
4 changed files with 43 additions and 9 deletions
+3 -2
View File
@@ -161,7 +161,7 @@ func (i versionedRegistry) loadAddon(ctx context.Context, name, version string)
sort.Sort(sort.Reverse(versions))
addonVersion, availableVersions := chooseVersion(version, versions)
if addonVersion == nil {
return nil, errors.Errorf("specified version %s not exist", utils.Sanitize(version))
return nil, errors.Errorf("specified version %s for addon %s not exist", utils.Sanitize(version), name)
}
for _, chartURL := range addonVersion.URLs {
if !utils.IsValidURL(chartURL) {
@@ -231,6 +231,7 @@ func loadAddonPackage(addonName string, files []*loader.BufferedFile) (*WholeAdd
}
// chooseVersion will return the target version and all available versions
// This function is not sensitive to v-prefix, which means if specifiedVersion=0.3.0, v0.3.0 can be chosen.
func chooseVersion(specifiedVersion string, versions []*repo.ChartVersion) (*repo.ChartVersion, []string) {
var addonVersion *repo.ChartVersion
var availableVersions []string
@@ -241,7 +242,7 @@ func chooseVersion(specifiedVersion string, versions []*repo.ChartVersion) (*rep
continue
}
if len(specifiedVersion) != 0 {
if v.Version == specifiedVersion {
if utils.IgnoreVPrefix(v.Version) == utils.IgnoreVPrefix(specifiedVersion) {
addonVersion = versions[i]
}
} else {
+30 -7
View File
@@ -51,13 +51,36 @@ func TestChooseAddonVersion(t *testing.T) {
},
},
}
targetVersion, availableVersion := chooseVersion("v1.2.0", versions)
assert.Equal(t, availableVersion, []string{"v1.4.0-beta1", "v1.3.6", "v1.2.0"})
assert.Equal(t, targetVersion.Version, "v1.2.0")
targetVersion, availableVersion = chooseVersion("", versions)
assert.Equal(t, availableVersion, []string{"v1.4.0-beta1", "v1.3.6", "v1.2.0"})
assert.Equal(t, targetVersion.Version, "v1.3.6")
avs := []string{"v1.4.0-beta1", "v1.3.6", "v1.2.0"}
for _, tc := range []struct {
name string
specifiedVersion string
wantVersion string
wantAVersions []string
}{
{
name: "choose specified",
specifiedVersion: "v1.2.0",
wantVersion: "v1.2.0",
wantAVersions: avs,
},
{
name: "choose specified, ignore v prefix",
specifiedVersion: "1.2.0",
wantVersion: "v1.2.0",
wantAVersions: avs,
},
{
name: "not specifying version, choose non-prerelease && highest version",
specifiedVersion: "",
wantVersion: "v1.3.6",
wantAVersions: avs,
},
} {
targetVersion, availableVersion := chooseVersion(tc.specifiedVersion, versions)
assert.Equal(t, availableVersion, tc.wantAVersions)
assert.Equal(t, targetVersion.Version, tc.wantVersion)
}
}
var versionedHandler http.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) {
+5
View File
@@ -80,3 +80,8 @@ func Sanitize(s string) string {
s = strings.ReplaceAll(s, "\r", "")
return s
}
// IgnoreVPrefix removes the leading "v" from a string
func IgnoreVPrefix(s string) string {
return strings.TrimPrefix(s, "v")
}
+5
View File
@@ -26,3 +26,8 @@ func TestSanitize(t *testing.T) {
s := "abc\ndef\rgh"
require.Equal(t, "abcdefgh", Sanitize(s))
}
func TestIgnoreVPrefix(t *testing.T) {
require.Equal(t, "1.2.0", IgnoreVPrefix("v1.2.0"))
require.Equal(t, "1.2.0", IgnoreVPrefix("1.2.0"))
}