From f879c2a6a6d855327d6ff551152d1218edbcf6f5 Mon Sep 17 00:00:00 2001 From: Leela Venkaiah G Date: Fri, 4 Oct 2024 05:29:46 +0000 Subject: [PATCH] do not always error out while checking for existence of resources controllers also get reconciles for deletion events and it may happen that resource at api server be deleted first before it being purged from cache and during that time if we error out the reconciliation will never be successful in standard conditions. Signed-off-by: Leela Venkaiah G --- internal/controller/clientprofile_controller.go | 4 ++++ internal/controller/driver_controller.go | 4 ++++ internal/utils/meta.go | 13 +++++++++++++ 3 files changed, 21 insertions(+) diff --git a/internal/controller/clientprofile_controller.go b/internal/controller/clientprofile_controller.go index e47c3950..c5a8c747 100644 --- a/internal/controller/clientprofile_controller.go +++ b/internal/controller/clientprofile_controller.go @@ -130,6 +130,10 @@ func (r *ClientProfileReconciler) Reconcile(ctx context.Context, req ctrl.Reques func (r *ClientProfileReconcile) reconcile() error { if err := r.loadAndValidate(); err != nil { + if utils.IsNotFoundWithName(err, r.clientProfile.Name) { + r.log.Info("Client profile resource does not exists anymore, skipping reconcile") + return nil + } return err } diff --git a/internal/controller/driver_controller.go b/internal/controller/driver_controller.go index 75a82b86..2f8c7770 100644 --- a/internal/controller/driver_controller.go +++ b/internal/controller/driver_controller.go @@ -226,6 +226,10 @@ func (r *DriverReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr func (r *driverReconcile) reconcile() error { // Load the driver desired state based on driver resource, operator config resource and default values. if err := r.LoadAndValidateDesiredState(); err != nil { + if utils.IsNotFoundWithName(err, r.driver.Name) { + r.log.Info("Driver resource does not exist anymore, skipping reconcile") + return nil + } return err } diff --git a/internal/utils/meta.go b/internal/utils/meta.go index 91ad4330..16d6d2f0 100644 --- a/internal/utils/meta.go +++ b/internal/utils/meta.go @@ -17,6 +17,9 @@ limitations under the License. package utils import ( + "errors" + + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -62,3 +65,13 @@ func ToggleOwnerReference(on bool, obj, owner metav1.Object, scheme *runtime.Sch } return false, nil } + +func IsNotFoundWithName(err error, name string) bool { + if !k8serrors.IsNotFound(err) { + return false + } + status, ok := err.(k8serrors.APIStatus) + return (ok || errors.As(err, &status)) && + status.Status().Details != nil && + status.Status().Details.Name == name +}