From 3106d92596d7f734f84a8e13c1f3c05f1f5254b1 Mon Sep 17 00:00:00 2001 From: Martin Schuppert Date: Mon, 5 Aug 2024 18:13:25 +0200 Subject: [PATCH] Return nil error in case NotFound and to use RequeueAfter Currently the reconciler returned both a non-zero result and a non-nil error. The result will always be ignored if the error is non-nil and the non-nil error causes reqeueuing with exponential backoff. In case of NotFound return nil that the ReqeueAfter is used. For more details, see: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/reconcile#Reconciler Signed-off-by: Martin Schuppert --- controllers/glance_common.go | 7 +++++-- controllers/glance_controller.go | 3 ++- controllers/glanceapi_controller.go | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/controllers/glance_common.go b/controllers/glance_common.go index 8b97b4e6..9748d1d3 100644 --- a/controllers/glance_common.go +++ b/controllers/glance_common.go @@ -39,6 +39,7 @@ import ( "k8s.io/apimachinery/pkg/labels" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" ) // fields to index to reconcile when change @@ -81,12 +82,13 @@ func ensureSecret( hash, res, err := oko_secret.VerifySecret(ctx, secretName, expectedFields, reader, requeueTimeout) if err != nil { if k8s_errors.IsNotFound(err) { + log.FromContext(ctx).Info(fmt.Sprintf("OpenStack secret %s not found", secretName)) conditionUpdater.Set(condition.FalseCondition( condition.InputReadyCondition, condition.RequestedReason, condition.SeverityInfo, condition.InputReadyWaitingMessage)) - return "", glance.ResultRequeue, fmt.Errorf("OpenStack secret %s not found", secretName) + return "", glance.ResultRequeue, nil } conditionUpdater.Set(condition.FalseCondition( condition.InputReadyCondition, @@ -117,13 +119,14 @@ func ensureNAD( _, err = nad.GetNADWithName(ctx, helper, netAtt, helper.GetBeforeObject().GetNamespace()) if err != nil { if k8s_errors.IsNotFound(err) { + helper.GetLogger().Info(fmt.Sprintf("network-attachment-definition %s not found", netAtt)) conditionUpdater.Set(condition.FalseCondition( condition.NetworkAttachmentsReadyCondition, condition.RequestedReason, condition.SeverityInfo, condition.NetworkAttachmentsReadyWaitingMessage, netAtt)) - return serviceAnnotations, ctrl.Result{RequeueAfter: time.Second * 10}, fmt.Errorf("network-attachment-definition %s not found", netAtt) + return serviceAnnotations, glance.ResultRequeue, nil } conditionUpdater.Set(condition.FalseCondition( condition.NetworkAttachmentsReadyCondition, diff --git a/controllers/glance_controller.go b/controllers/glance_controller.go index e5634c72..9759a935 100644 --- a/controllers/glance_controller.go +++ b/controllers/glance_controller.go @@ -408,7 +408,8 @@ func (r *GlanceReconciler) reconcileInit( _, _, err := oko_secret.GetSecret(ctx, helper, instance.Spec.Secret, instance.Namespace) if err != nil { if k8s_errors.IsNotFound(err) { - return glance.ResultRequeue, fmt.Errorf("OpenStack secret %s not found", instance.Spec.Secret) + r.Log.Info(fmt.Sprintf("OpenStack secret %s not found", instance.Spec.Secret)) + return glance.ResultRequeue, nil } return ctrl.Result{}, err } diff --git a/controllers/glanceapi_controller.go b/controllers/glanceapi_controller.go index dced08f0..7b87cdc0 100644 --- a/controllers/glanceapi_controller.go +++ b/controllers/glanceapi_controller.go @@ -612,12 +612,13 @@ func (r *GlanceAPIReconciler) reconcileNormal( memcached, err := memcachedv1.GetMemcachedByName(ctx, helper, instance.Spec.MemcachedInstance, instance.Namespace) if err != nil { if k8s_errors.IsNotFound(err) { + r.Log.Info(fmt.Sprintf("memcached %s not found", instance.Spec.MemcachedInstance)) instance.Status.Conditions.Set(condition.FalseCondition( condition.MemcachedReadyCondition, condition.RequestedReason, condition.SeverityInfo, condition.MemcachedReadyWaitingMessage)) - return glance.ResultRequeue, fmt.Errorf("memcached %s not found", instance.Spec.MemcachedInstance) + return glance.ResultRequeue, nil } instance.Status.Conditions.Set(condition.FalseCondition( condition.MemcachedReadyCondition, @@ -1161,7 +1162,8 @@ func (r *GlanceAPIReconciler) ensureKeystoneEndpoints( // the registered endpoints with the new URL. err = keystonev1.DeleteKeystoneEndpointWithName(ctx, helper, instance.Name, instance.Namespace) if err != nil { - return glance.ResultRequeue, fmt.Errorf("Endpoint %s not found", instance.Name) + r.Log.Info(fmt.Sprintf("Endpoint %s not found", instance.Name)) + return glance.ResultRequeue, nil } return ctrlResult, nil }