@@ -36,6 +36,7 @@ import (
36
36
apierrors "k8s.io/apimachinery/pkg/api/errors"
37
37
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38
38
"k8s.io/apimachinery/pkg/types"
39
+ kerrors "k8s.io/apimachinery/pkg/util/errors"
39
40
restclient "k8s.io/client-go/rest"
40
41
"k8s.io/client-go/tools/clientcmd"
41
42
"k8s.io/client-go/tools/clientcmd/api"
@@ -54,6 +55,7 @@ import (
54
55
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
55
56
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
56
57
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/rosa"
58
+ "sigs.k8s.io/cluster-api-provider-aws/v2/pkg/utils"
57
59
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
58
60
"sigs.k8s.io/cluster-api/util"
59
61
capiannotations "sigs.k8s.io/cluster-api/util/annotations"
@@ -300,6 +302,16 @@ func (r *ROSAControlPlaneReconciler) reconcileNormal(ctx context.Context, rosaSc
300
302
func (r * ROSAControlPlaneReconciler ) reconcileDelete (ctx context.Context , rosaScope * scope.ROSAControlPlaneScope ) (res ctrl.Result , reterr error ) {
301
303
rosaScope .Info ("Reconciling ROSAControlPlane delete" )
302
304
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
+
303
315
ocmClient , err := rosa .NewOCMClient (ctx , rosaScope )
304
316
if err != nil {
305
317
// TODO: need to expose in status, as likely the credentials are invalid
@@ -316,8 +328,9 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
316
328
return ctrl.Result {}, err
317
329
}
318
330
if cluster == nil {
319
- // cluster is fully deleted, remove finalizer.
331
+ // cluster and machinepools are deleted, removing finalizer.
320
332
controllerutil .RemoveFinalizer (rosaScope .ControlPlane , ROSAControlPlaneFinalizer )
333
+
321
334
return ctrl.Result {}, nil
322
335
}
323
336
@@ -350,6 +363,30 @@ func (r *ROSAControlPlaneReconciler) reconcileDelete(ctx context.Context, rosaSc
350
363
return ctrl.Result {RequeueAfter : time .Second * 60 }, nil
351
364
}
352
365
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
+
353
390
func (r * ROSAControlPlaneReconciler ) reconcileClusterVersion (rosaScope * scope.ROSAControlPlaneScope , ocmClient * ocm.Client , cluster * cmv1.Cluster ) error {
354
391
version := rosaScope .ControlPlane .Spec .Version
355
392
if version == rosa .RawVersionID (cluster .Version ()) {
0 commit comments