Skip to content

Commit 073e7ab

Browse files
committed
fix: add image version
1 parent 5c269fa commit 073e7ab

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

util/imagetools/image_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,38 @@ func TestNormalizeImageInfo(t *testing.T) {
173173
OsLang: "",
174174
OsArch: "x86_64",
175175
},
176+
{
177+
Name: "rocky9-base-image-20250418",
178+
OsDistro: "Rocky Linux",
179+
OsType: osprofile.OS_TYPE_LINUX,
180+
OsVersion: "9",
181+
OsLang: "",
182+
OsArch: "x86_64",
183+
},
184+
{
185+
Name: "almalinux9-aarch64-generic",
186+
OsDistro: "AlmaLinux",
187+
OsType: osprofile.OS_TYPE_LINUX,
188+
OsVersion: "9",
189+
OsLang: "",
190+
OsArch: "aarch64",
191+
},
192+
{
193+
Name: "Alibaba Cloud Linux 3 LTS 64bit",
194+
OsDistro: "Alibaba Cloud Linux",
195+
OsType: osprofile.OS_TYPE_LINUX,
196+
OsVersion: "3",
197+
OsLang: "",
198+
OsArch: "x86_64",
199+
},
200+
{
201+
Name: "Anolis OS 8 aarch64",
202+
OsDistro: "Anolis OS",
203+
OsType: osprofile.OS_TYPE_LINUX,
204+
OsVersion: "8",
205+
OsLang: "",
206+
OsArch: "aarch64",
207+
},
176208
}
177209

178210
for _, image := range images {

util/imagetools/imagetools.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,18 @@ var imageVersions = map[string][]string{
197197
OS_DIST_OPEN_EULER: {"2.0 SP1", "2.0 SP2", "2.0 SP3", "2.0 SP8", "3.0", "22.03", "23.09"},
198198
OS_DIST_EULER_OS: {"2"},
199199
// 阿里云Linux:补充1代和2/3代版本
200-
OS_DIST_ALIYUN: {"1", "2.1903", "3.2104", "3.2304"},
200+
OS_DIST_ALIYUN: {"1", "2.1903", "2", "3.2104", "3.2304", "3"},
201201

202202
// 阿里云轻量版:补充完整版本
203-
OS_DIST_ALIBABA_CLOUD_LINUX: {"2.1903", "3.2104", "3.2304", "3.2404"},
203+
OS_DIST_ALIBABA_CLOUD_LINUX: {"2.1903", "2", "3.2104", "3.2304", "3.2404", "3"},
204204
// 龙蜥OS:补充7/8系列完整小版本
205-
OS_DIST_ANOLIS: {"7.6", "7.9", "8.2", "8.4", "8.6", "8.8", "9.0", "9.2"},
205+
OS_DIST_ANOLIS: {"7.6", "7.9", "7", "8.2", "8.4", "8.6", "8.8", "8", "9.0", "9.2", "9"},
206206
// Rocky Linux:补充8/9全系列小版本
207-
OS_DIST_ROCKY_LINUX: {"8.5", "8.6", "8.7", "8.8", "8.9", "8.10", "9.0", "9.1", "9.2", "9.3", "9.4", "9.5"},
207+
OS_DIST_ROCKY_LINUX: {"8.5", "8.6", "8.7", "8.8", "8.9", "8.10", "8", "9.0", "9.1", "9.2", "9.3", "9.4", "9.5", "9"},
208208
// Fedora:补充近年主流版本(33到40)
209209
OS_DIST_FEDORA: {"33", "34", "35", "36", "37", "38", "39", "40"},
210210
// AlmaLinux:补充8/9全系列
211-
OS_DIST_ALMA_LINUX: {"8.5", "8.6", "8.7", "8.8", "8.9", "8.10", "9.0", "9.1", "9.2", "9.3", "9.4", "9.5"},
211+
OS_DIST_ALMA_LINUX: {"8.5", "8.6", "8.7", "8.8", "8.9", "8.10", "8", "9.0", "9.1", "9.2", "9.3", "9.4", "9.5", "9"},
212212
// Amazon Linux:补充1/2/2023版本
213213
OS_DIST_AMAZON_LINUX: {"2022", "2023", "1", "2"},
214214

@@ -242,7 +242,7 @@ func normalizeOsVersion(imageName string, osDist string, osVersion string) strin
242242
parts = append(parts, fmt.Sprintf(`(?P<verstr>%s[.\d]*)`, version), "")
243243
regexpStr := strings.Join(parts, `[\s-_]*`)
244244
m := regexp.MustCompile(regexpStr).FindAllStringSubmatch(strings.ToLower(imageName), -1)
245-
if m != nil && len(m) > 0 && len(m[0]) > 1 {
245+
if len(m) > 0 && len(m[0]) > 1 {
246246
verStr := m[0][1]
247247
if strings.HasPrefix(verStr, version) && len(verStr) > len(version) && !strings.HasPrefix(verStr, version+".") {
248248
verStr = version + "." + verStr[len(version):]
@@ -251,11 +251,18 @@ func normalizeOsVersion(imageName string, osDist string, osVersion string) strin
251251
}
252252
}
253253
}
254-
for i := len(versions) - 1; i > 0; i-- {
255-
if strings.Contains(imageName, versions[i]) {
256-
return versions[i]
254+
imageNameLower := strings.ToLower(imageName)
255+
bestMatch := ""
256+
for i := 0; i < len(versions); i++ {
257+
version := strings.ToLower(versions[i])
258+
pattern := fmt.Sprintf(`(^|[^0-9])%s([^0-9]|$)`, regexp.QuoteMeta(version))
259+
if regexp.MustCompile(pattern).MatchString(imageNameLower) && len(versions[i]) > len(bestMatch) {
260+
bestMatch = versions[i]
257261
}
258262
}
263+
if len(bestMatch) > 0 {
264+
return bestMatch
265+
}
259266
}
260267
return ""
261268
}

0 commit comments

Comments
 (0)