diff --git a/pkg/controller/machine/controller.go b/pkg/controller/machine/controller.go index 00b9c77f3dc9..74ed0a15e283 100644 --- a/pkg/controller/machine/controller.go +++ b/pkg/controller/machine/controller.go @@ -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) @@ -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) +} \ No newline at end of file