@@ -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
}
@@ -132,7 +135,7 @@ func setAvailableCondition(_ context.Context, machineDeployment *clusterv1.Machi
132
135
133
136
message := fmt .Sprintf ("%d available replicas, at least %d required" , * machineDeployment .Status .V1Beta2 .AvailableReplicas , minReplicasNeeded )
134
137
if machineDeployment .Spec .Strategy != nil && mdutil .IsRollingUpdate (machineDeployment ) && machineDeployment .Spec .Strategy .RollingUpdate != nil {
135
- message += fmt .Sprintf (" (spec.strategy.rollout.maxUnavailable is %s)" , machineDeployment .Spec .Strategy .RollingUpdate .MaxUnavailable )
138
+ message += fmt .Sprintf (" (spec.strategy.rollout.maxUnavailable is %s, spec.replicas is %d )" , machineDeployment .Spec .Strategy .RollingUpdate .MaxUnavailable , * machineDeployment . Spec . Replicas )
136
139
}
137
140
v1beta2conditions .Set (machineDeployment , metav1.Condition {
138
141
Type : clusterv1 .MachineDeploymentAvailableV1Beta2Condition ,
@@ -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,11 @@ 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
+ if len (machines ) == 1 {
410
+ message = fmt .Sprintf ("Deleting %d Machine" , len (machines ))
411
+ } else {
412
+ message = fmt .Sprintf ("Deleting %d Machines" , len (machines ))
413
+ }
405
414
staleMessage := aggregateStaleMachines (machines )
406
415
if staleMessage != "" {
407
416
message += fmt .Sprintf (" and %s" , staleMessage )
@@ -413,7 +422,7 @@ func setDeletingCondition(_ context.Context, machineDeployment *clusterv1.Machin
413
422
}
414
423
v1beta2conditions .Set (machineDeployment , metav1.Condition {
415
424
Type : clusterv1 .MachineDeploymentDeletingV1Beta2Condition ,
416
- Status : metav1 .ConditionFalse ,
425
+ Status : metav1 .ConditionTrue ,
417
426
Reason : clusterv1 .MachineDeploymentDeletingDeletionTimestampSetV1Beta2Reason ,
418
427
Message : message ,
419
428
})
@@ -474,6 +483,10 @@ func aggregateStaleMachines(machines collections.Machines) string {
474
483
}
475
484
476
485
func aggregateUnhealthyMachines (machines collections.Machines ) string {
486
+ if len (machines ) == 0 {
487
+ return ""
488
+ }
489
+
477
490
machineNames := []string {}
478
491
for _ , machine := range machines {
479
492
machineNames = append (machineNames , machine .GetName ())
@@ -496,7 +509,7 @@ func aggregateUnhealthyMachines(machines collections.Machines) string {
496
509
} else {
497
510
message += " are "
498
511
}
499
- message += "not healthy (not to be remediated by KCP )"
512
+ message += "not healthy (not to be remediated by MachineDeployment/MachineSet )"
500
513
501
514
return message
502
515
}
0 commit comments