Skip to content

Commit bd0146a

Browse files
authored
Merge pull request #2062 from anmazzotti/fix_import_reconcile_deletion
fix: improve import controller deletion reconcile
2 parents 95691a6 + d8aad7d commit bd0146a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

internal/controllers/import_controller.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ func (r *CAPIImportReconciler) Reconcile(ctx context.Context, req ctrl.Request)
138138
capiCluster := &clusterv1.Cluster{}
139139
if err := r.Client.Get(ctx, req.NamespacedName, capiCluster); err != nil {
140140
if apierrors.IsNotFound(err) {
141+
// These may be requests enqueued from ManagementV3 Cluster deletion,
142+
// for a no longer existing CAPI Cluster.
143+
// Safe to ignore.
141144
return ctrl.Result{RequeueAfter: defaultRequeueDuration}, nil
142145
}
143146

@@ -146,8 +149,9 @@ func (r *CAPIImportReconciler) Reconcile(ctx context.Context, req ctrl.Request)
146149

147150
log = log.WithValues("cluster", capiCluster.Name)
148151

149-
if capiCluster.DeletionTimestamp.IsZero() && !turtlesannotations.HasClusterImportAnnotation(capiCluster) &&
150-
controllerutil.AddFinalizer(capiCluster, managementv3.CapiClusterFinalizer) {
152+
if capiCluster.DeletionTimestamp.IsZero() &&
153+
!turtlesannotations.HasClusterImportAnnotation(capiCluster) &&
154+
!controllerutil.ContainsFinalizer(capiCluster, managementv3.CapiClusterFinalizer) {
151155
log.Info("CAPI cluster is marked for import, adding finalizer")
152156

153157
if err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
@@ -223,7 +227,11 @@ func (r *CAPIImportReconciler) reconcile(ctx context.Context, capiCluster *clust
223227
rancherCluster = &rancherClusterList.Items[0]
224228
}
225229

230+
// Reconcile ManagementV3 Cluster deletion.
226231
if rancherCluster != nil && !rancherCluster.DeletionTimestamp.IsZero() {
232+
// Patch CAPI Cluster with:
233+
// 1. `imported=true` annotation to prevent further-reimports.
234+
// 2. Removed capicluster.turtles.cattle.io finalizer to allow deletion.
227235
if err := r.reconcileDelete(ctx, capiCluster); err != nil {
228236
log.Error(err, "Removing CAPI Cluster failed, retrying")
229237
return ctrl.Result{}, err
@@ -234,12 +242,23 @@ func (r *CAPIImportReconciler) reconcile(ctx context.Context, capiCluster *clust
234242
return ctrl.Result{}, fmt.Errorf("error removing rancher cluster finalizer: %w", err)
235243
}
236244
}
245+
246+
return ctrl.Result{}, nil
237247
}
238248

249+
// Reconcile CAPI Cluster deletion.
239250
if !capiCluster.DeletionTimestamp.IsZero() {
240251
if err := r.deleteDependentRancherCluster(ctx, capiCluster); err != nil {
241252
return ctrl.Result{}, fmt.Errorf("error deleting associated managementv3.Cluster resources: %w", err)
242253
}
254+
255+
if controllerutil.RemoveFinalizer(capiCluster, managementv3.CapiClusterFinalizer) {
256+
if err := r.Client.Update(ctx, capiCluster); err != nil {
257+
return ctrl.Result{}, fmt.Errorf("error removing finalizer from CAPI Cluster: %w", err)
258+
}
259+
}
260+
261+
return ctrl.Result{}, nil
243262
}
244263

245264
patchBase := client.MergeFromWithOptions(rancherCluster.DeepCopy(), client.MergeFromWithOptimisticLock{})

0 commit comments

Comments
 (0)