From 58548c7ee0a07cd4e060c5357ce5b05193b8fbe2 Mon Sep 17 00:00:00 2001 From: Daniel Lipovetsky Date: Tue, 26 Nov 2024 15:20:01 -0800 Subject: [PATCH] feat: Do not create VM if system or bootstrap disks are marked for deletion --- controllers/helpers.go | 5 +++++ controllers/nutanixmachine_controller.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/controllers/helpers.go b/controllers/helpers.go index bc938cadf7..2405bb3a1d 100644 --- a/controllers/helpers.go +++ b/controllers/helpers.go @@ -337,6 +337,11 @@ func GetImageByNameOrUUID(ctx context.Context, client *prismclientv3.Client, ima } } +func ImageMarkedForDeletion(image *prismclientv3.ImageIntentResponse) bool { + state := *image.Status.State + return state == "DELETE_PENDING" || state == "DELETE_IN_PROGRESS" +} + // HasTaskInProgress returns true if the given task is in progress func HasTaskInProgress(ctx context.Context, client *prismclientv3.Client, taskUUID string) (bool, error) { log := ctrl.LoggerFrom(ctx) diff --git a/controllers/nutanixmachine_controller.go b/controllers/nutanixmachine_controller.go index d4fcb5914e..854938eeea 100644 --- a/controllers/nutanixmachine_controller.go +++ b/controllers/nutanixmachine_controller.go @@ -782,6 +782,15 @@ func getSystemDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error) { return nil, err } + // Consider this a precaution. If the image is marked for deletion after we + // create the "VM create" task, then that task will fail. We will handle that + // failure separately. + if ImageMarkedForDeletion(nodeOSImage) { + err := fmt.Errorf("system disk image %s is being deleted", *nodeOSImage.Metadata.UUID) + rctx.SetFailureStatus(capierrors.CreateMachineError, err) + return nil, err + } + systemDiskSizeMib := GetMibValueOfQuantity(rctx.NutanixMachine.Spec.SystemDiskSize) systemDisk, err := CreateSystemDiskSpec(*nodeOSImage.Metadata.UUID, systemDiskSizeMib) if err != nil { @@ -805,6 +814,15 @@ func getBootstrapDisk(rctx *nctx.MachineContext) (*prismclientv3.VMDisk, error) return nil, err } + // Consider this a precaution. If the image is marked for deletion after we + // create the "VM create" task, then that task will fail. We will handle that + // failure separately. + if ImageMarkedForDeletion(bootstrapImage) { + err := fmt.Errorf("bootstrap disk image %s is being deleted", *bootstrapImage.Metadata.UUID) + rctx.SetFailureStatus(capierrors.CreateMachineError, err) + return nil, err + } + bootstrapDisk := &prismclientv3.VMDisk{ DeviceProperties: &prismclientv3.VMDiskDeviceProperties{ DeviceType: ptr.To(deviceTypeCDROM),