Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions pkg/webhook/preflight/nutanix/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,32 @@ func (c *imageCheck) Run(ctx context.Context) preflight.CheckResult {
return result
}

if len(images) != 1 {
switch len(images) {
case 0:
result.Allowed = false
result.Causes = append(result.Causes, preflight.Cause{
Message: fmt.Sprintf(
"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.
"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.
c.machineDetails.Image,
),
Field: c.field + ".image",
})
return result
case 1:
result.Allowed = true
return result
default: // Found multiple images.
result.Allowed = false
result.Causes = append(result.Causes, preflight.Cause{
Message: fmt.Sprintf(
"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.
len(images),
c.machineDetails.Image,
),
Field: c.field + ".image",
})
return result
}

// Found exactly one image.
result.Allowed = true
return result
}

// Neither ImageLookup nor Image is specified.
Expand Down
27 changes: 25 additions & 2 deletions pkg/webhook/preflight/nutanix/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ func TestVMImageCheck(t *testing.T) {
Allowed: true,
},
},
{
name: "image not found by uuid",
nclient: &clientWrapper{
GetImageByIdFunc: func(uuid *string, args ...map[string]interface{}) (*vmmv4.GetImageApiResponse, error) {
return nil, nil
},
},
machineDetails: &carenv1.NutanixMachineDetails{
Image: &capxv1.NutanixResourceIdentifier{
Type: capxv1.NutanixIdentifierUUID,
UUID: ptr.To("test-uuid"),
},
},
want: preflight.CheckResult{
Allowed: false,
Causes: []preflight.Cause{
{
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.
Field: "machineDetails.image",
},
},
},
},
{
name: "image found by name",
nclient: &clientWrapper{
Expand Down Expand Up @@ -127,7 +150,7 @@ func TestVMImageCheck(t *testing.T) {
Allowed: false,
Causes: []preflight.Cause{
{
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.
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.
Field: "machineDetails.image",
},
},
Expand Down Expand Up @@ -169,7 +192,7 @@ func TestVMImageCheck(t *testing.T) {
Allowed: false,
Causes: []preflight.Cause{
{
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.
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.
Field: "machineDetails.image",
},
},
Expand Down
Loading