Skip to content

Commit b88801d

Browse files
committed
fix: More granular error message for NutanixVMImage preflight check
1 parent 520a837 commit b88801d

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

pkg/webhook/preflight/nutanix/image.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,32 @@ func (c *imageCheck) Run(ctx context.Context) preflight.CheckResult {
5454
return result
5555
}
5656

57-
if len(images) != 1 {
57+
switch len(images) {
58+
case 0:
5859
result.Allowed = false
5960
result.Causes = append(result.Causes, preflight.Cause{
6061
Message: fmt.Sprintf(
61-
"Found %d VM Images in Prism Central that match identifier %q. There must be exactly 1 VM Image that matches this identifier. Remove duplicate VM Images, use a different VM Image, or identify the VM Image by its UUID, then retry.", ///nolint:lll // Message is long.
62+
"No VM Images found in Prism Central that match identifier %q. Check that the correct VM Image is found in Prism Central. If the VM Image exists, you may need to change the identifier. If no VM Image is found, create one using the NKP CLI, or download a pre-built VM Image from the Nutanix portal, and upload it to Prism Central. Once you have the correct VM Image, retry.", ///nolint:lll // Message is long.
63+
c.machineDetails.Image,
64+
),
65+
Field: c.field + ".image",
66+
})
67+
return result
68+
case 1:
69+
result.Allowed = true
70+
return result
71+
default: // Found multiple images.
72+
result.Allowed = false
73+
result.Causes = append(result.Causes, preflight.Cause{
74+
Message: fmt.Sprintf(
75+
"Found %d VM Images in Prism Central that match name %q. There must be exactly 1 VM Image that matches this name. Remove duplicate VM Images, use a different VM Image, or identify the VM Image by its UUID, then retry.", ///nolint:lll // Message is long.
6276
len(images),
6377
c.machineDetails.Image,
6478
),
6579
Field: c.field + ".image",
6680
})
6781
return result
6882
}
69-
70-
// Found exactly one image.
71-
result.Allowed = true
72-
return result
7383
}
7484

7585
// Neither ImageLookup nor Image is specified.

pkg/webhook/preflight/nutanix/image_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ func TestVMImageCheck(t *testing.T) {
6868
Allowed: true,
6969
},
7070
},
71+
{
72+
name: "image not found by uuid",
73+
nclient: &clientWrapper{
74+
GetImageByIdFunc: func(uuid *string, args ...map[string]interface{}) (*vmmv4.GetImageApiResponse, error) {
75+
return nil, nil
76+
},
77+
},
78+
machineDetails: &carenv1.NutanixMachineDetails{
79+
Image: &capxv1.NutanixResourceIdentifier{
80+
Type: capxv1.NutanixIdentifierUUID,
81+
UUID: ptr.To("test-uuid"),
82+
},
83+
},
84+
want: preflight.CheckResult{
85+
Allowed: false,
86+
Causes: []preflight.Cause{
87+
{
88+
Message: "No VM Images found in Prism Central that match identifier \"test-uuid\". Check that the correct VM Image is found in Prism Central. If the VM Image exists, you may need to change the identifier. If no VM Image is found, create one using the NKP CLI, or download a pre-built VM Image from the Nutanix portal, and upload it to Prism Central. Once you have the correct VM Image, retry.", ///nolint:lll // Message is long.
89+
Field: "machineDetails.image",
90+
},
91+
},
92+
},
93+
},
7194
{
7295
name: "image found by name",
7396
nclient: &clientWrapper{
@@ -127,7 +150,7 @@ func TestVMImageCheck(t *testing.T) {
127150
Allowed: false,
128151
Causes: []preflight.Cause{
129152
{
130-
Message: "Found 0 VM Images in Prism Central that match identifier \"test-non-existent-image\". There must be exactly 1 VM Image that matches this identifier. Remove duplicate VM Images, use a different VM Image, or identify the VM Image by its UUID, then retry.", ///nolint:lll // Message is long.
153+
Message: "No VM Images found in Prism Central that match identifier \"test-non-existent-image\". Check that the correct VM Image is found in Prism Central. If the VM Image exists, you may need to change the identifier. If no VM Image is found, create one using the NKP CLI, or download a pre-built VM Image from the Nutanix portal, and upload it to Prism Central. Once you have the correct VM Image, retry.", ///nolint:lll // Message is long.
131154
Field: "machineDetails.image",
132155
},
133156
},
@@ -169,7 +192,7 @@ func TestVMImageCheck(t *testing.T) {
169192
Allowed: false,
170193
Causes: []preflight.Cause{
171194
{
172-
Message: "Found 2 VM Images in Prism Central that match identifier \"test-duplicate-image\". There must be exactly 1 VM Image that matches this identifier. Remove duplicate VM Images, use a different VM Image, or identify the VM Image by its UUID, then retry.", ///nolint:lll // Message is long.
195+
Message: "Found 2 VM Images in Prism Central that match name \"test-duplicate-image\". There must be exactly 1 VM Image that matches this name. Remove duplicate VM Images, use a different VM Image, or identify the VM Image by its UUID, then retry.", ///nolint:lll // Message is long.
173196
Field: "machineDetails.image",
174197
},
175198
},

0 commit comments

Comments
 (0)