Skip to content

Commit 87e98c1

Browse files
committed
✨ Add handler.FromMapFunc to simplify creation of custom mappers
1 parent 420cd15 commit 87e98c1

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

pkg/handler/enqueue_mapped.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ import (
2525
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
2626
)
2727

28+
// FromMapFunc enqueues Requests by running a transformation function that outputs a collection
29+
// of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
30+
// defined by some user specified transformation of the source Event. (e.g. trigger Reconciler for a set of objects
31+
// in response to a cluster resize event caused by adding or deleting a Node)
32+
//
33+
// FromMapFunc is frequently used to fan-out updates from one object to one or more other
34+
// objects of a differing type.
35+
//
36+
// For UpdateEvents which contain both a new and old object, the transformation function is run on both
37+
// objects and both sets of Requests are enqueue.
38+
func FromMapFunc(mapFN func(MapObject) []reconcile.Request) EventHandler {
39+
return &EnqueueRequestsFromMapFunc{
40+
ToRequests: ToRequestsFunc(mapFN),
41+
}
42+
}
43+
2844
var _ EventHandler = &EnqueueRequestsFromMapFunc{}
2945

3046
// EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection

pkg/handler/eventhandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// * Use EnqueueRequestForOwner to reconcile the owner of the object the event is for
3434
// - do this for events for the types the Controller creates. (e.g. ReplicaSets created by a Deployment Controller)
3535
//
36-
// * Use EnqueueRequestsFromMapFunc to transform an event for an object to a reconcile of an object
36+
// * Use EnqueueRequestsFromMapFunc or FromMapFunc to transform an event for an object to a reconcile of an object
3737
// of a different type - do this for events for types the Controller may be interested in, but doesn't create.
3838
// (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
3939
//

pkg/handler/example_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,19 @@ func ExampleEnqueueRequestsFromMapFunc() {
6565
// controller is a controller.controller
6666
err := c.Watch(
6767
&source.Kind{Type: &appsv1.Deployment{}},
68-
&handler.EnqueueRequestsFromMapFunc{
69-
ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
70-
return []reconcile.Request{
71-
{NamespacedName: types.NamespacedName{
72-
Name: a.Meta.GetName() + "-1",
73-
Namespace: a.Meta.GetNamespace(),
74-
}},
75-
{NamespacedName: types.NamespacedName{
76-
Name: a.Meta.GetName() + "-2",
77-
Namespace: a.Meta.GetNamespace(),
78-
}},
79-
}
80-
}),
81-
})
68+
handler.FromMapFunc(func(a handler.MapObject) []reconcile.Request {
69+
return []reconcile.Request{
70+
{NamespacedName: types.NamespacedName{
71+
Name: a.Meta.GetName() + "-1",
72+
Namespace: a.Meta.GetNamespace(),
73+
}},
74+
{NamespacedName: types.NamespacedName{
75+
Name: a.Meta.GetName() + "-2",
76+
Namespace: a.Meta.GetNamespace(),
77+
}},
78+
}
79+
}),
80+
)
8281
if err != nil {
8382
// handle it
8483
}

0 commit comments

Comments
 (0)