From 7f0aa48333a525ef06fc6d553ef67401f476d324 Mon Sep 17 00:00:00 2001 From: KubeSphere CI Bot <47586280+ks-ci-bot@users.noreply.github.com> Date: Mon, 30 Oct 2023 15:40:31 +0800 Subject: [PATCH] [release-3.4] Images tag (#5958) * Feat: Add pagination for listing repository tags (#683) * feat: add pagination for listing repository tags Signed-off-by: wenhaozhou * use ParseQueryParameter Signed-off-by: wenhaozhou --------- Signed-off-by: wenhaozhou * feat: add tags total count (#695) (#698) Signed-off-by: wenhaozhou --------- Signed-off-by: wenhaozhou Co-authored-by: Wenhao Zhou <34303854+zhou1203@users.noreply.github.com> --- pkg/kapis/resources/v1alpha3/handler.go | 10 ++++++++++ pkg/kapis/resources/v1alpha3/register.go | 4 +++- pkg/models/registries/v2/registries.go | 1 + pkg/models/registries/v2/types.go | 1 + 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/kapis/resources/v1alpha3/handler.go b/pkg/kapis/resources/v1alpha3/handler.go index 587c7314c..e04e8aaf1 100644 --- a/pkg/kapis/resources/v1alpha3/handler.go +++ b/pkg/kapis/resources/v1alpha3/handler.go @@ -19,6 +19,7 @@ package v1alpha3 import ( "fmt" "net/http" + "sort" "strings" "github.com/emicklei/go-restful/v3" @@ -251,6 +252,9 @@ func (h *Handler) handleGetRepositoryTags(request *restful.Request, response *re secretName := request.QueryParameter("secret") namespace := request.PathParameter("namespace") repository := request.QueryParameter("repository") + + q := query.ParseQueryParameter(request) + var secret *v1.Secret if len(repository) == 0 { @@ -272,6 +276,12 @@ func (h *Handler) handleGetRepositoryTags(request *restful.Request, response *re return } + if !q.Ascending { + sort.Sort(sort.Reverse(sort.StringSlice(tags.Tags))) + } + startIndex, endIndex := q.Pagination.GetValidPagination(len(tags.Tags)) + tags.Tags = tags.Tags[startIndex:endIndex] + response.WriteHeaderAndJson(http.StatusOK, tags, restful.MIME_JSON) } diff --git a/pkg/kapis/resources/v1alpha3/register.go b/pkg/kapis/resources/v1alpha3/register.go index f5d47093a..40e800bcd 100644 --- a/pkg/kapis/resources/v1alpha3/register.go +++ b/pkg/kapis/resources/v1alpha3/register.go @@ -141,7 +141,9 @@ func AddToContainer(c *restful.Container, informerFactory informers.InformerFact Param(webservice.PathParameter("namespace", "Namespace of the image repository secret.").Required(true)). Param(webservice.QueryParameter("repository", "Repository to query, e.g. calico/cni.").Required(true)). Param(webservice.QueryParameter("secret", "Secret name of the image repository credential, left empty means anonymous fetch.").Required(false)). - Metadata(restfulspec.KeyOpenAPITags, []string{tagNamespacedResource}). + Param(webservice.QueryParameter(query.ParameterPage, "page").Required(false).DataFormat("page=%d").DefaultValue("page=1")). + Param(webservice.QueryParameter(query.ParameterLimit, "limit").Required(false)). + Param(webservice.QueryParameter(query.ParameterAscending, "sort parameters, e.g. reverse=true").Required(false).DefaultValue("ascending=false")). Doc("List repository tags, this is an experimental API, use it by your own caution."). Returns(http.StatusOK, ok, v2.RepositoryTags{})) diff --git a/pkg/models/registries/v2/registries.go b/pkg/models/registries/v2/registries.go index c32afd9bb..54625036a 100644 --- a/pkg/models/registries/v2/registries.go +++ b/pkg/models/registries/v2/registries.go @@ -52,6 +52,7 @@ func (r *registryer) ListRepositoryTags(src string) (RepositoryTags, error) { Registry: repo.RegistryStr(), Repository: repo.RepositoryStr(), Tags: tags, + Total: len(tags), }, nil } diff --git a/pkg/models/registries/v2/types.go b/pkg/models/registries/v2/types.go index 177bc31c7..06dae587d 100644 --- a/pkg/models/registries/v2/types.go +++ b/pkg/models/registries/v2/types.go @@ -42,6 +42,7 @@ type RepositoryTags struct { Registry string `json:"registry"` Repository string `json:"repository"` Tags []string `json:"tags"` + Total int `json:"total"` } // ImageConfig wraps v1.ConfigFile to avoid direct dependency