Skip to content

Commit

Permalink
Perform finalizer removal independently from main controller loop
Browse files Browse the repository at this point in the history
Signed-off-by: Danil-Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Jul 24, 2024
1 parent 2cbdc02 commit ae3f420
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
15 changes: 8 additions & 7 deletions internal/controllers/import_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/controllers/import_controller_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ae3f420

Please sign in to comment.