Skip to content

Commit

Permalink
Delete node on machine deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed Mar 8, 2019
1 parent 9d16c16 commit 84d3282
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
return reconcile.Result{}, err
}

if m.Status.NodeRef != nil {
klog.Infof("Deleting node %q for machine %q", m.Status.NodeRef.Name, m.Name )
if err := r.deleteNode(ctx, m.Status.NodeRef.Name); err != nil {
klog.Errorf("Error deleting node %q for machine %q", name, err)
return reconcile.Result{}, err
}
}

// Remove finalizer on successful deletion.
klog.Infof("machine object %v deletion successful, removing finalizer.", name)
m.ObjectMeta.Finalizers = util.Filter(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer)
Expand Down Expand Up @@ -238,3 +246,16 @@ func (r *ReconcileMachine) isDeleteAllowed(machine *clusterv1.Machine) bool {
// own machine.
return node.UID != machine.Status.NodeRef.UID
}

func (r *ReconcileMachine) deleteNode(context context.Context, name string) error {
var node corev1.Node
if err := r.Client.Get(context, client.ObjectKey{Name: name}, &node); err != nil {
if apierrors.IsNotFound(err) {
klog.Infof("Node %q not found", name)
return nil
}
klog.Errorf("Failed to get node %q: %v", name, err)
return err
}
return r.Client.Delete(context, &node)
}

0 comments on commit 84d3282

Please sign in to comment.