Skip to content

Commit

Permalink
Delete node on machine deletion (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre authored and k8s-ci-robot committed Mar 11, 2019
1 parent fbb9787 commit 4708f8e
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 @@ -178,6 +178,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.
m.ObjectMeta.Finalizers = util.Filter(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer)
if err := r.Client.Update(context.Background(), m); err != nil {
Expand Down Expand Up @@ -263,3 +271,16 @@ func (r *ReconcileMachine) isDeleteAllowed(machine *clusterv1.Machine) bool {
// own machine.
return node.UID != machine.Status.NodeRef.UID
}

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

0 comments on commit 4708f8e

Please sign in to comment.