Skip to content

Commit f972389

Browse files
committed
Remove redundant major version number from instance name
almalinux and rocky use image names that include the major version number twice. This commit strips it: almalinux-8-8.10 → almalinux-8.10 almalinux-9-9.5 → almalinux-9.5 rocky-8-8.10 → rocky-8.10 rocky-9-9.5 → rocky-9.5 Signed-off-by: Jan Dubois <jan.dubois@suse.com>
1 parent a41c403 commit f972389

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/limatmpl/locator.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,17 @@ func InstNameFromImageURL(locator, imageArch string) string {
217217
name = regexp.MustCompile(`[-_.]20\d{6}([-_.]\d+)?\b`).ReplaceAllString(name, "")
218218
// Normalize archlinux name
219219
name = regexp.MustCompile(`^arch\b`).ReplaceAllString(name, "archlinux")
220+
// Remove redundant major version, e.g. rocky-8-8.10 becomes rocky-8.10
221+
// unfortunately regexp doesn't support back references, so we have to check manually of both numbers are the same
222+
re := regexp.MustCompile(`-(\d+)-(\d+)\.`)
223+
name = re.ReplaceAllStringFunc(name, func(match string) string {
224+
submatch := re.FindStringSubmatch(match)
225+
if submatch[1] == submatch[2] {
226+
// Replace -X-X. with -X.
227+
return "-" + submatch[1] + "."
228+
}
229+
return match
230+
})
220231
return name
221232
}
222233

pkg/limatmpl/locator_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ func TestInstNameFromImageURL(t *testing.T) {
5252
name := limatmpl.InstNameFromImageURL(image, arch)
5353
assert.Equal(t, name, "linux")
5454
})
55+
t.Run("removes redundant major version", func(t *testing.T) {
56+
name := limatmpl.InstNameFromImageURL("rocky-8-8.10.raw", "unknown")
57+
assert.Equal(t, name, "rocky-8.10")
58+
})
59+
t.Run("don't remove non-redundant major version", func(t *testing.T) {
60+
name := limatmpl.InstNameFromImageURL("rocky-8-9.10.raw", "unknown")
61+
assert.Equal(t, name, "rocky-8-9.10")
62+
})
5563
}

0 commit comments

Comments
 (0)