core: skip reconcile when adding the finalizers

When finalizers are added to the CRs, a follow-up reconcile
will be triggered due to the increased generation on the CR.
Therefore, abort the initial reconcile when adding the finalizer,
and allow the follow-up reconcile to complete the configuration.

Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
This commit is contained in:
Travis Nielsen committed 2025-04-08 13:42:58 -06:00
1 parent 916c0ab50e
commit f7fb1bc0f2
25 files changed
+118 -44

No files matched your search

+5 -1
View File
@@ -158,10 +158,14 @@ func (r *ReconcileCephClient) reconcile(request reconcile.Request) (reconcile.Re
observedGeneration := cephClient.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephClient)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephClient)
if err != nil {
return reconcile.Result{}, *cephClient, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the cephclient %q after adding finalizer", cephClient.Name)
return reconcile.Result{}, *cephClient, nil
}
// The CR was just created, initializing status fields
if cephClient.Status == nil {
+7 -3
View File
@@ -127,9 +127,13 @@ func TestCephClientController(t *testing.T) {
// A Pool resource with metadata and spec.
cephClient := &cephv1.CephClient{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Finalizers: []string{"cephclient.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephClient",
},
Spec: cephv1.ClientSpec{
Caps: map[string]string{
+5 -1
View File
@@ -274,10 +274,14 @@ func (r *ReconcileCephCluster) reconcile(request reconcile.Request) (reconcile.R
}
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephCluster)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephCluster)
if err != nil {
return reconcile.Result{}, *cephCluster, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the ceph cluster %q after adding finalizer", cephCluster.Name)
return reconcile.Result{}, *cephCluster, nil
}
// DELETE: the CR was deleted
if !cephCluster.GetDeletionTimestamp().IsZero() {
+2 -1
View File
@@ -183,7 +183,8 @@ func watchControllerPredicate[T *cephv1.CephCluster](ctx context.Context, c clie
return false
} else if objOld.GetGeneration() != objNew.GetGeneration() {
logger.Debugf("skipping resource %q update with unchanged spec", objNew.Name)
logger.Debugf("reconciling CephCluster %q with changed generation", objNew.Name)
return true
}
return false
+8 -5
View File
@@ -52,25 +52,28 @@ func remove(list []string, s string) []string {
// AddFinalizerIfNotPresent adds a finalizer an object to avoid instant deletion
// of the object without finalizing it.
func AddFinalizerIfNotPresent(ctx context.Context, client client.Client, obj client.Object) error {
func AddFinalizerIfNotPresent(ctx context.Context, client client.Client, obj client.Object) (bool, error) {
objectFinalizer := buildFinalizerName(obj.GetObjectKind().GroupVersionKind().Kind)
accessor, err := meta.Accessor(obj)
if err != nil {
return errors.Wrap(err, "failed to get meta information of object")
return false, errors.Wrap(err, "failed to get meta information of object")
}
if !contains(accessor.GetFinalizers(), objectFinalizer) {
logger.Infof("adding finalizer %q on %q", objectFinalizer, accessor.GetName())
accessor.SetFinalizers(append(accessor.GetFinalizers(), objectFinalizer))
originalGeneration := obj.GetGeneration()
// Update CR with finalizer
if err := client.Update(ctx, obj); err != nil {
return errors.Wrapf(err, "failed to add finalizer %q on %q", objectFinalizer, accessor.GetName())
return false, errors.Wrapf(err, "failed to add finalizer %q on %q", objectFinalizer, accessor.GetName())
}
newGeneration := obj.GetGeneration()
logger.Debugf("when adding finalizer on %q, original generation %d, new generation %d", accessor.GetName(), originalGeneration, newGeneration)
return originalGeneration != newGeneration, nil
}
return nil
return false, nil
}
// RemoveFinalizer removes a finalizer from an object
@@ -46,9 +46,10 @@ func TestAddFinalizerIfNotPresent(t *testing.T) {
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(object...).Build()
assert.Empty(t, fakeObject.Finalizers)
err := AddFinalizerIfNotPresent(context.TODO(), cl, fakeObject)
generationUpdated, err := AddFinalizerIfNotPresent(context.TODO(), cl, fakeObject)
assert.NoError(t, err)
assert.NotEmpty(t, fakeObject.Finalizers)
assert.False(t, generationUpdated)
}
func TestRemoveFinalizer(t *testing.T) {
+2 -1
View File
@@ -104,7 +104,8 @@ func WatchControllerPredicate[T client.Object](scheme *runtime.Scheme) predicate
logger.Debugf("resource %q: %q is going to be deleted", kind, nsName)
return true
} else if objOld.GetGeneration() != objNew.GetGeneration() {
logger.Debugf("skipping resource %q: %q, update event with unchanged spec", kind, nsName)
logger.Debugf("reconciling %s %q with changed generation", kind, nsName.String())
return true
}
return false
},
+5 -1
View File
@@ -214,10 +214,14 @@ func (r *ReconcileCephFilesystem) reconcile(request reconcile.Request) (reconcil
observedGeneration := cephFilesystem.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephFilesystem)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephFilesystem)
if err != nil {
return reconcile.Result{}, *cephFilesystem, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the filesystem %q after adding finalizer", cephFilesystem.Name)
return reconcile.Result{}, *cephFilesystem, nil
}
// The CR was just created, initialize status as 'Progressing'
if cephFilesystem.Status == nil {
+3 -2
View File
@@ -165,8 +165,9 @@ func TestCephFilesystemController(t *testing.T) {
// A Pool resource with metadata and spec.
fs := &cephv1.CephFilesystem{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Finalizers: []string{"cephfilesystem.ceph.rook.io"},
},
Spec: cephv1.FilesystemSpec{
MetadataServer: cephv1.MetadataServerSpec{
@@ -155,10 +155,14 @@ func (r *ReconcileCephFilesystemSubVolumeGroup) reconcile(request reconcile.Requ
observedGeneration := cephFilesystemSubVolumeGroup.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephFilesystemSubVolumeGroup)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephFilesystemSubVolumeGroup)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the subvolume group %q after adding finalizer", cephFilesystemSubVolumeGroup.Name)
return reconcile.Result{}, nil
}
// The CR was just created, initializing status fields
if cephFilesystemSubVolumeGroup.Status == nil {
@@ -42,7 +42,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func TestCephClientController(t *testing.T) {
func TestFilesystemSubvolumeGroupController(t *testing.T) {
ctx := context.TODO()
// Set DEBUG logging
capnslog.SetGlobalLogLevel(capnslog.DEBUG)
@@ -56,9 +56,13 @@ func TestCephClientController(t *testing.T) {
// A cephFilesystemSubVolumeGroup resource with metadata and spec.
cephFilesystemSubVolumeGroup := &cephv1.CephFilesystemSubVolumeGroup{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Finalizers: []string{"cephfilesystemsubvolumegroup.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephFilesystemSubvolumeGroup",
},
Spec: cephv1.CephFilesystemSubVolumeGroupSpec{
FilesystemName: namespace,
+5 -1
View File
@@ -176,10 +176,14 @@ func (r *ReconcileCephNFS) reconcile(request reconcile.Request) (reconcile.Resul
observedGeneration := cephNFS.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephNFS)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephNFS)
if err != nil {
return reconcile.Result{}, *cephNFS, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the nfs %q after adding finalizer", cephNFS.Name)
return reconcile.Result{}, *cephNFS, nil
}
// The CR was just created, initializing status fields
if cephNFS.Status == nil {
+3 -2
View File
@@ -141,8 +141,9 @@ func TestCephNFSController(t *testing.T) {
baseCephNFS := func() *cephv1.CephNFS {
return &cephv1.CephNFS{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Finalizers: []string{"cephnfs.ceph.rook.io"},
},
Spec: cephv1.NFSGaneshaSpec{
RADOS: cephv1.GaneshaRADOSSpec{
+5 -1
View File
@@ -293,10 +293,14 @@ func (r *ReconcileCephObjectStore) reconcile(request reconcile.Request) (reconci
observedGeneration := cephObjectStore.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectStore)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectStore)
if err != nil {
return reconcile.Result{}, *cephObjectStore, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the object store %q after adding finalizer", cephObjectStore.Name)
return reconcile.Result{}, *cephObjectStore, nil
}
// The CR was just created, initializing status fields
if cephObjectStore.Status == nil {
+6 -4
View File
@@ -343,8 +343,9 @@ func TestCephObjectStoreController(t *testing.T) {
// A Pool resource with metadata and spec.
objectStore := &cephv1.CephObjectStore{
ObjectMeta: metav1.ObjectMeta{
Name: store,
Namespace: namespace,
Name: store,
Namespace: namespace,
Finalizers: []string{"cephobjectstore.ceph.rook.io"},
},
Spec: cephv1.ObjectStoreSpec{MetadataPool: cephv1.PoolSpec{Replicated: cephv1.ReplicatedSpec{Size: 1}}, DataPool: cephv1.PoolSpec{Replicated: cephv1.ReplicatedSpec{Size: 1}}},
TypeMeta: controllerTypeMeta,
@@ -632,8 +633,9 @@ func TestCephObjectStoreControllerMultisite(t *testing.T) {
objectStore := &cephv1.CephObjectStore{
ObjectMeta: metav1.ObjectMeta{
Name: store,
Namespace: namespace,
Name: store,
Namespace: namespace,
Finalizers: []string{"cephobjectstore.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephObjectStore",
+5 -1
View File
@@ -121,10 +121,14 @@ func (r *ReconcileBucketTopic) reconcile(request reconcile.Request) (reconcile.R
observedGeneration := cephBucketTopic.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBucketTopic)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBucketTopic)
if err != nil {
return opcontroller.ImmediateRetryResult, errors.Wrapf(err, "failed to add finalizer to CephBucketTopic %q", request.NamespacedName)
}
if generationUpdated {
logger.Infof("reconciling the object bucket topic %q after adding finalizer", cephBucketTopic.Name)
return reconcile.Result{}, nil
}
// The CR was just created, initializing status fields
if cephBucketTopic.Status == nil {
@@ -71,8 +71,9 @@ func TestCephBucketTopicController(t *testing.T) {
bucketTopic := &cephv1.CephBucketTopic{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Finalizers: []string{"cephbuckettopic.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephBucketTopic",
+5 -1
View File
@@ -245,10 +245,14 @@ func (r *ReconcileObjectStoreUser) reconcile(request reconcile.Request) (reconci
observedGeneration := cephObjectStoreUser.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectStoreUser)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectStoreUser)
if err != nil {
return reconcile.Result{}, *cephObjectStoreUser, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the object user %q after adding finalizer", cephObjectStoreUser.Name)
return reconcile.Result{}, *cephObjectStoreUser, nil
}
clusterNamespace := request.NamespacedName
clusterNamespace.Namespace = clusterStoreNamespace(cephObjectStoreUser)
@@ -113,8 +113,9 @@ func TestCephObjectStoreUserController(t *testing.T) {
cephObjectStore := &cephv1.CephObjectStore{}
objectUser := &cephv1.CephObjectStoreUser{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Finalizers: []string{"cephobjectstoreuser.ceph.rook.io"},
},
Spec: cephv1.ObjectStoreUserSpec{
Store: store,
+6 -1
View File
@@ -158,10 +158,15 @@ func (r *ReconcileObjectZone) reconcile(request reconcile.Request) (reconcile.Re
// CR status will be updated at end of reconcile, so to reflect the reconcile has finished
observedGeneration := cephObjectZone.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectZone)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephObjectZone)
if err != nil {
return reconcile.Result{}, *cephObjectZone, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the object zone %q after adding finalizer", cephObjectZone.Name)
return reconcile.Result{}, *cephObjectZone, nil
}
// The CR was just created, initializing status fields
if cephObjectZone.Status == nil {
r.updateStatus(k8sutil.ObservedGenerationNotAvailable, request.NamespacedName, k8sutil.EmptyStatus)
@@ -163,6 +163,7 @@ func TestCephObjectZoneController(t *testing.T) {
Name: name,
Namespace: namespace,
Generation: 0,
Finalizers: []string{"cephobjectzone.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephObjectZone",
+5 -1
View File
@@ -189,10 +189,14 @@ func (r *ReconcileCephBlockPool) reconcile(request reconcile.Request) (reconcile
observedGeneration := cephBlockPool.ObjectMeta.Generation
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBlockPool)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBlockPool)
if err != nil {
return opcontroller.ImmediateRetryResult, *cephBlockPool, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the ceph block pool %q after adding finalizer", cephBlockPool.Name)
return reconcile.Result{}, *cephBlockPool, nil
}
// The CR was just created, initializing status fields
if cephBlockPool.Status == nil {
+7 -3
View File
@@ -218,9 +218,13 @@ func TestCephBlockPoolController(t *testing.T) {
// A Pool resource with metadata and spec.
pool := &cephv1.CephBlockPool{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Finalizers: []string{"cephblockpool.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephBlockPool",
},
Spec: cephv1.NamedBlockPoolSpec{
PoolSpec: cephv1.PoolSpec{
@@ -158,10 +158,14 @@ func (r *ReconcileCephBlockPoolRadosNamespace) reconcile(request reconcile.Reque
}
// Set a finalizer so we can do cleanup before the object goes away
err = opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBlockPoolRadosNamespace)
generationUpdated, err := opcontroller.AddFinalizerIfNotPresent(r.opManagerContext, r.client, cephBlockPoolRadosNamespace)
if err != nil {
return reconcile.Result{}, errors.Wrap(err, "failed to add finalizer")
}
if generationUpdated {
logger.Infof("reconciling the rados namespace %q after adding finalizer", cephBlockPoolRadosNamespace.Name)
return reconcile.Result{}, nil
}
// The CR was just created, initializing status fields
if cephBlockPoolRadosNamespace.Status == nil {
@@ -56,9 +56,13 @@ func TestCephBlockPoolRadosNamespaceController(t *testing.T) {
// A cephBlockPoolRadosNamespace resource with metadata and spec.
cephBlockPoolRadosNamespace := &cephv1.CephBlockPoolRadosNamespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Name: name,
Namespace: namespace,
UID: types.UID("c47cac40-9bee-4d52-823b-ccd803ba5bfe"),
Finalizers: []string{"cephblockpoolradosnamespace.ceph.rook.io"},
},
TypeMeta: metav1.TypeMeta{
Kind: "CephBlockPoolRadosNamespace",
},
Spec: cephv1.CephBlockPoolRadosNamespaceSpec{
BlockPoolName: namespace,