Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): support replicasets sort by version #2314

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
}
Loading