From 5101df3d760324f902183f01a05bc7f112cbf7ef Mon Sep 17 00:00:00 2001 From: Ben Ash Date: Fri, 4 Oct 2024 16:26:19 +0000 Subject: [PATCH] Update to support the TypedEventHandler interface Related upstream change: - https://github.com/kubernetes-sigs/controller-runtime/pull/2799/ --- controllers/eventhandlers.go | 30 +++++----- controllers/eventhandlers_test.go | 58 +++++++++---------- .../vaultdynamicsecret_controller_test.go | 4 +- 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/controllers/eventhandlers.go b/controllers/eventhandlers.go index 3eb853a85..52b645050 100644 --- a/controllers/eventhandlers.go +++ b/controllers/eventhandlers.go @@ -53,13 +53,13 @@ type enqueueRefRequestsHandler struct { } func (e *enqueueRefRequestsHandler) Create(ctx context.Context, - evt event.CreateEvent, q workqueue.RateLimitingInterface, + evt event.CreateEvent, q workqueue.TypedRateLimitingInterface[reconcile.Request], ) { e.enqueue(ctx, q, evt.Object) } func (e *enqueueRefRequestsHandler) Update(ctx context.Context, - evt event.UpdateEvent, q workqueue.RateLimitingInterface, + evt event.UpdateEvent, q workqueue.TypedRateLimitingInterface[reconcile.Request], ) { if evt.ObjectOld == nil { return @@ -73,19 +73,19 @@ func (e *enqueueRefRequestsHandler) Update(ctx context.Context, } } -func (e *enqueueRefRequestsHandler) Delete(ctx context.Context, - evt event.DeleteEvent, _ workqueue.RateLimitingInterface, +func (e *enqueueRefRequestsHandler) Delete(_ context.Context, + evt event.DeleteEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request], ) { e.refCache.Prune(e.kind, client.ObjectKeyFromObject(evt.Object)) } -func (e *enqueueRefRequestsHandler) Generic(ctx context.Context, - _ event.GenericEvent, _ workqueue.RateLimitingInterface, +func (e *enqueueRefRequestsHandler) Generic(_ context.Context, + _ event.GenericEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request], ) { } func (e *enqueueRefRequestsHandler) enqueue(ctx context.Context, - q workqueue.RateLimitingInterface, o client.Object, + q workqueue.TypedRateLimitingInterface[reconcile.Request], o client.Object, ) { logger := log.FromContext(ctx).WithName( "enqueueRefRequestsHandler"). @@ -138,17 +138,17 @@ type enqueueOnDeletionRequestHandler struct { } func (e *enqueueOnDeletionRequestHandler) Create(_ context.Context, - _ event.CreateEvent, _ workqueue.RateLimitingInterface, + _ event.CreateEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request], ) { } func (e *enqueueOnDeletionRequestHandler) Update(_ context.Context, - _ event.UpdateEvent, _ workqueue.RateLimitingInterface, + _ event.UpdateEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request], ) { } func (e *enqueueOnDeletionRequestHandler) Delete(ctx context.Context, - evt event.DeleteEvent, q workqueue.RateLimitingInterface, + evt event.DeleteEvent, q workqueue.TypedRateLimitingInterface[reconcile.Request], ) { logger := log.FromContext(ctx).WithName("enqueueOnDeletionRequestHandler"). WithValues("ownerGVK", e.gvk) @@ -179,7 +179,7 @@ func (e *enqueueOnDeletionRequestHandler) Delete(ctx context.Context, } func (e *enqueueOnDeletionRequestHandler) Generic(ctx context.Context, - _ event.GenericEvent, _ workqueue.RateLimitingInterface, + _ event.GenericEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request], ) { } @@ -189,16 +189,16 @@ type enqueueDelayingSyncEventHandler struct { enqueueDurationForJitter time.Duration } -func (e *enqueueDelayingSyncEventHandler) Create(_ context.Context, _ event.CreateEvent, _ workqueue.RateLimitingInterface) { +func (e *enqueueDelayingSyncEventHandler) Create(_ context.Context, _ event.CreateEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request]) { } -func (e *enqueueDelayingSyncEventHandler) Update(_ context.Context, _ event.UpdateEvent, _ workqueue.RateLimitingInterface) { +func (e *enqueueDelayingSyncEventHandler) Update(_ context.Context, _ event.UpdateEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request]) { } -func (e *enqueueDelayingSyncEventHandler) Delete(_ context.Context, _ event.DeleteEvent, _ workqueue.RateLimitingInterface) { +func (e *enqueueDelayingSyncEventHandler) Delete(_ context.Context, _ event.DeleteEvent, _ workqueue.TypedRateLimitingInterface[reconcile.Request]) { } -func (e *enqueueDelayingSyncEventHandler) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) { +func (e *enqueueDelayingSyncEventHandler) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.TypedRateLimitingInterface[reconcile.Request]) { logger := log.FromContext(ctx).WithName("enqueueDelayingSyncEventHandler") if evt.Object == nil { logger.Error(nil, diff --git a/controllers/eventhandlers_test.go b/controllers/eventhandlers_test.go index 762837b20..dd0fa538a 100644 --- a/controllers/eventhandlers_test.go +++ b/controllers/eventhandlers_test.go @@ -110,7 +110,7 @@ func Test_enqueueRefRequestsHandler_Create(t *testing.T) { refCache: cache, createEvents: createEvents, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantAddedAfter: wantAddedAfterValid, maxRequeueAfter: time.Second * 10, @@ -122,7 +122,7 @@ func Test_enqueueRefRequestsHandler_Create(t *testing.T) { refCache: cache, createEvents: createEvents, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantAddedAfter: wantAddedAfterValid, wantRefCache: cache, @@ -133,7 +133,7 @@ func Test_enqueueRefRequestsHandler_Create(t *testing.T) { refCache: cache, createEvents: createEvents, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantAddedAfter: wantAddedAfterValid, maxRequeueAfter: time.Second * -1, @@ -146,7 +146,7 @@ func Test_enqueueRefRequestsHandler_Create(t *testing.T) { createEvents: createEvents, validator: &validatorFunc{}, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantValidObjects: []client.Object{ createEvent.Object, @@ -162,7 +162,7 @@ func Test_enqueueRefRequestsHandler_Create(t *testing.T) { createEvents: createEvents, validator: &validatorFunc{}, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantInvalidObjects: []client.Object{ createEvent.Object, @@ -178,7 +178,7 @@ func Test_enqueueRefRequestsHandler_Create(t *testing.T) { }, createEvents: createEvents, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, }, } @@ -250,7 +250,7 @@ func Test_enqueueRefRequestsHandler_Update(t *testing.T) { refCache: cache, updateEvents: updateEventsEnqueue, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantAddedAfter: wantAddedAfterValid, wantRefCache: cache, @@ -262,7 +262,7 @@ func Test_enqueueRefRequestsHandler_Update(t *testing.T) { updateEvents: updateEventsEnqueue, validator: &validatorFunc{}, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantValidObjects: []client.Object{ objectNew, @@ -278,7 +278,7 @@ func Test_enqueueRefRequestsHandler_Update(t *testing.T) { updateEvents: updateEventsEnqueue, validator: &validatorFunc{}, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, wantInvalidObjects: []client.Object{ objectNew, @@ -294,7 +294,7 @@ func Test_enqueueRefRequestsHandler_Update(t *testing.T) { }, updateEvents: updateEventsEnqueue, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, }, { @@ -303,7 +303,7 @@ func Test_enqueueRefRequestsHandler_Update(t *testing.T) { refCache: cache, updateEvents: updateEventsNoEnqueue, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, }, } @@ -353,7 +353,7 @@ func Test_enqueueRefRequestsHandler_Delete(t *testing.T) { kind: SecretTransformation, refCache: cache, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, deleteEvents: []event.DeleteEvent{ { @@ -369,7 +369,7 @@ func Test_enqueueRefRequestsHandler_Delete(t *testing.T) { kind: SecretTransformation, refCache: cache, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, deleteEvents: []event.DeleteEvent{ { @@ -383,7 +383,7 @@ func Test_enqueueRefRequestsHandler_Delete(t *testing.T) { kind: SecretTransformation, refCache: cache, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, deleteEvents: []event.DeleteEvent{ { @@ -469,17 +469,17 @@ func assertEnqueueRefRequestHandler(t *testing.T, ctx context.Context, tt testCa } } -var _ workqueue.RateLimitingInterface = &DelegatingQueue{} +var _ workqueue.TypedRateLimitingInterface[reconcile.Request] = &DelegatingQueue{} type DelegatingQueue struct { - workqueue.Interface + workqueue.TypedRateLimitingInterface[reconcile.Request] mu sync.Mutex AddedAfter []any AddedAfterDuration []time.Duration } // AddAfter implements RateLimitingInterface. -func (q *DelegatingQueue) AddAfter(item interface{}, d time.Duration) { +func (q *DelegatingQueue) AddAfter(item reconcile.Request, d time.Duration) { q.mu.Lock() defer q.mu.Unlock() q.AddedAfter = append(q.AddedAfter, item) @@ -487,11 +487,11 @@ func (q *DelegatingQueue) AddAfter(item interface{}, d time.Duration) { q.Add(item) } -func (q *DelegatingQueue) AddRateLimited(item interface{}) {} +func (q *DelegatingQueue) AddRateLimited(item reconcile.Request) {} -func (q *DelegatingQueue) Forget(item interface{}) {} +func (q *DelegatingQueue) Forget(item reconcile.Request) {} -func (q *DelegatingQueue) NumRequeues(item interface{}) int { +func (q *DelegatingQueue) NumRequeues(item reconcile.Request) int { return 0 } @@ -553,7 +553,7 @@ func Test_enqueueOnDeletionRequestHandler_Delete(t *testing.T) { deleteEventSupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: wantAddedAfterValid, @@ -567,7 +567,7 @@ func Test_enqueueOnDeletionRequestHandler_Delete(t *testing.T) { deleteEventSupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: wantAddedAfterValid, @@ -581,7 +581,7 @@ func Test_enqueueOnDeletionRequestHandler_Delete(t *testing.T) { deleteEventSupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: wantAddedAfterValid, @@ -594,7 +594,7 @@ func Test_enqueueOnDeletionRequestHandler_Delete(t *testing.T) { deleteEventSupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: wantAddedAfterValid, @@ -607,7 +607,7 @@ func Test_enqueueOnDeletionRequestHandler_Delete(t *testing.T) { deleteEventUnsupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, }, @@ -671,7 +671,7 @@ func Test_enqueueOnDeletionRequestHandler_Create(t *testing.T) { createEvent, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: nil, @@ -683,7 +683,7 @@ func Test_enqueueOnDeletionRequestHandler_Create(t *testing.T) { createEventUnsupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: nil, @@ -767,7 +767,7 @@ func Test_enqueueOnDeletionRequestHandler_Update(t *testing.T) { updateEvent, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: nil, @@ -779,7 +779,7 @@ func Test_enqueueOnDeletionRequestHandler_Update(t *testing.T) { updateEventUnsupported, }, q: &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), }, gvk: gvk, wantAddedAfter: nil, diff --git a/controllers/vaultdynamicsecret_controller_test.go b/controllers/vaultdynamicsecret_controller_test.go index 13d088d05..abe315110 100644 --- a/controllers/vaultdynamicsecret_controller_test.go +++ b/controllers/vaultdynamicsecret_controller_test.go @@ -840,7 +840,7 @@ func Test_computeRelativeHorizonWithJitter(t *testing.T) { }, minHorizon: 1 * time.Second, wantMinHorizon: time.Duration(.8 * float64(time.Second)), - wantMaxHorizon: time.Duration(1 * time.Second), + wantMaxHorizon: 1 * time.Second, wantInWindow: true, }, } @@ -1157,7 +1157,7 @@ func TestVaultDynamicSecretReconciler_vaultClientCallback(t *testing.T) { cs := source.Channel(r.SourceCh, handler) q := &DelegatingQueue{ - Interface: workqueue.New(), + TypedRateLimitingInterface: workqueue.NewTypedRateLimitingQueue[reconcile.Request](nil), } go func() {