Merge pull request #16894 from rook/mergify/bp/release-1.19/pr-16874
core: allow skipping cephcluster reconcile via do-not-reconcile label (backport #16874)
This commit is contained in:
3 files changed
+88
-1
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user