forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
182 lines (158 loc) · 5.26 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2017 Red Hat, Inc.
*
*/
package kubecli
import (
"runtime/debug"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"kubevirt.io/kubevirt/pkg/logging"
)
// NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
func NewListWatchFromClient(c cache.Getter, resource string, namespace string, fieldSelector fields.Selector, labelSelector labels.Selector) *cache.ListWatch {
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
return c.Get().
Namespace(namespace).
Resource(resource).
VersionedParams(&options, metav1.ParameterCodec).
FieldsSelectorParam(fieldSelector).
LabelsSelectorParam(labelSelector).
Do().
Get()
}
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
return c.Get().
Prefix("watch").
Namespace(namespace).
Resource(resource).
VersionedParams(&options, metav1.ParameterCodec).
FieldsSelectorParam(fieldSelector).
LabelsSelectorParam(labelSelector).
Watch()
}
return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
}
func HandlePanic() {
if r := recover(); r != nil {
logging.DefaultLogger().Critical().Log("stacktrace", debug.Stack(), "msg", r)
}
}
func NewResourceEventHandlerFuncsForWorkqueue(queue workqueue.RateLimitingInterface) cache.ResourceEventHandlerFuncs {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
UpdateFunc: func(old interface{}, new interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(new)
if err == nil {
queue.Add(key)
}
},
DeleteFunc: func(obj interface{}) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
if err == nil {
queue.Add(key)
}
},
}
}
func NewResourceEventHandlerFuncsForFunc(f func(interface{})) cache.ResourceEventHandlerFuncs {
return cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
f(obj)
},
UpdateFunc: func(old interface{}, new interface{}) {
f(new)
},
DeleteFunc: func(obj interface{}) {
f(obj)
},
}
}
type Controller struct {
indexer cache.Store
queue workqueue.RateLimitingInterface
informer cache.Controller
dispatch ControllerDispatch
}
func NewController(lw cache.ListerWatcher, queue workqueue.RateLimitingInterface, objType runtime.Object, dispatch ControllerDispatch) (cache.Store, *Controller) {
indexer, informer := cache.NewIndexerInformer(lw, objType, 0, NewResourceEventHandlerFuncsForWorkqueue(queue), cache.Indexers{})
return NewControllerFromInformer(indexer, informer, queue, dispatch)
}
type ControllerDispatch interface {
Execute( /*cache*/ cache.Store /*queue*/, workqueue.RateLimitingInterface /*key*/, interface{})
}
func NewControllerFromInformer(indexer cache.Store, informer cache.Controller, queue workqueue.RateLimitingInterface, dispatch ControllerDispatch) (cache.Store, *Controller) {
c := &Controller{
informer: informer,
indexer: indexer,
queue: queue,
dispatch: dispatch,
}
return indexer, c
}
type ControllerFunc func(cache.Store, workqueue.RateLimitingInterface, interface{})
func (c *Controller) callControllerFn(s cache.Store, w workqueue.RateLimitingInterface) bool {
quit := !Dequeue(s, w, c.dispatch)
return quit
}
func Dequeue(s cache.Store, w workqueue.RateLimitingInterface, dispatch ControllerDispatch) bool {
key, quit := w.Get()
if quit {
return false
} else {
defer w.Done(key)
dispatch.Execute(s, w, key)
return true
}
}
func (c *Controller) Run(threadiness int, stopCh chan struct{}) {
defer HandlePanic()
defer c.queue.ShutDown()
logging.DefaultLogger().Info().Msg("Starting controller.")
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
}
<-stopCh
logging.DefaultLogger().Info().Msg("Stopping controller.")
}
func (c *Controller) StartInformer(stopCh chan struct{}) {
go c.informer.Run(stopCh)
}
func (c *Controller) WaitForSync(stopCh chan struct{}) {
cache.WaitForCacheSync(stopCh, c.informer.HasSynced)
}
func (c *Controller) runWorker() {
for c.callControllerFn(c.indexer, c.queue) {
}
}
// Shut down the embedded queue. After the shutdown was issued, all items already in the queue will be processed but no
// new items will be accepted. It is possible to wait via #WaitUntilDone() until the last item was processed.
func (c *Controller) ShutDownQueue() {
c.queue.ShutDown()
}