@@ -58,6 +58,10 @@ import (
58
58
"sigs.k8s.io/cluster-api/util/predicates"
59
59
)
60
60
61
+ const (
62
+ deleteRequeueAfter = 20 * time .Second
63
+ )
64
+
61
65
var defaultAWSSecurityGroupRoles = []infrav1.SecurityGroupRole {
62
66
infrav1 .SecurityGroupAPIServerLB ,
63
67
infrav1 .SecurityGroupLB ,
@@ -191,21 +195,34 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
191
195
192
196
// Handle deleted clusters
193
197
if ! awsCluster .DeletionTimestamp .IsZero () {
194
- return ctrl. Result {}, r .reconcileDelete (ctx , clusterScope )
198
+ return r .reconcileDelete (ctx , clusterScope )
195
199
}
196
200
197
201
// Handle non-deleted clusters
198
202
return r .reconcileNormal (clusterScope )
199
203
}
200
204
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 ) {
202
206
if ! controllerutil .ContainsFinalizer (clusterScope .AWSCluster , infrav1 .ClusterFinalizer ) {
203
207
clusterScope .Info ("No finalizer on AWSCluster, skipping deletion reconciliation" )
204
- return nil
208
+ return reconcile. Result {}, nil
205
209
}
206
210
207
211
clusterScope .Info ("Reconciling AWSCluster delete" )
208
212
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
+
209
226
ec2svc := r .getEC2Service (clusterScope )
210
227
elbsvc := r .getELBService (clusterScope )
211
228
networkSvc := r .getNetworkService (* clusterScope )
@@ -257,12 +274,12 @@ func (r *AWSClusterReconciler) reconcileDelete(ctx context.Context, clusterScope
257
274
}
258
275
259
276
if len (allErrs ) > 0 {
260
- return kerrors .NewAggregate (allErrs )
277
+ return reconcile. Result {}, kerrors .NewAggregate (allErrs )
261
278
}
262
279
263
280
// Cluster is deleted so remove the finalizer.
264
281
controllerutil .RemoveFinalizer (clusterScope .AWSCluster , infrav1 .ClusterFinalizer )
265
- return nil
282
+ return reconcile. Result {}, nil
266
283
}
267
284
268
285
func (r * AWSClusterReconciler ) reconcileLoadBalancer (clusterScope * scope.ClusterScope , awsCluster * infrav1.AWSCluster ) (* time.Duration , error ) {
@@ -484,3 +501,24 @@ func (r *AWSClusterReconciler) checkForExternalControlPlaneLoadBalancer(clusterS
484
501
return nil
485
502
}
486
503
}
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