Skip to content

Commit 241384f

Browse files
authored
Merge pull request #5365 from alexander-demicev/waitforclusterdependencies
✨ Wait for AWSCluster dependent object to be deleted
2 parents 777e8de + 4113f2a commit 241384f

3 files changed

+50
-12
lines changed

controllers/awscluster_controller.go

+43-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ import (
5858
"sigs.k8s.io/cluster-api/util/predicates"
5959
)
6060

61+
const (
62+
deleteRequeueAfter = 20 * time.Second
63+
)
64+
6165
var 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

268285
func (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+
}

controllers/awscluster_controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
568568
_, err = reconciler.reconcileNormal(cs)
569569
g.Expect(err.Error()).To(ContainSubstring("The maximum number of VPCs has been reached"))
570570

571-
err = reconciler.reconcileDelete(ctx, cs)
571+
_, err = reconciler.reconcileDelete(ctx, cs)
572572
g.Expect(err).To(BeNil())
573573
})
574574
t.Run("Should successfully delete AWSCluster with managed VPC", func(t *testing.T) {
@@ -646,7 +646,7 @@ func TestAWSClusterReconcilerIntegrationTests(t *testing.T) {
646646
return sgSvc
647647
}
648648

649-
err = reconciler.reconcileDelete(ctx, cs)
649+
_, err = reconciler.reconcileDelete(ctx, cs)
650650
g.Expect(err).To(BeNil())
651651
expectAWSClusterConditions(g, cs.AWSCluster, []conditionAssertion{
652652
{infrav1.LoadBalancerReadyCondition, corev1.ConditionFalse, clusterv1.ConditionSeverityInfo, clusterv1.DeletedReason},

controllers/awscluster_controller_unit_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
420420
},
421421
)
422422
g.Expect(err).To(BeNil())
423-
err = reconciler.reconcileDelete(ctx, cs)
423+
_, err = reconciler.reconcileDelete(ctx, cs)
424424
g.Expect(err).To(BeNil())
425425
g.Expect(awsCluster.GetFinalizers()).ToNot(ContainElement(infrav1.ClusterFinalizer))
426426
})
@@ -449,7 +449,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
449449
},
450450
)
451451
g.Expect(err).To(BeNil())
452-
err = reconciler.reconcileDelete(ctx, cs)
452+
_, err = reconciler.reconcileDelete(ctx, cs)
453453
g.Expect(err).ToNot(BeNil())
454454
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
455455
})
@@ -474,7 +474,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
474474
},
475475
)
476476
g.Expect(err).To(BeNil())
477-
err = reconciler.reconcileDelete(ctx, cs)
477+
_, err = reconciler.reconcileDelete(ctx, cs)
478478
g.Expect(err).ToNot(BeNil())
479479
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
480480
})
@@ -499,7 +499,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
499499
},
500500
)
501501
g.Expect(err).To(BeNil())
502-
err = reconciler.reconcileDelete(ctx, cs)
502+
_, err = reconciler.reconcileDelete(ctx, cs)
503503
g.Expect(err).ToNot(BeNil())
504504
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
505505
})
@@ -524,7 +524,7 @@ func TestAWSClusterReconcileOperations(t *testing.T) {
524524
},
525525
)
526526
g.Expect(err).To(BeNil())
527-
err = reconciler.reconcileDelete(ctx, cs)
527+
_, err = reconciler.reconcileDelete(ctx, cs)
528528
g.Expect(err).ToNot(BeNil())
529529
g.Expect(awsCluster.GetFinalizers()).To(ContainElement(infrav1.ClusterFinalizer))
530530
})

0 commit comments

Comments
 (0)