Skip to content

Commit

Permalink
feat: Do not create VM if system or bootstrap disks are marked for de…
Browse files Browse the repository at this point in the history
…letion
  • Loading branch information
dlipovetsky committed Dec 11, 2024
1 parent 1434308 commit 58548c7
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
@@ -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"

Check warning on line 342 in controllers/helpers.go

Codecov / codecov/patch

controllers/helpers.go#L340-L342

Added lines #L340 - L342 were not covered by tests
}

// 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)
18 changes: 18 additions & 0 deletions controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
@@ -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
}

Check warning on line 792 in controllers/nutanixmachine_controller.go

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L788-L792

Added lines #L788 - L792 were not covered by tests

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
}

Check warning on line 824 in controllers/nutanixmachine_controller.go

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L820-L824

Added lines #L820 - L824 were not covered by tests

bootstrapDisk := &prismclientv3.VMDisk{
DeviceProperties: &prismclientv3.VMDiskDeviceProperties{
DeviceType: ptr.To(deviceTypeCDROM),

0 comments on commit 58548c7

Please sign in to comment.