Skip to content

Commit 204ffd1

Browse files
committed
Fix delete rosaMachinePool and rosaControlPlane
Signed-off-by: melserngawy <[email protected]>
1 parent dd2962d commit 204ffd1

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

controlplane/rosa/controllers/rosacontrolplane_controller.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
apierrors "k8s.io/apimachinery/pkg/api/errors"
3737
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3838
"k8s.io/apimachinery/pkg/types"
39+
kerrors "k8s.io/apimachinery/pkg/util/errors"
3940
restclient "k8s.io/client-go/rest"
4041
"k8s.io/client-go/tools/clientcmd"
4142
"k8s.io/client-go/tools/clientcmd/api"
@@ -54,6 +55,7 @@ import (
5455
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
5556
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
5657
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/rosa"
58+
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/utils"
5759
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
5860
"sigs.k8s.io/cluster-api/util"
5961
capiannotations "sigs.k8s.io/cluster-api/util/annotations"
@@ -300,6 +302,16 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
300302
func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope) (res ctrl.Result, reterr error) {
301303
rosaScope.Info("Reconciling ROSAControlPlane delete")
302304

305+
// Deleting MachinePools first.
306+
deleted, err := r.deleteMachinePools(ctx, rosaScope)
307+
if err != nil {
308+
return ctrl.Result{}, err
309+
}
310+
if !deleted {
311+
// Reconcile after 1 min giving time for machinePools to be deleted.
312+
return ctrl.Result{RequeueAfter: time.Minute}, nil
313+
}
314+
303315
ocmClient, err := rosa.NewOCMClient(ctx, rosaScope)
304316
if err != nil {
305317
// TODO: need to expose in status, as likely the credentials are invalid
@@ -316,8 +328,9 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
316328
return ctrl.Result{}, err
317329
}
318330
if cluster == nil {
319-
// cluster is fully deleted, remove finalizer.
331+
// cluster and machinepools are deleted, removing finalizer.
320332
controllerutil.RemoveFinalizer(rosaScope.ControlPlane, ROSAControlPlaneFinalizer)
333+
321334
return ctrl.Result{}, nil
322335
}
323336

@@ -350,6 +363,30 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
350363
return ctrl.Result{RequeueAfter: time.Second * 60}, nil
351364
}
352365

366+
// deleteMachinePools check if the controlplane has related machinePools and delete them.
367+
func (r *ROSAControlPlaneReconciler) deleteMachinePools(ctx context.Context, rosaScope *scope.ROSAControlPlaneScope) (bool, error) {
368+
machinePools, err := utils.GetMachinePools(ctx, rosaScope.Client, rosaScope.Cluster.Name, rosaScope.Cluster.Namespace)
369+
if err != nil {
370+
return false, err
371+
}
372+
373+
var errs []error
374+
for id, mp := range machinePools {
375+
if !mp.DeletionTimestamp.IsZero() {
376+
continue
377+
}
378+
if err = rosaScope.Client.Delete(ctx, &machinePools[id]); err != nil {
379+
errs = append(errs, err)
380+
}
381+
}
382+
383+
if len(errs) > 0 {
384+
return false, kerrors.NewAggregate(errs)
385+
}
386+
387+
return len(machinePools) == 0, nil
388+
}
389+
353390
func (r *ROSAControlPlaneReconciler) reconcileClusterVersion(rosaScope *scope.ROSAControlPlaneScope, ocmClient *ocm.Client, cluster *cmv1.Cluster) error {
354391
version := rosaScope.ControlPlane.Spec.Version
355392
if version == rosa.RawVersionID(cluster.Version()) {

exp/controllers/rosamachinepool_controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
106106
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machinePool.ObjectMeta)
107107
if err != nil {
108108
log.Info("Failed to retrieve Cluster from MachinePool")
109-
return reconcile.Result{}, nil
109+
return ctrl.Result{}, nil
110110
}
111111

112112
if annotations.IsPaused(cluster, rosaMachinePool) {
@@ -123,7 +123,7 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
123123
controlPlane := &rosacontrolplanev1.ROSAControlPlane{}
124124
if err := r.Client.Get(ctx, controlPlaneKey, controlPlane); err != nil {
125125
log.Info("Failed to retrieve ControlPlane from MachinePool")
126-
return reconcile.Result{}, nil
126+
return ctrl.Result{}, err
127127
}
128128

129129
machinePoolScope, err := scope.NewRosaMachinePoolScope(scope.RosaMachinePoolScopeParams{
@@ -137,7 +137,7 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
137137
Endpoints: r.Endpoints,
138138
})
139139
if err != nil {
140-
return ctrl.Result{}, errors.Wrap(err, "failed to create scope")
140+
return ctrl.Result{}, errors.Wrap(err, "failed to create rosaMachinePool scope")
141141
}
142142

143143
rosaControlPlaneScope, err := scope.NewROSAControlPlaneScope(scope.ROSAControlPlaneScopeParams{
@@ -148,10 +148,10 @@ func (r *ROSAMachinePoolReconciler) Reconcile(ctx context.Context, req ctrl.Requ
148148
Endpoints: r.Endpoints,
149149
})
150150
if err != nil {
151-
return ctrl.Result{}, errors.Wrap(err, "failed to create control plane scope")
151+
return ctrl.Result{}, errors.Wrap(err, "failed to create rosaControlPlane scope")
152152
}
153153

154-
if !controlPlane.Status.Ready {
154+
if !controlPlane.Status.Ready && controlPlane.ObjectMeta.DeletionTimestamp.IsZero() {
155155
log.Info("Control plane is not ready yet")
156156
err := machinePoolScope.RosaMchinePoolReadyFalse(expinfrav1.WaitingForRosaControlPlaneReason, "")
157157
return ctrl.Result{}, err
@@ -305,6 +305,7 @@ func (r *ROSAMachinePoolReconciler) reconcileDelete(
305305
if err := ocmClient.DeleteNodePool(machinePoolScope.ControlPlane.Status.ID, nodePool.ID()); err != nil {
306306
return err
307307
}
308+
machinePoolScope.Info("Successfully deleted NodePool %s", nodePool.ID())
308309
}
309310

310311
controllerutil.RemoveFinalizer(machinePoolScope.RosaMachinePool, expinfrav1.RosaMachinePoolFinalizer)

pkg/utils/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Package utils has the common functions that can be used for cluster-api-provider-aws repo.
2+
package utils
3+
4+
import (
5+
"context"
6+
"fmt"
7+
8+
crclient "sigs.k8s.io/controller-runtime/pkg/client"
9+
10+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
11+
expclusterv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
12+
)
13+
14+
// GetMachinePools belong to a cluster.
15+
func GetMachinePools(ctx context.Context, client crclient.Client, clusterName string, clusterNS string) ([]expclusterv1.MachinePool, error) {
16+
machinePoolList := expclusterv1.MachinePoolList{}
17+
listOptions := []crclient.ListOption{
18+
crclient.InNamespace(clusterNS),
19+
crclient.MatchingLabels(map[string]string{clusterv1.ClusterNameLabel: clusterName}),
20+
}
21+
22+
if err := client.List(ctx, &machinePoolList, listOptions...); err != nil {
23+
return []expclusterv1.MachinePool{}, fmt.Errorf("failed to list machine pools for cluster %s: %v", clusterName, err)
24+
}
25+
26+
return machinePoolList.Items, nil
27+
}

0 commit comments

Comments
 (0)