-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
334 lines (281 loc) · 9.42 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"fmt"
"reflect"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
corelisters "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
const syncAnnotation string = "konfig-syncer"
// Controller is responsible for watching Secret/Configmap events and adding them to WQ for processing
type Controller struct {
kubeclientset kubernetes.Interface
configMapsLister corelisters.ConfigMapLister
configMapsSynced cache.InformerSynced
configMapWorkqueue workqueue.RateLimitingInterface
deletedConfigMapIndexer cache.Indexer
secretsLister corelisters.SecretLister
secretsSynced cache.InformerSynced
secretWorkqueue workqueue.RateLimitingInterface
deletedSecretIndexer cache.Indexer
namespacesLister corelisters.NamespaceLister
namespacesSynced cache.InformerSynced
namespaceWorkqueue workqueue.RateLimitingInterface
}
// NewController creates controller FIXME proper comment
func NewController(
kubeclientset kubernetes.Interface,
configMapInformer coreinformers.ConfigMapInformer,
secretInformer coreinformers.SecretInformer,
namespaceInformer coreinformers.NamespaceInformer) *Controller {
controller := &Controller{
kubeclientset: kubeclientset,
configMapsLister: configMapInformer.Lister(),
configMapsSynced: configMapInformer.Informer().HasSynced,
configMapWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ConfigMaps"),
deletedConfigMapIndexer: cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{}),
secretsLister: secretInformer.Lister(),
secretsSynced: secretInformer.Informer().HasSynced,
secretWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Secrets"),
deletedSecretIndexer: cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{}),
namespacesLister: namespaceInformer.Lister(),
namespacesSynced: namespaceInformer.Informer().HasSynced,
namespaceWorkqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Namespaces"),
}
secretInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(new interface{}) {
s := new.(*corev1.Secret)
if _, ok := s.Annotations[syncAnnotation]; ok {
log.Debug("Secret added to workqueue")
controller.enqueueSecret(new)
}
},
UpdateFunc: func(old, new interface{}) {
news := new.(*corev1.Secret)
olds := old.(*corev1.Secret)
nanno, newHasAnno := news.Annotations[syncAnnotation]
oanno, oldHasAnno := olds.Annotations[syncAnnotation]
if newHasAnno && (!oldHasAnno || !reflect.DeepEqual(news.Data, olds.Data)) {
log.Debug("Secret updated to have sync annotation or data changed")
controller.enqueueSecret(news)
} else if !newHasAnno && oldHasAnno {
log.Debug("Sync annotation was removed from Secret")
controller.deletedSecretIndexer.Add(olds)
controller.enqueueSecret(news)
} else {
if !reflect.DeepEqual(nanno, oanno) {
log.Debug("Sync annotation was was changed on secret")
controller.deletedSecretIndexer.Add(olds)
controller.enqueueSecret(news)
}
}
},
DeleteFunc: func(obj interface{}) {
log.Debug("Secret deleted")
s := obj.(*corev1.Secret)
if _, ok := s.Annotations[syncAnnotation]; ok {
controller.enqueueSecret(obj)
controller.deletedSecretIndexer.Add(obj)
}
},
})
configMapInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(new interface{}) {
s := new.(*corev1.ConfigMap)
if _, ok := s.Annotations[syncAnnotation]; ok {
log.Debug("ConfigMap added to workqueue")
controller.enqueueConfigMap(s)
}
},
UpdateFunc: func(old, new interface{}) {
news := new.(*corev1.ConfigMap)
olds := old.(*corev1.ConfigMap)
nanno, newHasAnno := news.Annotations[syncAnnotation]
oanno, oldHasAnno := olds.Annotations[syncAnnotation]
if newHasAnno && (!oldHasAnno || !reflect.DeepEqual(news.Data, olds.Data)) {
log.Debug("ConfigMap updated to have sync annotation or data changed")
controller.enqueueConfigMap(news)
} else if !newHasAnno && oldHasAnno {
log.Debug("Sync annotation was removed from ConfigMap")
controller.deletedConfigMapIndexer.Add(olds)
} else if !reflect.DeepEqual(nanno, oanno) {
log.Debug("Sync annotation was was changed on ConfigMap")
controller.deletedConfigMapIndexer.Add(olds)
controller.enqueueConfigMap(news)
}
},
DeleteFunc: func(obj interface{}) {
s := obj.(*corev1.ConfigMap)
if _, ok := s.Annotations[syncAnnotation]; ok {
log.Debug("ConfigMap deleted")
controller.enqueueConfigMap(obj)
controller.deletedConfigMapIndexer.Add(obj)
}
},
})
namespaceInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(new interface{}) {
log.Debug("Namespace added to workqueue")
controller.enqueueNamespace(new)
},
UpdateFunc: func(old, new interface{}) {
newNs := new.(*corev1.Namespace)
oldNs := old.(*corev1.Namespace)
if newNs.Status.Phase != corev1.NamespaceTerminating && !reflect.DeepEqual(newNs.Labels, oldNs.Labels) {
log.Debug("Namespace added to workqueue on update")
controller.enqueueNamespace(newNs)
}
},
})
return controller
}
// Run spins up the controller
func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error {
defer utilruntime.HandleCrash()
defer c.configMapWorkqueue.ShutDown()
defer c.secretWorkqueue.ShutDown()
defer c.namespaceWorkqueue.ShutDown()
// Wait for the caches to be synced before starting workers
log.Info("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, c.configMapsSynced, c.secretsSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
log.Info("Starting workers")
for i := 0; i < threadiness; i++ {
go wait.Until(c.runConfigMapWorker, time.Second, stopCh)
go wait.Until(c.runSecretWorker, time.Second, stopCh)
go wait.Until(c.runNamespaceWorker, time.Second, stopCh)
}
log.Info("Started workers")
<-stopCh
log.Info("Shutting down workers")
return nil
}
func (c *Controller) enqueueSecret(obj interface{}) {
var key string
var err error
if key, err = cache.DeletionHandlingMetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.secretWorkqueue.AddRateLimited(key)
}
func (c *Controller) enqueueConfigMap(obj interface{}) {
var key string
var err error
if key, err = cache.DeletionHandlingMetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.configMapWorkqueue.AddRateLimited(key)
}
func (c *Controller) enqueueNamespace(obj interface{}) {
var key string
var err error
if key, err = cache.DeletionHandlingMetaNamespaceKeyFunc(obj); err != nil {
runtime.HandleError(err)
return
}
c.namespaceWorkqueue.AddRateLimited(key)
}
func (c *Controller) runConfigMapWorker() {
for c.processNextConfigMap() {
}
}
func (c *Controller) runSecretWorker() {
for c.processNextSecret() {
}
}
func (c *Controller) runNamespaceWorker() {
for c.processNextNamespace() {
}
}
func (c *Controller) processNextConfigMap() bool {
obj, shutdown := c.configMapWorkqueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer c.configMapWorkqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
c.configMapWorkqueue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := c.syncConfigMap(key); err != nil {
c.configMapWorkqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.configMapWorkqueue.Forget(obj)
return nil
}(obj)
if err != nil {
utilruntime.HandleError(err)
return true
}
return true
}
func (c *Controller) processNextSecret() bool {
obj, shutdown := c.secretWorkqueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer c.secretWorkqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
c.secretWorkqueue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := c.syncSecret(key); err != nil {
c.secretWorkqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.secretWorkqueue.Forget(obj)
return nil
}(obj)
if err != nil {
utilruntime.HandleError(err)
return true
}
return true
}
func (c *Controller) processNextNamespace() bool {
obj, shutdown := c.namespaceWorkqueue.Get()
if shutdown {
return false
}
err := func(obj interface{}) error {
defer c.namespaceWorkqueue.Done(obj)
var key string
var ok bool
if key, ok = obj.(string); !ok {
c.namespaceWorkqueue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected string in workqueue but got %#v", obj))
return nil
}
if err := c.syncNamespace(key); err != nil {
c.namespaceWorkqueue.AddRateLimited(key)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error())
}
c.namespaceWorkqueue.Forget(obj)
return nil
}(obj)
if err != nil {
utilruntime.HandleError(err)
return true
}
return true
}