Skip to content

Commit 88c8bc6

Browse files
committed
Adapt handler.EventHandler and handler.Funcs and add context.Context as the first parameter for every method.
kubernetes-sigs/controller-runtime#2139
1 parent 8e5fb75 commit 88c8bc6

File tree

14 files changed

+70
-54
lines changed

14 files changed

+70
-54
lines changed

pkg/controllerutils/eventhandler.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package controllerutils
1616

1717
import (
18+
"context"
1819
"time"
1920

2021
"k8s.io/apimachinery/pkg/types"
@@ -39,19 +40,19 @@ func reconcileRequest(obj client.Object) reconcile.Request {
3940
// All other events are normally enqueued.
4041
func EnqueueCreateEventsOncePer24hDuration(clock clock.Clock) handler.Funcs {
4142
return handler.Funcs{
42-
CreateFunc: func(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
43+
CreateFunc: func(_ context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
4344
if evt.Object == nil {
4445
return
4546
}
4647
q.AddAfter(reconcileRequest(evt.Object), getDuration(evt.Object, clock))
4748
},
48-
UpdateFunc: func(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
49+
UpdateFunc: func(_ context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
4950
if evt.ObjectNew == nil {
5051
return
5152
}
5253
q.Add(reconcileRequest(evt.ObjectNew))
5354
},
54-
DeleteFunc: func(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
55+
DeleteFunc: func(_ context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
5556
if evt.Object == nil {
5657
return
5758
}

pkg/controllerutils/eventhandler_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package controllerutils_test
1616

1717
import (
18+
"context"
1819
"time"
1920

2021
. "github.com/onsi/ginkgo/v2"
@@ -35,6 +36,7 @@ import (
3536
var _ = Describe("EventHandler", func() {
3637
Describe("#EnqueueCreateEventsOncePer24hDuration", func() {
3738
var (
39+
ctx = context.TODO()
3840
handlerFuncs = handler.Funcs{}
3941
queue workqueue.RateLimitingInterface
4042
fakeClock *testclock.FakeClock
@@ -69,7 +71,7 @@ var _ = Describe("EventHandler", func() {
6971
Object: backupBucket,
7072
}
7173
fakeClock.Step(24 * time.Hour)
72-
handlerFuncs.Create(evt, queue)
74+
handlerFuncs.Create(ctx, evt, queue)
7375
verifyQueue(queue)
7476
})
7577

@@ -78,7 +80,7 @@ var _ = Describe("EventHandler", func() {
7880
evt := event.CreateEvent{
7981
Object: backupBucket,
8082
}
81-
handlerFuncs.Create(evt, queue)
83+
handlerFuncs.Create(ctx, evt, queue)
8284
Expect(queue.Len()).To(Equal(0))
8385
fakeClock.Step(1 * time.Second)
8486
Eventually(func() int {
@@ -92,15 +94,15 @@ var _ = Describe("EventHandler", func() {
9294
ObjectNew: backupBucket,
9395
ObjectOld: backupBucket,
9496
}
95-
handlerFuncs.Update(evt, queue)
97+
handlerFuncs.Update(ctx, evt, queue)
9698
verifyQueue(queue)
9799
})
98100

99101
It("should enqueue a Request with the Name / Namespace of the object in the DeleteEvent.", func() {
100102
evt := event.DeleteEvent{
101103
Object: backupBucket,
102104
}
103-
handlerFuncs.Delete(evt, queue)
105+
handlerFuncs.Delete(ctx, evt, queue)
104106
verifyQueue(queue)
105107
})
106108
})

pkg/controllerutils/mapper/enqueue_mapped.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ func (e *enqueueRequestsFromMapFunc) InjectStopChannel(stopCh <-chan struct{}) e
9696
return nil
9797
}
9898

99-
func (e *enqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
99+
func (e *enqueueRequestsFromMapFunc) Create(_ context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
100100
e.mapAndEnqueue(q, evt.Object)
101101
}
102102

103-
func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
103+
func (e *enqueueRequestsFromMapFunc) Update(_ context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
104104
switch e.updateBehavior {
105105
case UpdateWithOldAndNew:
106106
e.mapAndEnqueue(q, evt.ObjectOld)
@@ -112,11 +112,11 @@ func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.R
112112
}
113113
}
114114

115-
func (e *enqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
115+
func (e *enqueueRequestsFromMapFunc) Delete(_ context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
116116
e.mapAndEnqueue(q, evt.Object)
117117
}
118118

119-
func (e *enqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
119+
func (e *enqueueRequestsFromMapFunc) Generic(_ context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
120120
e.mapAndEnqueue(q, evt.Object)
121121
}
122122

pkg/controllerutils/mapper/enqueue_mapped_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
var _ = Describe("EnqueueMapped", func() {
3636
Describe("#EnqueueRequestsFrom", func() {
3737
var (
38+
ctx = context.TODO()
3839
mapper Mapper
3940
logger logr.Logger
4041
handler handler.EventHandler
@@ -62,21 +63,21 @@ var _ = Describe("EnqueueMapped", func() {
6263

6364
Describe("#Create", func() {
6465
It("should work map and enqueue", func() {
65-
handler.Create(event.CreateEvent{Object: secret1}, queue)
66+
handler.Create(ctx, event.CreateEvent{Object: secret1}, queue)
6667
expectItems(queue, secret1)
6768
})
6869
})
6970

7071
Describe("#Delete", func() {
7172
It("should work map and enqueue", func() {
72-
handler.Delete(event.DeleteEvent{Object: secret1}, queue)
73+
handler.Delete(ctx, event.DeleteEvent{Object: secret1}, queue)
7374
expectItems(queue, secret1)
7475
})
7576
})
7677

7778
Describe("#Generic", func() {
7879
It("should work map and enqueue", func() {
79-
handler.Generic(event.GenericEvent{Object: secret1}, queue)
80+
handler.Generic(ctx, event.GenericEvent{Object: secret1}, queue)
8081
expectItems(queue, secret1)
8182
})
8283
})
@@ -88,7 +89,7 @@ var _ = Describe("EnqueueMapped", func() {
8889

8990
Describe("#Update", func() {
9091
It("should work map and enqueue", func() {
91-
handler.Update(event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
92+
handler.Update(ctx, event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
9293
expectItems(queue, secret1, secret2)
9394
})
9495
})
@@ -101,7 +102,7 @@ var _ = Describe("EnqueueMapped", func() {
101102

102103
Describe("#Update", func() {
103104
It("should work map and enqueue", func() {
104-
handler.Update(event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
105+
handler.Update(ctx, event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
105106
expectItems(queue, secret2)
106107
})
107108
})
@@ -114,7 +115,7 @@ var _ = Describe("EnqueueMapped", func() {
114115

115116
Describe("#Update", func() {
116117
It("should work map and enqueue", func() {
117-
handler.Update(event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
118+
handler.Update(ctx, event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
118119
expectItems(queue, secret1)
119120
})
120121
})

pkg/controllerutils/source.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var EnqueueOnce = source.Func(func(_ context.Context, _ handler.EventHandler, q
3434

3535
// HandleOnce is a source.Source that simply triggers the reconciler once by calling 'Create' at the event handler with
3636
// an empty event.CreateEvent.
37-
var HandleOnce = source.Func(func(_ context.Context, handler handler.EventHandler, queue workqueue.RateLimitingInterface, _ ...predicate.Predicate) error {
38-
handler.Create(event.CreateEvent{}, queue)
37+
var HandleOnce = source.Func(func(ctx context.Context, handler handler.EventHandler, queue workqueue.RateLimitingInterface, _ ...predicate.Predicate) error {
38+
handler.Create(ctx, event.CreateEvent{}, queue)
3939
return nil
4040
})

pkg/controllerutils/source_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var _ = Describe("Source", func() {
5252
var (
5353
ctx = context.Background()
5454
eventHandler = handler.Funcs{
55-
CreateFunc: func(_ event.CreateEvent, queue workqueue.RateLimitingInterface) {
55+
CreateFunc: func(ctx context.Context, _ event.CreateEvent, queue workqueue.RateLimitingInterface) {
5656
queue.Add(reconcile.Request{})
5757
},
5858
}

pkg/gardenlet/controller/managedseed/add.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ var RandomDurationWithMetaDuration = utils.RandomDurationWithMetaDuration
249249
// All other events are normally enqueued.
250250
func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
251251
return &handler.Funcs{
252-
CreateFunc: func(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
252+
CreateFunc: func(_ context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
253253
managedSeed, ok := evt.Object.(*seedmanagementv1alpha1.ManagedSeed)
254254
if !ok {
255255
return
@@ -277,7 +277,7 @@ func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
277277
// roughly at the same time.
278278
q.AddAfter(reconcileRequest(evt.Object), RandomDurationWithMetaDuration(r.Config.Controllers.ManagedSeed.SyncJitterPeriod))
279279
},
280-
UpdateFunc: func(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
280+
UpdateFunc: func(_ context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
281281
managedSeed, ok := evt.ObjectNew.(*seedmanagementv1alpha1.ManagedSeed)
282282
if !ok {
283283
return
@@ -300,7 +300,7 @@ func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
300300
q.Add(reconcileRequest(evt.ObjectNew))
301301
}
302302
},
303-
DeleteFunc: func(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
303+
DeleteFunc: func(_ context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
304304
if evt.Object == nil {
305305
return
306306
}

pkg/gardenlet/controller/managedseed/add_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,14 @@ var _ = Describe("Add", func() {
320320

321321
now := metav1.Now()
322322
obj.SetDeletionTimestamp(&now)
323-
hdlr.Create(event.CreateEvent{Object: obj}, queue)
323+
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
324324
})
325325

326326
It("should enqueue the object without delay for Create events when generation is set to 1", func() {
327327
queue.EXPECT().Add(req)
328328

329329
obj.Generation = 1
330-
hdlr.Create(event.CreateEvent{Object: obj}, queue)
330+
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
331331
})
332332

333333
It("should enqueue the object without delay for Create events when generation changed and jitterudpates is set to false", func() {
@@ -337,7 +337,7 @@ var _ = Describe("Add", func() {
337337
obj.Generation = 2
338338
obj.Status.ObservedGeneration = 1
339339
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
340-
hdlr.Create(event.CreateEvent{Object: obj}, queue)
340+
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
341341
})
342342

343343
It("should enqueue the object with random delay for Create events when generation changed and jitterUpdates is set to true", func() {
@@ -347,7 +347,7 @@ var _ = Describe("Add", func() {
347347
obj.Generation = 2
348348
obj.Status.ObservedGeneration = 1
349349
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
350-
hdlr.Create(event.CreateEvent{Object: obj}, queue)
350+
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
351351
})
352352

353353
It("should enqueue the object with random delay for Create events when there is no change in generation", func() {
@@ -357,13 +357,13 @@ var _ = Describe("Add", func() {
357357
obj.Generation = 2
358358
obj.Status.ObservedGeneration = 2
359359
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
360-
hdlr.Create(event.CreateEvent{Object: obj}, queue)
360+
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
361361
})
362362

363363
It("should not enqueue the object for Update events when generation and observedGeneration are equal", func() {
364364
obj.Generation = 1
365365
obj.Status.ObservedGeneration = 1
366-
hdlr.Update(event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
366+
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
367367
})
368368

369369
It("should enqueue the object for Update events when deletion timestamp is set", func() {
@@ -373,15 +373,15 @@ var _ = Describe("Add", func() {
373373
obj.Status.ObservedGeneration = 1
374374
now := metav1.Now()
375375
obj.SetDeletionTimestamp(&now)
376-
hdlr.Update(event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
376+
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
377377
})
378378

379379
It("should enqueue the object for Update events when generation is 1", func() {
380380
queue.EXPECT().Add(req)
381381

382382
obj.Generation = 1
383383
obj.Status.ObservedGeneration = 0
384-
hdlr.Update(event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
384+
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
385385
})
386386

387387
It("should enqueue the object for Update events when jitterUpdates is set to false", func() {
@@ -391,7 +391,7 @@ var _ = Describe("Add", func() {
391391
obj.Generation = 2
392392
obj.Status.ObservedGeneration = 1
393393
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
394-
hdlr.Update(event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
394+
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
395395
})
396396

397397
It("should enqueue the object with random delay for Update events when jitterUpdates is set to true", func() {
@@ -401,17 +401,17 @@ var _ = Describe("Add", func() {
401401
obj.Generation = 2
402402
obj.Status.ObservedGeneration = 1
403403
hdlr = (&Reconciler{Config: cfg}).EnqueueWithJitterDelay()
404-
hdlr.Update(event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
404+
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
405405
})
406406

407407
It("should enqueue the object for Delete events", func() {
408408
queue.EXPECT().Add(req)
409409

410-
hdlr.Delete(event.DeleteEvent{Object: obj}, queue)
410+
hdlr.Delete(ctx, event.DeleteEvent{Object: obj}, queue)
411411
})
412412

413413
It("should not enqueue the object for Generic events", func() {
414-
hdlr.Generic(event.GenericEvent{Object: obj}, queue)
414+
hdlr.Generic(ctx, event.GenericEvent{Object: obj}, queue)
415415
})
416416
})
417417
})

pkg/gardenlet/controller/shoot/care/add.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package care
1616

1717
import (
18+
"context"
19+
1820
"k8s.io/apimachinery/pkg/types"
1921
"k8s.io/client-go/util/workqueue"
2022
"k8s.io/utils/clock"
@@ -72,7 +74,7 @@ var RandomDurationWithMetaDuration = utils.RandomDurationWithMetaDuration
7274
// EventHandler returns a handler for Shoot events.
7375
func (r *Reconciler) EventHandler() handler.EventHandler {
7476
return &handler.Funcs{
75-
CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) {
77+
CreateFunc: func(_ context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) {
7678
shoot, ok := e.Object.(*gardencorev1beta1.Shoot)
7779
if !ok {
7880
return
@@ -93,7 +95,7 @@ func (r *Reconciler) EventHandler() handler.EventHandler {
9395
// don't add random duration for enqueueing new Shoots which have never been health checked yet
9496
q.Add(req)
9597
},
96-
UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
98+
UpdateFunc: func(_ context.Context, e event.UpdateEvent, q workqueue.RateLimitingInterface) {
9799
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
98100
Name: e.ObjectNew.GetName(),
99101
Namespace: e.ObjectNew.GetNamespace(),

pkg/gardenlet/controller/shoot/care/add_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package care_test
1616

1717
import (
18+
"context"
1819
"time"
1920

2021
"github.com/golang/mock/gomock"
@@ -57,6 +58,7 @@ var _ = Describe("Add", func() {
5758

5859
Describe("#EventHandler", func() {
5960
var (
61+
ctx = context.TODO()
6062
hdlr handler.EventHandler
6163
queue *mockworkqueue.MockRateLimitingInterface
6264
req reconcile.Request
@@ -74,21 +76,21 @@ var _ = Describe("Add", func() {
7476
}))
7577
queue.EXPECT().AddAfter(req, reconciler.Config.Controllers.ShootCare.SyncPeriod.Duration)
7678

77-
hdlr.Create(event.CreateEvent{Object: shoot}, queue)
79+
hdlr.Create(ctx, event.CreateEvent{Object: shoot}, queue)
7880
})
7981

8082
It("should enqueue the object for Update events", func() {
8183
queue.EXPECT().Add(req)
8284

83-
hdlr.Update(event.UpdateEvent{ObjectNew: shoot, ObjectOld: shoot}, queue)
85+
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: shoot, ObjectOld: shoot}, queue)
8486
})
8587

8688
It("should not enqueue the object for Delete events", func() {
87-
hdlr.Delete(event.DeleteEvent{Object: shoot}, queue)
89+
hdlr.Delete(ctx, event.DeleteEvent{Object: shoot}, queue)
8890
})
8991

9092
It("should not enqueue the object for Generic events", func() {
91-
hdlr.Generic(event.GenericEvent{Object: shoot}, queue)
93+
hdlr.Generic(ctx, event.GenericEvent{Object: shoot}, queue)
9294
})
9395
})
9496

0 commit comments

Comments
 (0)