Skip to content

Commit 6e568a5

Browse files
fix(preflight): Loosen up the regex for checking vm image name
relax the image name kubernetes version regexp.
1 parent a548190 commit 6e568a5

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

pkg/webhook/preflight/nutanix/imagekubernetesversioncheck.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
)
1717

1818
// Examples: nkp-ubuntu-22.04-vgpu-1.32.3-20250604180644, nkp-rocky-9.5-release-cis-1.32.3-20250430150550.
19-
var kubernetesVersionRegex = regexp.MustCompile(`-(\d+\.\d+\.\d+)-\d{14}$`)
19+
// The regex captures the Kubernetes version in the format of 1.x.y, where x and y are digits.
20+
var kubernetesVersionRegex = regexp.MustCompile(`(?i)\b[vV]?(1\.\d+(?:\.\d+)?)\b`)
2021

2122
type imageKubernetesVersionCheck struct {
2223
machineDetails *carenv1.NutanixMachineDetails
@@ -89,8 +90,8 @@ func (c *imageKubernetesVersionCheck) checkKubernetesVersion(image *vmmv4.Image)
8990
imageK8sVersion, err := extractKubernetesVersionFromImageName(imageName)
9091
if err != nil {
9192
return fmt.Errorf("failed to extract Kubernetes version from image name '%s': %s. "+
92-
"This check assumes NKP image naming convention. "+
93-
"You can opt out of this check if using custom image naming", imageName, err)
93+
"This check assumes a naming convention that includes kubernetes version in the name. "+
94+
"You can opt out of this check if using non-compliant naming", imageName, err)
9495
}
9596

9697
if imageK8sVersion != c.clusterK8sVersion {
@@ -105,12 +106,14 @@ func (c *imageKubernetesVersionCheck) checkKubernetesVersion(image *vmmv4.Image)
105106
return nil
106107
}
107108

109+
// extractKubernetesVersionFromImageName extracts the Kubernetes version from the given image name.
110+
// It expects something that looks like a kubernetes version in the image name i.e. 1.x.y?,
108111
// Examples: nkp-ubuntu-22.04-vgpu-1.32.3-20250604180644 -> 1.32.3.
109112
func extractKubernetesVersionFromImageName(imageName string) (string, error) {
110113
matches := kubernetesVersionRegex.FindStringSubmatch(imageName)
111114
if len(matches) < 2 {
112115
return "", fmt.Errorf(
113-
"image name does not match expected NKP naming convention (expected pattern: *-<k8s-version>-<timestamp>)",
116+
"image name does not match expected naming convention (expected pattern: .*<k8s-version>.*)",
114117
)
115118
}
116119
return matches[1], nil

pkg/webhook/preflight/nutanix/imagekubernetesversioncheck_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,26 @@ func TestExtractKubernetesVersionFromImageName(t *testing.T) {
5757
wantErr: false,
5858
},
5959
{
60-
name: "custom image name - no match", // e.g., not following NKP naming convention
61-
imageName: "my-custom-image-name",
62-
want: "",
63-
wantErr: true,
60+
name: "custom image name with kubernetes-version at end", // e.g., not following NKP naming convention
61+
imageName: "custom-image-v1.23",
62+
want: "1.23",
63+
wantErr: false,
6464
},
6565
{
66-
name: "missing timestamp",
67-
imageName: "nkp-ubuntu-22.04-1.32.3",
68-
want: "",
69-
wantErr: true,
66+
name: "custom image name with kubernetes version in middle", // e.g., not following NKP naming convention
67+
imageName: "custom-v1.23.1-image",
68+
want: "1.23.1",
69+
wantErr: false,
70+
},
71+
{
72+
name: "custom image name with kubernetes version in start", // e.g., not following NKP naming convention
73+
imageName: "v1.23.1-alpha-custom-image",
74+
want: "1.23.1",
75+
wantErr: false,
7076
},
7177
{
72-
name: "invalid version format", // e.g., missing patch version
73-
imageName: "nkp-ubuntu-22.04-1.32-20250604180644",
78+
name: "custom image name - no match", // e.g., not following NKP naming convention
79+
imageName: "my-custom-image-name",
7480
want: "",
7581
wantErr: true,
7682
},
@@ -88,7 +94,7 @@ func TestExtractKubernetesVersionFromImageName(t *testing.T) {
8894

8995
if tc.wantErr {
9096
require.Error(t, err)
91-
assert.Contains(t, err.Error(), "image name does not match expected NKP naming convention")
97+
assert.Contains(t, err.Error(), "image name does not match expected naming convention")
9298
} else {
9399
require.NoError(t, err)
94100
assert.Equal(t, tc.want, got)
@@ -188,7 +194,7 @@ func TestVMImageCheckWithKubernetesVersion(t *testing.T) {
188194
Error: true,
189195
Causes: []preflight.Cause{
190196
{
191-
Message: "failed to extract Kubernetes version from image name 'my-custom-image-name': image name does not match expected NKP naming convention (expected pattern: *-<k8s-version>-<timestamp>). This check assumes NKP image naming convention. You can opt out of this check if using custom image naming", //nolint:lll // cause is long
197+
Message: "failed to extract Kubernetes version from image name 'my-custom-image-name': image name does not match expected naming convention (expected pattern: .*<k8s-version>.*). This check assumes a naming convention that includes kubernetes version in the name. You can opt out of this check if using non-compliant naming", //nolint:lll // cause is long
192198
Field: "test-field",
193199
},
194200
},

0 commit comments

Comments
 (0)