Skip to content

Commit 3413eb6

Browse files
authored
registry: implement pagination (#1430)
1 parent d3ed408 commit 3413eb6

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

pkg/registry/manifest.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,6 @@ func (m *manifests) handleTags(resp http.ResponseWriter, req *http.Request) *reg
237237
elem := strings.Split(req.URL.Path, "/")
238238
elem = elem[1:]
239239
repo := strings.Join(elem[1:len(elem)-2], "/")
240-
query := req.URL.Query()
241-
nStr := query.Get("n")
242-
n := 1000
243-
if nStr != "" {
244-
n, _ = strconv.Atoi(nStr)
245-
}
246240

247241
if req.Method == "GET" {
248242
m.lock.Lock()
@@ -258,19 +252,37 @@ func (m *manifests) handleTags(resp http.ResponseWriter, req *http.Request) *reg
258252
}
259253

260254
var tags []string
261-
countTags := 0
262-
// TODO: implement pagination https://github.com/opencontainers/distribution-spec/blob/b505e9cc53ec499edbd9c1be32298388921bb705/detail.md#tags-paginated
263255
for tag := range c {
264-
if countTags >= n {
265-
break
266-
}
267-
countTags++
268256
if !strings.Contains(tag, "sha256:") {
269257
tags = append(tags, tag)
270258
}
271259
}
272260
sort.Strings(tags)
273261

262+
// https://github.com/opencontainers/distribution-spec/blob/b505e9cc53ec499edbd9c1be32298388921bb705/detail.md#tags-paginated
263+
// Offset using last query parameter.
264+
if last := req.URL.Query().Get("last"); last != "" {
265+
for i, t := range tags {
266+
if t > last {
267+
tags = tags[i:]
268+
break
269+
}
270+
}
271+
}
272+
273+
// Limit using n query parameter.
274+
if ns := req.URL.Query().Get("n"); ns != "" {
275+
if n, err := strconv.Atoi(ns); err != nil {
276+
return &regError{
277+
Status: http.StatusBadRequest,
278+
Code: "BAD_REQUEST",
279+
Message: fmt.Sprintf("parsing n: %v", err),
280+
}
281+
} else if n < len(tags) {
282+
tags = tags[:n]
283+
}
284+
}
285+
274286
tagsToList := listTags{
275287
Name: repo,
276288
Tags: tags,

pkg/registry/registry_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,23 @@ func TestCalls(t *testing.T) {
407407
Method: "GET",
408408
URL: "/v2/foo/tags/list?n=1000",
409409
Code: http.StatusOK,
410+
Want: `{"name":"foo","tags":["latest","tag1"]}`,
411+
},
412+
{
413+
Description: "limit tags",
414+
Manifests: map[string]string{"foo/manifests/latest": "foo", "foo/manifests/tag1": "foo"},
415+
Method: "GET",
416+
URL: "/v2/foo/tags/list?n=1",
417+
Code: http.StatusOK,
418+
Want: `{"name":"foo","tags":["latest"]}`,
419+
},
420+
{
421+
Description: "offset tags",
422+
Manifests: map[string]string{"foo/manifests/latest": "foo", "foo/manifests/tag1": "foo"},
423+
Method: "GET",
424+
URL: "/v2/foo/tags/list?last=latest",
425+
Code: http.StatusOK,
426+
Want: `{"name":"foo","tags":["tag1"]}`,
410427
},
411428
{
412429
Description: "list non existing tags",

0 commit comments

Comments
 (0)