@@ -20,13 +20,13 @@ import (
20
20
"context"
21
21
"fmt"
22
22
23
- "github.com/pkg/errors"
24
23
corev1 "k8s.io/api/core/v1"
25
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
25
"k8s.io/apimachinery/pkg/labels"
27
26
"k8s.io/klog"
28
27
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
29
28
"sigs.k8s.io/cluster-api/pkg/controller/noderefutil"
29
+ "sigs.k8s.io/cluster-api/pkg/controller/remote"
30
30
"sigs.k8s.io/controller-runtime/pkg/client"
31
31
)
32
32
@@ -37,6 +37,7 @@ const (
37
37
38
38
func (c * ReconcileMachineSet ) calculateStatus (ms * v1alpha1.MachineSet , filteredMachines []* v1alpha1.Machine ) v1alpha1.MachineSetStatus {
39
39
newStatus := ms .Status
40
+
40
41
// Count the number of machines that have labels matching the labels of the machine
41
42
// template of the replica set, the matching machines may have more
42
43
// labels than are in the template. Because the label of machineTemplateSpec is
@@ -46,15 +47,28 @@ func (c *ReconcileMachineSet) calculateStatus(ms *v1alpha1.MachineSet, filteredM
46
47
readyReplicasCount := 0
47
48
availableReplicasCount := 0
48
49
templateLabel := labels .Set (ms .Spec .Template .Labels ).AsSelectorPreValidated ()
50
+
51
+ // Retrieve Cluster, if any.
52
+ cluster , _ := c .getCluster (ms )
53
+
49
54
for _ , machine := range filteredMachines {
50
55
if templateLabel .Matches (labels .Set (machine .Labels )) {
51
56
fullyLabeledReplicasCount ++
52
57
}
53
- node , err := c .getMachineNode (machine )
58
+
59
+ if machine .Status .NodeRef == nil {
60
+ klog .Warningf ("Unable to retrieve Node status for Machine %q in namespace %q: missing NodeRef" ,
61
+ machine .Name , machine .Namespace )
62
+ continue
63
+ }
64
+
65
+ node , err := c .getMachineNode (cluster , machine )
54
66
if err != nil {
55
- klog .V (4 ).Infof ("Unable to get node for machine %v, %v" , machine .Name , err )
67
+ klog .Warningf ("Unable to retrieve Node status for Machine %q in namespace %q: %v" ,
68
+ machine .Name , machine .Namespace , err )
56
69
continue
57
70
}
71
+
58
72
if noderefutil .IsNodeReady (node ) {
59
73
readyReplicasCount ++
60
74
if noderefutil .IsNodeAvailable (node , ms .Spec .MinReadySeconds , metav1 .Now ()) {
@@ -122,13 +136,24 @@ func updateMachineSetStatus(c client.Client, ms *v1alpha1.MachineSet, newStatus
122
136
return nil , updateErr
123
137
}
124
138
125
- func (c * ReconcileMachineSet ) getMachineNode (machine * v1alpha1.Machine ) (* corev1.Node , error ) {
126
- nodeRef := machine .Status .NodeRef
127
- if nodeRef == nil {
128
- return nil , errors .New ("machine has no node ref" )
139
+ func (c * ReconcileMachineSet ) getMachineNode (cluster * v1alpha1.Cluster , machine * v1alpha1.Machine ) (* corev1.Node , error ) {
140
+ if cluster == nil {
141
+ // Try to retrieve the Node from the local cluster, if no Cluster reference is found.
142
+ node := & corev1.Node {}
143
+ err := c .Client .Get (context .Background (), client.ObjectKey {Name : machine .Status .NodeRef .Name }, node )
144
+ return node , err
145
+ }
146
+
147
+ // Otherwise, proceed to get the remote cluster client and get the Node.
148
+ remoteClient , err := remote .NewClusterClient (c .Client , cluster )
149
+ if err != nil {
150
+ return nil , err
151
+ }
152
+
153
+ corev1Remote , err := remoteClient .CoreV1 ()
154
+ if err != nil {
155
+ return nil , err
129
156
}
130
157
131
- node := & corev1.Node {}
132
- err := c .Client .Get (context .Background (), client.ObjectKey {Name : nodeRef .Name }, node )
133
- return node , err
158
+ return corev1Remote .Nodes ().Get (machine .Status .NodeRef .Name , metav1.GetOptions {})
134
159
}
0 commit comments