Fix: return all items when limit=-1

Signed-off-by: LiHui <andrewli@yunify.com>
This commit is contained in:
LiHui
2021-04-15 16:50:00 +08:00
parent 330148094f
commit 5b3c524afa
10 changed files with 64 additions and 37 deletions

View File

@@ -21,7 +21,7 @@ import (
"strconv"
"strings"
"github.com/emicklei/go-restful"
restful "github.com/emicklei/go-restful"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apierrors "k8s.io/apimachinery/pkg/api/errors"

View File

@@ -16,7 +16,7 @@ package v1
import (
"net/http"
"github.com/emicklei/go-restful"
restful "github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
"k8s.io/apimachinery/pkg/runtime/schema"

View File

@@ -14,7 +14,7 @@ limitations under the License.
package v2alpha1
import (
"github.com/emicklei/go-restful"
restful "github.com/emicklei/go-restful"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog"

View File

@@ -16,7 +16,7 @@ package v2alpha1
import (
"net/http"
"github.com/emicklei/go-restful"
restful "github.com/emicklei/go-restful"
restfulspec "github.com/emicklei/go-restful-openapi"
"k8s.io/apimachinery/pkg/runtime/schema"

View File

@@ -22,6 +22,8 @@ import (
"strings"
"time"
"kubesphere.io/kubesphere/pkg/apiserver/query"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@@ -308,19 +310,20 @@ func (c *applicationOperator) ListApps(conditions *params.Conditions, orderBy st
sort.Sort(HelmApplicationList(apps))
}
items := make([]interface{}, 0, limit)
totalCount := len(apps)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
apps = apps[start:end]
items := make([]interface{}, 0, len(apps))
for i, j := offset, 0; i < len(apps) && j < limit; i, j = i+1, j+1 {
for i := range apps {
versions, err := c.getAppVersionsByAppId(apps[i].GetHelmApplicationId())
if err != nil && !apierrors.IsNotFound(err) {
return nil, err
}
ctg, _ := c.ctgLister.Get(apps[i].GetHelmCategoryId())
items = append(items, convertApp(apps[i], versions, ctg, 0))
}
return &models.PageableResponse{Items: items, TotalCount: len(apps)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}
func (c *applicationOperator) DeleteApp(id string) error {

View File

@@ -26,11 +26,12 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"reflect"
"sort"
"strings"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/go-openapi/strfmt"
@@ -263,12 +264,14 @@ func (c *applicationOperator) ListAppVersions(conditions *params.Conditions, ord
sort.Sort(AppVersions(versions))
}
items := make([]interface{}, 0, int(math.Min(float64(limit), float64(len(versions)))))
for i, j := offset, 0; i < len(versions) && j < limit; i, j = i+1, j+1 {
totalCount := len(versions)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
versions = versions[start:end]
items := make([]interface{}, 0, len(versions))
for i := range versions {
items = append(items, convertAppVersion(versions[i]))
}
return &models.PageableResponse{Items: items, TotalCount: len(versions)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}
func (c *applicationOperator) ListAppVersionReviews(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
@@ -286,9 +289,11 @@ func (c *applicationOperator) ListAppVersionReviews(conditions *params.Condition
sort.Sort(AppVersionReviews(filtered))
}
totalCount := len(filtered)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
filtered = filtered[start:end]
items := make([]interface{}, 0, len(filtered))
for i, j := offset, 0; i < len(filtered) && j < limit; i, j = i+1, j+1 {
for i := range filtered {
app, err := c.appLister.Get(filtered[i].GetHelmApplicationId())
if err != nil {
return nil, err
@@ -297,7 +302,7 @@ func (c *applicationOperator) ListAppVersionReviews(conditions *params.Condition
items = append(items, review)
}
return &models.PageableResponse{Items: items, TotalCount: len(filtered)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}
func (c *applicationOperator) ListAppVersionAudits(conditions *params.Conditions, orderBy string, reverse bool, limit, offset int) (*models.PageableResponse, error) {
@@ -330,13 +335,16 @@ func (c *applicationOperator) ListAppVersionAudits(conditions *params.Conditions
sort.Sort(AppVersionAuditList(allAudits))
items := make([]interface{}, 0, limit)
totalCount := len(allAudits)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
allAudits = allAudits[start:end]
items := make([]interface{}, 0, len(allAudits))
for i, j := offset, 0; i < len(allAudits) && j < limit; i, j = i+1, j+1 {
for i := range allAudits {
items = append(items, allAudits[i])
}
return &models.PageableResponse{Items: items, TotalCount: len(allAudits)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}
func (c *applicationOperator) DoAppVersionAction(versionId string, request *ActionRequest) error {

View File

@@ -17,6 +17,8 @@ import (
"context"
"sort"
"kubesphere.io/kubesphere/pkg/apiserver/query"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@@ -183,10 +185,13 @@ func (c *categoryOperator) ListCategories(conditions *params.Conditions, orderBy
sort.Sort(HelmCategoryList(ctgs))
items := make([]interface{}, 0, limit)
for i, j := offset, 0; i < len(ctgs) && j < limit; i, j = i+1, j+1 {
totalCount := len(ctgs)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
ctgs = ctgs[start:end]
items := make([]interface{}, 0, len(ctgs))
for i := range ctgs {
items = append(items, convertCategory(ctgs[i]))
}
return &models.PageableResponse{Items: items, TotalCount: len(ctgs)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}

View File

@@ -21,10 +21,11 @@ import (
"context"
"errors"
"fmt"
"math"
"sort"
"strings"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"github.com/go-openapi/strfmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -318,15 +319,16 @@ func (c *releaseOperator) ListApplications(workspace, clusterName, namespace str
sort.Sort(HelmReleaseList(releases))
}
result := models.PageableResponse{TotalCount: len(releases)}
result.Items = make([]interface{}, 0, int(math.Min(float64(limit), float64(len(releases)))))
for i, j := offset, 0; i < len(releases) && j < limit; i, j = i+1, j+1 {
totalCount := len(releases)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
releases = releases[start:end]
items := make([]interface{}, 0, len(releases))
for i := range releases {
app := convertApplication(releases[i], nil)
result.Items = append(result.Items, app)
items = append(items, app)
}
return &result, nil
return &models.PageableResponse{TotalCount: totalCount, Items: items}, nil
}
func (c *releaseOperator) DescribeApplication(workspace, clusterName, namespace, applicationId string) (*Application, error) {

View File

@@ -20,6 +20,8 @@ import (
"sort"
"strings"
"kubesphere.io/kubesphere/pkg/apiserver/query"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
@@ -266,11 +268,14 @@ func (c *repoOperator) ListRepos(conditions *params.Conditions, orderBy string,
sort.Sort(HelmRepoList(repos))
}
items := make([]interface{}, 0, limit)
totalCount := len(repos)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
repos = repos[start:end]
items := make([]interface{}, 0, len(repos))
for i, j := offset, 0; i < len(repos) && j < limit; i, j = i+1, j+1 {
items = append(items, convertRepo(repos[i]))
}
return &models.PageableResponse{Items: items, TotalCount: len(repos)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}
func helmRepoFilter(namePrefix string, list []*v1alpha1.HelmRepo) (res []*v1alpha1.HelmRepo) {
@@ -311,10 +316,14 @@ func (c *repoOperator) ListRepoEvents(repoId string, conditions *params.Conditio
return nil, err
}
items := make([]interface{}, 0, limit)
for i, j := offset, 0; i < len(repo.Status.SyncState) && j < limit; i, j = i+1, j+1 {
items = append(items, convertRepoEvent(&repo.ObjectMeta, &repo.Status.SyncState[j]))
states := repo.Status.SyncState
totalCount := len(states)
start, end := (&query.Pagination{Limit: limit, Offset: offset}).GetValidPagination(totalCount)
states = states[start:end]
items := make([]interface{}, 0, len(states))
for i := range states {
items = append(items, convertRepoEvent(&repo.ObjectMeta, &states[i]))
}
return &models.PageableResponse{Items: items, TotalCount: len(repo.Status.SyncState)}, nil
return &models.PageableResponse{Items: items, TotalCount: totalCount}, nil
}

View File

@@ -31,7 +31,7 @@ import (
"helm.sh/helm/v3/pkg/kube"
"k8s.io/apimachinery/pkg/util/wait"
"gopkg.in/yaml.v3"
yaml "gopkg.in/yaml.v3"
helmrelease "helm.sh/helm/v3/pkg/release"
"k8s.io/klog"
kpath "k8s.io/utils/path"