@@ -536,99 +536,86 @@ func RuntimeServiceCondition(nowFunc func() time.Time, // typically Kubelet.cloc
536
536
) Setter {
537
537
return func (node * v1.Node ) error {
538
538
currentTime := metav1 .NewTime (nowFunc ())
539
- var containerRuntimeCondition * v1.NodeCondition
540
- var vmRuntimeCondition * v1.NodeCondition
541
-
542
- // Check if node runtime ready condition already exists and if it does, just pick it up for update.
543
- for i := range node .Status .Conditions {
544
- if node .Status .Conditions [i ].Type == v1 .NodeContainerRuntimeReady {
545
- containerRuntimeCondition = & node .Status .Conditions [i ]
546
- }
547
- if node .Status .Conditions [i ].Type == v1 .NodeVmRuntimeReady {
548
- vmRuntimeCondition = & node .Status .Conditions [i ]
549
- }
550
- }
551
-
552
- newContainerCondition := false
553
- newVmCondition := false
554
-
555
- // If the NodeMemoryPressure condition doesn't exist, create one
556
- if vmRuntimeCondition == nil {
557
- vmRuntimeCondition = & v1.NodeCondition {
558
- Type : v1 .NodeVmRuntimeReady ,
559
- Status : v1 .ConditionUnknown ,
560
- }
561
- newVmCondition = true
562
- }
563
-
564
- if containerRuntimeCondition == nil {
565
- containerRuntimeCondition = & v1.NodeCondition {
566
- Type : v1 .NodeContainerRuntimeReady ,
567
- Status : v1 .ConditionUnknown ,
568
- }
569
- newContainerCondition = true
570
- }
571
-
572
- // Update the heartbeat time
573
- containerRuntimeCondition .LastHeartbeatTime = currentTime
574
- vmRuntimeCondition .LastHeartbeatTime = currentTime
575
-
576
539
runtimeStatuses , err := runtimeServiceStateFunc ()
577
540
if err != nil {
578
541
return err
579
542
}
580
543
544
+ // todo: we may need to lower log level to reduce unimportant logging data in production
581
545
klog .Infof ("runtime service status map: %v" , runtimeStatuses )
582
546
583
- // get the runtime status by workload types
547
+ // get the runtime status of container & vm
548
+ var containerRuntimeStatus , vmRuntimeStatus map [string ]bool
584
549
for workloadType , runtimeServicesStatus := range runtimeStatuses {
585
- klog .Infof ("runtime service [%s] map: [%v]" , workloadType , runtimeServicesStatus )
586
550
switch {
587
551
case workloadType == "container" :
588
- containerRuntimeCondition = getCurrentRuntimeReadiness (containerRuntimeCondition , workloadType , runtimeServicesStatus ,
589
- recordEventFunc , currentTime )
590
-
552
+ containerRuntimeStatus = runtimeServicesStatus
591
553
case workloadType == "vm" :
592
- vmRuntimeCondition = getCurrentRuntimeReadiness (vmRuntimeCondition , workloadType , runtimeServicesStatus ,
593
- recordEventFunc , currentTime )
554
+ vmRuntimeStatus = runtimeServicesStatus
594
555
}
595
556
}
596
557
597
- if newVmCondition {
598
- vmRuntimeCondition .LastTransitionTime = currentTime
599
- node .Status .Conditions = append (node .Status .Conditions , * vmRuntimeCondition )
600
- }
601
- if newContainerCondition {
602
- containerRuntimeCondition .LastTransitionTime = currentTime
603
- node .Status .Conditions = append (node .Status .Conditions , * containerRuntimeCondition )
558
+ subConditionSetter := func (node * v1.Node , conditionType v1.NodeConditionType , workloadType string , runtimeStatus map [string ]bool ) {
559
+ var condition * v1.NodeCondition
560
+
561
+ // Check if node runtime ready condition already exists and if it does, just pick it up for update.
562
+ for i := range node .Status .Conditions {
563
+ if node .Status .Conditions [i ].Type == conditionType {
564
+ condition = & node .Status .Conditions [i ]
565
+ }
566
+ }
567
+
568
+ newCondition := false
569
+ // If the specified condition doesn't exist, create one
570
+ if condition == nil {
571
+ condition = & v1.NodeCondition {
572
+ Type : conditionType ,
573
+ Status : v1 .ConditionUnknown ,
574
+ LastTransitionTime : currentTime ,
575
+ }
576
+
577
+ newCondition = true
578
+ }
579
+
580
+ // Update the heartbeat time
581
+ condition .LastHeartbeatTime = currentTime
582
+
583
+ if runtimeStatus != nil {
584
+ if runtimeIsReady (runtimeStatus ) {
585
+ if condition .Status != v1 .ConditionTrue {
586
+ condition .Status = v1 .ConditionTrue
587
+ condition .Reason = fmt .Sprintf ("At least one %s runtime is ready" , workloadType )
588
+ condition .LastTransitionTime = currentTime
589
+ recordEventFunc (v1 .EventTypeNormal , fmt .Sprintf ("%s is ready" , conditionType ))
590
+ }
591
+ } else if condition .Status != v1 .ConditionFalse {
592
+ condition .Status = v1 .ConditionFalse
593
+ condition .Status = v1 .ConditionFalse
594
+ condition .Reason = fmt .Sprintf ("None of %s runtime is ready" , workloadType )
595
+ condition .LastTransitionTime = currentTime
596
+ recordEventFunc (v1 .EventTypeNormal , fmt .Sprintf ("%s is not ready" , conditionType ))
597
+ }
598
+ }
599
+
600
+ if newCondition {
601
+ node .Status .Conditions = append (node .Status .Conditions , * condition )
602
+ }
604
603
}
604
+
605
+ subConditionSetter (node , v1 .NodeVmRuntimeReady , "vm" , vmRuntimeStatus )
606
+ subConditionSetter (node , v1 .NodeContainerRuntimeReady , "container" , containerRuntimeStatus )
605
607
return nil
606
608
}
607
609
}
608
610
609
- func getCurrentRuntimeReadiness (runtimeCondition * v1.NodeCondition , workloadType string ,
610
- runtimeServiceStatus map [string ]bool , recordEventFunc func (eventType , event string ),
611
- currentTime metav1.Time ) * v1.NodeCondition {
612
- statusSet := false
611
+ func runtimeIsReady (runtimeServiceStatus map [string ]bool ) bool {
613
612
for _ , status := range runtimeServiceStatus {
614
613
if status == true {
615
- runtimeCondition .Status = v1 .ConditionTrue
616
- runtimeCondition .Reason = fmt .Sprintf ("At least one %s runtime is ready" , workloadType )
617
- recordEventFunc (v1 .EventTypeNormal , fmt .Sprintf ("%s is ready" , runtimeCondition .Type ))
618
- statusSet = true
619
- break
614
+ return true
620
615
}
621
616
}
622
617
623
- if statusSet != true {
624
- runtimeCondition .Status = v1 .ConditionFalse
625
- runtimeCondition .Reason = fmt .Sprintf ("None of %s runtime is ready" , workloadType )
626
- recordEventFunc (v1 .EventTypeNormal , fmt .Sprintf ("%s is not ready" , runtimeCondition .Type ))
627
- }
628
-
629
- runtimeCondition .LastTransitionTime = currentTime
630
-
631
- return runtimeCondition
618
+ return false
632
619
}
633
620
634
621
// MemoryPressureCondition returns a Setter that updates the v1.NodeMemoryPressure condition on the node.
0 commit comments