Skip to content

Commit

Permalink
✨ Add handler.FromMapFunc to simplify creation of custom mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaroaleman committed Aug 6, 2020
1 parent 420cd15 commit 87e98c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
16 changes: 16 additions & 0 deletions pkg/handler/enqueue_mapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ import (
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
)

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

var _ EventHandler = &EnqueueRequestsFromMapFunc{}

// EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
Expand Down
2 changes: 1 addition & 1 deletion pkg/handler/eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
// * Use EnqueueRequestForOwner to reconcile the owner of the object the event is for
// - do this for events for the types the Controller creates. (e.g. ReplicaSets created by a Deployment Controller)
//
// * Use EnqueueRequestsFromMapFunc to transform an event for an object to a reconcile of an object
// * Use EnqueueRequestsFromMapFunc or FromMapFunc to transform an event for an object to a reconcile of an object
// of a different type - do this for events for types the Controller may be interested in, but doesn't create.
// (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
//
Expand Down
27 changes: 13 additions & 14 deletions pkg/handler/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,19 @@ func ExampleEnqueueRequestsFromMapFunc() {
// controller is a controller.controller
err := c.Watch(
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestsFromMapFunc{
ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-1",
Namespace: a.Meta.GetNamespace(),
}},
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-2",
Namespace: a.Meta.GetNamespace(),
}},
}
}),
})
handler.FromMapFunc(func(a handler.MapObject) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-1",
Namespace: a.Meta.GetNamespace(),
}},
{NamespacedName: types.NamespacedName{
Name: a.Meta.GetName() + "-2",
Namespace: a.Meta.GetNamespace(),
}},
}
}),
)
if err != nil {
// handle it
}
Expand Down

0 comments on commit 87e98c1

Please sign in to comment.