Skip to content

Commit

Permalink
fix(platform): sort workload pods for page (#2224)
Browse files Browse the repository at this point in the history
Co-authored-by: xdonggao <[email protected]>
  • Loading branch information
GaoXiaodong and xdonggao committed Jan 6, 2023
1 parent 5b97a0d commit 453512d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
10 changes: 8 additions & 2 deletions pkg/platform/proxy/apps/daemonset/storage/pod.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"
corev1 "k8s.io/api/core/v1"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 11 additions & 4 deletions pkg/platform/proxy/apps/deployment/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 11 additions & 6 deletions pkg/platform/proxy/apps/statefulset/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion pkg/platform/proxy/batch/job/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package storage

import (
"context"
"sort"

batchV1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 11 additions & 6 deletions pkg/platform/proxy/core/node/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions pkg/platform/util/pod.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 453512d

Please sign in to comment.