@@ -18,9 +18,10 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
22
+
21
23
"github.com/pkg/errors"
22
- apicorev1 "k8s.io/api/core/v1"
23
- kerrors "k8s.io/apimachinery/pkg/util/errors"
24
+ corev1 "k8s.io/api/core/v1"
24
25
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
25
26
"sigs.k8s.io/cluster-api/controllers/noderefutil"
26
27
"sigs.k8s.io/cluster-api/util"
@@ -52,35 +53,36 @@ func (r *MachineReconciler) reconcileNode(ctx context.Context, cluster *clusterv
52
53
node , err := r .getNode (remoteClient , providerID )
53
54
if err != nil {
54
55
logger .Error (err , "Failed to retrieve Node by ProviderID" )
55
- r .recorder .Event (machine , apicorev1 .EventTypeWarning , "Failed to retrieve Node by ProviderID" , err .Error ())
56
+ r .recorder .Event (machine , corev1 .EventTypeWarning , "Failed to retrieve Node by ProviderID" , err .Error ())
56
57
return reconcile.Result {}, err
57
58
}
58
59
if node == nil {
59
60
// While a NodeRef is set in the status, failing to get that node means the node is deleted.
60
61
// If Status.NodeRef is not set before, node still can be in the provisioning state.
61
62
if machine .Status .NodeRef != nil {
62
63
conditions .MarkFalse (machine , clusterv1 .MachineNodeHealthyCondition , clusterv1 .NodeNotFoundReason , clusterv1 .ConditionSeverityError , "" )
64
+ return reconcile.Result {}, errors .Wrapf (err , "no matching Node for Machine %q in namespace %q" , machine .Name , machine .Namespace )
63
65
}
64
- return reconcile.Result {}, errors .Wrapf (err , "no matching Node for Machine %q in namespace %q" , machine .Name , machine .Namespace )
66
+ conditions .MarkFalse (machine , clusterv1 .MachineNodeHealthyCondition , clusterv1 .NodeProvisioningReason , clusterv1 .ConditionSeverityWarning , "" )
67
+ return reconcile.Result {Requeue : true }, nil
65
68
}
66
69
67
70
// Set the Machine NodeRef.
68
71
if machine .Status .NodeRef == nil {
69
- machine .Status .NodeRef = & apicorev1 .ObjectReference {
72
+ machine .Status .NodeRef = & corev1 .ObjectReference {
70
73
Kind : node .Kind ,
71
74
APIVersion : node .APIVersion ,
72
75
Name : node .Name ,
73
76
UID : node .UID ,
74
77
}
75
78
logger .Info ("Set Machine's NodeRef" , "noderef" , machine .Status .NodeRef .Name )
76
- r .recorder .Event (machine , apicorev1 .EventTypeNormal , "SuccessfulSetNodeRef" , machine .Status .NodeRef .Name )
77
- conditions .MarkFalse (machine , clusterv1 .MachineNodeHealthyCondition , clusterv1 .NodeProvisioningReason , clusterv1 .ConditionSeverityWarning , "" )
78
- return reconcile.Result {}, nil
79
+ r .recorder .Event (machine , corev1 .EventTypeNormal , "SuccessfulSetNodeRef" , machine .Status .NodeRef .Name )
79
80
}
80
81
81
82
// Do the remaining node health checks, then set the node health to true if all checks pass.
82
- if err := summarizeNodeConditions (node ); err != nil {
83
- conditions .MarkFalse (machine , clusterv1 .MachineNodeHealthyCondition , clusterv1 .NodeConditionsFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
83
+ status , message := summarizeNodeConditions (node )
84
+ if status != corev1 .ConditionTrue {
85
+ conditions .MarkFalse (machine , clusterv1 .MachineNodeHealthyCondition , clusterv1 .NodeConditionsFailedReason , clusterv1 .ConditionSeverityWarning , message )
84
86
return reconcile.Result {}, nil
85
87
}
86
88
@@ -89,29 +91,40 @@ func (r *MachineReconciler) reconcileNode(ctx context.Context, cluster *clusterv
89
91
}
90
92
91
93
// summarizeNodeConditions checks if a Node is healthy and does not have any semantically negative Conditions.
92
- // Returns error if NodeMemoryPressure, NodeDiskPressure, or NodePIDPressure Conditions are false or Ready Condition is true.
93
- func summarizeNodeConditions (node * apicorev1.Node ) error {
94
- errList := []error {}
95
-
94
+ // Returns the summary of condition statuses (e.g., if all semantically correct, then status is true with nil message)
95
+ // and concatenate failed condition messages (semantically true conditions: NodeMemoryPressure/NodeDiskPressure/NodePIDPressure == false or Ready == true.)
96
+ func summarizeNodeConditions (node * corev1.Node ) (corev1.ConditionStatus , string ) {
97
+ status := corev1 .ConditionTrue
98
+ message := ""
96
99
for _ , condition := range node .Status .Conditions {
97
100
switch condition .Type {
98
- case apicorev1 .NodeMemoryPressure , apicorev1 .NodeDiskPressure , apicorev1 .NodePIDPressure :
99
- if condition .Status == apicorev1 .ConditionTrue {
100
- errList = append (errList , errors .Errorf ("Condition %s is true" , condition .Type ))
101
+ case corev1 .NodeMemoryPressure , corev1 .NodeDiskPressure , corev1 .NodePIDPressure :
102
+ if condition .Status != corev1 .ConditionFalse {
103
+ message += fmt .Sprintf ("Condition %s is %s" , condition .Type , condition .Status ) + ". "
104
+ if condition .Status == corev1 .ConditionUnknown {
105
+ status = corev1 .ConditionUnknown
106
+ continue
107
+ }
108
+ status = corev1 .ConditionFalse
101
109
}
102
- case apicorev1 .NodeReady :
103
- if condition .Status == apicorev1 .ConditionFalse {
104
- errList = append (errList , errors .Errorf ("Condition %s is false" , condition .Type ))
110
+ case corev1 .NodeReady :
111
+ if condition .Status != corev1 .ConditionTrue {
112
+ message += fmt .Sprintf ("Condition %s is %s" , condition .Type , condition .Status ) + ". "
113
+ if condition .Status == corev1 .ConditionUnknown {
114
+ status = corev1 .ConditionUnknown
115
+ continue
116
+ }
117
+ status = corev1 .ConditionFalse
105
118
}
106
119
}
107
120
}
108
- return kerrors . NewAggregate ( errList )
121
+ return status , message
109
122
}
110
123
111
- func (r * MachineReconciler ) getNode (c client.Reader , providerID * noderefutil.ProviderID ) (* apicorev1 .Node , error ) {
124
+ func (r * MachineReconciler ) getNode (c client.Reader , providerID * noderefutil.ProviderID ) (* corev1 .Node , error ) {
112
125
logger := r .Log .WithValues ("providerID" , providerID )
113
126
114
- nodeList := apicorev1 .NodeList {}
127
+ nodeList := corev1 .NodeList {}
115
128
for {
116
129
if err := c .List (context .TODO (), & nodeList , client .Continue (nodeList .Continue )); err != nil {
117
130
return nil , err
0 commit comments