@@ -57,6 +57,7 @@ import (
57
57
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
58
58
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
59
59
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/rosa"
60
+ "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/utils"
60
61
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
61
62
"sigs.k8s.io/cluster-api/util"
62
63
capiannotations "sigs.k8s.io/cluster-api/util/annotations"
@@ -316,6 +317,16 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
316
317
func (r * ROSAControlPlaneReconciler ) reconcileDelete (ctx context.Context , rosaScope * scope.ROSAControlPlaneScope ) (res ctrl.Result , reterr error ) {
317
318
rosaScope .Info ("Reconciling ROSAControlPlane delete" )
318
319
320
+ // Deleting MachinePools first.
321
+ deleted , err := r .deleteMachinePools (ctx , rosaScope )
322
+ if err != nil {
323
+ return ctrl.Result {}, err
324
+ }
325
+ if ! deleted {
326
+ // Reconcile after 1 min giving time for machinePools to be deleted.
327
+ return ctrl.Result {RequeueAfter : time .Minute }, nil
328
+ }
329
+
319
330
ocmClient , err := rosa .NewOCMClient (ctx , rosaScope )
320
331
if err != nil {
321
332
// TODO: need to expose in status, as likely the credentials are invalid
@@ -332,8 +343,9 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
332
343
return ctrl.Result {}, err
333
344
}
334
345
if cluster == nil {
335
- // cluster is fully deleted, remove finalizer.
346
+ // cluster and machinepools are deleted, removing finalizer.
336
347
controllerutil .RemoveFinalizer (rosaScope .ControlPlane , ROSAControlPlaneFinalizer )
348
+
337
349
return ctrl.Result {}, nil
338
350
}
339
351
@@ -366,6 +378,30 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
366
378
return ctrl.Result {RequeueAfter : time .Second * 60 }, nil
367
379
}
368
380
381
+ // deleteMachinePools check if the controlplane has related machinePools and delete them.
382
+ func (r * ROSAControlPlaneReconciler ) deleteMachinePools (ctx context.Context , rosaScope * scope.ROSAControlPlaneScope ) (bool , error ) {
383
+ machinePools , err := utils .GetMachinePools (ctx , rosaScope .Client , rosaScope .Cluster .Name , rosaScope .Cluster .Namespace )
384
+ if err != nil {
385
+ return false , err
386
+ }
387
+
388
+ var errs []error
389
+ for id , mp := range machinePools {
390
+ if ! mp .DeletionTimestamp .IsZero () {
391
+ continue
392
+ }
393
+ if err = rosaScope .Client .Delete (ctx , & machinePools [id ]); err != nil {
394
+ errs = append (errs , err )
395
+ }
396
+ }
397
+
398
+ if len (errs ) > 0 {
399
+ return false , kerrors .NewAggregate (errs )
400
+ }
401
+
402
+ return len (machinePools ) == 0 , nil
403
+ }
404
+
369
405
func (r * ROSAControlPlaneReconciler ) reconcileClusterVersion (rosaScope * scope.ROSAControlPlaneScope , ocmClient * ocm.Client , cluster * cmv1.Cluster ) error {
370
406
version := rosaScope .ControlPlane .Spec .Version
371
407
if version == rosa .RawVersionID (cluster .Version ()) {
0 commit comments