Skip to content

Commit 1728508

Browse files
committed
move smart requeue action into reconcile result
1 parent 93e4b09 commit 1728508

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

docs/libs/smartrequeue.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Use `NewStore` in the constructor of the reconciler. During reconciliation, the
77
- `Never` does not requeue the object
88
- `Backoff` requeues the object with an increasing backoff every time it is called on the same object
99
- `Reset` requeues the object, but resets the duration to its minimal value
10+
11+
There is also an integration into the [status updater](./status.md) for the smart requeuing logic.

docs/libs/status.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,10 @@ The `ReconcileResult` that is passed into the status updater is expected to cont
181181
- If this is nil, `Object` will be used instead.
182182
- If this is non-nil, it must not point to the same instance as `Object` - use the `DeepCopy()` function to create a different instance.
183183
- All changes to `Object`'s status that are not part to `OldObject`'s status will be included in the patch during the status update. This can be used to inject custom changes to the status into the status update (in addition to the `WithCustomUpdateFunc` mentioned above).
184+
- `SmartRequeue` contains the requeuing information for the [smart requeuing logic](./smartrequeue.md).
185+
- This field has no effect unless `WithSmartRequeue` has been called on the status updater builder.
186+
- If `ReconcileError` is not nil, the value has no effect and the smart requeue error logic is used instead.
187+
- Valid values are:
188+
- `Backoff` to requeue the object with an increasing backoff
189+
- `Reset` to requeue the object, but reset the backoff interval to its minimum
190+
- `NoRequeue` to not requeue the object

pkg/controller/status_updater.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,8 @@ const (
138138
// If the 'Result' field in the ReconcileResult has a non-zero RequeueAfter value set, that one is used if it is earlier than the one from smart requeue or if "NoRequeue" has been specified.
139139
// This function only has an effect if the Object in the ReconcileResult is not nil, the smart requeue store is not nil, and the action is one of the known values.
140140
// Also, if a reconciliation error occurred, the requeue interval will be reset, but no requeueAfter duration will be set, because controller-runtime will take care of requeuing the object anyway.
141-
func (b *StatusUpdaterBuilder[Obj]) WithSmartRequeue(store *smartrequeue.Store, action SmartRequeueAction) *StatusUpdaterBuilder[Obj] {
141+
func (b *StatusUpdaterBuilder[Obj]) WithSmartRequeue(store *smartrequeue.Store) *StatusUpdaterBuilder[Obj] {
142142
b.internal.smartRequeueStore = store
143-
b.internal.smartRequeueAction = action
144143
return b
145144
}
146145

@@ -183,7 +182,6 @@ type statusUpdater[Obj client.Object] struct {
183182
eventRecorder record.EventRecorder
184183
eventVerbosity conditions.EventVerbosity
185184
smartRequeueStore *smartrequeue.Store
186-
smartRequeueAction SmartRequeueAction
187185
}
188186

189187
func newStatusUpdater[Obj client.Object]() *statusUpdater[Obj] {
@@ -297,7 +295,7 @@ func (s *statusUpdater[Obj]) UpdateStatus(ctx context.Context, c client.Client,
297295
if rr.ReconcileError != nil {
298296
srRes, _ = s.smartRequeueStore.For(rr.Object).Error(rr.ReconcileError)
299297
} else {
300-
switch s.smartRequeueAction {
298+
switch rr.SmartRequeue {
301299
case SR_BACKOFF:
302300
srRes, _ = s.smartRequeueStore.For(rr.Object).Backoff()
303301
case SR_RESET:
@@ -424,6 +422,9 @@ type ReconcileResult[Obj client.Object] struct {
424422
// ConditionsToRemove is an optional slice of condition types for which the corresponding conditions should be removed from the status.
425423
// This is useful if you want to remove conditions that are no longer relevant.
426424
ConditionsToRemove []string
425+
// SmartRequeue determines if/when the object should be requeued.
426+
// Has no effect unless WithSmartRequeue() has been called on the status updater.
427+
SmartRequeue SmartRequeueAction
427428
}
428429

429430
// GenerateCreateConditionFunc returns a function that can be used to add a condition to the given ReconcileResult.

pkg/controller/status_updater_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,14 @@ var _ = Describe("Status Updater", func() {
196196
obj := &CustomObject{}
197197
Expect(env.Client().Get(env.Ctx, controller.ObjectKey("status", "default"), obj)).To(Succeed())
198198
rr := controller.ReconcileResult[*CustomObject]{
199-
Object: obj,
200-
Conditions: dummyConditions(),
199+
Object: obj,
200+
Conditions: dummyConditions(),
201+
SmartRequeue: controller.SR_RESET,
201202
}
202203
store := smartrequeue.NewStore(1*time.Second, 10*time.Second, 2.0)
203204
su := preconfiguredStatusUpdaterBuilder().WithPhaseUpdateFunc(func(obj *CustomObject, rr controller.ReconcileResult[*CustomObject]) (string, error) {
204205
return PhaseSucceeded, nil
205-
}).WithSmartRequeue(store, controller.SR_RESET).Build()
206+
}).WithSmartRequeue(store).Build()
206207
res, err := su.UpdateStatus(env.Ctx, env.Client(), rr)
207208
Expect(err).ToNot(HaveOccurred())
208209
Expect(res.RequeueAfter).To(Equal(1 * time.Second))
@@ -218,11 +219,12 @@ var _ = Describe("Status Updater", func() {
218219
Result: ctrl.Result{
219220
RequeueAfter: 30 * time.Second,
220221
},
222+
SmartRequeue: controller.SR_RESET,
221223
}
222224
store := smartrequeue.NewStore(1*time.Second, 10*time.Second, 2.0)
223225
su := preconfiguredStatusUpdaterBuilder().WithPhaseUpdateFunc(func(obj *CustomObject, rr controller.ReconcileResult[*CustomObject]) (string, error) {
224226
return PhaseSucceeded, nil
225-
}).WithSmartRequeue(store, controller.SR_RESET).Build()
227+
}).WithSmartRequeue(store).Build()
226228
res, err := su.UpdateStatus(env.Ctx, env.Client(), rr)
227229
Expect(err).ToNot(HaveOccurred())
228230
Expect(res.RequeueAfter).To(Equal(1 * time.Second))
@@ -231,7 +233,7 @@ var _ = Describe("Status Updater", func() {
231233
store = smartrequeue.NewStore(1*time.Minute, 10*time.Minute, 2.0)
232234
su = preconfiguredStatusUpdaterBuilder().WithPhaseUpdateFunc(func(obj *CustomObject, rr controller.ReconcileResult[*CustomObject]) (string, error) {
233235
return PhaseSucceeded, nil
234-
}).WithSmartRequeue(store, controller.SR_RESET).Build()
236+
}).WithSmartRequeue(store).Build()
235237
res, err = su.UpdateStatus(env.Ctx, env.Client(), rr)
236238
Expect(err).ToNot(HaveOccurred())
237239
Expect(res.RequeueAfter).To(Equal(30 * time.Second))

0 commit comments

Comments
 (0)