diff --git a/pkg/kapis/devops/v1alpha3/handler.go b/pkg/kapis/devops/v1alpha3/handler.go index 85c8879949e07e15a7ecb34dfa266a1ef999f3e4..060effc72976db0672e693083b32a0ab0ac93a1c 100644 --- a/pkg/kapis/devops/v1alpha3/handler.go +++ b/pkg/kapis/devops/v1alpha3/handler.go @@ -25,6 +25,7 @@ import ( "k8s.io/klog" "kubesphere.io/kubesphere/pkg/api" "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3" + "kubesphere.io/kubesphere/pkg/apiserver/query" kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned" "kubesphere.io/kubesphere/pkg/client/informers/externalversions" "kubesphere.io/kubesphere/pkg/models/devops" @@ -291,9 +292,8 @@ func (h *devopsHandler) GetCredential(request *restful.Request, response *restfu func (h *devopsHandler) ListCredential(request *restful.Request, response *restful.Response) { devops := request.PathParameter("devops") - limit, offset := params.ParsePaging(request) - - objs, err := h.devops.ListCredentialObj(devops, limit, offset) + query := query.ParseQueryParameter(request) + objs, err := h.devops.ListCredentialObj(devops, query) if err != nil { klog.Error(err) diff --git a/pkg/kapis/devops/v1alpha3/register.go b/pkg/kapis/devops/v1alpha3/register.go index 15b16c58e06a4f24292c794253da78bab7eadfcc..f4fd036786e9c106d12c6292e3bdec99f5680d30 100644 --- a/pkg/kapis/devops/v1alpha3/register.go +++ b/pkg/kapis/devops/v1alpha3/register.go @@ -27,6 +27,7 @@ import ( "k8s.io/client-go/kubernetes" "kubesphere.io/kubesphere/pkg/api" "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3" + "kubesphere.io/kubesphere/pkg/apiserver/query" "kubesphere.io/kubesphere/pkg/apiserver/runtime" kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned" "kubesphere.io/kubesphere/pkg/client/informers/externalversions" @@ -55,10 +56,11 @@ func AddToContainer(container *restful.Container, devopsClient devopsClient.Inte ws.Route(ws.GET("/devops/{devops}/credentials"). To(handler.ListCredential). Param(ws.PathParameter("devops", "devops name")). - Param(ws.QueryParameter(params.PagingParam, "paging query, e.g. limit=100,page=1"). - Required(false). - DataFormat("limit=%d,page=%d"). - DefaultValue("limit=10,page=1")). + Param(ws.QueryParameter(query.ParameterName, "name used to do filtering").Required(false)). + Param(ws.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")). + Param(ws.QueryParameter(query.ParameterLimit, "limit").Required(false)). + Param(ws.QueryParameter(query.ParameterAscending, "sort parameters, e.g. ascending=false").Required(false).DefaultValue("ascending=false")). + Param(ws.QueryParameter(query.ParameterOrderBy, "sort parameters, e.g. orderBy=createTime")). Doc("list the credentials of the specified devops for the current user"). Returns(http.StatusOK, api.StatusOK, api.ListResult{Items: []interface{}{}}). Metadata(restfulspec.KeyOpenAPITags, []string{constants.DevOpsProjectTag})) diff --git a/pkg/models/devops/devops.go b/pkg/models/devops/devops.go index 3a4491c3e21365ee98cee17dd7a3e700f64268f3..3fdd40453ecf79b27ef5364987e2b47b4560cc8f 100644 --- a/pkg/models/devops/devops.go +++ b/pkg/models/devops/devops.go @@ -26,6 +26,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" @@ -34,8 +35,10 @@ import ( "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3" devopsv1alpha3 "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3" tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1" + "kubesphere.io/kubesphere/pkg/apiserver/query" kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned" "kubesphere.io/kubesphere/pkg/client/informers/externalversions" + resourcesV1alpha3 "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3" "kubesphere.io/kubesphere/pkg/simple/client/devops" "net/http" "sync" @@ -62,7 +65,7 @@ type DevopsOperator interface { GetCredentialObj(projectName string, secretName string) (*v1.Secret, error) DeleteCredentialObj(projectName string, secretName string) error UpdateCredentialObj(projectName string, secret *v1.Secret) (*v1.Secret, error) - ListCredentialObj(projectName string, limit, offset int) (api.ListResult, error) + ListCredentialObj(projectName string, query *query.Query) (api.ListResult, error) GetPipeline(projectName, pipelineName string, req *http.Request) (*devops.Pipeline, error) ListPipelines(req *http.Request) (*devops.PipelineList, error) @@ -301,19 +304,16 @@ func (d devopsOperator) UpdateCredentialObj(projectName string, secret *v1.Secre return d.k8sclient.CoreV1().Secrets(projectObj.Status.AdminNamespace).Update(secret) } -func (d devopsOperator) ListCredentialObj(projectName string, limit, offset int) (api.ListResult, error) { +func (d devopsOperator) ListCredentialObj(projectName string, query *query.Query) (api.ListResult, error) { projectObj, err := d.ksInformers.Devops().V1alpha3().DevOpsProjects().Lister().Get(projectName) if err != nil { return api.ListResult{}, err } - - credentialList, err := d.k8sInformers.Core().V1().Secrets().Lister().Secrets(projectObj.Status.AdminNamespace).List(labels.Everything()) + credentialObjList, err := d.k8sInformers.Core().V1().Secrets().Lister().Secrets(projectObj.Status.AdminNamespace).List(query.Selector()) if err != nil { return api.ListResult{}, err } - - items := make([]interface{}, 0) - var result []interface{} + var result []runtime.Object credentialTypeList := []v1.SecretType{ v1alpha3.SecretTypeBasicAuth, @@ -321,22 +321,41 @@ func (d devopsOperator) ListCredentialObj(projectName string, limit, offset int) v1alpha3.SecretTypeSecretText, v1alpha3.SecretTypeKubeConfig, } - for _, credential := range credentialList { + for _, credential := range credentialObjList { for _, credentialType := range credentialTypeList { if credential.Type == credentialType { - result = append(result, *credential) + result = append(result, credential) } } } - if limit == -1 || limit+offset > len(result) { - limit = len(result) - offset + return *resourcesV1alpha3.DefaultList(result, query, d.compareCredentialObj, d.filterCredentialObj), nil +} + +func (d devopsOperator) compareCredentialObj(left runtime.Object, right runtime.Object, field query.Field) bool { + + leftObj, ok := left.(*v1.Secret) + if !ok { + return false } - items = result[offset : offset+limit] - if items == nil { - items = []interface{}{} + + rightObj, ok := right.(*v1.Secret) + if !ok { + return false } - return api.ListResult{TotalItems: len(result), Items: items}, nil + + return resourcesV1alpha3.DefaultObjectMetaCompare(leftObj.ObjectMeta, rightObj.ObjectMeta, field) +} + +func (d devopsOperator) filterCredentialObj(object runtime.Object, filter query.Filter) bool { + + secret, ok := object.(*v1.Secret) + + if !ok { + return false + } + + return resourcesV1alpha3.DefaultObjectMetaFilter(secret.ObjectMeta, filter) } // others