Skip to content

Commit 4f3a678

Browse files
Merge pull request #65 from canonical/KU-1496/fix-panic-on-machine-deletion
Prevent panicing when NodeRef is not set when deleting a machine
2 parents e6cfd78 + 7a64c9b commit 4f3a678

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

controllers/reconcile.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ func (r *MicroK8sControlPlaneReconciler) scaleDownControlPlane(ctx context.Conte
531531
for i := len(machines) - 1; i >= 0; i-- {
532532
machine = machines[i]
533533
logger := logger.WithValues("machineName", machine.Name)
534+
535+
// do not allow scaling down until all nodes have nodeRefs
536+
// NOTE(hue): this might happen when we're trying to delete a machine instance that CAN NOT
537+
// get a nodeRef, e.g. because the infra provider is not able to create the machine.
538+
// In this case, requeueing here will not solve the issue and only prevents the machine from being deleted.
539+
if machine.Status.NodeRef == nil {
540+
logger.Info("machine does not have a nodeRef yet")
541+
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
542+
}
543+
534544
if !machine.ObjectMeta.DeletionTimestamp.IsZero() {
535545
logger.Info("machine is in process of deletion")
536546

@@ -555,12 +565,6 @@ func (r *MicroK8sControlPlaneReconciler) scaleDownControlPlane(ctx context.Conte
555565
return ctrl.Result{RequeueAfter: 20 * time.Second}, nil
556566
}
557567

558-
// do not allow scaling down until all nodes have nodeRefs
559-
if machine.Status.NodeRef == nil {
560-
logger.Info("one of machines does not have NodeRef")
561-
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
562-
}
563-
564568
// mark the oldest machine to be deleted first
565569
if machine.CreationTimestamp.Before(&deleteMachine.CreationTimestamp) {
566570
deleteMachine = machine

0 commit comments

Comments
 (0)