From ae3f4207f87c74793e4131c90ae30fdb51f33bdf Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Wed, 24 Jul 2024 11:52:11 +0200 Subject: [PATCH] Perform finalizer removal independently from main controller loop Signed-off-by: Danil-Grigorev --- internal/controllers/import_controller.go | 15 ++++++++------- internal/controllers/import_controller_v3.go | 5 +++++ main.go | 16 ++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/controllers/import_controller.go b/internal/controllers/import_controller.go index 60da431a..eff3203d 100644 --- a/internal/controllers/import_controller.go +++ b/internal/controllers/import_controller.go @@ -361,14 +361,14 @@ func (r *CAPIImportReconciler) reconcileDelete(ctx context.Context, capiCluster return ctrl.Result{}, nil } -// CAPIDowngradeReconciler is a reconciler for downgraded managementv3 clusters. -type CAPIDowngradeReconciler struct { +// CAPICleanupReconciler is a reconciler for cleanup of managementv3 clusters. +type CAPICleanupReconciler struct { RancherClient client.Client Scheme *runtime.Scheme } // SetupWithManager sets up reconciler with manager. -func (r *CAPIDowngradeReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager, options controller.Options) error { +func (r *CAPICleanupReconciler) SetupWithManager(_ context.Context, mgr ctrl.Manager, options controller.Options) error { if err := ctrl.NewControllerManagedBy(mgr). For(&managementv3.Cluster{}). WithOptions(options). @@ -383,18 +383,19 @@ func (r *CAPIDowngradeReconciler) SetupWithManager(_ context.Context, mgr ctrl.M return nil } -// Reconcile performs check for downgraded clusters and removes finalizer on the clusters still owned by the previous management v3 controller. -func (r *CAPIDowngradeReconciler) Reconcile(ctx context.Context, cluster *managementv3.Cluster) (res ctrl.Result, err error) { +// Reconcile performs check for clusters and removes finalizer on the clusters in deleteion +// still containing the turtles finalizer. +func (r *CAPICleanupReconciler) Reconcile(ctx context.Context, cluster *managementv3.Cluster) (res ctrl.Result, err error) { log := log.FromContext(ctx) patchBase := client.MergeFromWithOptions(cluster.DeepCopy(), client.MergeFromWithOptimisticLock{}) - if !controllerutil.RemoveFinalizer(cluster, managementv3.CapiClusterFinalizer) { + if cluster.DeletionTimestamp.IsZero() || !controllerutil.RemoveFinalizer(cluster, managementv3.CapiClusterFinalizer) { return } if err = r.RancherClient.Patch(ctx, cluster, patchBase); err != nil { - log.Error(err, "Unable to remove turtles finalizer from cluster"+cluster.Name) + log.Error(err, "Unable to remove turtles finalizer from cluster"+cluster.GetName()) } return diff --git a/internal/controllers/import_controller_v3.go b/internal/controllers/import_controller_v3.go index dc9780c6..5726d37e 100644 --- a/internal/controllers/import_controller_v3.go +++ b/internal/controllers/import_controller_v3.go @@ -168,6 +168,11 @@ func (r *CAPIImportManagementV3Reconciler) Reconcile(ctx context.Context, req ct return ctrl.Result{RequeueAfter: defaultRequeueDuration}, nil } + if turtlesannotations.HasClusterImportAnnotation(capiCluster) { + log.Info("cluster was imported already and has imported=true annotation set, skipping re-import") + return ctrl.Result{}, nil + } + // Collect errors as an aggregate to return together after all patches have been performed. var errs []error diff --git a/main.go b/main.go index 7b0df8dd..b92585b3 100644 --- a/main.go +++ b/main.go @@ -226,15 +226,15 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) { setupLog.Error(err, "unable to create capi controller") os.Exit(1) } + } - if err := (&controllers.CAPIDowngradeReconciler{ - RancherClient: rancherClient, - }).SetupWithManager(ctx, mgr, controller.Options{ - MaxConcurrentReconciles: concurrencyNumber, - }); err != nil { - setupLog.Error(err, "unable to create rancher management v3 downgrade controller") - os.Exit(1) - } + if err := (&controllers.CAPICleanupReconciler{ + RancherClient: rancherClient, + }).SetupWithManager(ctx, mgr, controller.Options{ + MaxConcurrentReconciles: concurrencyNumber, + }); err != nil { + setupLog.Error(err, "unable to create rancher management v3 cleanup controller") + os.Exit(1) } if feature.Gates.Enabled(feature.RancherKubeSecretPatch) {