Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / handler / eventhandler.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         "k8s.io/client-go/util/workqueue"
21         "sigs.k8s.io/controller-runtime/pkg/event"
22 )
23
24 // EventHandler enqueues reconcile.Requests in response to events (e.g. Pod Create).  EventHandlers map an Event
25 // for one object to trigger Reconciles for either the same object or different objects - e.g. if there is an
26 // Event for object with type Foo (using source.KindSource) then reconcile one or more object(s) with type Bar.
27 //
28 // Identical reconcile.Requests will be batched together through the queuing mechanism before reconcile is called.
29 //
30 // * Use EnqueueRequestForObject to reconcile the object the event is for
31 // - do this for events for the type the Controller Reconciles. (e.g. Deployment for a Deployment Controller)
32 //
33 // * Use EnqueueRequestForOwner to reconcile the owner of the object the event is for
34 // - do this for events for the types the Controller creates.  (e.g. ReplicaSets created by a Deployment Controller)
35 //
36 // * Use EnqueueRequestFromMapFunc to transform an event for an object to a reconcile of an object
37 // of a different type - do this for events for types the Controller may be interested in, but doesn't create.
38 // (e.g. If Foo responds to cluster size events, map Node events to Foo objects.)
39 //
40 // Unless you are implementing your own EventHandler, you can ignore the functions on the EventHandler interface.
41 // Most users shouldn't need to implement their own EventHandler.
42 type EventHandler interface {
43         // Create is called in response to an create event - e.g. Pod Creation.
44         Create(event.CreateEvent, workqueue.RateLimitingInterface)
45
46         // Update is called in response to an update event -  e.g. Pod Updated.
47         Update(event.UpdateEvent, workqueue.RateLimitingInterface)
48
49         // Delete is called in response to a delete event - e.g. Pod Deleted.
50         Delete(event.DeleteEvent, workqueue.RateLimitingInterface)
51
52         // Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
53         // external trigger request - e.g. reconcile Autoscaling, or a Webhook.
54         Generic(event.GenericEvent, workqueue.RateLimitingInterface)
55 }
56
57 var _ EventHandler = Funcs{}
58
59 // Funcs implements EventHandler.
60 type Funcs struct {
61         // Create is called in response to an add event.  Defaults to no-op.
62         // RateLimitingInterface is used to enqueue reconcile.Requests.
63         CreateFunc func(event.CreateEvent, workqueue.RateLimitingInterface)
64
65         // Update is called in response to an update event.  Defaults to no-op.
66         // RateLimitingInterface is used to enqueue reconcile.Requests.
67         UpdateFunc func(event.UpdateEvent, workqueue.RateLimitingInterface)
68
69         // Delete is called in response to a delete event.  Defaults to no-op.
70         // RateLimitingInterface is used to enqueue reconcile.Requests.
71         DeleteFunc func(event.DeleteEvent, workqueue.RateLimitingInterface)
72
73         // GenericFunc is called in response to a generic event.  Defaults to no-op.
74         // RateLimitingInterface is used to enqueue reconcile.Requests.
75         GenericFunc func(event.GenericEvent, workqueue.RateLimitingInterface)
76 }
77
78 // Create implements EventHandler
79 func (h Funcs) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) {
80         if h.CreateFunc != nil {
81                 h.CreateFunc(e, q)
82         }
83 }
84
85 // Delete implements EventHandler
86 func (h Funcs) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface) {
87         if h.DeleteFunc != nil {
88                 h.DeleteFunc(e, q)
89         }
90 }
91
92 // Update implements EventHandler
93 func (h Funcs) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface) {
94         if h.UpdateFunc != nil {
95                 h.UpdateFunc(e, q)
96         }
97 }
98
99 // Generic implements EventHandler
100 func (h Funcs) Generic(e event.GenericEvent, q workqueue.RateLimitingInterface) {
101         if h.GenericFunc != nil {
102                 h.GenericFunc(e, q)
103         }
104 }