Skip to content

Commit 9479a6c

Browse files
Refactor: streamline Overcommit and OvercommitClass reconciliation logic and remove unused finalizer code (#29)
* Refactor: streamline Overcommit and OvercommitClass reconciliation logic and remove unused finalizer code * fix: add migration to remove legacy finalizers from existing CRs
1 parent f2a5573 commit 9479a6c

5 files changed

Lines changed: 167 additions & 269 deletions

File tree

internal/controller/overcommit/overcommit_controller.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,47 +65,36 @@ func (r *OvercommitReconciler) Reconcile(ctx context.Context, req ctrl.Request)
6565
return ctrl.Result{}, nil
6666
}
6767

68-
// Check if the CR is being deleted
69-
if !overcommit.ObjectMeta.DeletionTimestamp.IsZero() {
70-
logger.Info("Overcommit CR is being deleted, cleaning up resources")
71-
72-
// Clean up resources
73-
err := r.cleanupResources(ctx, overcommit)
74-
if err != nil {
75-
logger.Error(err, "Failed to clean up resources")
76-
return ctrl.Result{}, err
68+
// Migration: Remove legacy finalizers from previous versions
69+
// Previous versions of this operator added finalizers to CRs for manual cleanup.
70+
// To ensure smooth upgrades, we automatically remove any legacy finalizers
71+
// so that CRs created before this change can be deleted normally.
72+
legacyFinalizers := []string{"overcommit.finalizer", "webhook.finalizer"}
73+
finalizerRemoved := false
74+
for _, legacyFinalizer := range legacyFinalizers {
75+
if controllerutil.ContainsFinalizer(overcommit, legacyFinalizer) {
76+
logger.Info("Removing legacy finalizer during migration", "finalizer", legacyFinalizer)
77+
controllerutil.RemoveFinalizer(overcommit, legacyFinalizer)
78+
finalizerRemoved = true
7779
}
78-
79-
// Remove finalizer if cleanup is successful
80-
controllerutil.RemoveFinalizer(overcommit, "overcommit.finalizer")
81-
err = r.Update(ctx, overcommit)
82-
if err != nil {
83-
logger.Error(err, "Failed to remove finalizer")
84-
return ctrl.Result{}, err
85-
}
86-
87-
return ctrl.Result{}, nil
8880
}
8981

90-
// Add finalizer if not present
91-
if !controllerutil.ContainsFinalizer(overcommit, "overcommit.finalizer") {
92-
logger.Info("Adding finalizer to Overcommit CR")
93-
controllerutil.AddFinalizer(overcommit, "overcommit.finalizer")
82+
// If we removed any legacy finalizers, update the object and requeue
83+
if finalizerRemoved {
9484
err = r.Update(ctx, overcommit)
9585
if err != nil {
96-
logger.Error(err, "Failed to add finalizer")
86+
logger.Error(err, "Failed to remove legacy finalizers")
9787
return ctrl.Result{}, err
9888
}
99-
// Return early to trigger a new reconciliation with the updated object
100-
logger.Info("Finalizer added, requeuing reconciliation")
101-
return ctrl.Result{}, nil
89+
logger.Info("Legacy finalizers removed, requeuing reconciliation")
90+
return ctrl.Result{RequeueAfter: time.Second * 1}, nil
10291
}
10392

10493
// Reconcile Issuer
10594
issuer := resources.GenerateIssuer()
10695
if issuer == nil {
10796
logger.Error(nil, "Generated issuer is nil")
108-
return ctrl.Result{}, fmt.Errorf("Generated issuer is nil")
97+
return ctrl.Result{}, fmt.Errorf("generated issuer is nil")
10998
}
11099

111100
_, err = controllerutil.CreateOrUpdate(ctx, r.Client, issuer, func() error {
@@ -395,6 +384,12 @@ func (r *OvercommitReconciler) Reconcile(ctx context.Context, req ctrl.Request)
395384
return ctrl.Result{}, err
396385
}
397386

387+
// Update the status of all resources
388+
if err := r.updateOvercommitStatusSafely(ctx); err != nil {
389+
logger.Error(err, "Failed to update Overcommit status")
390+
// Don't fail the reconciliation for status update errors
391+
}
392+
398393
// Only requeue periodically for status checks, not immediately
399394
logger.Info("Reconciliation completed successfully", "nextReconcile", "10 seconds", "time", time.Now().Format("15:04:05"))
400395
return ctrl.Result{

0 commit comments

Comments
 (0)