-
Notifications
You must be signed in to change notification settings - Fork 114
/
daemon.go
780 lines (682 loc) · 26.8 KB
/
daemon.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
package daemon
import (
"context"
"fmt"
"math/rand"
"reflect"
"sync"
"time"
"golang.org/x/time/rate"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned"
sninformer "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/informers/externalversions"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/featuregate"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper"
snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms"
plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
)
const (
// updateDelay is the baseline speed at which we react to changes. We don't
// need to react in milliseconds as any change would involve rebooting the node.
updateDelay = 5 * time.Second
// maxUpdateBackoff is the maximum time to react to a change as we back off
// in the face of errors.
maxUpdateBackoff = 60 * time.Second
)
type Message struct {
syncStatus string
lastSyncError string
}
type Daemon struct {
client client.Client
sriovClient snclientset.Interface
// kubeClient allows interaction with Kubernetes, including the node we are running on.
kubeClient kubernetes.Interface
desiredNodeState *sriovnetworkv1.SriovNetworkNodeState
currentNodeState *sriovnetworkv1.SriovNetworkNodeState
// list of disabled plugins
disabledPlugins []string
loadedPlugins map[string]plugin.VendorPlugin
HostHelpers helper.HostHelpersInterface
platformHelpers platforms.Interface
// channel used by callbacks to signal Run() of an error
exitCh chan<- error
// channel used to ensure all spawned goroutines exit when we exit.
stopCh <-chan struct{}
syncCh <-chan struct{}
refreshCh chan<- Message
mu *sync.Mutex
disableDrain bool
workqueue workqueue.RateLimitingInterface
eventRecorder *EventRecorder
featureGate featuregate.FeatureGate
}
func New(
client client.Client,
sriovClient snclientset.Interface,
kubeClient kubernetes.Interface,
hostHelpers helper.HostHelpersInterface,
platformHelper platforms.Interface,
exitCh chan<- error,
stopCh <-chan struct{},
syncCh <-chan struct{},
refreshCh chan<- Message,
er *EventRecorder,
featureGates featuregate.FeatureGate,
disabledPlugins []string,
) *Daemon {
return &Daemon{
client: client,
sriovClient: sriovClient,
kubeClient: kubeClient,
HostHelpers: hostHelpers,
platformHelpers: platformHelper,
exitCh: exitCh,
stopCh: stopCh,
syncCh: syncCh,
refreshCh: refreshCh,
desiredNodeState: &sriovnetworkv1.SriovNetworkNodeState{},
currentNodeState: &sriovnetworkv1.SriovNetworkNodeState{},
workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.NewMaxOfRateLimiter(
&workqueue.BucketRateLimiter{Limiter: rate.NewLimiter(rate.Limit(updateDelay), 1)},
workqueue.NewItemExponentialFailureRateLimiter(1*time.Second, maxUpdateBackoff)), "SriovNetworkNodeState"),
eventRecorder: er,
featureGate: featureGates,
disabledPlugins: disabledPlugins,
mu: &sync.Mutex{},
}
}
// Run the config daemon
func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error {
log.Log.V(0).Info("Run()", "node", vars.NodeName)
if vars.ClusterType == consts.ClusterTypeOpenshift {
log.Log.V(0).Info("Run(): start daemon.", "openshiftFlavor", dn.platformHelpers.GetFlavor())
} else {
log.Log.V(0).Info("Run(): start daemon.")
}
if !vars.UsingSystemdMode {
log.Log.V(0).Info("Run(): daemon running in daemon mode")
dn.HostHelpers.CheckRDMAEnabled()
dn.HostHelpers.TryEnableTun()
dn.HostHelpers.TryEnableVhostNet()
err := systemd.CleanSriovFilesFromHost(vars.ClusterType == consts.ClusterTypeOpenshift)
if err != nil {
log.Log.Error(err, "failed to remove all the systemd sriov files")
}
} else {
log.Log.V(0).Info("Run(): daemon running in systemd mode")
}
// Only watch own SriovNetworkNodeState CR
defer utilruntime.HandleCrash()
defer dn.workqueue.ShutDown()
if err := dn.prepareNMUdevRule(); err != nil {
log.Log.Error(err, "failed to prepare udev files to disable network manager on requested VFs")
}
if err := dn.HostHelpers.PrepareVFRepUdevRule(); err != nil {
log.Log.Error(err, "failed to prepare udev files to rename VF representors for requested VFs")
}
var timeout int64 = 5
var metadataKey = "metadata.name"
informerFactory := sninformer.NewFilteredSharedInformerFactory(dn.sriovClient,
time.Second*15,
vars.Namespace,
func(lo *metav1.ListOptions) {
lo.FieldSelector = metadataKey + "=" + vars.NodeName
lo.TimeoutSeconds = &timeout
},
)
informer := informerFactory.Sriovnetwork().V1().SriovNetworkNodeStates().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: dn.enqueueNodeState,
UpdateFunc: func(old, new interface{}) {
dn.enqueueNodeState(new)
},
})
cfgInformerFactory := sninformer.NewFilteredSharedInformerFactory(dn.sriovClient,
time.Second*30,
vars.Namespace,
func(lo *metav1.ListOptions) {
lo.FieldSelector = metadataKey + "=" + "default"
},
)
cfgInformer := cfgInformerFactory.Sriovnetwork().V1().SriovOperatorConfigs().Informer()
cfgInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: dn.operatorConfigAddHandler,
UpdateFunc: dn.operatorConfigChangeHandler,
})
rand.Seed(time.Now().UnixNano())
go cfgInformer.Run(dn.stopCh)
time.Sleep(5 * time.Second)
go informer.Run(dn.stopCh)
if ok := cache.WaitForCacheSync(stopCh, cfgInformer.HasSynced, informer.HasSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}
log.Log.Info("Starting workers")
// Launch one worker to process
go wait.Until(dn.runWorker, time.Second, stopCh)
log.Log.Info("Started workers")
for {
select {
case <-stopCh:
log.Log.V(0).Info("Run(): stop daemon")
return nil
case err, more := <-exitCh:
log.Log.Error(err, "got an error")
if more {
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusFailed,
lastSyncError: err.Error(),
}
}
return err
}
}
}
func (dn *Daemon) runWorker() {
for dn.processNextWorkItem() {
}
}
func (dn *Daemon) enqueueNodeState(obj interface{}) {
var ns *sriovnetworkv1.SriovNetworkNodeState
var ok bool
if ns, ok = obj.(*sriovnetworkv1.SriovNetworkNodeState); !ok {
utilruntime.HandleError(fmt.Errorf("expected SriovNetworkNodeState but got %#v", obj))
return
}
key := ns.GetGeneration()
dn.workqueue.Add(key)
}
func (dn *Daemon) processNextWorkItem() bool {
log.Log.V(2).Info("processNextWorkItem", "worker-queue-size", dn.workqueue.Len())
obj, shutdown := dn.workqueue.Get()
if shutdown {
return false
}
log.Log.V(2).Info("get item from queue", "item", obj.(int64))
// We wrap this block in a func so we can defer c.workqueue.Done.
err := func(obj interface{}) error {
// We call Done here so the workqueue knows we have finished
// processing this item.
defer dn.workqueue.Done(obj)
var key int64
var ok bool
if key, ok = obj.(int64); !ok {
// As the item in the workqueue is actually invalid, we call
// Forget here.
dn.workqueue.Forget(obj)
utilruntime.HandleError(fmt.Errorf("expected workItem in workqueue but got %#v", obj))
return nil
}
err := dn.nodeStateSyncHandler()
if err != nil {
// Ereport error message, and put the item back to work queue for retry.
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusFailed,
lastSyncError: err.Error(),
}
<-dn.syncCh
dn.workqueue.AddRateLimited(key)
return fmt.Errorf("error syncing: %s, requeuing", err.Error())
}
// Finally, if no error occurs we Forget this item so it does not
// get queued again until another change happens.
dn.workqueue.Forget(obj)
log.Log.Info("Successfully synced")
return nil
}(obj)
if err != nil {
utilruntime.HandleError(err)
}
return true
}
func (dn *Daemon) operatorConfigAddHandler(obj interface{}) {
dn.operatorConfigChangeHandler(&sriovnetworkv1.SriovOperatorConfig{}, obj)
}
func (dn *Daemon) operatorConfigChangeHandler(old, new interface{}) {
oldCfg := old.(*sriovnetworkv1.SriovOperatorConfig)
newCfg := new.(*sriovnetworkv1.SriovOperatorConfig)
if newCfg.Namespace != vars.Namespace || newCfg.Name != consts.DefaultConfigName {
log.Log.V(2).Info("unsupported SriovOperatorConfig", "namespace", newCfg.Namespace, "name", newCfg.Name)
return
}
snolog.SetLogLevel(newCfg.Spec.LogLevel)
newDisableDrain := newCfg.Spec.DisableDrain
if dn.disableDrain != newDisableDrain {
dn.disableDrain = newDisableDrain
log.Log.Info("Set Disable Drain", "value", dn.disableDrain)
}
if !reflect.DeepEqual(oldCfg.Spec.FeatureGates, newCfg.Spec.FeatureGates) {
dn.featureGate.Init(newCfg.Spec.FeatureGates)
log.Log.Info("Updated featureGates", "featureGates", dn.featureGate.String())
}
vars.MlxPluginFwReset = dn.featureGate.IsEnabled(consts.MellanoxFirmwareResetFeatureGate)
}
func (dn *Daemon) nodeStateSyncHandler() error {
var err error
// Get the latest NodeState
var sriovResult = &systemd.SriovResult{SyncStatus: consts.SyncStatusSucceeded, LastSyncError: ""}
dn.desiredNodeState, err = dn.sriovClient.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{})
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", vars.NodeName)
return err
}
latest := dn.desiredNodeState.GetGeneration()
log.Log.V(0).Info("nodeStateSyncHandler(): new generation", "generation", latest)
// load plugins if it has not loaded
if len(dn.loadedPlugins) == 0 {
dn.loadedPlugins, err = loadPlugins(dn.desiredNodeState, dn.HostHelpers, dn.disabledPlugins)
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to enable vendor plugins")
return err
}
}
skipReconciliation := true
// if the operator complete the drain operator we should continue the configuration
if !dn.isDrainCompleted() {
if vars.UsingSystemdMode && dn.currentNodeState.GetGeneration() == latest {
serviceEnabled, err := dn.HostHelpers.IsServiceEnabled(systemd.SriovServicePath)
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to check if sriov-config service exist on host")
return err
}
postNetworkServiceEnabled, err := dn.HostHelpers.IsServiceEnabled(systemd.SriovPostNetworkServicePath)
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to check if sriov-config-post-network service exist on host")
return err
}
// if the service doesn't exist we should continue to let the k8s plugin to create the service files
// this is only for k8s base environments, for openshift the sriov-operator creates a machine config to will apply
// the system service and reboot the node the config-daemon doesn't need to do anything.
if !(serviceEnabled && postNetworkServiceEnabled) {
sriovResult = &systemd.SriovResult{SyncStatus: consts.SyncStatusFailed,
LastSyncError: fmt.Sprintf("some sriov systemd services are not available on node: "+
"sriov-config available:%t, sriov-config-post-network available:%t", serviceEnabled, postNetworkServiceEnabled)}
} else {
sriovResult, err = systemd.ReadSriovResult()
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to load sriov result file from host")
return err
}
}
if sriovResult.LastSyncError != "" || sriovResult.SyncStatus == consts.SyncStatusFailed {
log.Log.Info("nodeStateSyncHandler(): sync failed systemd service error", "last-sync-error", sriovResult.LastSyncError)
// add the error but don't requeue
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusFailed,
lastSyncError: sriovResult.LastSyncError,
}
<-dn.syncCh
return nil
}
}
skipReconciliation, err = dn.shouldSkipReconciliation(dn.desiredNodeState)
if err != nil {
return err
}
}
// we are done with the configuration just return here
if dn.currentNodeState.GetGeneration() == dn.desiredNodeState.GetGeneration() &&
dn.desiredNodeState.Status.SyncStatus == consts.SyncStatusSucceeded && skipReconciliation {
log.Log.Info("Current state and desire state are equal together with sync status succeeded nothing to do")
return nil
}
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusInProgress,
lastSyncError: "",
}
// wait for writer to refresh status then pull again the latest node state
<-dn.syncCh
// we need to load the latest status to our object
// if we don't do it we can have a race here where the user remove the virtual functions but the operator didn't
// trigger the refresh
updatedState, err := dn.sriovClient.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{})
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", vars.NodeName)
return err
}
dn.desiredNodeState.Status = updatedState.Status
reqReboot := false
reqDrain := false
// check if any of the plugins required to drain or reboot the node
for k, p := range dn.loadedPlugins {
d, r := false, false
if dn.currentNodeState.GetName() == "" {
log.Log.V(0).Info("nodeStateSyncHandler(): calling OnNodeStateChange for a new node state")
} else {
log.Log.V(0).Info("nodeStateSyncHandler(): calling OnNodeStateChange for an updated node state")
}
d, r, err = p.OnNodeStateChange(dn.desiredNodeState)
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): OnNodeStateChange plugin error", "plugin-name", k)
return err
}
log.Log.V(0).Info("nodeStateSyncHandler(): OnNodeStateChange result", "plugin", k, "drain-required", d, "reboot-required", r)
reqDrain = reqDrain || d
reqReboot = reqReboot || r
}
// When running using systemd check if the applied configuration is the latest one
// or there is a new config we need to apply
// When using systemd configuration we write the file
if vars.UsingSystemdMode {
log.Log.V(0).Info("nodeStateSyncHandler(): writing systemd config file to host")
systemdConfModified, err := systemd.WriteConfFile(dn.desiredNodeState)
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to write configuration file for systemd mode")
return err
}
if systemdConfModified {
// remove existing result file to make sure that we will not use outdated result, e.g. in case if
// systemd service was not triggered for some reason
err = systemd.RemoveSriovResult()
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to remove result file for systemd mode")
return err
}
}
reqDrain = reqDrain || systemdConfModified
// require reboot if drain needed for systemd mode
reqReboot = reqReboot || systemdConfModified || reqDrain
log.Log.V(0).Info("nodeStateSyncHandler(): systemd mode WriteConfFile results",
"drain-required", reqDrain, "reboot-required", reqReboot, "disable-drain", dn.disableDrain)
err = systemd.WriteSriovSupportedNics()
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): failed to write supported nic ids file for systemd mode")
return err
}
}
log.Log.V(0).Info("nodeStateSyncHandler(): aggregated daemon",
"drain-required", reqDrain, "reboot-required", reqReboot, "disable-drain", dn.disableDrain)
// handle drain only if the plugin request drain, or we are already in a draining request state
if reqDrain || !utils.ObjectHasAnnotation(dn.desiredNodeState,
consts.NodeStateDrainAnnotationCurrent,
consts.DrainIdle) {
drainInProcess, err := dn.handleDrain(reqReboot)
if err != nil {
log.Log.Error(err, "failed to handle drain")
return err
}
if drainInProcess {
return nil
}
}
// apply the vendor plugins after we are done with drain if needed
for k, p := range dn.loadedPlugins {
// Skip both the general and virtual plugin apply them last
if k != GenericPluginName && k != VirtualPluginName {
err := p.Apply()
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): plugin Apply failed", "plugin-name", k)
return err
}
}
}
// if we don't need to reboot, or we are not doing the configuration in systemd
// we apply the generic plugin
if !reqReboot && !vars.UsingSystemdMode {
// For BareMetal machines apply the generic plugin
selectedPlugin, ok := dn.loadedPlugins[GenericPluginName]
if ok {
// Apply generic plugin last
err = selectedPlugin.Apply()
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): generic plugin fail to apply")
return err
}
}
// For Virtual machines apply the virtual plugin
selectedPlugin, ok = dn.loadedPlugins[VirtualPluginName]
if ok {
// Apply virtual plugin last
err = selectedPlugin.Apply()
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): virtual plugin failed to apply")
return err
}
}
}
if reqReboot {
log.Log.Info("nodeStateSyncHandler(): reboot node")
dn.eventRecorder.SendEvent("RebootNode", "Reboot node has been initiated")
dn.rebootNode()
return nil
}
// restart device plugin pod
log.Log.Info("nodeStateSyncHandler(): restart device plugin pod")
if err := dn.restartDevicePluginPod(); err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): fail to restart device plugin pod")
return err
}
log.Log.Info("nodeStateSyncHandler(): apply 'Idle' annotation for node")
err = utils.AnnotateNode(context.Background(), vars.NodeName, consts.NodeDrainAnnotation, consts.DrainIdle, dn.client)
if err != nil {
log.Log.Error(err, "nodeStateSyncHandler(): Failed to annotate node")
return err
}
log.Log.Info("nodeStateSyncHandler(): apply 'Idle' annotation for nodeState")
if err := utils.AnnotateObject(context.Background(), dn.desiredNodeState,
consts.NodeStateDrainAnnotation,
consts.DrainIdle, dn.client); err != nil {
return err
}
log.Log.Info("nodeStateSyncHandler(): sync succeeded")
dn.currentNodeState = dn.desiredNodeState.DeepCopy()
if vars.UsingSystemdMode {
dn.refreshCh <- Message{
syncStatus: sriovResult.SyncStatus,
lastSyncError: sriovResult.LastSyncError,
}
} else {
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusSucceeded,
lastSyncError: "",
}
}
// wait for writer to refresh the status
<-dn.syncCh
return nil
}
func (dn *Daemon) shouldSkipReconciliation(latestState *sriovnetworkv1.SriovNetworkNodeState) (bool, error) {
log.Log.V(0).Info("shouldSkipReconciliation()")
var err error
// Skip when SriovNetworkNodeState object has just been created.
if latestState.GetGeneration() == 1 && len(latestState.Spec.Interfaces) == 0 {
err = dn.HostHelpers.ClearPCIAddressFolder()
if err != nil {
log.Log.Error(err, "failed to clear the PCI address configuration")
return false, err
}
log.Log.V(0).Info(
"shouldSkipReconciliation(): interface policy spec not yet set by controller for sriovNetworkNodeState",
"name", latestState.Name)
if latestState.Status.SyncStatus != consts.SyncStatusSucceeded {
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusSucceeded,
lastSyncError: "",
}
// wait for writer to refresh status
<-dn.syncCh
}
return true, nil
}
// Verify changes in the status of the SriovNetworkNodeState CR.
if dn.currentNodeState.GetGeneration() == latestState.GetGeneration() {
log.Log.V(0).Info("shouldSkipReconciliation() verifying status change")
for _, p := range dn.loadedPlugins {
// Verify changes in the status of the SriovNetworkNodeState CR.
log.Log.V(0).Info("shouldSkipReconciliation(): verifying status change for plugin", "pluginName", p.Name())
changed, err := p.CheckStatusChanges(latestState)
if err != nil {
return false, err
}
if changed {
log.Log.V(0).Info("shouldSkipReconciliation(): plugin require change", "pluginName", p.Name())
return false, nil
}
}
log.Log.V(0).Info("shouldSkipReconciliation(): Interface not changed")
if latestState.Status.LastSyncError != "" ||
latestState.Status.SyncStatus != consts.SyncStatusSucceeded {
dn.refreshCh <- Message{
syncStatus: consts.SyncStatusSucceeded,
lastSyncError: "",
}
// wait for writer to refresh the status
<-dn.syncCh
}
return true, nil
}
return false, nil
}
// handleDrain: adds the right annotation to the node and nodeState object
// returns true if we need to finish the reconcile loop and wait for a new object
func (dn *Daemon) handleDrain(reqReboot bool) (bool, error) {
// done with the drain we can continue with the configuration
if utils.ObjectHasAnnotation(dn.desiredNodeState, consts.NodeStateDrainAnnotationCurrent, consts.DrainComplete) {
log.Log.Info("handleDrain(): the node complete the draining")
return false, nil
}
// the operator is still draining the node so we reconcile
if utils.ObjectHasAnnotation(dn.desiredNodeState, consts.NodeStateDrainAnnotationCurrent, consts.Draining) {
log.Log.Info("handleDrain(): the node is still draining")
return true, nil
}
// drain is disabled we continue with the configuration
if dn.disableDrain {
log.Log.Info("handleDrain(): drain is disabled in sriovOperatorConfig")
return false, nil
}
if reqReboot {
log.Log.Info("handleDrain(): apply 'Reboot_Required' annotation for node")
err := utils.AnnotateNode(context.Background(), vars.NodeName, consts.NodeDrainAnnotation, consts.RebootRequired, dn.client)
if err != nil {
log.Log.Error(err, "applyDrainRequired(): Failed to annotate node")
return false, err
}
log.Log.Info("handleDrain(): apply 'Reboot_Required' annotation for nodeState")
if err := utils.AnnotateObject(context.Background(), dn.desiredNodeState,
consts.NodeStateDrainAnnotation,
consts.RebootRequired, dn.client); err != nil {
return false, err
}
// the node was annotated we need to wait for the operator to finish the drain
return true, nil
}
log.Log.Info("handleDrain(): apply 'Drain_Required' annotation for node")
err := utils.AnnotateNode(context.Background(), vars.NodeName, consts.NodeDrainAnnotation, consts.DrainRequired, dn.client)
if err != nil {
log.Log.Error(err, "handleDrain(): Failed to annotate node")
return false, err
}
log.Log.Info("handleDrain(): apply 'Drain_Required' annotation for nodeState")
if err := utils.AnnotateObject(context.Background(), dn.desiredNodeState,
consts.NodeStateDrainAnnotation,
consts.DrainRequired, dn.client); err != nil {
return false, err
}
// the node was annotated we need to wait for the operator to finish the drain
return true, nil
}
func (dn *Daemon) restartDevicePluginPod() error {
dn.mu.Lock()
defer dn.mu.Unlock()
log.Log.V(2).Info("restartDevicePluginPod(): try to restart device plugin pod")
pods, err := dn.kubeClient.CoreV1().Pods(vars.Namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: "app=sriov-device-plugin",
FieldSelector: "spec.nodeName=" + vars.NodeName,
ResourceVersion: "0",
})
if err != nil {
if errors.IsNotFound(err) {
log.Log.Info("restartDevicePluginPod(): device plugin pod exited")
return nil
}
log.Log.Error(err, "restartDevicePluginPod(): Failed to list device plugin pod, retrying")
return err
}
if len(pods.Items) == 0 {
log.Log.Info("restartDevicePluginPod(): device plugin pod exited")
return nil
}
for _, pod := range pods.Items {
podToDelete := pod.Name
log.Log.V(2).Info("restartDevicePluginPod(): Found device plugin pod, deleting it", "pod-name", podToDelete)
err = dn.kubeClient.CoreV1().Pods(vars.Namespace).Delete(context.Background(), podToDelete, metav1.DeleteOptions{})
if errors.IsNotFound(err) {
log.Log.Info("restartDevicePluginPod(): pod to delete not found")
continue
}
if err != nil {
log.Log.Error(err, "restartDevicePluginPod(): Failed to delete device plugin pod, retrying")
return err
}
if err := wait.PollImmediateUntil(3*time.Second, func() (bool, error) {
_, err := dn.kubeClient.CoreV1().Pods(vars.Namespace).Get(context.Background(), podToDelete, metav1.GetOptions{})
if errors.IsNotFound(err) {
log.Log.Info("restartDevicePluginPod(): device plugin pod exited")
return true, nil
}
if err != nil {
log.Log.Error(err, "restartDevicePluginPod(): Failed to check for device plugin exit, retrying")
} else {
log.Log.Info("restartDevicePluginPod(): waiting for device plugin pod to exit", "pod-name", podToDelete)
}
return false, nil
}, dn.stopCh); err != nil {
log.Log.Error(err, "restartDevicePluginPod(): failed to wait for checking pod deletion")
return err
}
}
return nil
}
func (dn *Daemon) rebootNode() {
log.Log.Info("rebootNode(): trigger node reboot")
exit, err := dn.HostHelpers.Chroot(consts.Host)
if err != nil {
log.Log.Error(err, "rebootNode(): chroot command failed")
}
defer exit()
// creates a new transient systemd unit to reboot the system.
// We explictily try to stop kubelet.service first, before anything else; this
// way we ensure the rest of system stays running, because kubelet may need
// to do "graceful" shutdown by e.g. de-registering with a load balancer.
// However note we use `;` instead of `&&` so we keep rebooting even
// if kubelet failed to shutdown - that way the machine will still eventually reboot
// as systemd will time out the stop invocation.
stdOut, StdErr, err := dn.HostHelpers.RunCommand("systemd-run", "--unit", "sriov-network-config-daemon-reboot",
"--description", "sriov-network-config-daemon reboot node", "/bin/sh", "-c", "systemctl stop kubelet.service; reboot")
if err != nil {
log.Log.Error(err, "failed to reboot node", "stdOut", stdOut, "StdErr", StdErr)
}
}
func (dn *Daemon) prepareNMUdevRule() error {
// we need to remove the Red Hat Virtio network device from the udev rule configuration
// if we don't remove it when running the config-daemon on a virtual node it will disconnect the node after a reboot
// even that the operator should not be installed on virtual environments that are not openstack
// we should not destroy the cluster if the operator is installed there
supportedVfIds := []string{}
for _, vfID := range sriovnetworkv1.GetSupportedVfIds() {
if vfID == "0x1000" || vfID == "0x1041" {
continue
}
supportedVfIds = append(supportedVfIds, vfID)
}
return dn.HostHelpers.PrepareNMUdevRule(supportedVfIds)
}
// isDrainCompleted returns true if the current-state annotation is drain completed
func (dn *Daemon) isDrainCompleted() bool {
return utils.ObjectHasAnnotation(dn.desiredNodeState, consts.NodeStateDrainAnnotationCurrent, consts.DrainComplete)
}