github.com/verrazzano/verrazzano@v1.7.0/pkg/controller/requeue.go (about)

     1  // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  package controller
     4  
     5  import (
     6  	"time"
     7  
     8  	"k8s.io/apimachinery/pkg/util/rand"
     9  	ctrl "sigs.k8s.io/controller-runtime"
    10  )
    11  
    12  // Create a new Result that will cause a reconcile requeue after a delay within the specified range
    13  func NewRequeueWithDelay(min int, max int, units time.Duration) ctrl.Result {
    14  	var seconds = rand.IntnRange(min, max)
    15  	delaySecs := time.Duration(seconds) * units
    16  	return ctrl.Result{Requeue: true, RequeueAfter: delaySecs}
    17  }
    18  
    19  // ShortRequeue returns a new Result that will cause a reconcile requeue after a short delay
    20  func ShortRequeue() ctrl.Result {
    21  	return NewRequeueWithDelay(2, 3, time.Second)
    22  }
    23  
    24  // LongRequeue returns a new Result that will cause a reconcile requeue after a long delay (2 -3 minutes)
    25  func LongRequeue() ctrl.Result {
    26  	return NewRequeueWithDelay(2, 3, time.Minute)
    27  }
    28  
    29  // Return true if requeue is needed
    30  func ShouldRequeue(r ctrl.Result) bool {
    31  	return r.Requeue || r.RequeueAfter > 0
    32  }