From 7fed0f236c96668ca1d35f3ca970d6813513e7ef Mon Sep 17 00:00:00 2001 From: "Homayoon (Hue) Alimohammadi" Date: Tue, 27 Aug 2024 21:44:00 +0400 Subject: [PATCH 1/2] Use Node name instead of Machine name on removal --- pkg/ck8s/workload_cluster.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/ck8s/workload_cluster.go b/pkg/ck8s/workload_cluster.go index 5177bc6d..19dc915a 100644 --- a/pkg/ck8s/workload_cluster.go +++ b/pkg/ck8s/workload_cluster.go @@ -230,11 +230,18 @@ func (w *Workload) requestJoinToken(ctx context.Context, name string, worker boo } func (w *Workload) RemoveMachineFromCluster(ctx context.Context, machine *clusterv1.Machine) error { - request := &apiv1.RemoveNodeRequest{Name: machine.Name, Force: true} + if machine == nil { + return fmt.Errorf("machine is nil") + } else if machine.Status.NodeRef == nil { + return fmt.Errorf("machine %s has no node reference", machine.Name) + } + + nodeName := machine.Status.NodeRef.Name + request := &apiv1.RemoveNodeRequest{Name: nodeName, Force: true} // If we see that ignoring control-planes is causing issues, let's consider removing it. // It *should* not be necessary as a machine should be able to remove itself from the cluster. - err := w.doK8sdRequest(ctx, http.MethodPost, "1.0/x/capi/remove-node", request, nil, k8sdProxyOptions{IgnoreNodes: map[string]struct{}{machine.Name: {}}}) + err := w.doK8sdRequest(ctx, http.MethodPost, "1.0/x/capi/remove-node", request, nil, k8sdProxyOptions{IgnoreNodes: map[string]struct{}{nodeName: {}}}) if err != nil { return fmt.Errorf("failed to remove %s from cluster: %w", machine.Name, err) } From 005e2cde215a75856ee83c07df6821344c9e6db5 Mon Sep 17 00:00:00 2001 From: "Homayoon (Hue) Alimohammadi" Date: Wed, 28 Aug 2024 10:03:09 +0400 Subject: [PATCH 2/2] Address comments --- pkg/ck8s/workload_cluster.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/ck8s/workload_cluster.go b/pkg/ck8s/workload_cluster.go index 19dc915a..311135e3 100644 --- a/pkg/ck8s/workload_cluster.go +++ b/pkg/ck8s/workload_cluster.go @@ -231,8 +231,9 @@ func (w *Workload) requestJoinToken(ctx context.Context, name string, worker boo func (w *Workload) RemoveMachineFromCluster(ctx context.Context, machine *clusterv1.Machine) error { if machine == nil { - return fmt.Errorf("machine is nil") - } else if machine.Status.NodeRef == nil { + return fmt.Errorf("machine object is not set") + } + if machine.Status.NodeRef == nil { return fmt.Errorf("machine %s has no node reference", machine.Name) }