Skip to content

Commit 5cd9c83

Browse files
feat: Do not create VM if system or bootstrap disks are marked for deletion (#492)
* refactor: Make available complete image info, instead of UUID only We plan to make decisions based on some of the information. * fixup! refactor: Make available complete image info, instead of UUID only Refactor further; add unit tests with mocks * fixup! refactor: Make available complete image info, instead of UUID only Use longer name for receivers * fixup! refactor: Make available complete image info, instead of UUID only Add unit test for new String method * fixup! refactor: Make available complete image info, instead of UUID only Add envtest-based test for getDiskList. I adapted this from Ilya's work in #493. Co-authored-by: Ilya Alekseyev <[email protected]> * fixup! refactor: Make available complete image info, instead of UUID only Rename function from GetImageByNameOrUUID to GetImage * fixup! refactor: Make available complete image info, instead of UUID only Add and use IsName and IsUUID helpers * fixup! refactor: Make available complete image info, instead of UUID only Rename input parameter from image to id, for clarity * fixup! refactor: Make available complete image info, instead of UUID only As requested, do not use nested switch * feat: Do not create VM if system or bootstrap disks are marked for deletion * fixup! feat: Do not create VM if system or bootstrap disks are marked for deletion Do not export function * fixup! feat: Do not create VM if system or bootstrap disks are marked for deletion Use constants for image state * fixup! feat: Do not create VM if system or bootstrap disks are marked for deletion Replace ginkgo with standard tests; cover failure cases * fixup! feat: Do not create VM if system or bootstrap disks are marked for deletion Export helper function, as requested --------- Co-authored-by: Ilya Alekseyev <[email protected]>
1 parent 41163a5 commit 5cd9c83

File tree

3 files changed

+229
-133
lines changed

3 files changed

+229
-133
lines changed

controllers/helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const (
5353
gpuUnused = "UNUSED"
5454

5555
detachVGRequeueAfter = 30 * time.Second
56+
57+
ImageStateDeletePending = "DELETE_PENDING"
58+
ImageStateDeleteInProgress = "DELETE_IN_PROGRESS"
5659
)
5760

5861
// DeleteVM deletes a VM and is invoked by the NutanixMachineReconciler
@@ -335,6 +338,11 @@ func GetImage(ctx context.Context, client *prismclientv3.Client, id infrav1.Nuta
335338
}
336339
}
337340

341+
func ImageMarkedForDeletion(image *prismclientv3.ImageIntentResponse) bool {
342+
state := *image.Status.State
343+
return state == ImageStateDeletePending || state == ImageStateDeleteInProgress
344+
}
345+
338346
// HasTaskInProgress returns true if the given task is in progress
339347
func HasTaskInProgress(ctx context.Context, client *prismclientv3.Client, taskUUID string) (bool, error) {
340348
log := ctrl.LoggerFrom(ctx)

controllers/nutanixmachine_controller.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,15 @@ func getSystemDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error) {
782782
return nil, err
783783
}
784784

785+
// Consider this a precaution. If the image is marked for deletion after we
786+
// create the "VM create" task, then that task will fail. We will handle that
787+
// failure separately.
788+
if ImageMarkedForDeletion(nodeOSImage) {
789+
err := fmt.Errorf("system disk image %s is being deleted", *nodeOSImage.Metadata.UUID)
790+
rctx.SetFailureStatus(capierrors.CreateMachineError, err)
791+
return nil, err
792+
}
793+
785794
systemDiskSizeMib := GetMibValueOfQuantity(rctx.NutanixMachine.Spec.SystemDiskSize)
786795
systemDisk, err := CreateSystemDiskSpec(*nodeOSImage.Metadata.UUID, systemDiskSizeMib)
787796
if err != nil {
@@ -805,6 +814,15 @@ func getBootstrapDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error)
805814
return nil, err
806815
}
807816

817+
// Consider this a precaution. If the image is marked for deletion after we
818+
// create the "VM create" task, then that task will fail. We will handle that
819+
// failure separately.
820+
if ImageMarkedForDeletion(bootstrapImage) {
821+
err := fmt.Errorf("bootstrap disk image %s is being deleted", *bootstrapImage.Metadata.UUID)
822+
rctx.SetFailureStatus(capierrors.CreateMachineError, err)
823+
return nil, err
824+
}
825+
808826
bootstrapDisk := &prismclientv3.VMDisk{
809827
DeviceProperties: &prismclientv3.VMDiskDeviceProperties{
810828
DeviceType: ptr.To(deviceTypeCDROM),

0 commit comments

Comments
 (0)