Skip to content

Commit

Permalink
feat(platform): support workload pods page
Browse files Browse the repository at this point in the history
  • Loading branch information
xdonggao committed Sep 20, 2022
1 parent 9c8f476 commit ee41cef
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 31 deletions.
87 changes: 77 additions & 10 deletions pkg/platform/proxy/apps/daemonset/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ package storage
import (
"context"

"tkestack.io/tke/pkg/platform/proxy"
"tkestack.io/tke/pkg/util/apiclient"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
Expand All @@ -35,6 +32,9 @@ 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/util/apiclient"
"tkestack.io/tke/pkg/util/page"
)

// PodREST implements the REST endpoint for find pods by a deployment.
Expand All @@ -43,7 +43,7 @@ type PodREST struct {
platformClient platforminternalclient.PlatformInterface
}

var _ rest.Getter = &PodREST{}
var _ rest.GetterWithOptions = &PodREST{}
var _ rest.GroupVersionKindProvider = &PodREST{}

// GroupVersionKind is used to specify a particular GroupVersionKind to discovery.
Expand All @@ -57,25 +57,34 @@ func (r *PodREST) New() runtime.Object {
return &corev1.PodList{}
}

// NewConnectOptions returns versioned resource that represents proxy parameters
func (r *PodREST) NewGetOptions() (runtime.Object, bool, string) {
return &metav1.ListOptions{}, false, ""
}

// Get retrieves the object from the storage. It is required to support Patch.
func (r *PodREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
func (r *PodREST) Get(ctx context.Context, name string, options runtime.Object) (runtime.Object, error) {
client, err := proxy.ClientSet(ctx, r.platformClient)
if err != nil {
return nil, err
}

listOpts := options.(*metav1.ListOptions)
metaOptions := &metav1.GetOptions{}
if listOpts.ResourceVersion != "" {
metaOptions.ResourceVersion = listOpts.ResourceVersion
}
namespaceName, ok := request.NamespaceFrom(ctx)
if !ok {
return nil, errors.NewBadRequest("a namespace must be specified")
}

if apiclient.ClusterVersionIsBefore19(client) {
return listPodsByExtensions(ctx, client, namespaceName, name, options)
return listPodsByExtensions(ctx, client, namespaceName, name, metaOptions, listOpts)
}
return listPodsByApps(ctx, client, namespaceName, name, options)
return listPodsByApps(ctx, client, namespaceName, name, metaOptions, listOpts)
}

func listPodsByExtensions(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions) (runtime.Object, error) {
func listPodsByExtensions(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions, listOpts *metav1.ListOptions) (runtime.Object, error) {
daemonSet, err := client.ExtensionsV1beta1().DaemonSets(namespaceName).Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(extensionsv1beta1.Resource("daemonSets/pods"), name)
Expand Down Expand Up @@ -103,10 +112,39 @@ func listPodsByExtensions(ctx context.Context, client *kubernetes.Clientset, nam
}
}
}

if listOpts.Continue != "" {
start, limit, err := page.DecodeContinue(ctx, "DaemonSet", name, listOpts.Continue)
if err != nil {
return nil, err
}
newStart := start + limit
if int(newStart+limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "DaemonSet", name, newStart, limit)
if err != nil {
return nil, err
}
items := podList.Items[newStart : newStart+limit]
podList.Items = items
} else {
items := podList.Items[newStart:len(podList.Items)]
podList.Items = items
}
} else if listOpts.Limit != 0 {
if int(listOpts.Limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "DaemonSet", name, 0, listOpts.Limit)
if err != nil {
return nil, err
}
items := podList.Items[:listOpts.Limit]
podList.Items = items
}
}

return podList, nil
}

func listPodsByApps(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions) (runtime.Object, error) {
func listPodsByApps(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions, listOpts *metav1.ListOptions) (runtime.Object, error) {
daemonSet, err := client.AppsV1().DaemonSets(namespaceName).Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(appsv1.Resource("daemonSets/pods"), name)
Expand Down Expand Up @@ -134,5 +172,34 @@ func listPodsByApps(ctx context.Context, client *kubernetes.Clientset, namespace
}
}
}

if listOpts.Continue != "" {
start, limit, err := page.DecodeContinue(ctx, "DaemonSet", name, listOpts.Continue)
if err != nil {
return nil, err
}
newStart := start + limit
if int(newStart+limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "DaemonSet", name, newStart, limit)
if err != nil {
return nil, err
}
items := podList.Items[newStart : newStart+limit]
podList.Items = items
} else {
items := podList.Items[newStart:len(podList.Items)]
podList.Items = items
}
} else if listOpts.Limit != 0 {
if int(listOpts.Limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "DaemonSet", name, 0, listOpts.Limit)
if err != nil {
return nil, err
}
items := podList.Items[:listOpts.Limit]
podList.Items = items
}
}

return podList, nil
}
79 changes: 73 additions & 6 deletions pkg/platform/proxy/apps/deployment/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"tkestack.io/tke/pkg/platform/proxy"
"tkestack.io/tke/pkg/util/apiclient"
"tkestack.io/tke/pkg/util/page"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -43,7 +44,7 @@ type PodREST struct {
platformClient platforminternalclient.PlatformInterface
}

var _ rest.Getter = &PodREST{}
var _ rest.GetterWithOptions = &PodREST{}
var _ rest.GroupVersionKindProvider = &PodREST{}

// GroupVersionKind is used to specify a particular GroupVersionKind to discovery.
Expand All @@ -57,26 +58,36 @@ func (r *PodREST) New() runtime.Object {
return &corev1.PodList{}
}

// NewConnectOptions returns versioned resource that represents proxy parameters
func (r *PodREST) NewGetOptions() (runtime.Object, bool, string) {
return &metav1.ListOptions{}, false, ""
}

// Get retrieves the object from the storage. It is required to support Patch.
func (r *PodREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
func (r *PodREST) Get(ctx context.Context, name string, options runtime.Object) (runtime.Object, error) {
client, err := proxy.ClientSet(ctx, r.platformClient)
if err != nil {
return nil, err
}
listOpts := options.(*metav1.ListOptions)
metaOptions := &metav1.GetOptions{}
if listOpts.ResourceVersion != "" {
metaOptions.ResourceVersion = listOpts.ResourceVersion
}

namespaceName, ok := request.NamespaceFrom(ctx)
if !ok {
return nil, errors.NewBadRequest("a namespace must be specified")
}

if apiclient.ClusterVersionIsBefore19(client) {
return listPodByExtensions(ctx, client, namespaceName, name, options)
return listPodByExtensions(ctx, client, namespaceName, name, metaOptions, listOpts)
}

return listPodByApps(ctx, client, namespaceName, name, options)
return listPodByApps(ctx, client, namespaceName, name, metaOptions, listOpts)
}

func listPodByExtensions(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions) (runtime.Object, error) {
func listPodByExtensions(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions, listOpts *metav1.ListOptions) (runtime.Object, error) {
deployment, err := client.ExtensionsV1beta1().Deployments(namespaceName).Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(extensionsv1beta1.Resource("deployments/pods"), name)
Expand Down Expand Up @@ -115,10 +126,38 @@ func listPodByExtensions(ctx context.Context, client *kubernetes.Clientset, name
}
}
}
if listOpts.Continue != "" {
start, limit, err := page.DecodeContinue(ctx, "Deployment", name, listOpts.Continue)
if err != nil {
return nil, err
}
newStart := start + limit
if int(newStart+limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "Deployment", name, newStart, limit)
if err != nil {
return nil, err
}
items := podList.Items[newStart : newStart+limit]
podList.Items = items
} else {
items := podList.Items[newStart:len(podList.Items)]
podList.Items = items
}
} else if listOpts.Limit != 0 {
if int(listOpts.Limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "Deployment", name, 0, listOpts.Limit)
if err != nil {
return nil, err
}
items := podList.Items[:listOpts.Limit]
podList.Items = items
}
}

return podList, nil
}

func listPodByApps(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions) (runtime.Object, error) {
func listPodByApps(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions, listOpts *metav1.ListOptions) (runtime.Object, error) {
deployment, err := client.AppsV1().Deployments(namespaceName).Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(appsv1.Resource("deployments/pods"), name)
Expand Down Expand Up @@ -157,5 +196,33 @@ func listPodByApps(ctx context.Context, client *kubernetes.Clientset, namespaceN
}
}
}
if listOpts.Continue != "" {
start, limit, err := page.DecodeContinue(ctx, "Deployment", name, listOpts.Continue)
if err != nil {
return nil, err
}
newStart := start + limit
if int(newStart+limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "Deployment", name, newStart, limit)
if err != nil {
return nil, err
}
items := podList.Items[newStart : newStart+limit]
podList.Items = items
} else {
items := podList.Items[newStart:len(podList.Items)]
podList.Items = items
}
} else if listOpts.Limit != 0 {
if int(listOpts.Limit) < len(podList.Items) {
podList.Continue, err = page.EncodeContinue(ctx, "Deployment", name, 0, listOpts.Limit)
if err != nil {
return nil, err
}
items := podList.Items[:listOpts.Limit]
podList.Items = items
}
}

return podList, nil
}
3 changes: 1 addition & 2 deletions pkg/platform/proxy/apps/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
appsv1beta2 "k8s.io/api/apps/v1beta2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
genericserver "k8s.io/apiserver/pkg/server"
Expand All @@ -49,7 +48,7 @@ var _ storage.RESTStorageProvider = &StorageProvider{}

// NewRESTStorage is a factory constructor to creates and returns the APIGroupInfo
func (s *StorageProvider) NewRESTStorage(apiResourceConfigSource serverstorage.APIResourceConfigSource, restOptionsGetter generic.RESTOptionsGetter) (genericserver.APIGroupInfo, bool) {
apiGroupInfo := genericserver.NewDefaultAPIGroupInfo(appsv1.GroupName, platform.Scheme, metav1.ParameterCodec, platform.Codecs)
apiGroupInfo := genericserver.NewDefaultAPIGroupInfo(appsv1.GroupName, platform.Scheme, platform.ParameterCodec, platform.Codecs)

if apiResourceConfigSource.VersionEnabled(appsv1.SchemeGroupVersion) {
apiGroupInfo.VersionedResourcesStorageMap[appsv1.SchemeGroupVersion.Version] = s.v1Storage(restOptionsGetter, s.LoopbackClientConfig)
Expand Down
Loading

0 comments on commit ee41cef

Please sign in to comment.