@@ -5,10 +5,19 @@ import (
55 "sync"
66 "time"
77
8- ctrl "sigs.k8s.io/controller-runtime"
98 "sigs.k8s.io/controller-runtime/pkg/client"
109)
1110
11+ // Store is used to manage requeue entries for different objects.
12+ // It holds a map of entries indexed by a key that uniquely identifies the object.
13+ type Store struct {
14+ minInterval time.Duration
15+ maxInterval time.Duration
16+ multiplier float32
17+ objects map [key ]* Entry
18+ objectsLock sync.Mutex
19+ }
20+
1221// NewStore creates a new Store with the specified minimum and maximum intervals
1322// and a multiplier for the exponential backoff logic.
1423func NewStore (minInterval , maxInterval time.Duration , multiplier float32 ) * Store {
@@ -20,16 +29,6 @@ func NewStore(minInterval, maxInterval time.Duration, multiplier float32) *Store
2029 }
2130}
2231
23- // Store is used to manage requeue entries for different objects.
24- // It holds a map of entries indexed by a key that uniquely identifies the object.
25- type Store struct {
26- minInterval time.Duration
27- maxInterval time.Duration
28- multiplier float32
29- objects map [key ]* Entry
30- objectsLock sync.Mutex
31- }
32-
3332func (s * Store ) For (obj client.Object ) * Entry {
3433 s .objectsLock .Lock ()
3534 defer s .objectsLock .Unlock ()
@@ -70,59 +69,3 @@ type key struct {
7069 Name string
7170 Namespace string
7271}
73-
74- func newEntry (s * Store ) * Entry {
75- return & Entry {
76- store : s ,
77- nextDuration : s .minInterval ,
78- }
79- }
80-
81- // Entry is used to manage the requeue logic for a specific object.
82- // It holds the next duration to requeue and the store it belongs to.
83- type Entry struct {
84- store * Store
85- nextDuration time.Duration
86- }
87-
88- // Error resets the duration to the minInterval and returns an empty Result and the error
89- // so that the controller-runtime can handle the exponential backoff for errors.
90- func (e * Entry ) Error (err error ) (ctrl.Result , error ) {
91- e .nextDuration = e .store .minInterval
92- e .setNext ()
93- return ctrl.Result {}, err
94- }
95-
96- // Stable returns a Result and increments the interval for the next iteration.
97- // Used when the external resource is stable (healthy or unhealthy).
98- func (e * Entry ) Stable () (ctrl.Result , error ) {
99- defer e .setNext ()
100- return ctrl.Result {RequeueAfter : e .nextDuration }, nil
101- }
102-
103- // Progressing resets the duration to the minInterval and returns a Result with that interval.
104- // Used when the external resource is still doing something (creating, deleting, updating, etc.)
105- func (e * Entry ) Progressing () (ctrl.Result , error ) {
106- e .nextDuration = e .store .minInterval
107- defer e .setNext ()
108- return ctrl.Result {RequeueAfter : e .nextDuration }, nil
109- }
110-
111- // Never deletes the entry from the store and returns an empty Result.
112- func (e * Entry ) Never () (ctrl.Result , error ) {
113- e .store .deleteEntry (e )
114- return ctrl.Result {}, nil
115- }
116-
117- // setNext updates the next requeue duration using exponential backoff.
118- // It multiplies the current duration by the store's multiplier and ensures
119- // the result doesn't exceed the configured maximum interval.
120- func (e * Entry ) setNext () {
121- newDuration := time .Duration (float32 (e .nextDuration ) * e .store .multiplier )
122-
123- if newDuration > e .store .maxInterval {
124- newDuration = e .store .maxInterval
125- }
126-
127- e .nextDuration = newDuration
128- }
0 commit comments