/
container_runtime_config_controller.go
760 lines (658 loc) · 25.8 KB
/
container_runtime_config_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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
package containerruntimeconfig
import (
"encoding/json"
"fmt"
"reflect"
"time"
igntypes "github.com/coreos/ignition/config/v2_2/types"
"github.com/golang/glog"
"github.com/vincent-petithory/dataurl"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/jsonmergepatch"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
coreclientsetv1 "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/retry"
"k8s.io/client-go/util/workqueue"
apicfgv1 "github.com/openshift/api/config/v1"
configclientset "github.com/openshift/client-go/config/clientset/versioned"
cligoinformersv1 "github.com/openshift/client-go/config/informers/externalversions/config/v1"
cligolistersv1 "github.com/openshift/client-go/config/listers/config/v1"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
ctrlcommon "github.com/openshift/machine-config-operator/pkg/controller/common"
mtmpl "github.com/openshift/machine-config-operator/pkg/controller/template"
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"
)
const (
// maxRetries is the number of times a containerruntimeconfig 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
)
var (
// controllerKind contains the schema.GroupVersionKind for this controller type.
controllerKind = mcfgv1.SchemeGroupVersion.WithKind("ContainerRuntimeConfig")
)
var updateBackoff = wait.Backoff{
Steps: 5,
Duration: 100 * time.Millisecond,
Jitter: 1.0,
}
// Controller defines the container runtime config controller.
type Controller struct {
templatesDir string
client mcfgclientset.Interface
configClient configclientset.Interface
eventRecorder record.EventRecorder
syncHandler func(mcp string) error
syncImgHandler func(mcp string) error
enqueueContainerRuntimeConfig func(*mcfgv1.ContainerRuntimeConfig)
ccLister mcfglistersv1.ControllerConfigLister
ccListerSynced cache.InformerSynced
mccrLister mcfglistersv1.ContainerRuntimeConfigLister
mccrListerSynced cache.InformerSynced
imgLister cligolistersv1.ImageLister
imgListerSynced cache.InformerSynced
mcpLister mcfglistersv1.MachineConfigPoolLister
mcpListerSynced cache.InformerSynced
clusterVersionLister cligolistersv1.ClusterVersionLister
clusterVersionListerSynced cache.InformerSynced
queue workqueue.RateLimitingInterface
imgQueue workqueue.RateLimitingInterface
// we need this method to mock out patch calls in unit until https://github.com/openshift/machine-config-operator/pull/611#issuecomment-481397185
// which is probably going to be in kube 1.14
patchContainerRuntimeConfigsFunc func(string, []byte) error
}
// New returns a new container runtime config controller
func New(
templatesDir string,
mcpInformer mcfginformersv1.MachineConfigPoolInformer,
ccInformer mcfginformersv1.ControllerConfigInformer,
mcrInformer mcfginformersv1.ContainerRuntimeConfigInformer,
imgInformer cligoinformersv1.ImageInformer,
clusterVersionInformer cligoinformersv1.ClusterVersionInformer,
kubeClient clientset.Interface,
mcfgClient mcfgclientset.Interface,
configClient configclientset.Interface,
) *Controller {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(&coreclientsetv1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
ctrl := &Controller{
templatesDir: templatesDir,
client: mcfgClient,
configClient: configClient,
eventRecorder: eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "machineconfigcontroller-containerruntimeconfigcontroller"}),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "machineconfigcontroller-containerruntimeconfigcontroller"),
imgQueue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()),
}
mcrInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.addContainerRuntimeConfig,
UpdateFunc: ctrl.updateContainerRuntimeConfig,
DeleteFunc: ctrl.deleteContainerRuntimeConfig,
})
imgInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.imageConfAdded,
UpdateFunc: ctrl.imageConfUpdated,
DeleteFunc: ctrl.imageConfDeleted,
})
ctrl.syncHandler = ctrl.syncContainerRuntimeConfig
ctrl.syncImgHandler = ctrl.syncImageConfig
ctrl.enqueueContainerRuntimeConfig = ctrl.enqueue
ctrl.mcpLister = mcpInformer.Lister()
ctrl.mcpListerSynced = mcpInformer.Informer().HasSynced
ctrl.ccLister = ccInformer.Lister()
ctrl.ccListerSynced = ccInformer.Informer().HasSynced
ctrl.mccrLister = mcrInformer.Lister()
ctrl.mccrListerSynced = mcrInformer.Informer().HasSynced
ctrl.imgLister = imgInformer.Lister()
ctrl.imgListerSynced = imgInformer.Informer().HasSynced
ctrl.clusterVersionLister = clusterVersionInformer.Lister()
ctrl.clusterVersionListerSynced = clusterVersionInformer.Informer().HasSynced
ctrl.patchContainerRuntimeConfigsFunc = ctrl.patchContainerRuntimeConfigs
return ctrl
}
// Run executes the container runtime config controller.
func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer ctrl.queue.ShutDown()
defer ctrl.imgQueue.ShutDown()
if !cache.WaitForCacheSync(stopCh, ctrl.mcpListerSynced, ctrl.mccrListerSynced, ctrl.ccListerSynced,
ctrl.imgListerSynced, ctrl.clusterVersionListerSynced) {
return
}
glog.Info("Starting MachineConfigController-ContainerRuntimeConfigController")
defer glog.Info("Shutting down MachineConfigController-ContainerRuntimeConfigController")
for i := 0; i < workers; i++ {
go wait.Until(ctrl.worker, time.Second, stopCh)
}
// Just need one worker for the image config
go wait.Until(ctrl.imgWorker, time.Second, stopCh)
<-stopCh
}
func ctrConfigTriggerObjectChange(old, new *mcfgv1.ContainerRuntimeConfig) bool {
if old.DeletionTimestamp != new.DeletionTimestamp {
return true
}
if !reflect.DeepEqual(old.Spec, new.Spec) {
return true
}
return false
}
func (ctrl *Controller) imageConfAdded(obj interface{}) {
ctrl.imgQueue.Add("openshift-config")
}
func (ctrl *Controller) imageConfUpdated(oldObj, newObj interface{}) {
ctrl.imgQueue.Add("openshift-config")
}
func (ctrl *Controller) imageConfDeleted(obj interface{}) {
ctrl.imgQueue.Add("openshift-config")
}
func (ctrl *Controller) updateContainerRuntimeConfig(oldObj, newObj interface{}) {
oldCtrCfg := oldObj.(*mcfgv1.ContainerRuntimeConfig)
newCtrCfg := newObj.(*mcfgv1.ContainerRuntimeConfig)
if ctrConfigTriggerObjectChange(oldCtrCfg, newCtrCfg) {
glog.V(4).Infof("Update ContainerRuntimeConfig %s", oldCtrCfg.Name)
ctrl.enqueueContainerRuntimeConfig(newCtrCfg)
}
}
func (ctrl *Controller) addContainerRuntimeConfig(obj interface{}) {
cfg := obj.(*mcfgv1.ContainerRuntimeConfig)
glog.V(4).Infof("Adding ContainerRuntimeConfig %s", cfg.Name)
ctrl.enqueueContainerRuntimeConfig(cfg)
}
func (ctrl *Controller) deleteContainerRuntimeConfig(obj interface{}) {
cfg, ok := obj.(*mcfgv1.ContainerRuntimeConfig)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
utilruntime.HandleError(fmt.Errorf("couldn't get object from tombstone %#v", obj))
return
}
cfg, ok = tombstone.Obj.(*mcfgv1.ContainerRuntimeConfig)
if !ok {
utilruntime.HandleError(fmt.Errorf("tombstone contained object that is not a ContainerRuntimeConfig %#v", obj))
return
}
}
ctrl.cascadeDelete(cfg)
glog.V(4).Infof("Deleted ContainerRuntimeConfig %s and restored default config", cfg.Name)
}
func (ctrl *Controller) cascadeDelete(cfg *mcfgv1.ContainerRuntimeConfig) error {
if len(cfg.GetFinalizers()) == 0 {
return nil
}
mcName := cfg.GetFinalizers()[0]
err := ctrl.client.MachineconfigurationV1().MachineConfigs().Delete(mcName, &metav1.DeleteOptions{})
if err != nil && !errors.IsNotFound(err) {
return err
}
if err := ctrl.popFinalizerFromContainerRuntimeConfig(cfg); err != nil {
return err
}
return nil
}
func (ctrl *Controller) enqueue(cfg *mcfgv1.ContainerRuntimeConfig) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cfg)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", cfg, err))
return
}
ctrl.queue.Add(key)
}
func (ctrl *Controller) enqueueRateLimited(cfg *mcfgv1.ContainerRuntimeConfig) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cfg)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", cfg, err))
return
}
ctrl.queue.AddRateLimited(key)
}
// 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) imgWorker() {
for ctrl.processNextImgWorkItem() {
}
}
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) processNextImgWorkItem() bool {
key, quit := ctrl.imgQueue.Get()
if quit {
return false
}
defer ctrl.imgQueue.Done(key)
err := ctrl.syncImgHandler(key.(string))
ctrl.handleImgErr(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 containerruntimeconfig %v: %v", key, err)
ctrl.queue.AddRateLimited(key)
return
}
utilruntime.HandleError(err)
glog.V(2).Infof("Dropping containerruntimeconfig %q out of the queue: %v", key, err)
ctrl.queue.Forget(key)
ctrl.queue.AddAfter(key, 1*time.Minute)
}
func (ctrl *Controller) handleImgErr(err error, key interface{}) {
if err == nil {
ctrl.imgQueue.Forget(key)
return
}
if ctrl.imgQueue.NumRequeues(key) < maxRetries {
glog.V(2).Infof("Error syncing image config %v: %v", key, err)
ctrl.imgQueue.AddRateLimited(key)
return
}
utilruntime.HandleError(err)
glog.V(2).Infof("Dropping image config %q out of the queue: %v", key, err)
ctrl.imgQueue.Forget(key)
ctrl.imgQueue.AddAfter(key, 1*time.Minute)
}
// generateOriginalContainerRuntimeConfigs returns rendered default storage, and crio config files
func (ctrl *Controller) generateOriginalContainerRuntimeConfigs(role string) (*igntypes.File, *igntypes.File, *igntypes.File, error) {
cc, err := ctrl.ccLister.Get(ctrlcommon.ControllerConfigName)
if err != nil {
return nil, nil, nil, fmt.Errorf("could not get ControllerConfig %v", err)
}
// Render the default templates
rc := &mtmpl.RenderConfig{ControllerConfigSpec: &cc.Spec}
generatedConfigs, err := mtmpl.GenerateMachineConfigsForRole(rc, role, ctrl.templatesDir)
if err != nil {
return nil, nil, nil, fmt.Errorf("generateMachineConfigsforRole failed with error %s", err)
}
// Find generated storage.config, and crio.config
var (
config, gmcStorageConfig, gmcCRIOConfig, gmcRegistriesConfig *igntypes.File
errStorage, errCRIO, errRegistries error
)
// Find storage config
for _, gmc := range generatedConfigs {
config, errStorage = findStorageConfig(gmc)
if errStorage == nil {
gmcStorageConfig = config
break
}
}
// Find CRIO config
for _, gmc := range generatedConfigs {
config, errCRIO = findCRIOConfig(gmc)
if errCRIO == nil {
gmcCRIOConfig = config
break
}
}
// Find Registries config
for _, gmc := range generatedConfigs {
config, errRegistries = findRegistriesConfig(gmc)
if errRegistries == nil {
gmcRegistriesConfig = config
break
}
}
if errStorage != nil || errCRIO != nil || errRegistries != nil {
return nil, nil, nil, fmt.Errorf("could not generate old container runtime configs: %v, %v, %v", errStorage, errCRIO, errRegistries)
}
return gmcStorageConfig, gmcCRIOConfig, gmcRegistriesConfig, nil
}
func (ctrl *Controller) syncStatusOnly(cfg *mcfgv1.ContainerRuntimeConfig, err error, args ...interface{}) error {
statusUpdateErr := retry.RetryOnConflict(updateBackoff, func() error {
if cfg.GetGeneration() != cfg.Status.ObservedGeneration {
cfg.Status.ObservedGeneration = cfg.GetGeneration()
cfg.Status.Conditions = append(cfg.Status.Conditions, wrapErrorWithCondition(err, args...))
} else if cfg.GetGeneration() == cfg.Status.ObservedGeneration && err == nil {
// If the CR was created before a matching label was added, the CR would be in failure state
// However the observed generation would be the same, so check if err is nil as well
// Which means that, the ctrcfg was finally successfully able to sync. In that case update the status
// to success and clear the previous failure status
cfg.Status.Conditions = []mcfgv1.ContainerRuntimeConfigCondition{wrapErrorWithCondition(err, args...)}
}
_, updateErr := ctrl.client.MachineconfigurationV1().ContainerRuntimeConfigs().UpdateStatus(cfg)
return updateErr
})
// If an error occurred in updating the status just log it
if statusUpdateErr != nil {
glog.Warningf("error updating container runtime config status: %v", statusUpdateErr)
}
// Want to return the actual error received from the sync function
return err
}
// syncContainerRuntimeConfig will sync the ContainerRuntimeconfig with the given key.
// This function is not meant to be invoked concurrently with the same key.
func (ctrl *Controller) syncContainerRuntimeConfig(key string) error {
startTime := time.Now()
glog.V(4).Infof("Started syncing ContainerRuntimeconfig %q (%v)", key, startTime)
defer func() {
glog.V(4).Infof("Finished syncing ContainerRuntimeconfig %q (%v)", key, time.Since(startTime))
}()
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
// Fetch the ContainerRuntimeConfig
cfg, err := ctrl.mccrLister.Get(name)
if errors.IsNotFound(err) {
glog.V(2).Infof("ContainerRuntimeConfig %v has been deleted", key)
return nil
}
if err != nil {
return err
}
// Deep-copy otherwise we are mutating our cache.
cfg = cfg.DeepCopy()
// Check for Deleted ContainerRuntimeConfig and optionally delete finalizers
if cfg.DeletionTimestamp != nil {
if len(cfg.GetFinalizers()) > 0 {
return ctrl.cascadeDelete(cfg)
}
return nil
}
// If we have seen this generation and the sync didn't fail, then skip
if cfg.Status.ObservedGeneration >= cfg.Generation && cfg.Status.Conditions[len(cfg.Status.Conditions)-1].Type == mcfgv1.ContainerRuntimeConfigSuccess {
return nil
}
// Validate the ContainerRuntimeConfig CR
if err := validateUserContainerRuntimeConfig(cfg); err != nil {
return ctrl.syncStatusOnly(cfg, err)
}
// Find all MachineConfigPools
mcpPools, err := ctrl.getPoolsForContainerRuntimeConfig(cfg)
if err != nil {
return ctrl.syncStatusOnly(cfg, err)
}
if len(mcpPools) == 0 {
err := fmt.Errorf("containerRuntimeConfig %v does not match any MachineConfigPools", key)
glog.V(2).Infof("%v", err)
return ctrl.syncStatusOnly(cfg, err)
}
for _, pool := range mcpPools {
role := pool.Name
// Get MachineConfig
managedKey := getManagedKeyCtrCfg(pool)
if err := retry.RetryOnConflict(updateBackoff, func() error {
mc, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Get(managedKey, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return ctrl.syncStatusOnly(cfg, err, "could not find MachineConfig: %v", managedKey)
}
isNotFound := errors.IsNotFound(err)
// Generate the original ContainerRuntimeConfig
originalStorageIgn, originalCRIOIgn, _, err := ctrl.generateOriginalContainerRuntimeConfigs(role)
if err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not generate origin ContainerRuntime Configs: %v", err)
}
var storageTOML, crioTOML []byte
ctrcfg := cfg.Spec.ContainerRuntimeConfig
if ctrcfg.OverlaySize != (resource.Quantity{}) {
storageTOML, err = ctrl.mergeConfigChanges(originalStorageIgn, cfg, updateStorageConfig)
if err != nil {
glog.V(2).Infoln(cfg, err, "error merging user changes to storage.conf: %v", err)
}
}
if ctrcfg.LogLevel != "" || ctrcfg.PidsLimit != 0 || ctrcfg.LogSizeMax != (resource.Quantity{}) {
crioTOML, err = ctrl.mergeConfigChanges(originalCRIOIgn, cfg, updateCRIOConfig)
if err != nil {
glog.V(2).Infoln(cfg, err, "error merging user changes to crio.conf: %v", err)
}
}
if isNotFound {
tempIgnCfg := ctrlcommon.NewIgnConfig()
mc = mtmpl.MachineConfigFromIgnConfig(role, managedKey, &tempIgnCfg)
}
mc.Spec.Config = createNewCtrRuntimeConfigIgnition(storageTOML, crioTOML)
mc.SetAnnotations(map[string]string{
ctrlcommon.GeneratedByControllerVersionAnnotationKey: version.Hash,
})
oref := metav1.NewControllerRef(cfg, controllerKind)
mc.SetOwnerReferences([]metav1.OwnerReference{*oref})
// Create or Update, on conflict retry
if isNotFound {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Create(mc)
} else {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Update(mc)
}
// Add Finalizers to the ContainerRuntimeConfigs
if err := ctrl.addFinalizerToContainerRuntimeConfig(cfg, mc); err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not add finalizers to ContainerRuntimeConfig: %v", err)
}
return err
}); err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not Create/Update MachineConfig: %v", err)
}
glog.Infof("Applied ContainerRuntimeConfig %v on MachineConfigPool %v", key, pool.Name)
}
return ctrl.syncStatusOnly(cfg, nil)
}
// mergeConfigChanges retrieves the original/default config data from the templates, decodes it and merges in the changes given by the Custom Resource.
// It then encodes the new data and returns it.
func (ctrl *Controller) mergeConfigChanges(origFile *igntypes.File, cfg *mcfgv1.ContainerRuntimeConfig, update updateConfigFunc) ([]byte, error) {
dataURL, err := dataurl.DecodeString(origFile.Contents.Source)
if err != nil {
return nil, ctrl.syncStatusOnly(cfg, err, "could not decode original Container Runtime config: %v", err)
}
cfgTOML, err := update(dataURL.Data, cfg.Spec.ContainerRuntimeConfig)
if err != nil {
return nil, ctrl.syncStatusOnly(cfg, err, "could not update container runtime config with new changes: %v", err)
}
return cfgTOML, ctrl.syncStatusOnly(cfg, nil)
}
func (ctrl *Controller) syncImageConfig(key string) error {
startTime := time.Now()
glog.V(4).Infof("Started syncing ImageConfig %q (%v)", key, startTime)
defer func() {
glog.V(4).Infof("Finished syncing ImageConfig %q (%v)", key, time.Since(startTime))
}()
// Fetch the ImageConfig
imgcfg, err := ctrl.imgLister.Get("cluster")
if errors.IsNotFound(err) {
glog.V(2).Infof("ImageConfig 'cluster' does not exist or has been deleted")
return nil
}
if err != nil {
return err
}
// Deep-copy otherwise we are mutating our cache.
imgcfg = imgcfg.DeepCopy()
// Fetch the ClusterVersionConfig needed to get the registry being used by the payload
// so that we can avoid adding that registry to blocked registries in /etc/containers/registries.conf
clusterVersionCfg, err := ctrl.clusterVersionLister.Get("version")
if errors.IsNotFound(err) {
glog.Infof("ClusterVersionConfig 'version' does not exist or has been deleted")
return nil
}
if err != nil {
return err
}
// Go through the registries in the image spec to get and validate the registries
insecureRegs, blockedRegs, err := getValidRegistries(&clusterVersionCfg.Status, &imgcfg.Spec)
if err != nil && err != errParsingReference {
glog.V(2).Infof("%v, skipping....", err)
} else if err == errParsingReference {
return err
}
// Find all the MachineConfig pools
mcpPools, err := ctrl.mcpLister.List(labels.Everything())
if err != nil {
return err
}
for _, pool := range mcpPools {
// To keep track of whether we "actually" got an updated image config
applied := true
role := pool.Name
// Get MachineConfig
managedKey := getManagedKeyReg(pool)
if err := retry.RetryOnConflict(updateBackoff, func() error {
// Generate the original registries config
_, _, originalRegistriesIgn, err := ctrl.generateOriginalContainerRuntimeConfigs(role)
if err != nil {
return fmt.Errorf("could not generate origin ContainerRuntime Configs: %v", err)
}
var registriesTOML []byte
if insecureRegs != nil || blockedRegs != nil {
dataURL, err := dataurl.DecodeString(originalRegistriesIgn.Contents.Source)
if err != nil {
return fmt.Errorf("could not decode original registries config: %v", err)
}
registriesTOML, err = updateRegistriesConfig(dataURL.Data, insecureRegs, blockedRegs)
if err != nil {
return fmt.Errorf("could not update registries config with new changes: %v", err)
}
}
mc, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Get(managedKey, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("could not find MachineConfig: %v", err)
}
isNotFound := errors.IsNotFound(err)
registriesIgn := createNewRegistriesConfigIgnition(registriesTOML)
if !isNotFound && equality.Semantic.DeepEqual(registriesIgn, mc.Spec.Config) {
// if the configuration for the registries is equal, we still need to compare
// the generated controller version because during an upgrade we need a new one
mcCtrlVersion := mc.Annotations[ctrlcommon.GeneratedByControllerVersionAnnotationKey]
if mcCtrlVersion == version.Hash {
applied = false
return nil
}
}
if isNotFound {
tempIgnCfg := ctrlcommon.NewIgnConfig()
mc = mtmpl.MachineConfigFromIgnConfig(role, managedKey, &tempIgnCfg)
}
mc.Spec.Config = registriesIgn
mc.ObjectMeta.Annotations = map[string]string{
ctrlcommon.GeneratedByControllerVersionAnnotationKey: version.Hash,
}
mc.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
{
APIVersion: apicfgv1.SchemeGroupVersion.String(),
Kind: "Image",
Name: imgcfg.Name,
UID: imgcfg.UID,
},
}
// Create or Update, on conflict retry
if isNotFound {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Create(mc)
} else {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Update(mc)
}
return err
}); err != nil {
return fmt.Errorf("could not Create/Update MachineConfig: %v", err)
}
if applied {
glog.Infof("Applied ImageConfig cluster on MachineConfigPool %v", pool.Name)
}
}
return nil
}
func (ctrl *Controller) popFinalizerFromContainerRuntimeConfig(ctrCfg *mcfgv1.ContainerRuntimeConfig) error {
return retry.RetryOnConflict(updateBackoff, func() error {
newcfg, err := ctrl.mccrLister.Get(ctrCfg.Name)
if errors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
curJSON, err := json.Marshal(newcfg)
if err != nil {
return err
}
ctrCfgTmp := newcfg.DeepCopy()
ctrCfgTmp.Finalizers = append(ctrCfg.Finalizers[:0], ctrCfg.Finalizers[1:]...)
modJSON, err := json.Marshal(ctrCfgTmp)
if err != nil {
return err
}
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(curJSON, modJSON, curJSON)
if err != nil {
return err
}
return ctrl.patchContainerRuntimeConfigsFunc(ctrCfg.Name, patch)
})
}
func (ctrl *Controller) patchContainerRuntimeConfigs(name string, patch []byte) error {
_, err := ctrl.client.MachineconfigurationV1().ContainerRuntimeConfigs().Patch(name, types.MergePatchType, patch)
return err
}
func (ctrl *Controller) addFinalizerToContainerRuntimeConfig(ctrCfg *mcfgv1.ContainerRuntimeConfig, mc *mcfgv1.MachineConfig) error {
return retry.RetryOnConflict(updateBackoff, func() error {
newcfg, err := ctrl.mccrLister.Get(ctrCfg.Name)
if errors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
curJSON, err := json.Marshal(newcfg)
if err != nil {
return err
}
ctrCfgTmp := newcfg.DeepCopy()
ctrCfgTmp.Finalizers = append(ctrCfgTmp.Finalizers, mc.Name)
modJSON, err := json.Marshal(ctrCfgTmp)
if err != nil {
return err
}
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(curJSON, modJSON, curJSON)
if err != nil {
return err
}
return ctrl.patchContainerRuntimeConfigsFunc(ctrCfg.Name, patch)
})
}
func (ctrl *Controller) getPoolsForContainerRuntimeConfig(config *mcfgv1.ContainerRuntimeConfig) ([]*mcfgv1.MachineConfigPool, error) {
pList, err := ctrl.mcpLister.List(labels.Everything())
if err != nil {
return nil, err
}
selector, err := metav1.LabelSelectorAsSelector(config.Spec.MachineConfigPoolSelector)
if err != nil {
return nil, fmt.Errorf("invalid label selector: %v", err)
}
var pools []*mcfgv1.MachineConfigPool
for _, p := range pList {
// If a pool with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() || !selector.Matches(labels.Set(p.Labels)) {
continue
}
pools = append(pools, p)
}
if len(pools) == 0 {
return nil, fmt.Errorf("could not find any MachineConfigPool set for ContainerRuntimeConfig %s", config.Name)
}
return pools, nil
}