Skip to content

Commit 742a0b9

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 <[email protected]>
1 parent a41c403 commit 742a0b9

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

pkg/limatmpl/locator.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,24 +199,36 @@ images:
199199
func InstNameFromImageURL(locator, imageArch string) string {
200200
// We intentionally call both path.Base and filepath.Base in case we are running on Windows.
201201
name := strings.ToLower(filepath.Base(path.Base(locator)))
202-
// Remove file format and compression file types
202+
// Remove file format and compression file types.
203203
name = imageURLRegex.ReplaceAllString(name, "")
204-
// The Alpine "nocloud_" prefix does not fit the genericTags pattern
204+
// The Alpine "nocloud_" prefix does not fit the genericTags pattern.
205205
name = strings.TrimPrefix(name, "nocloud_")
206206
for _, tag := range genericTags {
207207
re := regexp.MustCompile(fmt.Sprintf(`[-_.]%s\b`, tag))
208208
name = re.ReplaceAllString(name, "")
209209
}
210-
// Remove imageArch as well if it is the native arch
210+
// Remove imageArch as well if it is the native arch.
211211
if limayaml.IsNativeArch(imageArch) {
212212
re := regexp.MustCompile(fmt.Sprintf(`[-_.]%s\b`, imageArch))
213213
name = re.ReplaceAllString(name, "")
214214
}
215215
// Remove timestamps from name: 8 digit date, optionally followed by
216-
// a delimiter and one or more digits before a word boundary
216+
// a delimiter and one or more digits before a word boundary.
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
222+
// check manually if both numbers are the same.
223+
re := regexp.MustCompile(`-(\d+)-(\d+)\.`)
224+
name = re.ReplaceAllStringFunc(name, func(match string) string {
225+
submatch := re.FindStringSubmatch(match)
226+
if submatch[1] == submatch[2] {
227+
// Replace -X-X. with -X.
228+
return "-" + submatch[1] + "."
229+
}
230+
return match
231+
})
220232
return name
221233
}
222234

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)