From fcc52ed1919192f0b6289ef9c1f4d548682171ed Mon Sep 17 00:00:00 2001 From: Aaron3S <34506289+Aaron3S@users.noreply.github.com> Date: Wed, 15 Sep 2021 16:09:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(proxy):=20=E4=BF=AE=E5=A4=8D=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/v1/proxy/proxy.go | 61 ++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/internal/api/v1/proxy/proxy.go b/internal/api/v1/proxy/proxy.go index d0af99c4f..55ac4e4b6 100644 --- a/internal/api/v1/proxy/proxy.go +++ b/internal/api/v1/proxy/proxy.go @@ -135,7 +135,20 @@ func (h *Handler) KubernetesAPIProxy() iris.Handler { ctx.Values().Set("message", err) return } - _, _ = ctx.JSON(resp) + klo := K8sListObj{ + Kind: resp.Kind, + ApiVersion: resp.APIVersion, + Metadata: resp.ListMeta, + Items: resp.Items, + } + + p, err := pagerAndSearch(ctx, klo, keywords) + if err != nil { + ctx.StatusCode(iris.StatusInternalServerError) + ctx.Values().Set("message", err) + return + } + _, _ = ctx.JSON(p) return } if http.MethodGet == requestMethod && namespaced && namespace != "" && !hasNsFilter { @@ -162,30 +175,19 @@ func (h *Handler) KubernetesAPIProxy() iris.Handler { resp.StatusCode = http.StatusInternalServerError } if req.Method == http.MethodGet && search { - num, err1 := ctx.Values().GetInt("pageNum") - size, err2 := ctx.Values().GetInt("pageSize") var listObj K8sListObj if err := json.Unmarshal(rawResp, &listObj); err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.Values().Set("message", err) return } - listObj.Sort() - if keywords != "" { - listObj.Items = fieldFilter(listObj.Items, withNamespaceAndNameMatcher(keywords)) - } - total := len(listObj.Items) - if err1 == nil && err2 == nil { - tt, items, err := pageFilter(num, size, listObj.Items) - if err != nil { - ctx.StatusCode(iris.StatusInternalServerError) - ctx.Values().Set("message", err) - return - } - total = tt - listObj.Items = items + p, err := pagerAndSearch(ctx, listObj, keywords) + if err != nil { + ctx.StatusCode(iris.StatusInternalServerError) + ctx.Values().Set("message", err.Error()) + return } - _, _ = ctx.JSON(pkgV1.Page{Items: listObj.Items, Total: total}) + _, _ = ctx.JSON(p) return } ctx.StatusCode(resp.StatusCode) @@ -196,6 +198,29 @@ func (h *Handler) KubernetesAPIProxy() iris.Handler { var timeTemplate = "2006-01-02T15:04:05Z" +func pagerAndSearch(ctx *context.Context, listObj K8sListObj, keywords string) (*pkgV1.Page, error) { + num, err1 := ctx.Values().GetInt("pageNum") + size, err2 := ctx.Values().GetInt("pageSize") + var p pkgV1.Page + listObj.Sort() + if keywords != "" { + listObj.Items = fieldFilter(listObj.Items, withNamespaceAndNameMatcher(keywords)) + } + total := len(listObj.Items) + if err1 == nil && err2 == nil { + tt, items, err := pageFilter(num, size, listObj.Items) + if err != nil { + return nil, err + } + total = tt + listObj.Items = items + p.Total = total + p.Items = listObj.Items + return &p, nil + } + return nil, nil +} + func getTime(obj interface{}) time.Time { //判断是否存在lasttime o := obj.(map[string]interface{})