From 0e9cd8e5ae267390d155736ab47cf07c187b3bc4 Mon Sep 17 00:00:00 2001 From: Oded Viner Date: Mon, 29 Dec 2025 16:48:18 +0200 Subject: [PATCH] core: allow skipping cephcluster reconcile via do-not-reconcile label add support for ceph.rook.io/do-not-reconcile on cephcluster crs skip reconcile early and emit a normal reconcile-skipped event avoid reloadmanager on updates for labeled cephclusters add unit test for skip behavior Signed-off-by: Oded Viner (cherry picked from commit 5be726b38666f8b82e5afe4d219a998976da76eb) --- pkg/operator/ceph/cluster/controller.go | 13 +++++ pkg/operator/ceph/cluster/controller_test.go | 56 ++++++++++++++++++++ pkg/operator/ceph/cluster/predicate.go | 20 ++++++- 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/pkg/operator/ceph/cluster/controller.go b/pkg/operator/ceph/cluster/controller.go index b49f86e0d..61c16b1c0 100644 --- a/pkg/operator/ceph/cluster/controller.go +++ b/pkg/operator/ceph/cluster/controller.go @@ -354,6 +354,19 @@ func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.R return r.reconcileDelete(cephCluster) } + // If the cluster has the skip reconcile label, do not reconcile it. This allows other CephClusters + // to continue being reconciled even if one cluster is "stuck". + if _, ok := cephCluster.GetLabels()[cephv1.SkipReconcileLabelKey]; ok { + log.NamespacedInfo(request.Namespace, logger, "skipping reconcile of CephCluster %q since label %q is set", cephCluster.Name, cephv1.SkipReconcileLabelKey) + if r.clusterController.recorder != nil { + r.clusterController.recorder.Eventf(cephCluster, corev1.EventTypeNormal, "ReconcileSkipped", + "Skipping reconcile because label %q is set on the CephCluster", cephv1.SkipReconcileLabelKey) + // Extra log (tests/validation): makes it easy to confirm the event emission via operator logs. + log.NamespacedInfo(request.Namespace, logger, "recorded event %q for CephCluster %q (label %q is set)", "ReconcileSkipped", cephCluster.Name, cephv1.SkipReconcileLabelKey) + } + return reconcile.Result{}, *cephCluster, nil + } + // Do reconcile here! ownerInfo := k8sutil.NewOwnerInfo(cephCluster, r.scheme) if err := r.clusterController.reconcileCephCluster(cephCluster, ownerInfo); err != nil { diff --git a/pkg/operator/ceph/cluster/controller_test.go b/pkg/operator/ceph/cluster/controller_test.go index b01c10ea6..56f43fa2e 100644 --- a/pkg/operator/ceph/cluster/controller_test.go +++ b/pkg/operator/ceph/cluster/controller_test.go @@ -210,3 +210,59 @@ func TestRemoveFinalizers(t *testing.T) { }) } } + +func TestReconcileSkipsWhenSkipReconcileLabelSet(t *testing.T) { + cephNs := "rook-ceph" + clusterName := "my-cluster" + nsName := types.NamespacedName{ + Name: clusterName, + Namespace: cephNs, + } + + // create a Rook-Ceph scheme to use for our tests + s := runtime.NewScheme() + assert.NoError(t, cephv1.AddToScheme(s)) + assert.NoError(t, corev1.AddToScheme(s)) + + fakeCluster := &cephv1.CephCluster{ + TypeMeta: metav1.TypeMeta{ + Kind: "CephCluster", + APIVersion: "ceph.rook.io/v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: clusterName, + Namespace: cephNs, + Finalizers: []string{ + "cephcluster.ceph.rook.io", + }, + Labels: map[string]string{ + cephv1.SkipReconcileLabelKey: "true", + }, + }, + } + + clusterdCtx := &clusterd.Context{} + controller := NewClusterController(clusterdCtx, "") + fakeRecorder := record.NewFakeRecorder(5) + controller.recorder = fakeRecorder + + // Create a fake client to mock API calls + client := clientfake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(fakeCluster).Build() + + reconcileCephCluster := &ReconcileCephCluster{ + client: client, + scheme: s, + context: clusterdCtx, + clusterController: controller, + opManagerContext: context.TODO(), + } + + resp, _, err := reconcileCephCluster.reconcile(reconcile.Request{NamespacedName: nsName}) + assert.NoError(t, err) + assert.True(t, resp.IsZero()) + + // Verify we emit the ReconcileSkipped event when the skip-reconcile label is set. + event := <-fakeRecorder.Events + assert.Contains(t, event, "ReconcileSkipped") + assert.Contains(t, event, cephv1.SkipReconcileLabelKey) +} diff --git a/pkg/operator/ceph/cluster/predicate.go b/pkg/operator/ceph/cluster/predicate.go index 639a9329a..019c98def 100644 --- a/pkg/operator/ceph/cluster/predicate.go +++ b/pkg/operator/ceph/cluster/predicate.go @@ -210,7 +210,25 @@ func watchControllerPredicate[T *cephv1.CephCluster](ctx context.Context, c clie objNew := (*cephv1.CephCluster)(e.ObjectNew) log.NamespacedDebug(objNew.Namespace, logger, "update event on CephCluster CR") - // If the labels "do_not_reconcile" is set on the object, let's not reconcile that request + // If the user sets the skip-reconcile label, avoid doing anything (including manager reloads) + // for this CephCluster. + // + // However, we still want to trigger a reconcile when the label is added/removed so the + // controller can early-exit and record a "ReconcileSkipped" event (when label is added), + // and also resume reconciliation immediately when the label is removed. + _, oldHasSkipLabel := objOld.GetLabels()[cephv1.SkipReconcileLabelKey] + _, newHasSkipLabel := objNew.GetLabels()[cephv1.SkipReconcileLabelKey] + if oldHasSkipLabel != newHasSkipLabel { + log.NamespacedDebug(objNew.Namespace, logger, "CephCluster %q skip-reconcile label changed (%v -> %v); triggering reconcile", objNew.Name, oldHasSkipLabel, newHasSkipLabel) + return true + } + if newHasSkipLabel { + log.NamespacedInfo(objNew.Namespace, logger, "object %q matched on update but %q label is set, doing nothing", objNew.Name, cephv1.SkipReconcileLabelKey) + return false + } + + // Backwards/alternative label used by some controllers to avoid reconciling an object. + // If this label is set on the object, let's not reconcile that request. if opcontroller.IsDoNotReconcile(objNew.GetLabels()) { log.NamespacedDebug(objNew.Namespace, logger, "object %q matched on update but %q label is set, doing nothing", opcontroller.DoNotReconcileLabelName, objNew.Name) return false