Skip to content

Commit

Permalink
Return nil error in case NotFound and to use RequeueAfter
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
stuggi committed Aug 6, 2024
1 parent 5387db6 commit 3106d92
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions controllers/glance_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion controllers/glance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions controllers/glanceapi_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 3106d92

Please sign in to comment.