@@ -58,6 +58,10 @@ import (
5858 "sigs.k8s.io/cluster-api/util/predicates"
5959)
6060
61+ const (
62+ deleteRequeueAfter = 20 * time .Second
63+ )
64+
6165var defaultAWSSecurityGroupRoles = []infrav1.SecurityGroupRole {
6266 infrav1 .SecurityGroupAPIServerLB ,
6367 infrav1 .SecurityGroupLB ,
@@ -191,21 +195,34 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
191195
192196 // Handle deleted clusters
193197 if ! awsCluster .DeletionTimestamp .IsZero () {
194- return ctrl. Result {}, r .reconcileDelete (ctx , clusterScope )
198+ return r .reconcileDelete (ctx , clusterScope )
195199 }
196200
197201 // Handle non-deleted clusters
198202 return r .reconcileNormal (clusterScope )
199203}
200204
201- func (r * AWSClusterReconciler ) reconcileDelete (ctx context.Context , clusterScope * scope.ClusterScope ) error {
205+ func (r * AWSClusterReconciler ) reconcileDelete (ctx context.Context , clusterScope * scope.ClusterScope ) (ctrl. Result , error ) {
202206 if ! controllerutil .ContainsFinalizer (clusterScope .AWSCluster , infrav1 .ClusterFinalizer ) {
203207 clusterScope .Info ("No finalizer on AWSCluster, skipping deletion reconciliation" )
204- return nil
208+ return reconcile. Result {}, nil
205209 }
206210
207211 clusterScope .Info ("Reconciling AWSCluster delete" )
208212
213+ numDependencies , err := r .dependencyCount (ctx , clusterScope )
214+ if err != nil {
215+ clusterScope .Error (err , "error getting AWSCluster dependencies" )
216+ return reconcile.Result {}, err
217+ }
218+
219+ if numDependencies > 0 {
220+ clusterScope .Info ("AWSCluster cluster still has dependencies - requeue needed" , "dependencyCount" , numDependencies )
221+ return reconcile.Result {RequeueAfter : deleteRequeueAfter }, nil
222+ }
223+
224+ clusterScope .Info ("AWSCluster has no dependent CAPI resources, proceeding with its deletion" )
225+
209226 ec2svc := r .getEC2Service (clusterScope )
210227 elbsvc := r .getELBService (clusterScope )
211228 networkSvc := r .getNetworkService (* clusterScope )
@@ -257,12 +274,12 @@ func (r *AWSClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
257274 }
258275
259276 if len (allErrs ) > 0 {
260- return kerrors .NewAggregate (allErrs )
277+ return reconcile. Result {}, kerrors .NewAggregate (allErrs )
261278 }
262279
263280 // Cluster is deleted so remove the finalizer.
264281 controllerutil .RemoveFinalizer (clusterScope .AWSCluster , infrav1 .ClusterFinalizer )
265- return nil
282+ return reconcile. Result {}, nil
266283}
267284
268285func (r * AWSClusterReconciler ) reconcileLoadBalancer (clusterScope * scope.ClusterScope , awsCluster * infrav1.AWSCluster ) (* time.Duration , error ) {
@@ -484,3 +501,24 @@ func (r *AWSClusterReconciler) checkForExternalControlPlaneLoadBalancer(clusterS
484501 return nil
485502 }
486503}
504+
505+ func (r * AWSClusterReconciler ) dependencyCount (ctx context.Context , clusterScope * scope.ClusterScope ) (int , error ) {
506+ clusterName := clusterScope .Name ()
507+ namespace := clusterScope .Namespace ()
508+
509+ clusterScope .Info ("Looking for AWSCluster dependencies" )
510+
511+ listOptions := []client.ListOption {
512+ client .InNamespace (namespace ),
513+ client .MatchingLabels (map [string ]string {clusterv1 .ClusterNameLabel : clusterName }),
514+ }
515+
516+ machines := & infrav1.AWSMachineList {}
517+ if err := r .Client .List (ctx , machines , listOptions ... ); err != nil {
518+ return 0 , fmt .Errorf ("failed to list machines for cluster %s/%s: %w" , namespace , clusterName , err )
519+ }
520+
521+ clusterScope .Debug ("Found dependent machines" , "count" , len (machines .Items ))
522+
523+ return len (machines .Items ), nil
524+ }
0 commit comments