Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / handler / enqueue_mapped.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package handler
18
19 import (
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"
25 )
26
27 var _ EventHandler = &EnqueueRequestsFromMapFunc{}
28
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)
33 //
34 // EnqueueRequestsFromMapFunc is frequently used to fan-out updates from one object to one or more other
35 // objects of a differing type.
36 //
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
41         ToRequests Mapper
42 }
43
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})
47 }
48
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})
53 }
54
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})
58 }
59
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})
63 }
64
65 func (e *EnqueueRequestsFromMapFunc) mapAndEnqueue(q workqueue.RateLimitingInterface, object MapObject) {
66         for _, req := range e.ToRequests.Map(object) {
67                 q.Add(req)
68         }
69 }
70
71 // Mapper maps an object to a collection of keys to be enqueued
72 type Mapper interface {
73         // Map maps an object
74         Map(MapObject) []reconcile.Request
75 }
76
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.
80         Meta metav1.Object
81
82         // Object is the object from an event.
83         Object runtime.Object
84 }
85
86 var _ Mapper = ToRequestsFunc(nil)
87
88 // ToRequestsFunc implements Mapper using a function.
89 type ToRequestsFunc func(MapObject) []reconcile.Request
90
91 // Map implements Mapper
92 func (m ToRequestsFunc) Map(i MapObject) []reconcile.Request {
93         return m(i)
94 }