Skip to content

Commit 606f970

Browse files
committed
Add context to EventHandler(s)
This changeset adds a context.Context parameter to every EventHandler call. Most project might use MapFunc specifically to retrieve other objects with clients and potentially enqueue requests to a watching object. A context is useful in these cases to avoid using context.TODO() or Background() which never gets cancelled. Signed-off-by: Vince Prignano <[email protected]>
1 parent cb29997 commit 606f970

14 files changed

+244
-228
lines changed

pkg/builder/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ var _ = Describe("application", func() {
497497
For(&appsv1.Deployment{}, OnlyMetadata).
498498
Owns(&appsv1.ReplicaSet{}, OnlyMetadata).
499499
Watches(&appsv1.StatefulSet{},
500-
handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
500+
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
501501
defer GinkgoRecover()
502502

503503
ometa := o.(*metav1.PartialObjectMetadata)

pkg/handler/enqueue.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package handler
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/types"
2123
"k8s.io/client-go/util/workqueue"
2224
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -36,7 +38,7 @@ var _ EventHandler = &EnqueueRequestForObject{}
3638
type EnqueueRequestForObject struct{}
3739

3840
// Create implements EventHandler.
39-
func (e *EnqueueRequestForObject) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
41+
func (e *EnqueueRequestForObject) Create(ctx context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
4042
if evt.Object == nil {
4143
enqueueLog.Error(nil, "CreateEvent received with no metadata", "event", evt)
4244
return
@@ -48,7 +50,7 @@ func (e *EnqueueRequestForObject) Create(evt event.CreateEvent, q workqueue.Rate
4850
}
4951

5052
// Update implements EventHandler.
51-
func (e *EnqueueRequestForObject) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
53+
func (e *EnqueueRequestForObject) Update(ctx context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
5254
switch {
5355
case evt.ObjectNew != nil:
5456
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
@@ -66,7 +68,7 @@ func (e *EnqueueRequestForObject) Update(evt event.UpdateEvent, q workqueue.Rate
6668
}
6769

6870
// Delete implements EventHandler.
69-
func (e *EnqueueRequestForObject) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
71+
func (e *EnqueueRequestForObject) Delete(ctx context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
7072
if evt.Object == nil {
7173
enqueueLog.Error(nil, "DeleteEvent received with no metadata", "event", evt)
7274
return
@@ -78,7 +80,7 @@ func (e *EnqueueRequestForObject) Delete(evt event.DeleteEvent, q workqueue.Rate
7880
}
7981

8082
// Generic implements EventHandler.
81-
func (e *EnqueueRequestForObject) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
83+
func (e *EnqueueRequestForObject) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
8284
if evt.Object == nil {
8385
enqueueLog.Error(nil, "GenericEvent received with no metadata", "event", evt)
8486
return

pkg/handler/enqueue_mapped.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package handler
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/client-go/util/workqueue"
2123
"sigs.k8s.io/controller-runtime/pkg/client"
2224
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -25,7 +27,7 @@ import (
2527

2628
// MapFunc is the signature required for enqueueing requests from a generic function.
2729
// This type is usually used with EnqueueRequestsFromMapFunc when registering an event handler.
28-
type MapFunc func(client.Object) []reconcile.Request
30+
type MapFunc func(context.Context, client.Object) []reconcile.Request
2931

3032
// EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
3133
// of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
@@ -51,32 +53,32 @@ type enqueueRequestsFromMapFunc struct {
5153
}
5254

5355
// Create implements EventHandler.
54-
func (e *enqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
56+
func (e *enqueueRequestsFromMapFunc) Create(ctx context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
5557
reqs := map[reconcile.Request]empty{}
56-
e.mapAndEnqueue(q, evt.Object, reqs)
58+
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
5759
}
5860

5961
// Update implements EventHandler.
60-
func (e *enqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
62+
func (e *enqueueRequestsFromMapFunc) Update(ctx context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
6163
reqs := map[reconcile.Request]empty{}
62-
e.mapAndEnqueue(q, evt.ObjectOld, reqs)
63-
e.mapAndEnqueue(q, evt.ObjectNew, reqs)
64+
e.mapAndEnqueue(ctx, q, evt.ObjectOld, reqs)
65+
e.mapAndEnqueue(ctx, q, evt.ObjectNew, reqs)
6466
}
6567

6668
// Delete implements EventHandler.
67-
func (e *enqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
69+
func (e *enqueueRequestsFromMapFunc) Delete(ctx context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
6870
reqs := map[reconcile.Request]empty{}
69-
e.mapAndEnqueue(q, evt.Object, reqs)
71+
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
7072
}
7173

7274
// Generic implements EventHandler.
73-
func (e *enqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
75+
func (e *enqueueRequestsFromMapFunc) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
7476
reqs := map[reconcile.Request]empty{}
75-
e.mapAndEnqueue(q, evt.Object, reqs)
77+
e.mapAndEnqueue(ctx, q, evt.Object, reqs)
7678
}
7779

78-
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object client.Object, reqs map[reconcile.Request]empty) {
79-
for _, req := range e.toRequests(object) {
80+
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(ctx context.Context, q workqueue.RateLimitingInterface, object client.Object, reqs map[reconcile.Request]empty) {
81+
for _, req := range e.toRequests(ctx, object) {
8082
_, ok := reqs[req]
8183
if !ok {
8284
q.Add(req)

pkg/handler/enqueue_owner.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package handler
1818

1919
import (
20+
"context"
2021
"fmt"
2122

2223
"k8s.io/apimachinery/pkg/api/meta"
@@ -82,7 +83,7 @@ type enqueueRequestForOwner struct {
8283
}
8384

8485
// Create implements EventHandler.
85-
func (e *enqueueRequestForOwner) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
86+
func (e *enqueueRequestForOwner) Create(ctx context.Context, evt event.CreateEvent, q workqueue.RateLimitingInterface) {
8687
reqs := map[reconcile.Request]empty{}
8788
e.getOwnerReconcileRequest(evt.Object, reqs)
8889
for req := range reqs {
@@ -91,7 +92,7 @@ func (e *enqueueRequestForOwner) Create(evt event.CreateEvent, q workqueue.RateL
9192
}
9293

9394
// Update implements EventHandler.
94-
func (e *enqueueRequestForOwner) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
95+
func (e *enqueueRequestForOwner) Update(ctx context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
9596
reqs := map[reconcile.Request]empty{}
9697
e.getOwnerReconcileRequest(evt.ObjectOld, reqs)
9798
e.getOwnerReconcileRequest(evt.ObjectNew, reqs)
@@ -101,7 +102,7 @@ func (e *enqueueRequestForOwner) Update(evt event.UpdateEvent, q workqueue.RateL
101102
}
102103

103104
// Delete implements EventHandler.
104-
func (e *enqueueRequestForOwner) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
105+
func (e *enqueueRequestForOwner) Delete(ctx context.Context, evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
105106
reqs := map[reconcile.Request]empty{}
106107
e.getOwnerReconcileRequest(evt.Object, reqs)
107108
for req := range reqs {
@@ -110,7 +111,7 @@ func (e *enqueueRequestForOwner) Delete(evt event.DeleteEvent, q workqueue.RateL
110111
}
111112

112113
// Generic implements EventHandler.
113-
func (e *enqueueRequestForOwner) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
114+
func (e *enqueueRequestForOwner) Generic(ctx context.Context, evt event.GenericEvent, q workqueue.RateLimitingInterface) {
114115
reqs := map[reconcile.Request]empty{}
115116
e.getOwnerReconcileRequest(evt.Object, reqs)
116117
for req := range reqs {

pkg/handler/eventhandler.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package handler
1818

1919
import (
20+
"context"
21+
2022
"k8s.io/client-go/util/workqueue"
2123
"sigs.k8s.io/controller-runtime/pkg/event"
2224
)
@@ -41,17 +43,17 @@ import (
4143
// Most users shouldn't need to implement their own EventHandler.
4244
type EventHandler interface {
4345
// Create is called in response to an create event - e.g. Pod Creation.
44-
Create(event.CreateEvent, workqueue.RateLimitingInterface)
46+
Create(context.Context, event.CreateEvent, workqueue.RateLimitingInterface)
4547

4648
// Update is called in response to an update event - e.g. Pod Updated.
47-
Update(event.UpdateEvent, workqueue.RateLimitingInterface)
49+
Update(context.Context, event.UpdateEvent, workqueue.RateLimitingInterface)
4850

4951
// Delete is called in response to a delete event - e.g. Pod Deleted.
50-
Delete(event.DeleteEvent, workqueue.RateLimitingInterface)
52+
Delete(context.Context, event.DeleteEvent, workqueue.RateLimitingInterface)
5153

5254
// Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
5355
// external trigger request - e.g. reconcile Autoscaling, or a Webhook.
54-
Generic(event.GenericEvent, workqueue.RateLimitingInterface)
56+
Generic(context.Context, event.GenericEvent, workqueue.RateLimitingInterface)
5557
}
5658

5759
var _ EventHandler = Funcs{}
@@ -60,45 +62,45 @@ var _ EventHandler = Funcs{}
6062
type Funcs struct {
6163
// Create is called in response to an add event. Defaults to no-op.
6264
// RateLimitingInterface is used to enqueue reconcile.Requests.
63-
CreateFunc func(event.CreateEvent, workqueue.RateLimitingInterface)
65+
CreateFunc func(context.Context, event.CreateEvent, workqueue.RateLimitingInterface)
6466

6567
// Update is called in response to an update event. Defaults to no-op.
6668
// RateLimitingInterface is used to enqueue reconcile.Requests.
67-
UpdateFunc func(event.UpdateEvent, workqueue.RateLimitingInterface)
69+
UpdateFunc func(context.Context, event.UpdateEvent, workqueue.RateLimitingInterface)
6870

6971
// Delete is called in response to a delete event. Defaults to no-op.
7072
// RateLimitingInterface is used to enqueue reconcile.Requests.
71-
DeleteFunc func(event.DeleteEvent, workqueue.RateLimitingInterface)
73+
DeleteFunc func(context.Context, event.DeleteEvent, workqueue.RateLimitingInterface)
7274

7375
// GenericFunc is called in response to a generic event. Defaults to no-op.
7476
// RateLimitingInterface is used to enqueue reconcile.Requests.
75-
GenericFunc func(event.GenericEvent, workqueue.RateLimitingInterface)
77+
GenericFunc func(context.Context, event.GenericEvent, workqueue.RateLimitingInterface)
7678
}
7779

7880
// Create implements EventHandler.
79-
func (h Funcs) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) {
81+
func (h Funcs) Create(ctx context.Context, e event.CreateEvent, q workqueue.RateLimitingInterface) {
8082
if h.CreateFunc != nil {
81-
h.CreateFunc(e, q)
83+
h.CreateFunc(ctx, e, q)
8284
}
8385
}
8486

8587
// Delete implements EventHandler.
86-
func (h Funcs) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
88+
func (h Funcs) Delete(ctx context.Context, e event.DeleteEvent, q workqueue.RateLimitingInterface) {
8789
if h.DeleteFunc != nil {
88-
h.DeleteFunc(e, q)
90+
h.DeleteFunc(ctx, e, q)
8991
}
9092
}
9193

9294
// Update implements EventHandler.
93-
func (h Funcs) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
95+
func (h Funcs) Update(ctx context.Context, e event.UpdateEvent, q workqueue.RateLimitingInterface) {
9496
if h.UpdateFunc != nil {
95-
h.UpdateFunc(e, q)
97+
h.UpdateFunc(ctx, e, q)
9698
}
9799
}
98100

99101
// Generic implements EventHandler.
100-
func (h Funcs) Generic(e event.GenericEvent, q workqueue.RateLimitingInterface) {
102+
func (h Funcs) Generic(ctx context.Context, e event.GenericEvent, q workqueue.RateLimitingInterface) {
101103
if h.GenericFunc != nil {
102-
h.GenericFunc(e, q)
104+
h.GenericFunc(ctx, e, q)
103105
}
104106
}

0 commit comments

Comments
 (0)