Skip to content

Commit

Permalink
feat(platform): support list workload replicasets
Browse files Browse the repository at this point in the history
  • Loading branch information
xdonggao committed Dec 5, 2022
1 parent 6e1a4a9 commit a189729
Show file tree
Hide file tree
Showing 11 changed files with 755 additions and 16 deletions.
22 changes: 18 additions & 4 deletions pkg/platform/proxy/apps/daemonset/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,15 @@ func listPodsByExtensions(ctx context.Context, client *kubernetes.Clientset, nam
}

// list all of the pod, by deployment labels
listOptions := metav1.ListOptions{LabelSelector: selector.String()}
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, listOptions)
listPodsOptions := listOpts.DeepCopy()
listPodsOptions.Continue = ""
listPodsOptions.Limit = 0
if listPodsOptions.LabelSelector == "" {
listPodsOptions.LabelSelector = selector.String()
} else {
listPodsOptions.LabelSelector = listPodsOptions.LabelSelector + "," + selector.String()
}
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, *listPodsOptions)
if err != nil {
return nil, errors.NewInternalError(err)
}
Expand Down Expand Up @@ -156,8 +163,15 @@ func listPodsByApps(ctx context.Context, client *kubernetes.Clientset, namespace
}

// list all of the pod, by deployment labels
listOptions := metav1.ListOptions{LabelSelector: selector.String()}
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, listOptions)
listPodsOptions := listOpts.DeepCopy()
listPodsOptions.Continue = ""
listPodsOptions.Limit = 0
if listPodsOptions.LabelSelector == "" {
listPodsOptions.LabelSelector = selector.String()
} else {
listPodsOptions.LabelSelector = listPodsOptions.LabelSelector + "," + selector.String()
}
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, *listPodsOptions)
if err != nil {
return nil, errors.NewInternalError(err)
}
Expand Down
215 changes: 215 additions & 0 deletions pkg/platform/proxy/apps/daemonset/storage/replicasets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package storage

import (
"context"

appsv1 "k8s.io/api/apps/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/endpoints/request"
"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"
)

// ReplicaSetsREST implements the REST endpoint for find replicasets by a deployment.
type ReplicaSetsREST struct {
rest.Storage
platformClient platforminternalclient.PlatformInterface
}

var _ rest.GetterWithOptions = &ReplicaSetsREST{}
var _ rest.GroupVersionKindProvider = &ReplicaSetsREST{}

// GroupVersionKind is used to specify a particular GroupVersionKind to discovery.
func (r *ReplicaSetsREST) GroupVersionKind(containingGV schema.GroupVersion) schema.GroupVersionKind {
return appsv1.SchemeGroupVersion.WithKind("ReplicaSetList")
}

// New returns an empty object that can be used with Create and Update after
// request data has been put into it.
func (r *ReplicaSetsREST) New() runtime.Object {
return &appsv1.ReplicaSetList{}
}

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

// Get retrieves the object from the storage. It is required to support Patch.
func (r *ReplicaSetsREST) 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 listReplicaSetsByExtensions(ctx, client, namespaceName, name, metaOptions, listOpts)
}
return listReplicaSetsByApps(ctx, client, namespaceName, name, metaOptions, listOpts)
}

func listReplicaSetsByExtensions(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/replicasets"), name)
}

selector, err := metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
if err != nil {
return nil, errors.NewInternalError(err)
}

listPodsOptions := listOpts.DeepCopy()
listPodsOptions.Continue = ""
listPodsOptions.Limit = 0
if listPodsOptions.LabelSelector == "" {
listPodsOptions.LabelSelector = selector.String()
} else {
listPodsOptions.LabelSelector = listPodsOptions.LabelSelector + "," + selector.String()
}
rsAllList, err := client.ExtensionsV1beta1().ReplicaSets(namespaceName).List(ctx, *listPodsOptions)
if err != nil {
return nil, errors.NewInternalError(err)
}
rsList := &extensionsv1beta1.ReplicaSetList{
Items: make([]extensionsv1beta1.ReplicaSet, 0),
}
for _, rs := range rsAllList.Items {
for _, references := range rs.ObjectMeta.OwnerReferences {
if references.UID == daemonSet.GetUID() {
rsList.Items = append(rsList.Items, rs)
}
}
}

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

return rsList, nil
}

func listReplicaSetsByApps(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/replicasets"), name)
}

selector, err := metav1.LabelSelectorAsSelector(daemonSet.Spec.Selector)
if err != nil {
return nil, errors.NewInternalError(err)
}

listPodsOptions := listOpts.DeepCopy()
listPodsOptions.Continue = ""
listPodsOptions.Limit = 0
if listPodsOptions.LabelSelector == "" {
listPodsOptions.LabelSelector = selector.String()
} else {
listPodsOptions.LabelSelector = listPodsOptions.LabelSelector + "," + selector.String()
}
rsAllList, err := client.AppsV1().ReplicaSets(namespaceName).List(ctx, *listPodsOptions)
if err != nil {
return nil, errors.NewInternalError(err)
}

rsList := &appsv1.ReplicaSetList{
Items: make([]appsv1.ReplicaSet, 0),
}
for _, rs := range rsAllList.Items {
for _, references := range rs.ObjectMeta.OwnerReferences {
if references.UID == daemonSet.GetUID() {
rsList.Items = append(rsList.Items, rs)
}
}
}

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

return rsList, nil
}
18 changes: 14 additions & 4 deletions pkg/platform/proxy/apps/daemonset/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import (

// Storage includes storage for resources.
type Storage struct {
DaemonSet *REST
Status *StatusREST
Events *EventREST
Pods *PodREST
DaemonSet *REST
Status *StatusREST
Events *EventREST
Pods *PodREST
ReplicaSets *ReplicaSetsREST
}

// REST implements pkg/api/rest.StandardStorage
Expand Down Expand Up @@ -67,6 +68,9 @@ func NewStorageV1(_ genericregistry.RESTOptionsGetter, platformClient platformin
Events: &EventREST{
platformClient: platformClient,
},
ReplicaSets: &ReplicaSetsREST{
platformClient: platformClient,
},
}
}

Expand All @@ -92,6 +96,9 @@ func NewStorageV1Beta2(_ genericregistry.RESTOptionsGetter, platformClient platf
Events: &EventREST{
platformClient: platformClient,
},
ReplicaSets: &ReplicaSetsREST{
platformClient: platformClient,
},
}
}

Expand All @@ -117,6 +124,9 @@ func NewStorageExtensionsV1Beta1(_ genericregistry.RESTOptionsGetter, platformCl
Events: &EventREST{
platformClient: platformClient,
},
ReplicaSets: &ReplicaSetsREST{
platformClient: platformClient,
},
}
}

Expand Down
20 changes: 18 additions & 2 deletions pkg/platform/proxy/apps/deployment/storage/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ func listPodByExtensions(ctx context.Context, client *kubernetes.Clientset, name
}

// list all of the pod, by deployment labels
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, listOptions)
listPodsOptions := listOpts.DeepCopy()
listPodsOptions.Continue = ""
listPodsOptions.Limit = 0
if listPodsOptions.LabelSelector == "" {
listPodsOptions.LabelSelector = selector.String()
} else {
listPodsOptions.LabelSelector = listPodsOptions.LabelSelector + "," + selector.String()
}
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, *listPodsOptions)
if err != nil {
return nil, errors.NewInternalError(err)
}
Expand Down Expand Up @@ -175,7 +183,15 @@ func listPodByApps(ctx context.Context, client *kubernetes.Clientset, namespaceN
}

// list all of the pod, by deployment labels
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, listOptions)
listPodsOptions := listOpts.DeepCopy()
listPodsOptions.Continue = ""
listPodsOptions.Limit = 0
if listPodsOptions.LabelSelector == "" {
listPodsOptions.LabelSelector = selector.String()
} else {
listPodsOptions.LabelSelector = listPodsOptions.LabelSelector + "," + selector.String()
}
podAllList, err := client.CoreV1().Pods(namespaceName).List(ctx, *listPodsOptions)
if err != nil {
return nil, errors.NewInternalError(err)
}
Expand Down
Loading

0 comments on commit a189729

Please sign in to comment.