@@ -21,7 +21,7 @@ import (
21
21
"context"
22
22
"fmt"
23
23
"reflect"
24
- "sort "
24
+ "slices "
25
25
"strconv"
26
26
"strings"
27
27
"time"
@@ -42,64 +42,93 @@ import (
42
42
"sigs.k8s.io/cluster-api/util/conversion"
43
43
)
44
44
45
- // MachineSetsByDecreasingReplicas sorts the list of MachineSets in decreasing order of replicas,
46
- // using creation time (ascending order) and name (alphabetical) as tie breakers.
47
- type MachineSetsByDecreasingReplicas []* clusterv1.MachineSet
45
+ // Sort functions in this file, use these constants to determine the sorting order.
46
+ const (
47
+ // Used to sort elements in Ascending order.
48
+ // Also used in cases, where the order is from older to newer occurrences.
49
+ ASCENDING = iota
48
50
49
- func (o MachineSetsByDecreasingReplicas ) Len () int { return len (o ) }
50
- func (o MachineSetsByDecreasingReplicas ) Swap (i , j int ) { o [i ], o [j ] = o [j ], o [i ] }
51
- func (o MachineSetsByDecreasingReplicas ) Less (i , j int ) bool {
52
- if o [i ].Spec .Replicas == nil {
53
- return false
54
- }
55
- if o [j ].Spec .Replicas == nil {
56
- return true
51
+ // Used to sort elements in Descending order.
52
+ // Also used in cases, where the order is from newer to older occurrences.
53
+ DESCENDING
54
+ )
55
+
56
+ // SortByCreationTimestamp sorts a list of MachineSet by creation timestamp, using their names as a tie breaker.
57
+ func SortByCreationTimestamp (a , b * clusterv1.MachineSet , order int ) int {
58
+ if b .CreationTimestamp .Equal (& a .CreationTimestamp ) {
59
+ if b .Name > a .Name {
60
+ if order == ASCENDING {
61
+ return - 1
62
+ }
63
+ return 1
64
+ } else if b .Name < a .Name {
65
+ if order == ASCENDING {
66
+ return 1
67
+ }
68
+ return - 1
69
+ }
70
+ return 0
57
71
}
58
- if * o [i ].Spec .Replicas == * o [j ].Spec .Replicas {
59
- if o [i ].CreationTimestamp .Equal (& o [j ].CreationTimestamp ) {
60
- return o [i ].Name < o [j ].Name
72
+ if a .CreationTimestamp .Before (& b .CreationTimestamp ) {
73
+ if order == ASCENDING {
74
+ return - 1
75
+ }
76
+ return 1
77
+ } else if b .CreationTimestamp .Before (& a .CreationTimestamp ) {
78
+ if order == ASCENDING {
79
+ return 1
61
80
}
62
- return o [ i ]. CreationTimestamp . Before ( & o [ j ]. CreationTimestamp )
81
+ return - 1
63
82
}
64
- return * o [ i ]. Spec . Replicas > * o [ j ]. Spec . Replicas
83
+ return 0
65
84
}
66
85
67
- // MachineSetsByCreationTimestamp sorts a list of MachineSet by creation timestamp, using their names as a tie breaker.
68
- type MachineSetsByCreationTimestamp []* clusterv1.MachineSet
69
-
70
- func (o MachineSetsByCreationTimestamp ) Len () int { return len (o ) }
71
- func (o MachineSetsByCreationTimestamp ) Swap (i , j int ) { o [i ], o [j ] = o [j ], o [i ] }
72
- func (o MachineSetsByCreationTimestamp ) Less (i , j int ) bool {
73
- if o [i ].CreationTimestamp .Equal (& o [j ].CreationTimestamp ) {
74
- return o [i ].Name < o [j ].Name
86
+ // SortBySize sorts a list of MachineSet by size in descending order, using their creation timestamp or name as a tie breaker.
87
+ // By using the creation timestamp, this sorts from old to new machine sets if order is ASCENDING or new to old machine sets if order is DESCENDING.
88
+ func SortBySize (a , b * clusterv1.MachineSet , order int ) int {
89
+ if * (a .Spec .Replicas ) == * (b .Spec .Replicas ) {
90
+ if order == ASCENDING {
91
+ if a .CreationTimestamp .Before (& b .CreationTimestamp ) {
92
+ return - 1
93
+ }
94
+ return 1
95
+ }
96
+ if b .CreationTimestamp .Before (& a .CreationTimestamp ) {
97
+ return - 1
98
+ }
99
+ return 1
75
100
}
76
- return o [i ].CreationTimestamp .Before (& o [j ].CreationTimestamp )
77
- }
78
-
79
- // MachineSetsBySizeOlder sorts a list of MachineSet by size in descending order, using their creation timestamp or name as a tie breaker.
80
- // By using the creation timestamp, this sorts from old to new machine sets.
81
- type MachineSetsBySizeOlder []* clusterv1.MachineSet
82
-
83
- func (o MachineSetsBySizeOlder ) Len () int { return len (o ) }
84
- func (o MachineSetsBySizeOlder ) Swap (i , j int ) { o [i ], o [j ] = o [j ], o [i ] }
85
- func (o MachineSetsBySizeOlder ) Less (i , j int ) bool {
86
- if * (o [i ].Spec .Replicas ) == * (o [j ].Spec .Replicas ) {
87
- return o [i ].CreationTimestamp .Before (& o [j ].CreationTimestamp )
101
+ if * (a .Spec .Replicas ) > * (b .Spec .Replicas ) {
102
+ return - 1
88
103
}
89
- return * ( o [ i ]. Spec . Replicas ) > * ( o [ j ]. Spec . Replicas )
104
+ return 1
90
105
}
91
106
92
- // MachineSetsBySizeNewer sorts a list of MachineSet by size in descending order, using their creation timestamp or name as a tie breaker.
93
- // By using the creation timestamp, this sorts from new to old machine sets.
94
- type MachineSetsBySizeNewer []* clusterv1.MachineSet
95
-
96
- func (o MachineSetsBySizeNewer ) Len () int { return len (o ) }
97
- func (o MachineSetsBySizeNewer ) Swap (i , j int ) { o [i ], o [j ] = o [j ], o [i ] }
98
- func (o MachineSetsBySizeNewer ) Less (i , j int ) bool {
99
- if * (o [i ].Spec .Replicas ) == * (o [j ].Spec .Replicas ) {
100
- return o [j ].CreationTimestamp .Before (& o [i ].CreationTimestamp )
107
+ // SortByDecreasingReplicas sorts the list of MachineSets in decreasing order of replicas,
108
+ // using creation time (ascending order) and name (alphabetical) as tie breakers.
109
+ func SortByDecreasingReplicas (a , b * clusterv1.MachineSet ) int {
110
+ if a .Spec .Replicas == nil {
111
+ return 1
112
+ }
113
+ if b .Spec .Replicas == nil {
114
+ return - 1
115
+ }
116
+ if * a .Spec .Replicas == * b .Spec .Replicas {
117
+ if a .CreationTimestamp .Equal (& b .CreationTimestamp ) {
118
+ if a .Name < b .Name {
119
+ return - 1
120
+ }
121
+ return 1
122
+ }
123
+ if a .CreationTimestamp .Before (& b .CreationTimestamp ) {
124
+ return - 1
125
+ }
126
+ return 1
127
+ }
128
+ if * a .Spec .Replicas > * b .Spec .Replicas {
129
+ return - 1
101
130
}
102
- return * ( o [ i ]. Spec . Replicas ) > * ( o [ j ]. Spec . Replicas )
131
+ return 1
103
132
}
104
133
105
134
// SetDeploymentRevision updates the revision for a deployment.
@@ -256,7 +285,9 @@ func FindOneActiveOrLatest(newMS *clusterv1.MachineSet, oldMSs []*clusterv1.Mach
256
285
return nil
257
286
}
258
287
259
- sort .Sort (sort .Reverse (MachineSetsByCreationTimestamp (oldMSs )))
288
+ slices .SortFunc (oldMSs , func (a , b * clusterv1.MachineSet ) int {
289
+ return SortByCreationTimestamp (a , b , DESCENDING )
290
+ })
260
291
allMSs := FilterActiveMachineSets (append (oldMSs , newMS ))
261
292
262
293
switch len (allMSs ) {
@@ -467,7 +498,7 @@ func FindNewMachineSet(deployment *clusterv1.MachineDeployment, msList []*cluste
467
498
// having more than one new MachineSets that have the same template,
468
499
// see https://github.com/kubernetes/kubernetes/issues/40415
469
500
// We deterministically choose the oldest new MachineSet with matching template hash.
470
- sort . Sort ( MachineSetsByDecreasingReplicas ( msList ) )
501
+ slices . SortFunc ( msList , SortByDecreasingReplicas )
471
502
472
503
var matchingMachineSets []* clusterv1.MachineSet
473
504
var diffs []string
0 commit comments