Skip to content

Commit

Permalink
Clean up controlnode if machine related to K0sControlPlane is deleted (
Browse files Browse the repository at this point in the history
…#790)

Signed-off-by: Adrian Pedriza <adripedriza@gmail.com>
Co-authored-by: Adrian Pedriza <adripedriza@gmail.com>
  • Loading branch information
apedriza and AdrianPedriza authored Oct 31, 2024
1 parent 8aecb14 commit 9e78c1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/controller/controlplane/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ func (c *K0sController) markChildControlNodeToLeave(ctx context.Context, name st
return nil
}

func (c *K0sController) deleteControlNode(ctx context.Context, name string, clientset *kubernetes.Clientset) error {
if clientset == nil {
return nil
}

err := clientset.RESTClient().
Delete().
AbsPath("/apis/autopilot.k0sproject.io/v1beta2/controlnodes/" + name).
Do(ctx).
Error()
if err != nil && !apierrors.IsNotFound(err) {
return err
}

return nil
}

func (c *K0sController) createAutopilotPlan(ctx context.Context, kcp *cpv1beta1.K0sControlPlane, cluster *clusterv1.Cluster, clientset *kubernetes.Clientset) error {
if clientset == nil {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ func (c *K0sController) reconcileMachines(ctx context.Context, cluster *clusterv
return replicasToReport, fmt.Errorf("error deleting machine from template: %w", err)
}

if err := c.deleteControlNode(ctx, name, kubeClient); err != nil {
return replicasToReport, fmt.Errorf("error deleting controlnode: %w", err)
}

logger.Info("Deleted machine", "machine", name, "replicasToReport", replicasToReport)
return replicasToReport, nil
}
Expand Down

0 comments on commit 9e78c1f

Please sign in to comment.