Skip to content

Commit fe3b43a

Browse files
committed
improve and first unit tests
1 parent 0caeb65 commit fe3b43a

File tree

2 files changed

+501
-61
lines changed

2 files changed

+501
-61
lines changed

internal/controllers/machineset/machineset_controller_status.go

+38-61
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package machineset
1919
import (
2020
"context"
2121
"fmt"
22+
"sort"
2223
"strings"
2324
"time"
2425

@@ -45,7 +46,7 @@ func (r *Reconciler) reconcileV1Beta2Status(ctx context.Context, s *scope) {
4546
// Conditions
4647

4748
// 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)
4950
setScalingDownCondition(ctx, s.machineSet, s.machines, s.getAndAdoptMachinesForMachineSetSucceeded)
5051

5152
// MachinesReady condition: aggregate the Machine's Ready condition.
@@ -85,7 +86,7 @@ func setReplicas(_ context.Context, ms *clusterv1.MachineSet, machines []*cluste
8586
ms.Status.V1Beta2.UpToDateReplicas = ptr.To(upToDateReplicas)
8687
}
8788

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) {
8990
// If we got unexpected errors in listing the machines (this should never happen), surface them.
9091
if !getAndAdoptMachinesForMachineSetSucceeded {
9192
v1beta2conditions.Set(ms, metav1.Condition{
@@ -118,7 +119,7 @@ func setScalingUpCondition(_ context.Context, ms *clusterv1.MachineSet, machines
118119
if currentReplicas >= desiredReplicas {
119120
var message string
120121
if missingReferencesMessage != "" {
121-
message = fmt.Sprintf("Scaling up can't happen %s", missingReferencesMessage)
122+
message = fmt.Sprintf("Scaling up would be blocked %s", missingReferencesMessage)
122123
}
123124
v1beta2conditions.Set(ms, metav1.Condition{
124125
Type: clusterv1.MachineSetScalingUpV1Beta2Condition,
@@ -130,26 +131,16 @@ func setScalingUpCondition(_ context.Context, ms *clusterv1.MachineSet, machines
130131
}
131132

132133
// 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+
}
133138
v1beta2conditions.Set(ms, metav1.Condition{
134139
Type: clusterv1.MachineSetScalingUpV1Beta2Condition,
135140
Status: metav1.ConditionTrue,
136141
Reason: clusterv1.MachineSetScalingUpV1Beta2Reason,
137142
// 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,
153144
})
154145
}
155146

@@ -166,7 +157,6 @@ func setScalingDownCondition(_ context.Context, ms *clusterv1.MachineSet, machin
166157
}
167158

168159
// 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.
170160
if ms.Spec.Replicas == nil {
171161
v1beta2conditions.Set(ms, metav1.Condition{
172162
Type: clusterv1.MachineSetScalingDownV1Beta2Condition,
@@ -183,21 +173,17 @@ func setScalingDownCondition(_ context.Context, ms *clusterv1.MachineSet, machin
183173
}
184174

185175
// 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+
}
186181
if int32(len(machines)) > (desiredReplicas) {
187182
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,
201187
})
202188
return
203189
}
@@ -310,41 +296,32 @@ func calculateMissingReferencesMessage(ms *clusterv1.MachineSet, bootstrapTempla
310296
return fmt.Sprintf("because %s do not exist", strings.Join(missingObjects, " and "))
311297
}
312298

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())
319304
}
320305
}
321306

322-
if len(lines) == 0 {
323-
return header
307+
if len(machineNames) == 0 {
308+
return ""
324309
}
325310

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+
}
342315

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)
347318

348-
return strings.Join([]string{prefix, clog.StringListToString(machineNames), suffix}, " ")
319+
if len(machineNames) == 1 {
320+
message += " is "
321+
} else {
322+
message += " are "
349323
}
324+
message += "in deletion by more than 30 minutes"
325+
326+
return message
350327
}

0 commit comments

Comments
 (0)