2 Copyright 2018 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21 "k8s.io/apimachinery/pkg/runtime"
22 "k8s.io/client-go/util/workqueue"
23 "sigs.k8s.io/controller-runtime/pkg/event"
24 "sigs.k8s.io/controller-runtime/pkg/reconcile"
27 var _ EventHandler = &EnqueueRequestsFromMapFunc{}
29 // EnqueueRequestsFromMapFunc enqueues Requests by running a transformation function that outputs a collection
30 // of reconcile.Requests on each Event. The reconcile.Requests may be for an arbitrary set of objects
31 // defined by some user specified transformation of the source Event. (e.g. trigger Reconciler for a set of objects
32 // in response to a cluster resize event caused by adding or deleting a Node)
34 // EnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other
35 // objects of a differing type.
37 // For UpdateEvents which contain both a new and old object, the transformation function is run on both
38 // objects and both sets of Requests are enqueue.
39 type EnqueueRequestsFromMapFunc struct {
40 // Mapper transforms the argument into a slice of keys to be reconciled
44 // Create implements EventHandler
45 func (e *EnqueueRequestsFromMapFunc) Create(evt event.CreateEvent, q workqueue.RateLimitingInterface) {
46 e.mapAndEnqueue(q, MapObject{Meta: evt.Meta, Object: evt.Object})
49 // Update implements EventHandler
50 func (e *EnqueueRequestsFromMapFunc) Update(evt event.UpdateEvent, q workqueue.RateLimitingInterface) {
51 e.mapAndEnqueue(q, MapObject{Meta: evt.MetaOld, Object: evt.ObjectOld})
52 e.mapAndEnqueue(q, MapObject{Meta: evt.MetaNew, Object: evt.ObjectNew})
55 // Delete implements EventHandler
56 func (e *EnqueueRequestsFromMapFunc) Delete(evt event.DeleteEvent, q workqueue.RateLimitingInterface) {
57 e.mapAndEnqueue(q, MapObject{Meta: evt.Meta, Object: evt.Object})
60 // Generic implements EventHandler
61 func (e *EnqueueRequestsFromMapFunc) Generic(evt event.GenericEvent, q workqueue.RateLimitingInterface) {
62 e.mapAndEnqueue(q, MapObject{Meta: evt.Meta, Object: evt.Object})
65 func (e *EnqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object MapObject) {
66 for _, req := range e.ToRequests.Map(object) {
71 // Mapper maps an object to a collection of keys to be enqueued
72 type Mapper interface {
74 Map(MapObject) []reconcile.Request
77 // MapObject contains information from an event to be transformed into a Request.
78 type MapObject struct {
79 // Meta is the meta data for an object from an event.
82 // Object is the object from an event.
86 var _ Mapper = ToRequestsFunc(nil)
88 // ToRequestsFunc implements Mapper using a function.
89 type ToRequestsFunc func(MapObject) []reconcile.Request
91 // Map implements Mapper
92 func (m ToRequestsFunc) Map(i MapObject) []reconcile.Request {