-
Notifications
You must be signed in to change notification settings - Fork 424
/
Copy pathkubelet_config_controller.go
838 lines (731 loc) · 29.7 KB
/
kubelet_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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
package kubeletconfig
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/clarketm/json"
ign3types "github.com/coreos/ignition/v2/config/v3_2/types"
"github.com/golang/glog"
"github.com/imdario/mergo"
corev1 "k8s.io/api/core/v1"
macherrors "k8s.io/apimachinery/pkg/api/errors"
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/uuid"
"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"
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
configv1 "github.com/openshift/api/config/v1"
configclientset "github.com/openshift/client-go/config/clientset/versioned"
oseinformersv1 "github.com/openshift/client-go/config/informers/externalversions/config/v1"
oselistersv1 "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/library-go/pkg/crypto"
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 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:
//
// 18 allows for retries up to about 10 minutes to allow for slower machines to catchup.
maxRetries = 18
// defaultOpenshiftTLSSecurityProfileConfig is the singleton object in
// Openshift containing the config for the tls security settings.
defaultOpenshiftTLSSecurityProfileConfig = "apiserver.v1.config.openshift.io"
)
var (
// controllerKind contains the schema.GroupVersionKind for this controller type.
controllerKind = mcfgv1.SchemeGroupVersion.WithKind("KubeletConfig")
)
var updateBackoff = wait.Backoff{
Steps: 5,
Duration: 100 * time.Millisecond,
Jitter: 1.0,
}
var errCouldNotFindMCPSet = errors.New("could not find any MachineConfigPool set for KubeletConfig")
// Controller defines the kubelet config controller.
type Controller struct {
templatesDir string
client mcfgclientset.Interface
configClient configclientset.Interface
eventRecorder record.EventRecorder
syncHandler func(mcp string) error
enqueueKubeletConfig func(*mcfgv1.KubeletConfig)
ccLister mcfglistersv1.ControllerConfigLister
ccListerSynced cache.InformerSynced
mckLister mcfglistersv1.KubeletConfigLister
mckListerSynced cache.InformerSynced
mcpLister mcfglistersv1.MachineConfigPoolLister
mcpListerSynced cache.InformerSynced
featLister oselistersv1.FeatureGateLister
featListerSynced cache.InformerSynced
nodeConfigLister oselistersv1.NodeLister
nodeConfigListerSynced cache.InformerSynced
apiserverLister oselistersv1.APIServerLister
apiserverListerSynced cache.InformerSynced
queue workqueue.RateLimitingInterface
featureQueue workqueue.RateLimitingInterface
nodeConfigQueue workqueue.RateLimitingInterface
}
// New returns a new kubelet config controller
func New(
templatesDir string,
mcpInformer mcfginformersv1.MachineConfigPoolInformer,
ccInformer mcfginformersv1.ControllerConfigInformer,
mkuInformer mcfginformersv1.KubeletConfigInformer,
featInformer oseinformersv1.FeatureGateInformer,
nodeConfigInformer oseinformersv1.NodeInformer,
apiserverInformer oseinformersv1.APIServerInformer,
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-kubeletconfigcontroller"}),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "machineconfigcontroller-kubeletconfigcontroller"),
featureQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "machineconfigcontroller-featurecontroller"),
nodeConfigQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "machineconfigcontroller-nodeConfigcontroller"),
}
mkuInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.addKubeletConfig,
UpdateFunc: ctrl.updateKubeletConfig,
DeleteFunc: ctrl.deleteKubeletConfig,
})
featInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.addFeature,
UpdateFunc: ctrl.updateFeature,
DeleteFunc: ctrl.deleteFeature,
})
nodeConfigInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: ctrl.addNodeConfig,
UpdateFunc: ctrl.updateNodeConfig,
DeleteFunc: ctrl.deleteNodeConfig,
})
ctrl.syncHandler = ctrl.syncKubeletConfig
ctrl.enqueueKubeletConfig = ctrl.enqueue
ctrl.mcpLister = mcpInformer.Lister()
ctrl.mcpListerSynced = mcpInformer.Informer().HasSynced
ctrl.ccLister = ccInformer.Lister()
ctrl.ccListerSynced = ccInformer.Informer().HasSynced
ctrl.mckLister = mkuInformer.Lister()
ctrl.mckListerSynced = mkuInformer.Informer().HasSynced
ctrl.featLister = featInformer.Lister()
ctrl.featListerSynced = featInformer.Informer().HasSynced
ctrl.nodeConfigLister = nodeConfigInformer.Lister()
ctrl.nodeConfigListerSynced = nodeConfigInformer.Informer().HasSynced
ctrl.apiserverLister = apiserverInformer.Lister()
ctrl.apiserverListerSynced = apiserverInformer.Informer().HasSynced
return ctrl
}
// Run executes the kubelet config controller.
func (ctrl *Controller) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer ctrl.queue.ShutDown()
defer ctrl.featureQueue.ShutDown()
defer ctrl.nodeConfigQueue.ShutDown()
if !cache.WaitForCacheSync(stopCh, ctrl.mcpListerSynced, ctrl.mckListerSynced, ctrl.ccListerSynced, ctrl.featListerSynced, ctrl.apiserverListerSynced) {
return
}
glog.Info("Starting MachineConfigController-KubeletConfigController")
defer glog.Info("Shutting down MachineConfigController-KubeletConfigController")
for i := 0; i < workers; i++ {
go wait.Until(ctrl.worker, time.Second, stopCh)
}
for i := 0; i < workers; i++ {
go wait.Until(ctrl.featureWorker, time.Second, stopCh)
}
for i := 0; i < workers; i++ {
go wait.Until(ctrl.nodeConfigWorker, time.Second, stopCh)
}
<-stopCh
}
func kubeletConfigTriggerObjectChange(old, new *mcfgv1.KubeletConfig) bool {
if old.DeletionTimestamp != new.DeletionTimestamp {
return true
}
if !reflect.DeepEqual(old.Spec, new.Spec) {
return true
}
return false
}
func (ctrl *Controller) updateKubeletConfig(old, cur interface{}) {
oldConfig := old.(*mcfgv1.KubeletConfig)
newConfig := cur.(*mcfgv1.KubeletConfig)
if kubeletConfigTriggerObjectChange(oldConfig, newConfig) {
glog.V(4).Infof("Update KubeletConfig %s", oldConfig.Name)
ctrl.enqueueKubeletConfig(newConfig)
}
}
func (ctrl *Controller) addKubeletConfig(obj interface{}) {
cfg := obj.(*mcfgv1.KubeletConfig)
glog.V(4).Infof("Adding KubeletConfig %s", cfg.Name)
ctrl.enqueueKubeletConfig(cfg)
}
func (ctrl *Controller) deleteKubeletConfig(obj interface{}) {
cfg, ok := obj.(*mcfgv1.KubeletConfig)
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.KubeletConfig)
if !ok {
utilruntime.HandleError(fmt.Errorf("Tombstone contained object that is not a KubeletConfig %#v", obj))
return
}
}
if err := ctrl.cascadeDelete(cfg); err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't delete object %#v: %w", cfg, err))
} else {
glog.V(4).Infof("Deleted KubeletConfig %s and restored default config", cfg.Name)
}
}
func (ctrl *Controller) cascadeDelete(cfg *mcfgv1.KubeletConfig) error {
if len(cfg.GetFinalizers()) == 0 {
return nil
}
finalizerName := cfg.GetFinalizers()[0]
mcs, err := ctrl.client.MachineconfigurationV1().MachineConfigs().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
for _, mc := range mcs.Items {
if string(mc.ObjectMeta.GetUID()) == finalizerName || mc.GetName() == finalizerName {
err := ctrl.client.MachineconfigurationV1().MachineConfigs().Delete(context.TODO(), mc.GetName(), metav1.DeleteOptions{})
if err != nil && !macherrors.IsNotFound(err) {
return err
}
break
}
}
if err := ctrl.popFinalizerFromKubeletConfig(cfg); err != nil {
return err
}
return nil
}
func (ctrl *Controller) enqueue(cfg *mcfgv1.KubeletConfig) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cfg)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %w", cfg, err))
return
}
ctrl.queue.Add(key)
}
func (ctrl *Controller) enqueueRateLimited(cfg *mcfgv1.KubeletConfig) {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(cfg)
if err != nil {
utilruntime.HandleError(fmt.Errorf("couldn't get key for object %#v: %w", 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) 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 _, ok := err.(*forgetError); ok {
ctrl.queue.Forget(key)
return
}
if ctrl.queue.NumRequeues(key) < maxRetries {
glog.V(2).Infof("Error syncing kubeletconfig %v: %v", key, err)
ctrl.queue.AddRateLimited(key)
return
}
utilruntime.HandleError(err)
glog.V(2).Infof("Dropping kubeletconfig %q out of the queue: %v", key, err)
ctrl.queue.Forget(key)
ctrl.queue.AddAfter(key, 1*time.Minute)
}
func (ctrl *Controller) handleFeatureErr(err error, key interface{}) {
if err == nil {
ctrl.featureQueue.Forget(key)
return
}
if ctrl.featureQueue.NumRequeues(key) < maxRetries {
glog.V(2).Infof("Error syncing kubeletconfig %v: %v", key, err)
ctrl.featureQueue.AddRateLimited(key)
return
}
utilruntime.HandleError(err)
glog.V(2).Infof("Dropping featureconfig %q out of the queue: %v", key, err)
ctrl.featureQueue.Forget(key)
ctrl.featureQueue.AddAfter(key, 1*time.Minute)
}
// generateOriginalKubeletConfigWithFeatureGates generates a KubeletConfig and ensure the correct feature gates are set
// based on the given FeatureGate.
func generateOriginalKubeletConfigWithFeatureGates(cc *mcfgv1.ControllerConfig, templatesDir, role string, features *configv1.FeatureGate) (*kubeletconfigv1beta1.KubeletConfiguration, error) {
originalKubeletIgn, err := generateOriginalKubeletConfigIgn(cc, templatesDir, role, features)
if err != nil {
return nil, fmt.Errorf("could not generate the original Kubelet config ignition: %w", err)
}
if originalKubeletIgn.Contents.Source == nil {
return nil, fmt.Errorf("the original Kubelet source string is empty: %w", err)
}
contents, err := ctrlcommon.DecodeIgnitionFileContents(originalKubeletIgn.Contents.Source, originalKubeletIgn.Contents.Compression)
if err != nil {
return nil, fmt.Errorf("could not decode the original Kubelet source string: %w", err)
}
originalKubeConfig, err := decodeKubeletConfig(contents)
if err != nil {
return nil, fmt.Errorf("could not deserialize the Kubelet source: %w", err)
}
featureGates, err := generateFeatureMap(features, openshiftOnlyFeatureGates...)
if err != nil {
return nil, fmt.Errorf("could not generate features map: %w", err)
}
// Merge in Feature Gates.
// If they are the same, this will be a no-op
if err := mergo.Merge(&originalKubeConfig.FeatureGates, featureGates, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("could not merge feature gates: %w", err)
}
return originalKubeConfig, nil
}
func generateOriginalKubeletConfigIgn(cc *mcfgv1.ControllerConfig, templatesDir, role string, featureGate *configv1.FeatureGate) (*ign3types.File, error) {
// Render the default templates
rc := &mtmpl.RenderConfig{ControllerConfigSpec: &cc.Spec, FeatureGate: featureGate}
generatedConfigs, err := mtmpl.GenerateMachineConfigsForRole(rc, role, templatesDir)
if err != nil {
return nil, fmt.Errorf("GenerateMachineConfigsforRole failed with error: %w", err)
}
// Find generated kubelet.config
for _, gmc := range generatedConfigs {
gmcKubeletConfig, err := findKubeletConfig(gmc)
if err != nil {
continue
}
return gmcKubeletConfig, nil
}
return nil, fmt.Errorf("could not generate old kubelet config")
}
func (ctrl *Controller) syncStatusOnly(cfg *mcfgv1.KubeletConfig, err error, args ...interface{}) error {
statusUpdateError := retry.RetryOnConflict(updateBackoff, func() error {
newcfg, getErr := ctrl.mckLister.Get(cfg.Name)
if getErr != nil {
return getErr
}
// Keeps a list of three status to avoid a long list of same statuses,
// only append a status if it is the first status
// or if the status message is different from the message of the last status recorded
// If the last status message is the same as the new one, then update the last status to
// reflect the latest time stamp from the new status message.
newStatusCondition := wrapErrorWithCondition(err, args...)
cleanUpStatusConditions(&newcfg.Status.Conditions, newStatusCondition)
_, lerr := ctrl.client.MachineconfigurationV1().KubeletConfigs().UpdateStatus(context.TODO(), newcfg, metav1.UpdateOptions{})
return lerr
})
if statusUpdateError != nil {
glog.Warningf("error updating kubeletconfig status: %v", statusUpdateError)
}
return err
}
// cleanUpStatusConditions keeps at most three conditions of different timestamps for the kubelet config object
func cleanUpStatusConditions(statusConditions *[]mcfgv1.KubeletConfigCondition, newStatusCondition mcfgv1.KubeletConfigCondition) {
statusLimit := 3
statusLen := len(*statusConditions)
if statusLen > 0 && (*statusConditions)[statusLen-1].Message == newStatusCondition.Message {
(*statusConditions)[statusLen-1].LastTransitionTime = newStatusCondition.LastTransitionTime
} else {
*statusConditions = append(*statusConditions, newStatusCondition)
}
if len(*statusConditions) > statusLimit {
*statusConditions = (*statusConditions)[len(*statusConditions)-statusLimit:]
}
}
// addAnnotation adds the annotions for a kubeletconfig object with the given annotationKey and annotationVal
func (ctrl *Controller) addAnnotation(cfg *mcfgv1.KubeletConfig, annotationKey, annotationVal string) error {
annotationUpdateErr := retry.RetryOnConflict(updateBackoff, func() error {
newcfg, getErr := ctrl.mckLister.Get(cfg.Name)
if getErr != nil {
return getErr
}
newcfg.SetAnnotations(map[string]string{
annotationKey: annotationVal,
})
_, updateErr := ctrl.client.MachineconfigurationV1().KubeletConfigs().Update(context.TODO(), newcfg, metav1.UpdateOptions{})
return updateErr
})
if annotationUpdateErr != nil {
glog.Warningf("error updating the kubelet config with annotation key %q and value %q: %v", annotationKey, annotationVal, annotationUpdateErr)
}
return annotationUpdateErr
}
// syncKubeletConfig will sync the kubeletconfig with the given key.
// This function is not meant to be invoked concurrently with the same key.
//
//nolint:gocyclo
func (ctrl *Controller) syncKubeletConfig(key string) error {
startTime := time.Now()
glog.V(4).Infof("Started syncing kubeletconfig %q (%v)", key, startTime)
defer func() {
glog.V(4).Infof("Finished syncing kubeletconfig %q (%v)", key, time.Since(startTime))
}()
// Wait to apply a kubelet config if the controller config is not completed
if err := mcfgv1.IsControllerConfigCompleted(ctrlcommon.ControllerConfigName, ctrl.ccLister.Get); err != nil {
return err
}
_, name, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return err
}
// Fetch the KubeletConfig
cfg, err := ctrl.mckLister.Get(name)
if macherrors.IsNotFound(err) {
glog.V(2).Infof("KubeletConfig %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 KubeletConfig 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 then skip
if cfg.Status.ObservedGeneration >= cfg.Generation {
return nil
}
// Validate the KubeletConfig CR
if err := validateUserKubeletConfig(cfg); err != nil {
return ctrl.syncStatusOnly(cfg, newForgetError(err))
}
// Find all MachineConfigPools
mcpPools, err := ctrl.getPoolsForKubeletConfig(cfg)
if err != nil {
return ctrl.syncStatusOnly(cfg, err)
}
if len(mcpPools) == 0 {
err := fmt.Errorf("KubeletConfig %v does not match any MachineConfigPools", key)
glog.V(2).Infof("%v", err)
return ctrl.syncStatusOnly(cfg, err)
}
features, err := ctrl.featLister.Get(ctrlcommon.ClusterFeatureInstanceName)
if macherrors.IsNotFound(err) {
features = createNewDefaultFeatureGate()
} else if err != nil {
glog.V(2).Infof("%v", err)
err := fmt.Errorf("could not fetch FeatureGates: %w", err)
return ctrl.syncStatusOnly(cfg, err)
}
// Fetch the NodeConfig
nodeConfig, err := ctrl.nodeConfigLister.Get(ctrlcommon.ClusterNodeInstanceName)
if macherrors.IsNotFound(err) {
nodeConfig = createNewDefaultNodeconfig()
}
for _, pool := range mcpPools {
if pool.Spec.Configuration.Name == "" {
updateDelay := 5 * time.Second
// Previously we spammed the logs about empty pools.
// Let's just pause for a bit here to let the renderer
// initialize them.
time.Sleep(updateDelay)
return fmt.Errorf("Pool %s is unconfigured, pausing %v for renderer to initialize", pool.Name, updateDelay)
}
role := pool.Name
// Get MachineConfig
managedKey, err := getManagedKubeletConfigKey(pool, ctrl.client, cfg)
if err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not get kubelet config key: %v", err)
}
mc, err := ctrl.client.MachineconfigurationV1().MachineConfigs().Get(context.TODO(), managedKey, metav1.GetOptions{})
if err != nil && !macherrors.IsNotFound(err) {
return ctrl.syncStatusOnly(cfg, err, "could not find MachineConfig: %v", managedKey)
}
isNotFound := macherrors.IsNotFound(err)
// Generate the original KubeletConfig
cc, err := ctrl.ccLister.Get(ctrlcommon.ControllerConfigName)
if err != nil {
return fmt.Errorf("could not get ControllerConfig %w", err)
}
originalKubeConfig, err := generateOriginalKubeletConfigWithFeatureGates(cc, ctrl.templatesDir, role, features)
if err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not get original kubelet config: %v", err)
}
// updating the originalKubeConfig based on the nodeConfig on a worker node
if role == ctrlcommon.MachineConfigPoolWorker {
updateOriginalKubeConfigwithNodeConfig(nodeConfig, originalKubeConfig)
}
// Get the default API Server Security Profile
var profile *configv1.TLSSecurityProfile
if apiServerSettings, err := ctrl.apiserverLister.Get(defaultOpenshiftTLSSecurityProfileConfig); err != nil {
if !macherrors.IsNotFound(err) {
return ctrl.syncStatusOnly(cfg, err, "could not get the TLSSecurityProfile from %v: %v", defaultOpenshiftTLSSecurityProfileConfig, err)
}
} else {
profile = apiServerSettings.Spec.TLSSecurityProfile
}
if cfg.Spec.TLSSecurityProfile != nil {
profile = cfg.Spec.TLSSecurityProfile
}
// Inject TLS Options from Spec
observedMinTLSVersion, observedCipherSuites := getSecurityProfileCiphers(profile)
originalKubeConfig.TLSMinVersion = observedMinTLSVersion
originalKubeConfig.TLSCipherSuites = observedCipherSuites
kubeletIgnition, logLevelIgnition, autoSizingReservedIgnition, err := generateKubeletIgnFiles(cfg, originalKubeConfig)
if err != nil {
return ctrl.syncStatusOnly(cfg, err)
}
if isNotFound {
ignConfig := ctrlcommon.NewIgnConfig()
mc, err = ctrlcommon.MachineConfigFromIgnConfig(role, managedKey, ignConfig)
if err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not create MachineConfig from new Ignition config: %v", err)
}
mc.ObjectMeta.UID = uuid.NewUUID()
_, ok := cfg.GetAnnotations()[ctrlcommon.MCNameSuffixAnnotationKey]
arr := strings.Split(managedKey, "-")
// the first managed key value 99-poolname-generated-kubelet does not have a suffix
// set "" as suffix annotation to the kubelet config object
if _, err := strconv.Atoi(arr[len(arr)-1]); err != nil && !ok {
if err := ctrl.addAnnotation(cfg, ctrlcommon.MCNameSuffixAnnotationKey, ""); err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not update annotation for kubeletConfig")
}
}
// If the MC name suffix annotation does not exist and the managed key value returned has a suffix, then add the MC name
// suffix annotation and suffix value to the kubelet config object
if len(arr) > 4 && !ok {
_, err := strconv.Atoi(arr[len(arr)-1])
if err == nil {
if err := ctrl.addAnnotation(cfg, ctrlcommon.MCNameSuffixAnnotationKey, arr[len(arr)-1]); err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not update annotation for kubeletConfig")
}
}
}
}
tempIgnConfig := ctrlcommon.NewIgnConfig()
if autoSizingReservedIgnition != nil {
tempIgnConfig.Storage.Files = append(tempIgnConfig.Storage.Files, *autoSizingReservedIgnition)
}
if logLevelIgnition != nil {
tempIgnConfig.Storage.Files = append(tempIgnConfig.Storage.Files, *logLevelIgnition)
}
if kubeletIgnition != nil {
tempIgnConfig.Storage.Files = append(tempIgnConfig.Storage.Files, *kubeletIgnition)
}
rawIgn, err := json.Marshal(tempIgnConfig)
if err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not marshal kubelet config Ignition: %v", err)
}
mc.Spec.Config.Raw = rawIgn
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 err := retry.RetryOnConflict(updateBackoff, func() error {
var err error
if isNotFound {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Create(context.TODO(), mc, metav1.CreateOptions{})
} else {
_, err = ctrl.client.MachineconfigurationV1().MachineConfigs().Update(context.TODO(), mc, metav1.UpdateOptions{})
}
return err
}); err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not Create/Update MachineConfig: %v", err)
}
// Add Finalizers to the KubletConfig
if err := ctrl.addFinalizerToKubeletConfig(cfg, mc); err != nil {
return ctrl.syncStatusOnly(cfg, err, "could not add finalizers to KubeletConfig: %v", err)
}
glog.Infof("Applied KubeletConfig %v on MachineConfigPool %v", key, pool.Name)
}
if err := ctrl.cleanUpDuplicatedMC(managedKubeletConfigKeyPrefix); err != nil {
return err
}
return ctrl.syncStatusOnly(cfg, nil)
}
// cleanUpDuplicatedMC removes the MC of non-updated GeneratedByControllerVersionKey if its name contains 'generated-kubelet'.
// BZ 1955517: upgrade when there are more than one configs, the duplicated and upgraded MC will be generated (func getManagedKubeletConfigKey())
// MC with old GeneratedByControllerVersionKey fails the upgrade.
// This can also clean up unmanaged machineconfigs that their correcponding pool is removed.
func (ctrl *Controller) cleanUpDuplicatedMC(prefix string) error {
generatedKubeletCfg := "generated-kubelet"
// Get all machine configs
mcList, err := ctrl.client.MachineconfigurationV1().MachineConfigs().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing kubelet machine configs: %w", err)
}
for _, mc := range mcList.Items {
if !strings.Contains(mc.Name, generatedKubeletCfg) {
continue
}
if !strings.HasPrefix(mc.Name, prefix) {
continue
}
// delete the mc if its degraded
if mc.Annotations[ctrlcommon.GeneratedByControllerVersionAnnotationKey] != version.Hash {
if err := ctrl.client.MachineconfigurationV1().MachineConfigs().Delete(context.TODO(), mc.Name, metav1.DeleteOptions{}); err != nil && !macherrors.IsNotFound(err) {
return fmt.Errorf("error deleting degraded kubelet machine config %s: %w", mc.Name, err)
}
}
}
return nil
}
func (ctrl *Controller) popFinalizerFromKubeletConfig(kc *mcfgv1.KubeletConfig) error {
return retry.RetryOnConflict(updateBackoff, func() error {
newcfg, err := ctrl.mckLister.Get(kc.Name)
if macherrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
curJSON, err := json.Marshal(newcfg)
if err != nil {
return err
}
kcTmp := newcfg.DeepCopy()
kcTmp.Finalizers = append(kc.Finalizers[:0], kc.Finalizers[1:]...)
modJSON, err := json.Marshal(kcTmp)
if err != nil {
return err
}
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(curJSON, modJSON, curJSON)
if err != nil {
return err
}
return ctrl.patchKubeletConfigs(newcfg.Name, patch)
})
}
func (ctrl *Controller) patchKubeletConfigs(name string, patch []byte) error {
_, err := ctrl.client.MachineconfigurationV1().KubeletConfigs().Patch(context.TODO(), name, types.MergePatchType, patch, metav1.PatchOptions{})
return err
}
func (ctrl *Controller) addFinalizerToKubeletConfig(kc *mcfgv1.KubeletConfig, mc *mcfgv1.MachineConfig) error {
return retry.RetryOnConflict(updateBackoff, func() error {
newcfg, err := ctrl.mckLister.Get(kc.Name)
if macherrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
curJSON, err := json.Marshal(newcfg)
if err != nil {
return err
}
kcTmp := newcfg.DeepCopy()
// We want to use the mc name as the finalizer instead of the uid because
// every time a resync happens, a new uid is generated. This is why the list
// of finalizers had multiple entries. So check if the list of finalizers consists
// of uids, if it does then clear the list of finalizers and we will add the mc
// name to it, ensuring we don't have duplicate or multiple finalizers.
for _, finalizerName := range newcfg.Finalizers {
if !strings.Contains(finalizerName, "kubelet") {
kcTmp.ObjectMeta.SetFinalizers([]string{})
}
}
// Only append the mc name if it is not already in the list of finalizers.
// When we update an existing kubeletconfig, the generation number increases causing
// a resync to happen. When this happens, the mc name is the same, so we don't
// want to add duplicate entries to the list of finalizers.
if !ctrlcommon.InSlice(mc.Name, kcTmp.ObjectMeta.Finalizers) {
kcTmp.ObjectMeta.Finalizers = append(kcTmp.ObjectMeta.Finalizers, mc.Name)
}
modJSON, err := json.Marshal(kcTmp)
if err != nil {
return err
}
patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(curJSON, modJSON, curJSON)
if err != nil {
return err
}
return ctrl.patchKubeletConfigs(newcfg.Name, patch)
})
}
func (ctrl *Controller) getPoolsForKubeletConfig(config *mcfgv1.KubeletConfig) ([]*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: %w", 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, errCouldNotFindMCPSet
}
return pools, nil
}
// Extracts the minimum TLS version and cipher suites from TLSSecurityProfile object,
// Converts the ciphers to IANA names as supported by Kube ServingInfo config.
// If profile is nil, returns config defined by the Intermediate TLS Profile
func getSecurityProfileCiphers(profile *configv1.TLSSecurityProfile) (string, []string) {
var profileType configv1.TLSProfileType
if profile == nil {
profileType = configv1.TLSProfileIntermediateType
} else {
profileType = profile.Type
}
var profileSpec *configv1.TLSProfileSpec
if profileType == configv1.TLSProfileCustomType {
if profile.Custom != nil {
profileSpec = &profile.Custom.TLSProfileSpec
}
} else {
profileSpec = configv1.TLSProfiles[profileType]
}
// nothing found / custom type set but no actual custom spec
if profileSpec == nil {
profileSpec = configv1.TLSProfiles[configv1.TLSProfileIntermediateType]
}
// need to remap all Ciphers to their respective IANA names used by Go
return string(profileSpec.MinTLSVersion), crypto.OpenSSLToIANACipherSuites(profileSpec.Ciphers)
}