@@ -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,91 @@ 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
+ const (
46
+ ASCENDING = iota
47
+ DESCENDING
48
+ OLD_TO_NEW
49
+ NEW_TO_OLD
50
+ )
48
51
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
52
+ // MachineSetsByCreationTimestamp sorts a list of MachineSet by creation timestamp, using their names as a tie breaker.
53
+ // type MachineSetsByCreationTimestamp []*clusterv1.MachineSet
54
+ func SortByCreationTimestamp (a , b * clusterv1.MachineSet , order int ) int {
55
+ if b .CreationTimestamp .Equal (& a .CreationTimestamp ) {
56
+ if b .Name > a .Name {
57
+ if order == ASCENDING {
58
+ return 1
59
+ }
60
+ return - 1
61
+ } else if b .Name < a .Name {
62
+ if order == ASCENDING {
63
+ return - 1
64
+ }
65
+ return 1
66
+ }
67
+ return 0
57
68
}
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
69
+ if a . CreationTimestamp . Before ( & b . CreationTimestamp ) {
70
+ if order == ASCENDING {
71
+ return 1
61
72
}
62
- return o [i ].CreationTimestamp .Before (& o [j ].CreationTimestamp )
73
+ return - 1
74
+ } else if b .CreationTimestamp .Before (& a .CreationTimestamp ) {
75
+ if order == ASCENDING {
76
+ return - 1
77
+ }
78
+ return 1
63
79
}
64
- return * o [ i ]. Spec . Replicas > * o [ j ]. Spec . Replicas
80
+ return 0
65
81
}
66
82
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
83
+ // SortBySize sorts a list of MachineSet by size in descending order, using their creation timestamp or name as a tie breaker.
84
+ // By using the creation timestamp, this sorts from old to new machine sets if order is OLD_TO_NEW or new to old machine sets if order is NEW_TO_OLD.
85
+ func SortBySize (a , b * clusterv1.MachineSet , order int ) int {
86
+ if * (a .Spec .Replicas ) == * (b .Spec .Replicas ) {
87
+ if order == OLD_TO_NEW {
88
+ if a .CreationTimestamp .Before (& b .CreationTimestamp ) {
89
+ return 1
90
+ }
91
+ return - 1
92
+ } else {
93
+ if b .CreationTimestamp .Before (& a .CreationTimestamp ) {
94
+ return 1
95
+ }
96
+ return - 1
97
+ }
75
98
}
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 )
99
+ if * (a .Spec .Replicas ) > * (b .Spec .Replicas ) {
100
+ return 1
88
101
}
89
- return * ( o [ i ]. Spec . Replicas ) > * ( o [ j ]. Spec . Replicas )
102
+ return - 1
90
103
}
91
104
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 )
105
+ // MachineSetsByDecreasingReplicas sorts the list of MachineSets in decreasing order of replicas,
106
+ // using creation time (ascending order) and name (alphabetical) as tie breakers.
107
+ func SortByDecreasingReplicas (a , b * clusterv1.MachineSet ) int {
108
+ if a .Spec .Replicas == nil {
109
+ return - 1
110
+ }
111
+ if b .Spec .Replicas == nil {
112
+ return 1
113
+ }
114
+ if * a .Spec .Replicas == * b .Spec .Replicas {
115
+ if a .CreationTimestamp .Equal (& b .CreationTimestamp ) {
116
+ if a .Name < b .Name {
117
+ return 1
118
+ }
119
+ return - 1
120
+ }
121
+ if a .CreationTimestamp .Before (& b .CreationTimestamp ) {
122
+ return 1
123
+ }
124
+ return - 1
125
+ }
126
+ if * a .Spec .Replicas > * b .Spec .Replicas {
127
+ return 1
101
128
}
102
- return * ( o [ i ]. Spec . Replicas ) > * ( o [ j ]. Spec . Replicas )
129
+ return - 1
103
130
}
104
131
105
132
// SetDeploymentRevision updates the revision for a deployment.
@@ -256,7 +283,10 @@ func FindOneActiveOrLatest(newMS *clusterv1.MachineSet, oldMSs []*clusterv1.Mach
256
283
return nil
257
284
}
258
285
259
- sort .Sort (sort .Reverse (MachineSetsByCreationTimestamp (oldMSs )))
286
+ // sort.Sort(sort.Reverse(MachineSetsByCreationTimestamp(oldMSs)))
287
+ slices .SortFunc (oldMSs , func (a , b * clusterv1.MachineSet ) int {
288
+ return SortByCreationTimestamp (a , b , DESCENDING )
289
+ })
260
290
allMSs := FilterActiveMachineSets (append (oldMSs , newMS ))
261
291
262
292
switch len (allMSs ) {
@@ -467,7 +497,10 @@ func FindNewMachineSet(deployment *clusterv1.MachineDeployment, msList []*cluste
467
497
// having more than one new MachineSets that have the same template,
468
498
// see https://github.com/kubernetes/kubernetes/issues/40415
469
499
// We deterministically choose the oldest new MachineSet with matching template hash.
470
- sort .Sort (MachineSetsByDecreasingReplicas (msList ))
500
+ // sort.Sort(MachineSetsByDecreasingReplicas(msList))
501
+ slices .SortFunc (msList , func (a , b * clusterv1.MachineSet ) int {
502
+ return SortByDecreasingReplicas (a , b )
503
+ })
471
504
472
505
var matchingMachineSets []* clusterv1.MachineSet
473
506
var diffs []string
0 commit comments