@@ -21,7 +21,7 @@ import (
2121	"context" 
2222	"fmt" 
2323	"reflect" 
24- 	"sort " 
24+ 	"slices " 
2525	"strconv" 
2626	"strings" 
2727	"time" 
@@ -42,64 +42,91 @@ import (
4242	"sigs.k8s.io/cluster-api/util/conversion" 
4343)
4444
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+ )
4851
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 
5768	}
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 
6172		}
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 
6379	}
64- 	return  * o [ i ]. Spec . Replicas   >   * o [ j ]. Spec . Replicas 
80+ 	return  0 
6581}
6682
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+ 		}
7598	}
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 
88101	}
89- 	return  * ( o [ i ]. Spec . Replicas )  >   * ( o [ j ]. Spec . Replicas ) 
102+ 	return  - 1 
90103}
91104
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 
101128	}
102- 	return  * ( o [ i ]. Spec . Replicas )  >   * ( o [ j ]. Spec . Replicas ) 
129+ 	return  - 1 
103130}
104131
105132// SetDeploymentRevision updates the revision for a deployment. 
@@ -256,7 +283,10 @@ func FindOneActiveOrLatest(newMS *clusterv1.MachineSet, oldMSs []*clusterv1.Mach
256283		return  nil 
257284	}
258285
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+ 	})
260290	allMSs  :=  FilterActiveMachineSets (append (oldMSs , newMS ))
261291
262292	switch  len (allMSs ) {
@@ -467,7 +497,10 @@ func FindNewMachineSet(deployment *clusterv1.MachineDeployment, msList []*cluste
467497	// having more than one new MachineSets that have the same template, 
468498	// see https://github.com/kubernetes/kubernetes/issues/40415 
469499	// 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+ 	})
471504
472505	var  matchingMachineSets  []* clusterv1.MachineSet 
473506	var  diffs  []string 
0 commit comments