-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(platform): support replicasets sort by version (#2314)
Co-authored-by: xdonggao <xdonggao@tencent.com>
- Loading branch information
1 parent
b7900fc
commit a379406
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package util | ||
|
||
import ( | ||
appsv1 "k8s.io/api/apps/v1" | ||
extensionsv1beta1 "k8s.io/api/extensions/v1beta1" | ||
) | ||
|
||
const reversion = "deployment.kubernetes.io/revision" | ||
|
||
type ReplicaSets struct { | ||
replicaSet []appsv1.ReplicaSet | ||
} | ||
|
||
func NewReplicaSets(replicaSet []appsv1.ReplicaSet) ReplicaSets { | ||
return ReplicaSets{ | ||
replicaSet: replicaSet, | ||
} | ||
} | ||
|
||
func (p ReplicaSets) Len() int { | ||
return len(p.replicaSet) | ||
} | ||
|
||
func (p ReplicaSets) Swap(i, j int) { | ||
p.replicaSet[i], p.replicaSet[j] = p.replicaSet[j], p.replicaSet[i] | ||
} | ||
|
||
func (p ReplicaSets) Less(i, j int) bool { | ||
ireVersion := MayNilMapGet(p.replicaSet[i].Annotations, reversion) | ||
jreVersion := MayNilMapGet(p.replicaSet[j].Annotations, reversion) | ||
return ireVersion > jreVersion | ||
} | ||
|
||
func (p ReplicaSets) GetReplicaSets() []appsv1.ReplicaSet { | ||
return p.replicaSet | ||
} | ||
|
||
type EXReplicaSets struct { | ||
replicaSet []extensionsv1beta1.ReplicaSet | ||
} | ||
|
||
func NewEXReplicaSets(replicaSet []extensionsv1beta1.ReplicaSet) EXReplicaSets { | ||
return EXReplicaSets{ | ||
replicaSet: replicaSet, | ||
} | ||
} | ||
|
||
func (p EXReplicaSets) Len() int { | ||
return len(p.replicaSet) | ||
} | ||
|
||
func (p EXReplicaSets) Swap(i, j int) { | ||
p.replicaSet[i], p.replicaSet[j] = p.replicaSet[j], p.replicaSet[i] | ||
} | ||
|
||
func (p EXReplicaSets) Less(i, j int) bool { | ||
ireVersion := MayNilMapGet(p.replicaSet[i].Annotations, reversion) | ||
jreVersion := MayNilMapGet(p.replicaSet[j].Annotations, reversion) | ||
return ireVersion > jreVersion | ||
} | ||
|
||
func (p EXReplicaSets) GetReplicaSets() []extensionsv1beta1.ReplicaSet { | ||
return p.replicaSet | ||
} | ||
|
||
func MayNilMapGet(m map[string]string, k string) string { | ||
if m == nil { | ||
return "" | ||
} | ||
return m[k] | ||
} |