Skip to content

Commit d61f561

Browse files
committed
🌱 Expose handler.MapFunc to allow implementers to test mappers
This is a follow-up from a previous change to the handler package that deprecated the old handler.ToRequestFunc in favor of EnqueueRequestsFromMapFunc. While we still want to prefer using EnqueueRequestsFromMapFunc, this change also exposes the type of MapFunc signature expected. Users can use this type to test their own mappers in unit tests. Signed-off-by: Vince Prignano <[email protected]>
1 parent 7f050c2 commit d61f561

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

pkg/handler/enqueue_mapped.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
2525
)
2626

27+
// MapFunc is the signature required for enqueueing requests from a generic function.
28+
// This type is usually used with EnqueueRequestsFromMapFunc when registering an event handler.
29+
type MapFunc func(MapObject) []reconcile.Request
30+
2731
// EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
2832
// of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
2933
// defined by some user specified transformation of the source Event. (e.g. trigger Reconciler for a set of objects
@@ -34,17 +38,17 @@ import (
3438
//
3539
// For UpdateEvents which contain both a new and old object, the transformation function is run on both
3640
// objects and both sets of Requests are enqueue.
37-
func EnqueueRequestsFromMapFunc(mapFN func(MapObject) []reconcile.Request) EventHandler {
41+
func EnqueueRequestsFromMapFunc(fn MapFunc) EventHandler {
3842
return &enqueueRequestsFromMapFunc{
39-
ToRequests: toRequestsFunc(mapFN),
43+
toRequests: fn,
4044
}
4145
}
4246

4347
var _ EventHandler = &enqueueRequestsFromMapFunc{}
4448

4549
type enqueueRequestsFromMapFunc struct {
4650
// Mapper transforms the argument into a slice of keys to be reconciled
47-
ToRequests mapper
51+
toRequests MapFunc
4852
}
4953

5054
// Create implements EventHandler
@@ -69,7 +73,7 @@ func (e *enqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue
6973
}
7074

7175
func (e *enqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object MapObject) {
72-
for _, req := range e.ToRequests.Map(object) {
76+
for _, req := range e.toRequests(object) {
7377
q.Add(req)
7478
}
7579
}
@@ -81,26 +85,10 @@ func (e *enqueueRequestsFromMapFunc) InjectFunc(f inject.Func) error {
8185
if f == nil {
8286
return nil
8387
}
84-
return f(e.ToRequests)
85-
}
86-
87-
// mapper maps an object to a collection of keys to be enqueued
88-
type mapper interface {
89-
// Map maps an object
90-
Map(MapObject) []reconcile.Request
88+
return f(e.toRequests)
9189
}
9290

9391
// MapObject contains information from an event to be transformed into a Request.
9492
type MapObject struct {
9593
Object controllerutil.Object
9694
}
97-
98-
var _ mapper = toRequestsFunc(nil)
99-
100-
// toRequestsFunc implements Mapper using a function.
101-
type toRequestsFunc func(MapObject) []reconcile.Request
102-
103-
// Map implements Mapper
104-
func (m toRequestsFunc) Map(i MapObject) []reconcile.Request {
105-
return m(i)
106-
}

0 commit comments

Comments
 (0)