From 453512d81cd3c3588d71a7d89f3a90735ec4ba92 Mon Sep 17 00:00:00 2001 From: GaoXiaodong <13814823514@163.com> Date: Fri, 6 Jan 2023 17:46:47 +0800 Subject: [PATCH] fix(platform): sort workload pods for page (#2224) Co-authored-by: xdonggao --- .../proxy/apps/daemonset/storage/pod.go | 10 ++++-- .../proxy/apps/deployment/storage/pod.go | 15 +++++--- .../proxy/apps/statefulset/storage/pod.go | 17 ++++++---- pkg/platform/proxy/batch/job/storage/pod.go | 6 +++- pkg/platform/proxy/core/node/storage/pod.go | 17 ++++++---- pkg/platform/util/pod.go | 34 +++++++++++++++++++ 6 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 pkg/platform/util/pod.go diff --git a/pkg/platform/proxy/apps/daemonset/storage/pod.go b/pkg/platform/proxy/apps/daemonset/storage/pod.go index 3a832f57c4..1332ff6449 100644 --- a/pkg/platform/proxy/apps/daemonset/storage/pod.go +++ b/pkg/platform/proxy/apps/daemonset/storage/pod.go @@ -20,6 +20,7 @@ package storage import ( "context" + "sort" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -33,6 +34,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" ) @@ -119,7 +121,9 @@ func listPodsByExtensions(ctx context.Context, client *kubernetes.Clientset, nam } } } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "DaemonSet", name, listOpts.Continue) if err != nil { @@ -186,7 +190,9 @@ func listPodsByApps(ctx context.Context, client *kubernetes.Clientset, namespace } } } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "DaemonSet", name, listOpts.Continue) if err != nil { diff --git a/pkg/platform/proxy/apps/deployment/storage/pod.go b/pkg/platform/proxy/apps/deployment/storage/pod.go index ef76414a36..9060f0fe0c 100644 --- a/pkg/platform/proxy/apps/deployment/storage/pod.go +++ b/pkg/platform/proxy/apps/deployment/storage/pod.go @@ -20,10 +20,7 @@ package storage import ( "context" - - "tkestack.io/tke/pkg/platform/proxy" - "tkestack.io/tke/pkg/util/apiclient" - "tkestack.io/tke/pkg/util/page" + "sort" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -36,6 +33,10 @@ import ( "k8s.io/apiserver/pkg/registry/rest" "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" ) // PodREST implements the REST endpoint for find pods by a deployment. @@ -134,6 +135,9 @@ func listPodByExtensions(ctx context.Context, client *kubernetes.Clientset, name } } } + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "Deployment", name, listOpts.Continue) if err != nil { @@ -212,6 +216,9 @@ func listPodByApps(ctx context.Context, client *kubernetes.Clientset, namespaceN } } } + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "Deployment", name, listOpts.Continue) if err != nil { diff --git a/pkg/platform/proxy/apps/statefulset/storage/pod.go b/pkg/platform/proxy/apps/statefulset/storage/pod.go index 26a8aef8ed..31316612d9 100644 --- a/pkg/platform/proxy/apps/statefulset/storage/pod.go +++ b/pkg/platform/proxy/apps/statefulset/storage/pod.go @@ -20,10 +20,7 @@ package storage import ( "context" - - "tkestack.io/tke/pkg/platform/proxy" - "tkestack.io/tke/pkg/util/apiclient" - "tkestack.io/tke/pkg/util/page" + "sort" appsv1beta1 "k8s.io/api/apps/v1beta1" corev1 "k8s.io/api/core/v1" @@ -35,6 +32,10 @@ import ( "k8s.io/apiserver/pkg/registry/rest" "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" ) // PodREST implements the REST endpoint for find pods by a deployment. @@ -119,7 +120,9 @@ func listPodsByAppsBeta(ctx context.Context, client *kubernetes.Clientset, names } } } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "StatefulSet", name, listOpts.Continue) if err != nil { @@ -186,7 +189,9 @@ func listPodsByApps(ctx context.Context, client *kubernetes.Clientset, namespace } } } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "StatefulSet", name, listOpts.Continue) if err != nil { diff --git a/pkg/platform/proxy/batch/job/storage/pod.go b/pkg/platform/proxy/batch/job/storage/pod.go index 82e17e8ce8..69b033981e 100644 --- a/pkg/platform/proxy/batch/job/storage/pod.go +++ b/pkg/platform/proxy/batch/job/storage/pod.go @@ -20,6 +20,7 @@ package storage import ( "context" + "sort" batchV1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" @@ -31,6 +32,7 @@ import ( "k8s.io/apiserver/pkg/registry/rest" 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/page" ) @@ -109,7 +111,9 @@ func (r *PodREST) Get(ctx context.Context, name string, options runtime.Object) } } } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "Job", name, listOpts.Continue) if err != nil { diff --git a/pkg/platform/proxy/core/node/storage/pod.go b/pkg/platform/proxy/core/node/storage/pod.go index a44b631add..196d3040fa 100644 --- a/pkg/platform/proxy/core/node/storage/pod.go +++ b/pkg/platform/proxy/core/node/storage/pod.go @@ -21,10 +21,7 @@ package storage import ( "context" "fmt" - - "tkestack.io/tke/pkg/platform/proxy" - "tkestack.io/tke/pkg/util/apiclient" - "tkestack.io/tke/pkg/util/page" + "sort" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -34,6 +31,10 @@ import ( "k8s.io/apiserver/pkg/registry/rest" "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" ) // PodREST implements the REST endpoint for find pods by a node. @@ -89,7 +90,9 @@ func listPodByExtensions(ctx context.Context, client *kubernetes.Clientset, name if err != nil { return nil, errors.NewInternalError(err) } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "Node", name, listOpts.Continue) if err != nil { @@ -135,7 +138,9 @@ func listPodByApps(ctx context.Context, client *kubernetes.Clientset, name strin if err != nil { return nil, errors.NewInternalError(err) } - + pods := util.NewPods(podList.Items) + sort.Sort(pods) + podList.Items = pods.GetPods() if listOpts.Continue != "" { start, limit, err := page.DecodeContinue(ctx, "Node", name, listOpts.Continue) if err != nil { diff --git a/pkg/platform/util/pod.go b/pkg/platform/util/pod.go new file mode 100644 index 0000000000..99dc956977 --- /dev/null +++ b/pkg/platform/util/pod.go @@ -0,0 +1,34 @@ +package util + +import corev1 "k8s.io/api/core/v1" + +type Pods struct { + pods []corev1.Pod +} + +func NewPods(pods []corev1.Pod) Pods { + return Pods{ + pods: pods, + } +} + +func (p Pods) Len() int { + return len(p.pods) +} + +func (p Pods) Swap(i, j int) { + p.pods[i], p.pods[j] = p.pods[j], p.pods[i] +} + +func (p Pods) Less(i, j int) bool { + // created at the same time, sort by the first letter of the pod name + if p.pods[i].CreationTimestamp.Time == p.pods[j].CreationTimestamp.Time { + return p.pods[i].Name > p.pods[j].Name + } + // the earliest created time comes first + return p.pods[j].CreationTimestamp.After(p.pods[i].CreationTimestamp.Time) +} + +func (p Pods) GetPods() []corev1.Pod { + return p.pods +}