Skip to content

Commit

Permalink
Adapt handler.EventHandler and handler.Funcs and add context.Context …
Browse files Browse the repository at this point in the history
…as the first parameter for every method.

kubernetes-sigs/controller-runtime#2139
  • Loading branch information
ary1992 committed Jun 23, 2023
1 parent 8e5fb75 commit 88c8bc6
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 54 deletions.
7 changes: 4 additions & 3 deletions pkg/controllerutils/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package controllerutils

import (
"context"
"time"

"k8s.io/apimachinery/pkg/types"
Expand All @@ -39,19 +40,19 @@ func reconcileRequest(obj client.Object) reconcile.Request {
// All other events are normally enqueued.
func EnqueueCreateEventsOncePer24hDuration(clock clock.Clock) handler.Funcs {
return handler.Funcs{
CreateFunc: func(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
CreateFunc: func(_ context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
if evt.Object == nil {
return
}
q.AddAfter(reconcileRequest(evt.Object), getDuration(evt.Object, clock))
},
UpdateFunc: func(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
UpdateFunc: func(_ context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
if evt.ObjectNew == nil {
return
}
q.Add(reconcileRequest(evt.ObjectNew))
},
DeleteFunc: func(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
DeleteFunc: func(_ context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
if evt.Object == nil {
return
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/controllerutils/eventhandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package controllerutils_test

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -35,6 +36,7 @@ import (
var _ = Describe("EventHandler", func() {
Describe("#EnqueueCreateEventsOncePer24hDuration", func() {
var (
ctx = context.TODO()
handlerFuncs = handler.Funcs{}
queue workqueue.RateLimitingInterface
fakeClock *testclock.FakeClock
Expand Down Expand Up @@ -69,7 +71,7 @@ var _ = Describe("EventHandler", func() {
Object: backupBucket,
}
fakeClock.Step(24 * time.Hour)
handlerFuncs.Create(evt, queue)
handlerFuncs.Create(ctx, evt, queue)
verifyQueue(queue)
})

Expand All @@ -78,7 +80,7 @@ var _ = Describe("EventHandler", func() {
evt := event.CreateEvent{
Object: backupBucket,
}
handlerFuncs.Create(evt, queue)
handlerFuncs.Create(ctx, evt, queue)
Expect(queue.Len()).To(Equal(0))
fakeClock.Step(1 * time.Second)
Eventually(func() int {
Expand All @@ -92,15 +94,15 @@ var _ = Describe("EventHandler", func() {
ObjectNew: backupBucket,
ObjectOld: backupBucket,
}
handlerFuncs.Update(evt, queue)
handlerFuncs.Update(ctx, evt, queue)
verifyQueue(queue)
})

It("should enqueue a Request with the Name / Namespace of the object in the DeleteEvent.", func() {
evt := event.DeleteEvent{
Object: backupBucket,
}
handlerFuncs.Delete(evt, queue)
handlerFuncs.Delete(ctx, evt, queue)
verifyQueue(queue)
})
})
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllerutils/mapper/enqueue_mapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func (e *enqueueRequestsFromMapFunc) InjectStopChannel(stopCh <-chan struct{}) e
return nil
}

func (e *enqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
func (e *enqueueRequestsFromMapFunc) Create(_ context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, evt.Object)
}

func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
func (e *enqueueRequestsFromMapFunc) Update(_ context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
switch e.updateBehavior {
case UpdateWithOldAndNew:
e.mapAndEnqueue(q, evt.ObjectOld)
Expand All @@ -112,11 +112,11 @@ func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.R
}
}

func (e *enqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
func (e *enqueueRequestsFromMapFunc) Delete(_ context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, evt.Object)
}

func (e *enqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
func (e *enqueueRequestsFromMapFunc) Generic(_ context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
e.mapAndEnqueue(q, evt.Object)
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/controllerutils/mapper/enqueue_mapped_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
var _ = Describe("EnqueueMapped", func() {
Describe("#EnqueueRequestsFrom", func() {
var (
ctx = context.TODO()
mapper Mapper
logger logr.Logger
handler handler.EventHandler
Expand Down Expand Up @@ -62,21 +63,21 @@ var _ = Describe("EnqueueMapped", func() {

Describe("#Create", func() {
It("should work map and enqueue", func() {
handler.Create(event.CreateEvent{Object: secret1}, queue)
handler.Create(ctx, event.CreateEvent{Object: secret1}, queue)
expectItems(queue, secret1)
})
})

Describe("#Delete", func() {
It("should work map and enqueue", func() {
handler.Delete(event.DeleteEvent{Object: secret1}, queue)
handler.Delete(ctx, event.DeleteEvent{Object: secret1}, queue)
expectItems(queue, secret1)
})
})

Describe("#Generic", func() {
It("should work map and enqueue", func() {
handler.Generic(event.GenericEvent{Object: secret1}, queue)
handler.Generic(ctx, event.GenericEvent{Object: secret1}, queue)
expectItems(queue, secret1)
})
})
Expand All @@ -88,7 +89,7 @@ var _ = Describe("EnqueueMapped", func() {

Describe("#Update", func() {
It("should work map and enqueue", func() {
handler.Update(event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
handler.Update(ctx, event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
expectItems(queue, secret1, secret2)
})
})
Expand All @@ -101,7 +102,7 @@ var _ = Describe("EnqueueMapped", func() {

Describe("#Update", func() {
It("should work map and enqueue", func() {
handler.Update(event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
handler.Update(ctx, event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
expectItems(queue, secret2)
})
})
Expand All @@ -114,7 +115,7 @@ var _ = Describe("EnqueueMapped", func() {

Describe("#Update", func() {
It("should work map and enqueue", func() {
handler.Update(event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
handler.Update(ctx, event.UpdateEvent{ObjectOld: secret1, ObjectNew: secret2}, queue)
expectItems(queue, secret1)
})
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllerutils/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var EnqueueOnce = source.Func(func(_ context.Context, _ handler.EventHandler, q

// HandleOnce is a source.Source that simply triggers the reconciler once by calling 'Create' at the event handler with
// an empty event.CreateEvent.
var HandleOnce = source.Func(func(_ context.Context, handler handler.EventHandler, queue workqueue.RateLimitingInterface, _ ...predicate.Predicate) error {
handler.Create(event.CreateEvent{}, queue)
var HandleOnce = source.Func(func(ctx context.Context, handler handler.EventHandler, queue workqueue.RateLimitingInterface, _ ...predicate.Predicate) error {
handler.Create(ctx, event.CreateEvent{}, queue)
return nil
})
2 changes: 1 addition & 1 deletion pkg/controllerutils/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("Source", func() {
var (
ctx = context.Background()
eventHandler = handler.Funcs{
CreateFunc: func(_ event.CreateEvent, queue workqueue.RateLimitingInterface) {
CreateFunc: func(ctx context.Context, _ event.CreateEvent, queue workqueue.RateLimitingInterface) {
queue.Add(reconcile.Request{})
},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/gardenlet/controller/managedseed/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ var RandomDurationWithMetaDuration = utils.RandomDurationWithMetaDuration
// All other events are normally enqueued.
func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
return &handler.Funcs{
CreateFunc: func(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
CreateFunc: func(_ context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
managedSeed, ok := evt.Object.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return
Expand Down Expand Up @@ -277,7 +277,7 @@ func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
// roughly at the same time.
q.AddAfter(reconcileRequest(evt.Object), RandomDurationWithMetaDuration(r.Config.Controllers.ManagedSeed.SyncJitterPeriod))
},
UpdateFunc: func(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
UpdateFunc: func(_ context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
managedSeed, ok := evt.ObjectNew.(*seedmanagementv1alpha1.ManagedSeed)
if !ok {
return
Expand All @@ -300,7 +300,7 @@ func (r *Reconciler) EnqueueWithJitterDelay() handler.EventHandler {
q.Add(reconcileRequest(evt.ObjectNew))
}
},
DeleteFunc: func(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
DeleteFunc: func(_ context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
if evt.Object == nil {
return
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/gardenlet/controller/managedseed/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ var _ = Describe("Add", func() {

now := metav1.Now()
obj.SetDeletionTimestamp(&now)
hdlr.Create(event.CreateEvent{Object: obj}, queue)
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})

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

obj.Generation = 1
hdlr.Create(event.CreateEvent{Object: obj}, queue)
hdlr.Create(ctx, event.CreateEvent{Object: obj}, queue)
})

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

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

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

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

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

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

obj.Generation = 1
obj.Status.ObservedGeneration = 0
hdlr.Update(event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: obj, ObjectOld: obj}, queue)
})

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

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

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

hdlr.Delete(event.DeleteEvent{Object: obj}, queue)
hdlr.Delete(ctx, event.DeleteEvent{Object: obj}, queue)
})

It("should not enqueue the object for Generic events", func() {
hdlr.Generic(event.GenericEvent{Object: obj}, queue)
hdlr.Generic(ctx, event.GenericEvent{Object: obj}, queue)
})
})
})
6 changes: 4 additions & 2 deletions pkg/gardenlet/controller/shoot/care/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package care

import (
"context"

"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/clock"
Expand Down Expand Up @@ -72,7 +74,7 @@ var RandomDurationWithMetaDuration = utils.RandomDurationWithMetaDuration
// EventHandler returns a handler for Shoot events.
func (r *Reconciler) EventHandler() handler.EventHandler {
return &handler.Funcs{
CreateFunc: func(e event.CreateEvent, q workqueue.RateLimitingInterface) {
CreateFunc: func(_ context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) {
shoot, ok := e.Object.(*gardencorev1beta1.Shoot)
if !ok {
return
Expand All @@ -93,7 +95,7 @@ func (r *Reconciler) EventHandler() handler.EventHandler {
// don't add random duration for enqueueing new Shoots which have never been health checked yet
q.Add(req)
},
UpdateFunc: func(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
UpdateFunc: func(_ context.Context, e event.UpdateEvent, q workqueue.RateLimitingInterface) {
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
Name: e.ObjectNew.GetName(),
Namespace: e.ObjectNew.GetNamespace(),
Expand Down
10 changes: 6 additions & 4 deletions pkg/gardenlet/controller/shoot/care/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package care_test

import (
"context"
"time"

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

Describe("#EventHandler", func() {
var (
ctx = context.TODO()
hdlr handler.EventHandler
queue *mockworkqueue.MockRateLimitingInterface
req reconcile.Request
Expand All @@ -74,21 +76,21 @@ var _ = Describe("Add", func() {
}))
queue.EXPECT().AddAfter(req, reconciler.Config.Controllers.ShootCare.SyncPeriod.Duration)

hdlr.Create(event.CreateEvent{Object: shoot}, queue)
hdlr.Create(ctx, event.CreateEvent{Object: shoot}, queue)
})

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

hdlr.Update(event.UpdateEvent{ObjectNew: shoot, ObjectOld: shoot}, queue)
hdlr.Update(ctx, event.UpdateEvent{ObjectNew: shoot, ObjectOld: shoot}, queue)
})

It("should not enqueue the object for Delete events", func() {
hdlr.Delete(event.DeleteEvent{Object: shoot}, queue)
hdlr.Delete(ctx, event.DeleteEvent{Object: shoot}, queue)
})

It("should not enqueue the object for Generic events", func() {
hdlr.Generic(event.GenericEvent{Object: shoot}, queue)
hdlr.Generic(ctx, event.GenericEvent{Object: shoot}, queue)
})
})

Expand Down
Loading

0 comments on commit 88c8bc6

Please sign in to comment.