Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Do not create VM if system or bootstrap disks are marked for deletion #492

Open
wants to merge 2 commits into
base: jira/NCN-101014-refactor-get-image
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func GetImage(ctx context.Context, client *prismclientv3.Client, id infrav1.Nuta
}
}

func imageMarkedForDeletion(image *prismclientv3.ImageIntentResponse) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we going to use this helper function only in controllers package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have enough information to answer.

I see that most functions in helpers.go are exported. I can export this one, too, if you want. I default to unexported, unless there's a clear need.

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)
Expand Down
18 changes: 18 additions & 0 deletions controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down
Loading