@@ -19,6 +19,7 @@ package machineset
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "sort"
22
23
"strings"
23
24
"time"
24
25
@@ -45,7 +46,7 @@ func (r *Reconciler) reconcileV1Beta2Status(ctx context.Context, s *scope) {
45
46
// Conditions
46
47
47
48
// Update the ScalingUp and ScalingDown condition.
48
- setScalingUpCondition (ctx , s .machineSet , s .machines , s . bootstrapObjectNotFound , s .infrastructureObjectNotFound , s .getAndAdoptMachinesForMachineSetSucceeded )
49
+ setScalingUpCondition (ctx , s .machineSet , s .bootstrapObjectNotFound , s .infrastructureObjectNotFound , s .getAndAdoptMachinesForMachineSetSucceeded )
49
50
setScalingDownCondition (ctx , s .machineSet , s .machines , s .getAndAdoptMachinesForMachineSetSucceeded )
50
51
51
52
// MachinesReady condition: aggregate the Machine's Ready condition.
@@ -85,7 +86,7 @@ func setReplicas(_ context.Context, ms *clusterv1.MachineSet, machines []*cluste
85
86
ms .Status .V1Beta2 .UpToDateReplicas = ptr .To (upToDateReplicas )
86
87
}
87
88
88
- func setScalingUpCondition (_ context.Context , ms * clusterv1.MachineSet , machines [] * clusterv1. Machine , bootstrapObjectNotFound , infrastructureObjectNotFound , getAndAdoptMachinesForMachineSetSucceeded bool ) {
89
+ func setScalingUpCondition (_ context.Context , ms * clusterv1.MachineSet , bootstrapObjectNotFound , infrastructureObjectNotFound , getAndAdoptMachinesForMachineSetSucceeded bool ) {
89
90
// If we got unexpected errors in listing the machines (this should never happen), surface them.
90
91
if ! getAndAdoptMachinesForMachineSetSucceeded {
91
92
v1beta2conditions .Set (ms , metav1.Condition {
@@ -118,7 +119,7 @@ func setScalingUpCondition(_ context.Context, ms *clusterv1.MachineSet, machines
118
119
if currentReplicas >= desiredReplicas {
119
120
var message string
120
121
if missingReferencesMessage != "" {
121
- message = fmt .Sprintf ("Scaling up can't happen %s" , missingReferencesMessage )
122
+ message = fmt .Sprintf ("Scaling up would be blocked %s" , missingReferencesMessage )
122
123
}
123
124
v1beta2conditions .Set (ms , metav1.Condition {
124
125
Type : clusterv1 .MachineSetScalingUpV1Beta2Condition ,
@@ -130,26 +131,16 @@ func setScalingUpCondition(_ context.Context, ms *clusterv1.MachineSet, machines
130
131
}
131
132
132
133
// Scaling up.
134
+ message := fmt .Sprintf ("Scaling up from %d to %d replicas" , currentReplicas , desiredReplicas )
135
+ if missingReferencesMessage != "" {
136
+ message += fmt .Sprintf (" is blocked %s" , missingReferencesMessage )
137
+ }
133
138
v1beta2conditions .Set (ms , metav1.Condition {
134
139
Type : clusterv1 .MachineSetScalingUpV1Beta2Condition ,
135
140
Status : metav1 .ConditionTrue ,
136
141
Reason : clusterv1 .MachineSetScalingUpV1Beta2Reason ,
137
142
// Message: message,
138
- Message : bulletMessage (
139
- fmt .Sprintf ("Scaling up from %d to %d replicas" , currentReplicas , desiredReplicas ),
140
- aggregateMachinesFunc (
141
- machines ,
142
- func (machine * clusterv1.Machine ) bool {
143
- return machine .Status .NodeRef == nil && time .Since (machine .GetCreationTimestamp ().Time ) >= time .Minute * 30
144
- },
145
- "not reporting a .status.nodeRef by more than 30 minutes" ,
146
- ),
147
- func () (message string ) {
148
- if missingReferencesMessage != "" {
149
- message = fmt .Sprintf ("%s is blocked %s" , message , missingReferencesMessage )
150
- }
151
- return message
152
- }),
143
+ Message : message ,
153
144
})
154
145
}
155
146
@@ -166,7 +157,6 @@ func setScalingDownCondition(_ context.Context, ms *clusterv1.MachineSet, machin
166
157
}
167
158
168
159
// Surface if .spec.replicas is not yet set (this should never happen).
169
- // This could e.g. be the case when using autoscaling with MachineSets.
170
160
if ms .Spec .Replicas == nil {
171
161
v1beta2conditions .Set (ms , metav1.Condition {
172
162
Type : clusterv1 .MachineSetScalingDownV1Beta2Condition ,
@@ -183,21 +173,17 @@ func setScalingDownCondition(_ context.Context, ms *clusterv1.MachineSet, machin
183
173
}
184
174
185
175
// Scaling down.
176
+ message := fmt .Sprintf ("Scaling down from %d to %d replicas" , len (machines ), desiredReplicas )
177
+ staleMessage := aggregateStaleMachines (machines )
178
+ if staleMessage != "" {
179
+ message += fmt .Sprintf (" and %s" , staleMessage )
180
+ }
186
181
if int32 (len (machines )) > (desiredReplicas ) {
187
182
v1beta2conditions .Set (ms , metav1.Condition {
188
- Type : clusterv1 .MachineSetScalingDownV1Beta2Condition ,
189
- Status : metav1 .ConditionTrue ,
190
- Reason : clusterv1 .MachineSetScalingDownV1Beta2Reason ,
191
- Message : bulletMessage (
192
- fmt .Sprintf ("Scaling down from %d to %d replicas" , len (machines ), desiredReplicas ),
193
- aggregateMachinesFunc (
194
- machines ,
195
- func (machine * clusterv1.Machine ) bool {
196
- return ! machine .GetDeletionTimestamp ().IsZero () && time .Since (machine .GetDeletionTimestamp ().Time ) >= time .Minute * 30
197
- },
198
- "reported in deletion by more than 30 minutes" ,
199
- ),
200
- ),
183
+ Type : clusterv1 .MachineSetScalingDownV1Beta2Condition ,
184
+ Status : metav1 .ConditionTrue ,
185
+ Reason : clusterv1 .MachineSetScalingDownV1Beta2Reason ,
186
+ Message : message ,
201
187
})
202
188
return
203
189
}
@@ -310,41 +296,32 @@ func calculateMissingReferencesMessage(ms *clusterv1.MachineSet, bootstrapTempla
310
296
return fmt .Sprintf ("because %s do not exist" , strings .Join (missingObjects , " and " ))
311
297
}
312
298
313
- func bulletMessage (header string , bulletGenerators ... func () string ) string {
314
- lines := []string {}
315
-
316
- for _ , bulletbulletGenerator := range bulletGenerators {
317
- if s := bulletbulletGenerator (); s != "" {
318
- lines = append (lines , fmt .Sprintf ("* %s" , s ))
299
+ func aggregateStaleMachines (machines []* clusterv1.Machine ) string {
300
+ machineNames := []string {}
301
+ for _ , machine := range machines {
302
+ if ! machine .GetDeletionTimestamp ().IsZero () && time .Since (machine .GetDeletionTimestamp ().Time ) >= time .Minute * 30 {
303
+ machineNames = append (machineNames , machine .GetName ())
319
304
}
320
305
}
321
306
322
- if len (lines ) == 0 {
323
- return header
307
+ if len (machineNames ) == 0 {
308
+ return ""
324
309
}
325
310
326
- lines = append ([]string {header }, lines ... )
327
- return strings .Join (lines , "\n " )
328
- }
329
-
330
- func aggregateMachinesFunc (machines []* clusterv1.Machine , filter func (* clusterv1.Machine ) bool , suffix string ) func () string {
331
- return func () string {
332
- machineNames := []string {}
333
- for _ , machine := range machines {
334
- if filter (machine ) {
335
- machineNames = append (machineNames , machine .GetName ())
336
- }
337
- }
338
-
339
- if len (machineNames ) == 0 {
340
- return ""
341
- }
311
+ message := "Machine"
312
+ if len (machineNames ) > 1 {
313
+ message += "s"
314
+ }
342
315
343
- prefix := "Machine"
344
- if len (machineNames ) > 1 {
345
- prefix += "s"
346
- }
316
+ sort .Strings (machineNames )
317
+ message += " " + clog .ListToString (machineNames , func (s string ) string { return s }, 3 )
347
318
348
- return strings .Join ([]string {prefix , clog .StringListToString (machineNames ), suffix }, " " )
319
+ if len (machineNames ) == 1 {
320
+ message += " is "
321
+ } else {
322
+ message += " are "
349
323
}
324
+ message += "in deletion by more than 30 minutes"
325
+
326
+ return message
350
327
}
0 commit comments