From 87e98c1516311c8d06de16dae7377413a5627ad0 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Wed, 5 Aug 2020 20:59:01 -0400 Subject: [PATCH] :sparkles: Add handler.FromMapFunc to simplify creation of custom mappers --- pkg/handler/enqueue_mapped.go | 16 ++++++++++++++++ pkg/handler/eventhandler.go | 2 +- pkg/handler/example_test.go | 27 +++++++++++++-------------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/pkg/handler/enqueue_mapped.go b/pkg/handler/enqueue_mapped.go index a60790242c..6bfe44f781 100644 --- a/pkg/handler/enqueue_mapped.go +++ b/pkg/handler/enqueue_mapped.go @@ -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 diff --git a/pkg/handler/eventhandler.go b/pkg/handler/eventhandler.go index c9b93f8b97..5970e7724c 100644 --- a/pkg/handler/eventhandler.go +++ b/pkg/handler/eventhandler.go @@ -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.) // diff --git a/pkg/handler/example_test.go b/pkg/handler/example_test.go index 7ead7632cb..77be76c532 100644 --- a/pkg/handler/example_test.go +++ b/pkg/handler/example_test.go @@ -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 }