-
Notifications
You must be signed in to change notification settings - Fork 410
/
render_controller.go
548 lines (464 loc) · 17 KB
/
render_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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package render
import (
"fmt"
"reflect"
"time"
"github.com/golang/glog"
"github.com/openshift/machine-config-operator/lib/resourceapply"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
"github.com/openshift/machine-config-operator/pkg/controller/common"
mcfgclientset "github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned"
"github.com/openshift/machine-config-operator/pkg/generated/clientset/versioned/scheme"
mcfginformersv1 "github.com/openshift/machine-config-operator/pkg/generated/informers/externalversions/machineconfiguration.openshift.io/v1"
mcfglistersv1 "github.com/openshift/machine-config-operator/pkg/generated/listers/machineconfiguration.openshift.io/v1"
"github.com/openshift/machine-config-operator/pkg/version"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
)
const (
// maxRetries is the number of times a machineconfig pool will be retried before it is dropped out of the queue.
// With the current rate-limiter in use (5ms*2^(maxRetries-1)) the following numbers represent the times
// a machineconfig pool is going to be requeued:
//
// 5ms, 10ms, 20ms, 40ms, 80ms, 160ms, 320ms, 640ms, 1.3s, 2.6s, 5.1s, 10.2s, 20.4s, 41s, 82s
maxRetries = 15
// renderDelay is a pause to avoid churn in MachineConfigs; see
// https://github.com/openshift/machine-config-operator/issues/301
renderDelay = 5 * time.Second
)
var (
// controllerKind contains the schema.GroupVersionKind for this controller type.
controllerKind = mcfgv1.SchemeGroupVersion.WithKind("MachineConfigPool")
machineconfigKind = mcfgv1.SchemeGroupVersion.WithKind("MachineConfig")
)
// Controller defines the render controller.
type Controller struct {
client mcfgclientset.Interface
eventRecorder record.EventRecorder
syncHandler func(mcp string) error
enqueueMachineConfigPool func(*mcfgv1.MachineConfigPool)
mcpLister mcfglistersv1.MachineConfigPoolLister
mcLister mcfglistersv1.MachineConfigLister
mcpListerSynced cache.InformerSynced
mcListerSynced cache.InformerSynced
queue workqueue.RateLimitingInterface
}
// New returns a new render controller.
func New(
mcpInformer mcfginformersv1.MachineConfigPoolInformer,
mcInformer mcfginformersv1.MachineConfigInformer,
kubeClient clientset.Interface,
mcfgClient mcfgclientset.Interface,
) *Controller {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&corev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
ctrl := &Controller{
client: mcfgClient,
eventRecorder: eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "machineconfigcontroller-rendercontroller"}),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "machineconfigcontroller-rendercontroller"),
}
mcpInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.addMachineConfigPool,
UpdateFunc: ctrl.updateMachineConfigPool,
DeleteFunc: ctrl.deleteMachineConfigPool,
})
mcInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.addMachineConfig,
UpdateFunc: ctrl.updateMachineConfig,
DeleteFunc: ctrl.deleteMachineConfig,
})
ctrl.syncHandler = ctrl.syncMachineConfigPool
ctrl.enqueueMachineConfigPool = ctrl.enqueueDefault
ctrl.mcpLister = mcpInformer.Lister()
ctrl.mcLister = mcInformer.Lister()
ctrl.mcpListerSynced = mcpInformer.Informer().HasSynced
ctrl.mcListerSynced = mcInformer.Informer().HasSynced
return ctrl
}
// Run executes the render controller.
func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer ctrl.queue.ShutDown()
glog.Info("Starting MachineConfigController-RenderController")
defer glog.Info("Shutting down MachineConfigController-RenderController")
if !cache.WaitForCacheSync(stopCh, ctrl.mcpListerSynced, ctrl.mcListerSynced) {
return
}
for i := 0; i < workers; i++ {
go wait.Until(ctrl.worker, time.Second, stopCh)
}
<-stopCh
}
func (ctrl *Controller) addMachineConfigPool(obj interface{}) {
pool := obj.(*mcfgv1.MachineConfigPool)
glog.V(4).Infof("Adding MachineConfigPool %s", pool.Name)
ctrl.enqueueMachineConfigPool(pool)
}
func (ctrl *Controller) updateMachineConfigPool(old, cur interface{}) {
oldPool := old.(*mcfgv1.MachineConfigPool)
curPool := cur.(*mcfgv1.MachineConfigPool)
glog.V(4).Infof("Updating MachineConfigPool %s", oldPool.Name)
ctrl.enqueueMachineConfigPool(curPool)
}
func (ctrl *Controller) deleteMachineConfigPool(obj interface{}) {
pool, ok := obj.(*mcfgv1.MachineConfigPool)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj))
return
}
pool, ok = tombstone.Obj.(*mcfgv1.MachineConfigPool)
if !ok {
utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not a MachineConfigPool %#v", obj))
return
}
}
glog.V(4).Infof("Deleting MachineConfigPool %s", pool.Name)
// TODO(abhinavdahiya): handle deletes.
}
func (ctrl *Controller) addMachineConfig(obj interface{}) {
mc := obj.(*mcfgv1.MachineConfig)
if mc.DeletionTimestamp != nil {
ctrl.deleteMachineConfig(mc)
return
}
if controllerRef := metav1.GetControllerOf(mc); controllerRef != nil {
pool := ctrl.resolveControllerRef(controllerRef)
if pool == nil {
return
}
glog.V(4).Infof("MachineConfig %s added", mc.Name)
ctrl.enqueueMachineConfigPool(pool)
return
}
pools, err := ctrl.getPoolsForMachineConfig(mc)
if err != nil {
glog.Errorf("error finding pools for machineconfig: %v", err)
return
}
glog.V(4).Infof("MachineConfig %s added", mc.Name)
for _, p := range pools {
ctrl.enqueueMachineConfigPool(p)
}
}
func (ctrl *Controller) updateMachineConfig(old, cur interface{}) {
oldMC := old.(*mcfgv1.MachineConfig)
curMC := cur.(*mcfgv1.MachineConfig)
curControllerRef := metav1.GetControllerOf(curMC)
oldControllerRef := metav1.GetControllerOf(oldMC)
controllerRefChanged := !reflect.DeepEqual(curControllerRef, oldControllerRef)
if controllerRefChanged && oldControllerRef != nil {
glog.Errorf("machineconfig has changed controller, not allowed.")
return
}
if curControllerRef != nil {
pool := ctrl.resolveControllerRef(curControllerRef)
if pool == nil {
return
}
glog.V(4).Infof("MachineConfig %s updated", curMC.Name)
ctrl.enqueueMachineConfigPool(pool)
return
}
pools, err := ctrl.getPoolsForMachineConfig(curMC)
if err != nil {
glog.Errorf("error finding pools for machineconfig: %v", err)
return
}
glog.V(4).Infof("MachineConfig %s updated", curMC.Name)
for _, p := range pools {
ctrl.enqueueMachineConfigPool(p)
}
}
func (ctrl *Controller) deleteMachineConfig(obj interface{}) {
mc, ok := obj.(*mcfgv1.MachineConfig)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("Couldn't get object from tombstone %#v", obj))
return
}
mc, ok = tombstone.Obj.(*mcfgv1.MachineConfig)
if !ok {
utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not a MachineConfig %#v", obj))
return
}
}
if controllerRef := metav1.GetControllerOf(mc); controllerRef != nil {
pool := ctrl.resolveControllerRef(controllerRef)
if pool == nil {
return
}
glog.V(4).Infof("MachineConfig %s deleted", mc.Name)
ctrl.enqueueMachineConfigPool(pool)
return
}
pools, err := ctrl.getPoolsForMachineConfig(mc)
if err != nil {
glog.Errorf("error finding pools for machineconfig: %v", err)
return
}
glog.V(4).Infof("MachineConfig %s deleted", mc.Name)
for _, p := range pools {
ctrl.enqueueMachineConfigPool(p)
}
}
func (ctrl *Controller) resolveControllerRef(controllerRef *metav1.OwnerReference) *mcfgv1.MachineConfigPool {
// We can't look up by UID, so look up by Name and then verify UID.
// Don't even try to look up by Name if it's the wrong Kind.
if controllerRef.Kind != controllerKind.Kind {
return nil
}
pool, err := ctrl.mcpLister.Get(controllerRef.Name)
if err != nil {
return nil
}
if pool.UID != controllerRef.UID {
// The controller we found with this Name is not the same one that the
// ControllerRef points to.
return nil
}
return pool
}
func (ctrl *Controller) getPoolsForMachineConfig(config *mcfgv1.MachineConfig) ([]*mcfgv1.MachineConfigPool, error) {
if len(config.Labels) == 0 {
return nil, fmt.Errorf("no MachineConfigPool found for MachineConfig %v because it has no labels", config.Name)
}
pList, err := ctrl.mcpLister.List(labels.Everything())
if err != nil {
return nil, err
}
var pools []*mcfgv1.MachineConfigPool
for _, p := range pList {
selector, err := metav1.LabelSelectorAsSelector(p.Spec.MachineConfigSelector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
}
// If a pool with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(config.Labels)) {
continue
}
pools = append(pools, p)
}
if len(pools) == 0 {
return nil, fmt.Errorf("could not find any MachineConfigPool set for MachineConfig %s with labels: %v", config.Name, config.Labels)
}
return pools, nil
}
func (ctrl *Controller) enqueue(pool *mcfgv1.MachineConfigPool) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(pool)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %#v: %v", pool, err))
return
}
ctrl.queue.Add(key)
}
func (ctrl *Controller) enqueueRateLimited(pool *mcfgv1.MachineConfigPool) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(pool)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %#v: %v", pool, err))
return
}
ctrl.queue.AddRateLimited(key)
}
// enqueueAfter will enqueue a pool after the provided amount of time.
func (ctrl *Controller) enqueueAfter(pool *mcfgv1.MachineConfigPool, after time.Duration) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(pool)
if err != nil {
utilruntime.HandleError(fmt.Errorf("Couldn't get key for object %#v: %v", pool, err))
return
}
ctrl.queue.AddAfter(key, after)
}
// enqueueDefault calls a default enqueue function
func (ctrl *Controller) enqueueDefault(pool *mcfgv1.MachineConfigPool) {
ctrl.enqueueAfter(pool, renderDelay)
}
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
// It enforces that the syncHandler is never invoked concurrently with the same key.
func (ctrl *Controller) worker() {
for ctrl.processNextWorkItem() {
}
}
func (ctrl *Controller) processNextWorkItem() bool {
key, quit := ctrl.queue.Get()
if quit {
return false
}
defer ctrl.queue.Done(key)
err := ctrl.syncHandler(key.(string))
ctrl.handleErr(err, key)
return true
}
func (ctrl *Controller) handleErr(err error, key interface{}) {
if err == nil {
ctrl.queue.Forget(key)
return
}
if ctrl.queue.NumRequeues(key) < maxRetries {
glog.V(2).Infof("Error syncing machineconfigpool %v: %v", key, err)
ctrl.queue.AddRateLimited(key)
return
}
utilruntime.HandleError(err)
glog.V(2).Infof("Dropping machineconfigpool %q out of the queue: %v", key, err)
ctrl.queue.Forget(key)
ctrl.queue.AddAfter(key, 1*time.Minute)
}
// syncMachineConfigPool will sync the machineconfig pool with the given key.
// This function is not meant to be invoked concurrently with the same key.
func (ctrl *Controller) syncMachineConfigPool(key string) error {
startTime := time.Now()
glog.V(4).Infof("Started syncing machineconfigpool %q (%v)", key, startTime)
defer func() {
glog.V(4).Infof("Finished syncing machineconfigpool %q (%v)", key, time.Since(startTime))
}()
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
machineconfigpool, err := ctrl.mcpLister.Get(name)
if errors.IsNotFound(err) {
glog.V(2).Infof("MachineConfigPool %v has been deleted", key)
return nil
}
if err != nil {
return err
}
// Deep-copy otherwise we are mutating our cache.
// TODO: Deep-copy only when needed.
pool := machineconfigpool.DeepCopy()
everything := metav1.LabelSelector{}
if reflect.DeepEqual(pool.Spec.MachineConfigSelector, &everything) {
ctrl.eventRecorder.Eventf(pool, v1.EventTypeWarning, "SelectingAll", "This machineconfigpool is selecting all machineconfigs. A non-empty selector is require.")
return nil
}
selector, err := metav1.LabelSelectorAsSelector(pool.Spec.MachineConfigSelector)
if err != nil {
return err
}
mcs, err := ctrl.mcLister.List(selector)
if err != nil {
return err
}
if len(mcs) == 0 {
return fmt.Errorf("no MachineConfigs found matching selector %v", selector)
}
return ctrl.syncGeneratedMachineConfig(pool, mcs)
}
// This function will eventually contain a sane garbage collection policy for rendered MachineConfigs;
// see https://github.com/openshift/machine-config-operator/issues/301
// It will probably involve making sure we're only GCing a config after all nodes don't have it
// in either desired or current config.
func (ctrl *Controller) garbageCollectRenderedConfigs(pool *mcfgv1.MachineConfigPool) error {
// Temporarily until https://github.com/openshift/machine-config-operator/pull/318
// which depends on the strategy for https://github.com/openshift/machine-config-operator/issues/301
return nil
}
func (ctrl *Controller) syncGeneratedMachineConfig(pool *mcfgv1.MachineConfigPool, configs []*mcfgv1.MachineConfig) error {
if len(configs) == 0 {
return nil
}
generated, err := generateMachineConfig(pool, configs)
if err != nil {
return err
}
source := []v1.ObjectReference{}
for _, cfg := range configs {
source = append(source, v1.ObjectReference{Kind: machineconfigKind.Kind, Name: cfg.GetName(), APIVersion: machineconfigKind.GroupVersion().String()})
}
_, err = ctrl.mcLister.Get(generated.Name)
if apierrors.IsNotFound(err) {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Create(generated)
glog.V(2).Infof("Generated machineconfig %s from %d configs: %s", generated.Name, len(source), source)
}
if err != nil {
return err
}
if pool.Status.Configuration.Name == generated.Name {
_, _, err = resourceapply.ApplyMachineConfig(ctrl.client.MachineconfigurationV1(), generated)
return err
}
pool.Status.Configuration.Name = generated.Name
pool.Status.Configuration.Source = source
_, err = ctrl.client.MachineconfigurationV1().MachineConfigPools().UpdateStatus(pool)
if err != nil {
return err
}
if err := ctrl.garbageCollectRenderedConfigs(pool); err != nil {
return err
}
return nil
}
func generateMachineConfig(pool *mcfgv1.MachineConfigPool, configs []*mcfgv1.MachineConfig) (*mcfgv1.MachineConfig, error) {
merged := mcfgv1.MergeMachineConfigs(configs)
hashedName, err := getMachineConfigHashedName(pool, merged)
if err != nil {
return nil, err
}
oref := metav1.NewControllerRef(pool, controllerKind)
merged.SetName(hashedName)
merged.SetOwnerReferences([]metav1.OwnerReference{*oref})
if merged.Annotations == nil {
merged.Annotations = map[string]string{}
}
merged.Annotations[common.GeneratedByControllerVersionAnnotationKey] = version.Version.String()
return merged, nil
}
// RunBootstrap runs the render controller in bootstrap mode.
// For each pool, it matches the machineconfigs based on label selector and
// returns the generated machineconfigs and pool with CurrentMachineConfig status field set.
func RunBootstrap(pools []*mcfgv1.MachineConfigPool, configs []*mcfgv1.MachineConfig) ([]*mcfgv1.MachineConfigPool, []*mcfgv1.MachineConfig, error) {
var (
opools []*mcfgv1.MachineConfigPool
oconfigs []*mcfgv1.MachineConfig
)
for idx := range pools {
pool := pools[idx]
pcs, err := getMachineConfigsForPool(pool, configs)
if err != nil {
return nil, nil, err
}
generated, err := generateMachineConfig(pool, pcs)
if err != nil {
return nil, nil, err
}
pool.Status.Configuration.Name = generated.Name
opools = append(opools, pool)
oconfigs = append(oconfigs, generated)
}
return opools, oconfigs, nil
}
// getMachineConfigsForPool returns configs that match label from configs for a pool.
func getMachineConfigsForPool(pool *mcfgv1.MachineConfigPool, configs []*mcfgv1.MachineConfig) ([]*mcfgv1.MachineConfig, error) {
selector, err := metav1.LabelSelectorAsSelector(pool.Spec.MachineConfigSelector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
}
var out []*mcfgv1.MachineConfig
// If a pool with a nil or empty selector creeps in, it should match nothing
if selector.Empty() {
return out, nil
}
for idx, config := range configs {
if selector.Matches(labels.Set(config.Labels)) {
out = append(out, configs[idx])
}
}
if len(out) == 0 {
return nil, fmt.Errorf("couldn't find any MachineConfigs for pool: %v", pool.Name)
}
return out, nil
}