@@ -37,35 +37,38 @@ import (
37
37
38
38
func (r * Reconciler ) updateStatus (ctx context.Context , s * scope ) (retErr error ) {
39
39
// Get all Machines controlled by this MachineDeployment.
40
- var machines , machinesToBeRemediated , unHealthyMachines collections.Machines
40
+ var machines , machinesToBeRemediated , unhealthyMachines collections.Machines
41
+ var getMachinesSucceeded bool
41
42
if selectorMap , err := metav1 .LabelSelectorAsMap (& s .machineDeployment .Spec .Selector ); err == nil {
42
43
machineList := & clusterv1.MachineList {}
43
44
if err := r .Client .List (ctx , machineList , client .InNamespace (s .machineDeployment .Namespace ), client .MatchingLabels (selectorMap )); err != nil {
44
45
retErr = errors .Wrap (err , "failed to list machines" )
46
+ } else {
47
+ getMachinesSucceeded = true
48
+ machines = collections .FromMachineList (machineList )
49
+ machinesToBeRemediated = machines .Filter (collections .IsUnhealthyAndOwnerRemediated )
50
+ unhealthyMachines = machines .Filter (collections .IsUnhealthy )
45
51
}
46
- machines = collections .FromMachineList (machineList )
47
- machinesToBeRemediated = machines .Filter (collections .IsUnhealthyAndOwnerRemediated )
48
- unHealthyMachines = machines .Filter (collections .IsUnhealthy )
49
52
} else {
50
53
retErr = errors .Wrap (err , "failed to convert label selector to a map" )
51
54
}
52
55
53
- // If the controller failed to read MachineSets, do not update replica counters.
54
- if ! s .getAndAdoptMachineSetsForDeploymentSucceeded {
56
+ // If the controller could read MachineSets, update replica counters.
57
+ if s .getAndAdoptMachineSetsForDeploymentSucceeded {
55
58
setReplicas (s .machineDeployment , s .machineSets )
56
59
}
57
60
58
61
setAvailableCondition (ctx , s .machineDeployment , s .getAndAdoptMachineSetsForDeploymentSucceeded )
59
62
60
63
setScalingUpCondition (ctx , s .machineDeployment , s .machineSets , s .bootstrapTemplateNotFound , s .infrastructureTemplateNotFound , s .getAndAdoptMachineSetsForDeploymentSucceeded )
61
- setScalingDownCondition (ctx , s .machineDeployment , s .machineSets , machines , s .getAndAdoptMachineSetsForDeploymentSucceeded )
64
+ setScalingDownCondition (ctx , s .machineDeployment , s .machineSets , machines , s .getAndAdoptMachineSetsForDeploymentSucceeded , getMachinesSucceeded )
62
65
63
- setMachinesReadyCondition (ctx , s .machineDeployment , machines )
64
- setMachinesUpToDateCondition (ctx , s .machineDeployment , machines )
66
+ setMachinesReadyCondition (ctx , s .machineDeployment , machines , getMachinesSucceeded )
67
+ setMachinesUpToDateCondition (ctx , s .machineDeployment , machines , getMachinesSucceeded )
65
68
66
- setRemediatingCondition (ctx , s .machineDeployment , machinesToBeRemediated , unHealthyMachines )
69
+ setRemediatingCondition (ctx , s .machineDeployment , machinesToBeRemediated , unhealthyMachines , getMachinesSucceeded )
67
70
68
- setDeletingCondition (ctx , s .machineDeployment , s .machineSets , machines , s .getAndAdoptMachineSetsForDeploymentSucceeded )
71
+ setDeletingCondition (ctx , s .machineDeployment , s .machineSets , machines , s .getAndAdoptMachineSetsForDeploymentSucceeded , getMachinesSucceeded )
69
72
70
73
return retErr
71
74
}
@@ -168,7 +171,7 @@ func setScalingUpCondition(_ context.Context, machineDeployment *clusterv1.Machi
168
171
if ! machineDeployment .DeletionTimestamp .IsZero () {
169
172
desiredReplicas = 0
170
173
}
171
- currentReplicas := mdutil .GetReplicaCountForMachineSets (machineSets )
174
+ currentReplicas := mdutil .GetActualReplicaCountForMachineSets (machineSets )
172
175
173
176
missingReferencesMessage := calculateMissingReferencesMessage (machineDeployment , bootstrapObjectNotFound , infrastructureObjectNotFound )
174
177
@@ -199,7 +202,7 @@ func setScalingUpCondition(_ context.Context, machineDeployment *clusterv1.Machi
199
202
})
200
203
}
201
204
202
- func setScalingDownCondition (_ context.Context , machineDeployment * clusterv1.MachineDeployment , machineSets []* clusterv1.MachineSet , machines collections.Machines , getAndAdoptMachineSetsForDeploymentSucceeded bool ) {
205
+ func setScalingDownCondition (_ context.Context , machineDeployment * clusterv1.MachineDeployment , machineSets []* clusterv1.MachineSet , machines collections.Machines , getAndAdoptMachineSetsForDeploymentSucceeded , getMachinesSucceeded bool ) {
203
206
// If we got unexpected errors in listing the machines sets (this should never happen), surface them.
204
207
if ! getAndAdoptMachineSetsForDeploymentSucceeded {
205
208
v1beta2conditions .Set (machineDeployment , metav1.Condition {
@@ -225,14 +228,16 @@ func setScalingDownCondition(_ context.Context, machineDeployment *clusterv1.Mac
225
228
if ! machineDeployment .DeletionTimestamp .IsZero () {
226
229
desiredReplicas = 0
227
230
}
228
- currentReplicas := mdutil .GetReplicaCountForMachineSets (machineSets )
231
+ currentReplicas := mdutil .GetActualReplicaCountForMachineSets (machineSets )
229
232
230
233
// Scaling down.
231
234
if currentReplicas > desiredReplicas {
232
235
message := fmt .Sprintf ("Scaling down from %d to %d replicas" , currentReplicas , desiredReplicas )
233
- staleMessage := aggregateStaleMachines (machines )
234
- if staleMessage != "" {
235
- message += fmt .Sprintf (" and %s" , staleMessage )
236
+ if getMachinesSucceeded {
237
+ staleMessage := aggregateStaleMachines (machines )
238
+ if staleMessage != "" {
239
+ message += fmt .Sprintf (" and %s" , staleMessage )
240
+ }
236
241
}
237
242
v1beta2conditions .Set (machineDeployment , metav1.Condition {
238
243
Type : clusterv1 .MachineDeploymentScalingDownV1Beta2Condition ,
@@ -251,10 +256,10 @@ func setScalingDownCondition(_ context.Context, machineDeployment *clusterv1.Mac
251
256
})
252
257
}
253
258
254
- func setMachinesReadyCondition (ctx context.Context , machineDeployment * clusterv1.MachineDeployment , machines collections.Machines ) {
259
+ func setMachinesReadyCondition (ctx context.Context , machineDeployment * clusterv1.MachineDeployment , machines collections.Machines , getMachinesSucceeded bool ) {
255
260
log := ctrl .LoggerFrom (ctx )
256
261
// If we got unexpected errors in listing the machines (this should never happen), surface them.
257
- if machines == nil {
262
+ if ! getMachinesSucceeded {
258
263
v1beta2conditions .Set (machineDeployment , metav1.Condition {
259
264
Type : clusterv1 .MachineDeploymentMachinesReadyV1Beta2Condition ,
260
265
Status : metav1 .ConditionUnknown ,
@@ -291,10 +296,10 @@ func setMachinesReadyCondition(ctx context.Context, machineDeployment *clusterv1
291
296
v1beta2conditions .Set (machineDeployment , * readyCondition )
292
297
}
293
298
294
- func setMachinesUpToDateCondition (ctx context.Context , machineDeployment * clusterv1.MachineDeployment , machines collections.Machines ) {
299
+ func setMachinesUpToDateCondition (ctx context.Context , machineDeployment * clusterv1.MachineDeployment , machines collections.Machines , getMachinesSucceeded bool ) {
295
300
log := ctrl .LoggerFrom (ctx )
296
301
// If we got unexpected errors in listing the machines (this should never happen), surface them.
297
- if machines == nil {
302
+ if ! getMachinesSucceeded {
298
303
v1beta2conditions .Set (machineDeployment , metav1.Condition {
299
304
Type : clusterv1 .MachineDeploymentMachinesUpToDateV1Beta2Condition ,
300
305
Status : metav1 .ConditionUnknown ,
@@ -331,8 +336,8 @@ func setMachinesUpToDateCondition(ctx context.Context, machineDeployment *cluste
331
336
v1beta2conditions .Set (machineDeployment , * upToDateCondition )
332
337
}
333
338
334
- func setRemediatingCondition (ctx context.Context , machineDeployment * clusterv1.MachineDeployment , machinesToBeRemediated , unhealthyMachines collections.Machines ) {
335
- if machinesToBeRemediated == nil || unhealthyMachines == nil {
339
+ func setRemediatingCondition (ctx context.Context , machineDeployment * clusterv1.MachineDeployment , machinesToBeRemediated , unhealthyMachines collections.Machines , getMachinesSucceeded bool ) {
340
+ if ! getMachinesSucceeded {
336
341
v1beta2conditions .Set (machineDeployment , metav1.Condition {
337
342
Type : clusterv1 .MachineDeploymentRemediatingV1Beta2Condition ,
338
343
Status : metav1 .ConditionUnknown ,
@@ -378,9 +383,9 @@ func setRemediatingCondition(ctx context.Context, machineDeployment *clusterv1.M
378
383
})
379
384
}
380
385
381
- func setDeletingCondition (_ context.Context , machineDeployment * clusterv1.MachineDeployment , machineSets []* clusterv1.MachineSet , machines collections.Machines , getAndAdoptMachineSetsForDeploymentSucceeded bool ) {
386
+ func setDeletingCondition (_ context.Context , machineDeployment * clusterv1.MachineDeployment , machineSets []* clusterv1.MachineSet , machines collections.Machines , getAndAdoptMachineSetsForDeploymentSucceeded , getMachinesSucceeded bool ) {
382
387
// If we got unexpected errors in listing the machines sets or machines (this should never happen), surface them.
383
- if ! getAndAdoptMachineSetsForDeploymentSucceeded || machines == nil {
388
+ if ! getAndAdoptMachineSetsForDeploymentSucceeded || ! getMachinesSucceeded {
384
389
v1beta2conditions .Set (machineDeployment , metav1.Condition {
385
390
Type : clusterv1 .MachineDeploymentDeletingV1Beta2Condition ,
386
391
Status : metav1 .ConditionUnknown ,
@@ -401,7 +406,7 @@ func setDeletingCondition(_ context.Context, machineDeployment *clusterv1.Machin
401
406
402
407
message := ""
403
408
if len (machines ) > 0 {
404
- message = fmt .Sprintf ("Deleting %d replicas " , len (machines ))
409
+ message = fmt .Sprintf ("Deleting %d Machines " , len (machines ))
405
410
staleMessage := aggregateStaleMachines (machines )
406
411
if staleMessage != "" {
407
412
message += fmt .Sprintf (" and %s" , staleMessage )
@@ -413,7 +418,7 @@ func setDeletingCondition(_ context.Context, machineDeployment *clusterv1.Machin
413
418
}
414
419
v1beta2conditions .Set (machineDeployment , metav1.Condition {
415
420
Type : clusterv1 .MachineDeploymentDeletingV1Beta2Condition ,
416
- Status : metav1 .ConditionFalse ,
421
+ Status : metav1 .ConditionTrue ,
417
422
Reason : clusterv1 .MachineDeploymentDeletingDeletionTimestampSetV1Beta2Reason ,
418
423
Message : message ,
419
424
})
@@ -474,6 +479,10 @@ func aggregateStaleMachines(machines collections.Machines) string {
474
479
}
475
480
476
481
func aggregateUnhealthyMachines (machines collections.Machines ) string {
482
+ if len (machines ) == 0 {
483
+ return ""
484
+ }
485
+
477
486
machineNames := []string {}
478
487
for _ , machine := range machines {
479
488
machineNames = append (machineNames , machine .GetName ())
0 commit comments