Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / controller / controller.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 controller
18
19 import (
20         "fmt"
21
22         "k8s.io/client-go/util/workqueue"
23         "sigs.k8s.io/controller-runtime/pkg/handler"
24         "sigs.k8s.io/controller-runtime/pkg/internal/controller"
25         "sigs.k8s.io/controller-runtime/pkg/manager"
26         "sigs.k8s.io/controller-runtime/pkg/predicate"
27         "sigs.k8s.io/controller-runtime/pkg/reconcile"
28         "sigs.k8s.io/controller-runtime/pkg/source"
29 )
30
31 // Options are the arguments for creating a new Controller
32 type Options struct {
33         // MaxConcurrentReconciles is the maximum number of concurrent Reconciles which can be run. Defaults to 1.
34         MaxConcurrentReconciles int
35
36         // Reconciler reconciles an object
37         Reconciler reconcile.Reconciler
38 }
39
40 // Controller implements a Kubernetes API.  A Controller manages a work queue fed reconcile.Requests
41 // from source.Sources.  Work is performed through the reconcile.Reconciler for each enqueued item.
42 // Work typically is reads and writes Kubernetes objects to make the system state match the state specified
43 // in the object Spec.
44 type Controller interface {
45         // Reconciler is called to Reconciler an object by Namespace/Name
46         reconcile.Reconciler
47
48         // Watch takes events provided by a Source and uses the EventHandler to enqueue reconcile.Requests in
49         // response to the events.
50         //
51         // Watch may be provided one or more Predicates to filter events before they are given to the EventHandler.
52         // Events will be passed to the EventHandler iff all provided Predicates evaluate to true.
53         Watch(src source.Source, eventhandler handler.EventHandler, predicates ...predicate.Predicate) error
54
55         // Start starts the controller.  Start blocks until stop is closed or a controller has an error starting.
56         Start(stop <-chan struct{}) error
57 }
58
59 // New returns a new Controller registered with the Manager.  The Manager will ensure that shared Caches have
60 // been synced before the Controller is Started.
61 func New(name string, mgr manager.Manager, options Options) (Controller, error) {
62         if options.Reconciler == nil {
63                 return nil, fmt.Errorf("must specify Reconciler")
64         }
65
66         if len(name) == 0 {
67                 return nil, fmt.Errorf("must specify Name for Controller")
68         }
69
70         if options.MaxConcurrentReconciles <= 0 {
71                 options.MaxConcurrentReconciles = 1
72         }
73
74         // Inject dependencies into Reconciler
75         if err := mgr.SetFields(options.Reconciler); err != nil {
76                 return nil, err
77         }
78
79         // Create controller with dependencies set
80         c := &controller.Controller{
81                 Do:       options.Reconciler,
82                 Cache:    mgr.GetCache(),
83                 Config:   mgr.GetConfig(),
84                 Scheme:   mgr.GetScheme(),
85                 Client:   mgr.GetClient(),
86                 Recorder: mgr.GetRecorder(name),
87                 Queue:    workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
88                 MaxConcurrentReconciles: options.MaxConcurrentReconciles,
89                 Name: name,
90         }
91
92         // Add the controller as a Manager components
93         return c, mgr.Add(c)
94 }