Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderboltsid committed Jul 30, 2024
1 parent ca42cfa commit 618f939
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions controllers/nutanixmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,26 +314,23 @@ func (r *NutanixMachineReconciler) reconcileDelete(rctx *nctx.MachineContext) (r

// Check if VMUUID is absent
if vmUUID == "" {
log.Info(fmt.Sprintf("VMUUID was not found in spec for VM %s. Skipping delete", vmName))
log.Info(fmt.Sprintf("VM UUID was not found in spec for VM %s. Skipping delete", vmName))
log.Info(fmt.Sprintf("Removing finalizers for VM %s during delete reconciliation", vmName))
ctrlutil.RemoveFinalizer(rctx.NutanixMachine, infrav1.NutanixMachineFinalizer)
return reconcile.Result{}, nil

Check warning on line 320 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L317-L320

Added lines #L317 - L320 were not covered by tests
}

// Search for VM by UUID
vm, err := FindVMByUUID(ctx, v3Client, vmUUID)
// Error while finding VM
if err != nil {
errorMsg := fmt.Errorf("error finding vm %s with uuid %s: %v", vmName, vmUUID, err)
log.Error(errorMsg, "error finding vm")
errorMsg := fmt.Errorf("error finding VM %s with UUID %s: %v", vmName, vmUUID, err)
log.Error(errorMsg, "error finding VM")
conditions.MarkFalse(rctx.NutanixMachine, infrav1.VMProvisionedCondition, infrav1.DeletionFailed, capiv1.ConditionSeverityWarning, errorMsg.Error())
return reconcile.Result{}, errorMsg

Check warning on line 328 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L323-L328

Added lines #L323 - L328 were not covered by tests
}

// Vm not found
if vm == nil {
log.Info(fmt.Sprintf("no vm found with UUID %s ... Already deleted? Skipping delete", vmUUID))
log.Info(fmt.Sprintf("Removing finalizers for VM %s during delete reconciliation", vmName))
log.Info(fmt.Sprintf("no VM found with UUID %s: assuming it is already deleted; skipping delete", vmUUID))
log.Info(fmt.Sprintf("removing finalizers for VM %s during delete reconciliation", vmName))
ctrlutil.RemoveFinalizer(rctx.NutanixMachine, infrav1.NutanixMachineFinalizer)
return reconcile.Result{}, nil

Check warning on line 335 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L331-L335

Added lines #L331 - L335 were not covered by tests
}
Expand All @@ -350,7 +347,7 @@ func (r *NutanixMachineReconciler) reconcileDelete(rctx *nctx.MachineContext) (r
log.V(1).Info(fmt.Sprintf("VM %s with UUID %s was found.", *vm.Spec.Name, vmUUID))
lastTaskUUID, err := GetTaskUUIDFromVM(vm)
if err != nil {
errorMsg := fmt.Errorf("error occurred fetching task UUID from vm: %v", err)
errorMsg := fmt.Errorf("error occurred fetching task UUID from VM: %v", err)
log.Error(errorMsg, "error fetching task UUID")
conditions.MarkFalse(rctx.NutanixMachine, infrav1.VMProvisionedCondition, infrav1.DeletionFailed, capiv1.ConditionSeverityWarning, errorMsg.Error())
return reconcile.Result{}, errorMsg

Check warning on line 353 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L347-L353

Added lines #L347 - L353 were not covered by tests
Expand All @@ -366,7 +363,7 @@ func (r *NutanixMachineReconciler) reconcileDelete(rctx *nctx.MachineContext) (r
log.Info(fmt.Sprintf("VM %s task with UUID %s still in progress. Requeuing", vmName, vmUUID))
return reconcile.Result{RequeueAfter: 5 * time.Second}, nil

Check warning on line 364 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L362-L364

Added lines #L362 - L364 were not covered by tests
}
log.V(1).Info(fmt.Sprintf("No running tasks anymore... Initiating delete for vm %s with UUID %s", vmName, vmUUID))
log.V(1).Info(fmt.Sprintf("no running tasks anymore... Initiating delete for VM %s with UUID %s", vmName, vmUUID))

Check warning on line 366 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L366

Added line #L366 was not covered by tests
} else {
log.V(1).Info(fmt.Sprintf("no task UUID found on VM %s. Starting delete.", *vm.Spec.Name))

Check warning on line 368 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L368

Added line #L368 was not covered by tests
}
Expand All @@ -382,25 +379,8 @@ func (r *NutanixMachineReconciler) reconcileDelete(rctx *nctx.MachineContext) (r
}

if vgDetachNeeded {
createV4Client, err := isPrismCentralV4Compatible(ctx, v3Client)
if err != nil {
log.Info("error occurred while checking compatibility for Prism Central v4 APIs", "error", err.Error())
}

if createV4Client {
log.Info("Creating Prism Central v4 client for cluster", "cluster", rctx.NutanixCluster.Name)
v4Client, err := getPrismCentralV4ClientForCluster(ctx, rctx.NutanixCluster, r.SecretInformer, r.ConfigMapInformer)
if err != nil {
log.Error(err, "error occurred while fetching Prism Central v4 client")
return reconcile.Result{}, err
}

if err := detachVolumeGroupsFromVM(ctx, v4Client, vmUUID); err != nil {
errorMsg := fmt.Errorf("failed to detach volume groups from VM %s with UUID %s: %v", vmName, vmUUID, err)
conditions.MarkFalse(rctx.NutanixMachine, infrav1.VMProvisionedCondition, infrav1.VolumeGroupDetachFailed, capiv1.ConditionSeverityWarning, errorMsg.Error())
log.Error(errorMsg, "failed to detach volume groups", "cluster", rctx.NutanixCluster.Name)
return reconcile.Result{}, err
}
if err := r.detachVolumeGroups(rctx, vmUUID); err != nil {
return reconcile.Result{}, err

Check warning on line 383 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L381-L383

Added lines #L381 - L383 were not covered by tests
}
}

Expand All @@ -416,6 +396,29 @@ func (r *NutanixMachineReconciler) reconcileDelete(rctx *nctx.MachineContext) (r
return reconcile.Result{RequeueAfter: 5 * time.Second}, nil

Check warning on line 396 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L395-L396

Added lines #L395 - L396 were not covered by tests
}

func (r *NutanixMachineReconciler) detachVolumeGroups(rctx *nctx.MachineContext, vmUUID string) error {
createV4Client, err := isPrismCentralV4Compatible(rctx.Context, rctx.NutanixClient)
if err != nil {
err := fmt.Errorf("error occurred while checking compatibility for Prism Central v4 APIs: %w", err)
return err

Check warning on line 403 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L400-L403

Added lines #L400 - L403 were not covered by tests
}

if createV4Client {
v4Client, err := getPrismCentralV4ClientForCluster(rctx.Context, rctx.NutanixCluster, r.SecretInformer, r.ConfigMapInformer)
if err != nil {
err := fmt.Errorf("error occurred while fetching Prism Central v4 client: %w", err)
return err

Check warning on line 410 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L406-L410

Added lines #L406 - L410 were not covered by tests
}

if err := detachVolumeGroupsFromVM(rctx.Context, v4Client, vmUUID); err != nil {
err := fmt.Errorf("failed to detach volume groups from VM %s with UUID %s: %w", rctx.Machine.Name, vmUUID, err)
return err

Check warning on line 415 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L413-L415

Added lines #L413 - L415 were not covered by tests
}
}

return nil

Check warning on line 419 in controllers/nutanixmachine_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/nutanixmachine_controller.go#L419

Added line #L419 was not covered by tests
}

func (r *NutanixMachineReconciler) reconcileNormal(rctx *nctx.MachineContext) (reconcile.Result, error) {
log := ctrl.LoggerFrom(rctx.Context)
if rctx.NutanixMachine.Status.FailureReason != nil || rctx.NutanixMachine.Status.FailureMessage != nil {
Expand Down

0 comments on commit 618f939

Please sign in to comment.