Skip to content

Commit

Permalink
Avoid superfluous error log in DELETE case (#3751)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthchr authored Jan 31, 2024
1 parent 27e6a82 commit d517358
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion v2/internal/reconcilers/generic/generic_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/pkg/errors"
"golang.org/x/time/rate"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
Expand Down Expand Up @@ -130,14 +131,28 @@ func (gr *GenericReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
// but since we don't trigger updates on all changes (some annotations are ignored) we also MIGHT NOT get a fresh event
// and get stuck. The solution is to let the GET at the top of the controller check for the not-found case and requeue
// on everything else.
log.Error(err, "Failed to commit object to etcd")
// We avoid logging the "Failed to commit object to etcd" log if we're removing the finalizer and the error is a conflict
// as that means there's a VERY high probability that the error is expected (the Status write fails after the Spec
// write removes the finalizer). We still return the error to controller-runtime though to allow it to re-queue to us.
// The GET at the top of the controller will realize the object is deleted and discard the message.
if !wasFinalizerRemoved(originalObj, metaObj) || !apierrors.IsConflict(err) {
log.Error(err, "Failed to commit object to etcd")
}
return ctrl.Result{}, kubeclient.IgnoreNotFound(err)
}

log.V(Verbose).Info("Done with reconcile", "result", result)
return result, nil
}

// wasFinalizerRemoved returns true if the finalizer was removed from original.
func wasFinalizerRemoved(original genruntime.MetaObject, updated genruntime.MetaObject) bool {
originalHasFinalizer := controllerutil.ContainsFinalizer(original, genruntime.ReconcilerFinalizer)
updatedHasFinalizer := controllerutil.ContainsFinalizer(updated, genruntime.ReconcilerFinalizer)

return originalHasFinalizer && !updatedHasFinalizer
}

func (gr *GenericReconciler) getObjectToReconcile(ctx context.Context, req ctrl.Request) (genruntime.MetaObject, error) {
obj, err := gr.KubeClient.GetObjectOrDefault(ctx, req.NamespacedName, gr.GVK)
if err != nil {
Expand Down

0 comments on commit d517358

Please sign in to comment.