Skip to content

Commit

Permalink
fix(platform): support replicasets sort by version (#2314)
Browse files Browse the repository at this point in the history
Co-authored-by: xdonggao <xdonggao@tencent.com>
  • Loading branch information
GaoXiaodong and xdonggao authored Dec 6, 2023
1 parent b7900fc commit a379406
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/platform/proxy/apps/deployment/storage/replicasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package storage

import (
"context"
"sort"

appsv1 "k8s.io/api/apps/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
Expand All @@ -32,6 +33,7 @@ import (
"k8s.io/client-go/kubernetes"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/proxy"
"tkestack.io/tke/pkg/platform/util"
"tkestack.io/tke/pkg/util/apiclient"
"tkestack.io/tke/pkg/util/page"
)
Expand Down Expand Up @@ -119,6 +121,9 @@ func listReplicaSetsByExtensions(ctx context.Context, client *kubernetes.Clients
}
}

rss := util.NewEXReplicaSets(rsList.Items)
sort.Sort(rss)
rsList.Items = rss.GetReplicaSets()
if listOpts.Continue != "" {
start, limit, err := page.DecodeContinue(ctx, "Deployment/ReplicaSets", name, listOpts.Continue)
if err != nil {
Expand Down Expand Up @@ -185,6 +190,9 @@ func listReplicaSetsByApps(ctx context.Context, client *kubernetes.Clientset, na
}
}

rss := util.NewReplicaSets(rsList.Items)
sort.Sort(rss)
rsList.Items = rss.GetReplicaSets()
if listOpts.Continue != "" {
start, limit, err := page.DecodeContinue(ctx, "Deployment/ReplicaSets", name, listOpts.Continue)
if err != nil {
Expand Down
71 changes: 71 additions & 0 deletions pkg/platform/util/replicasets.go
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]
}

0 comments on commit a379406

Please sign in to comment.