Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable referrers pagination #56

Merged
merged 8 commits into from
Jan 20, 2023
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion registry/handlers/referrers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package handlers
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"strconv"

"github.com/distribution/distribution/v3"
dcontext "github.com/distribution/distribution/v3/context"
Expand All @@ -20,6 +23,12 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

const (
pageSizeMin = 1
pageSizeDefault = 10
pageSizeMax = 100
)

// referrersDispatcher takes the request context and builds the
// appropriate handler for handling referrers requests.
func referrersDispatcher(ctx *Context, r *http.Request) http.Handler {
Expand Down Expand Up @@ -59,13 +68,30 @@ func (h *referrersHandler) GetReferrers(w http.ResponseWriter, r *http.Request)
return
}

// extract the artifactType filter.
var annotations map[string]string
var artifactTypeFilter string
if artifactTypeFilter = r.URL.Query().Get("artifactType"); artifactTypeFilter != "" {
annotations = map[string]string{
v1.AnnotationReferrersFiltersApplied: "artifactType",
}
}

// extract the page size info. Users define page size by using the n
// query parameter. The default page size is 10.
pageSize, err := strconv.Atoi(r.URL.Query().Get("n"))
if err != nil || pageSize < pageSizeMin || pageSize > pageSizeMax {
pageSize = pageSizeDefault
}

// extract the page number info.
pageNumber, err := strconv.Atoi(r.URL.Query().Get("p"))
if err != nil || pageNumber < 0 {
pageNumber = 0
}

// currently, pagination would call generateReferrersList multiple times
// and this is not efficient. This implementation is for testing purpose.
referrers, err := h.generateReferrersList(h, h.Digest, artifactTypeFilter)
shizhMSFT marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if _, ok := err.(distribution.ErrManifestUnknownRevision); ok {
Expand All @@ -76,8 +102,18 @@ func (h *referrersHandler) GetReferrers(w http.ResponseWriter, r *http.Request)
return
}

if referrers == nil {
startIndex := pageNumber * pageSize

if referrers == nil || startIndex >= len(referrers) {
referrers = []v1.Descriptor{}
} else {
// if there's only 1 page of results left
if len(referrers)-startIndex <= pageSize {
referrers = referrers[startIndex:]
shizhMSFT marked this conversation as resolved.
Show resolved Hide resolved
} else {
referrers = referrers[startIndex:(startIndex + pageSize)]
w.Header().Set("Link", generateLinkHeader(h.Repository.Named().Name(), h.Digest.String(), artifactTypeFilter, pageSize, pageNumber+1))
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: code style: readability

Suggested change
startIndex := pageNumber * pageSize
if referrers == nil || startIndex >= len(referrers) {
referrers = []v1.Descriptor{}
} else {
// if there's only 1 page of results left
if len(referrers)-startIndex <= pageSize {
referrers = referrers[startIndex:]
} else {
referrers = referrers[startIndex:(startIndex + pageSize)]
w.Header().Set("Link", generateLinkHeader(h.Repository.Named().Name(), h.Digest.String(), artifactTypeFilter, pageSize, pageNumber+1))
}
}
startIndex := pageNumber * pageSize
if referrers == nil || len(referrers) <= startIndex {
referrers = []v1.Descriptor{}
} else if len(referrers) <= startIndex+pageSize {
// only 1 page of results left
referrers = referrers[startIndex:]
} else {
referrers = referrers[startIndex:(startIndex + pageSize)]
w.Header().Set("Link", generateLinkHeader(h.Repository.Named().Name(), h.Digest.String(), artifactTypeFilter, pageSize, pageNumber+1))
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.


response := v1.Index{
Expand Down Expand Up @@ -195,6 +231,19 @@ func readlink(ctx context.Context, path string, stDriver driver.StorageDriver) (
return digest.Parse(string(content))
}

func generateLinkHeader(repoName, subjectDigest, artifactType string, pageSize int, p int) string {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: code style: variable naming

Suggested change
func generateLinkHeader(repoName, subjectDigest, artifactType string, pageSize int, p int) string {
func generateLinkHeader(repoName, subjectDigest, artifactType string, pageSize int, pageNumber int) string {

linkURL := fmt.Sprintf("/v2/%s/referrers/%s", repoName, subjectDigest)
v := url.Values{}
v.Add("p", fmt.Sprint(p))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer strconv.Itoa to fmt.Sprint for performance.

Suggested change
v.Add("p", fmt.Sprint(p))
v.Add("p", strconv.Itoa(p))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.

if artifactType != "" {
v.Add("artifactType", artifactType)
}
if pageSize > 0 {
v.Add("n", fmt.Sprint(pageSize))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed.

}
return fmt.Sprintf("<%s?%s>; rel=\"next\"", linkURL, v.Encode())
}

func generateReferrerFromArtifact(ctx context.Context,
blobStatter distribution.BlobStatter,
referrerDigest digest.Digest,
Expand Down