-
Notifications
You must be signed in to change notification settings - Fork 77
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
Registry catalog #87
Merged
openshift-merge-robot
merged 1 commit into
openshift:master
from
miminar:repository-catalog
Jun 26, 2018
Merged
Registry catalog #87
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/cache" | ||
|
||
"github.com/docker/distribution/context" | ||
|
||
imageapiv1 "github.com/openshift/api/image/v1" | ||
"github.com/openshift/image-registry/pkg/dockerregistry/server/client" | ||
) | ||
|
||
const paginationEntryTTL = time.Minute | ||
|
||
// RepositoryEnumerator allows to enumerate repositories known to the registry. | ||
type RepositoryEnumerator interface { | ||
// EnumerateRepositories fills the given repos slice with image stream names. The slice's length | ||
// determines the maximum number of repositories returned. The repositories are lexicographically sorted. | ||
// The last argument allows for pagination. It is the offset in the catalog. Returned is a number of | ||
// repositories filled. If there are no more repositories to return, io.EOF is returned. | ||
EnumerateRepositories(ctx context.Context, repos []string, last string) (n int, err error) | ||
} | ||
|
||
// cachingRepositoryEnumerator is an enumerator that supports chunking by caching associations between | ||
// repository names and opaque continuation tokens. | ||
type cachingRepositoryEnumerator struct { | ||
client client.RegistryClient | ||
// a cache of opaque continue tokens for repository enumeration | ||
cache *cache.LRUExpireCache | ||
} | ||
|
||
var _ RepositoryEnumerator = &cachingRepositoryEnumerator{} | ||
|
||
// NewCachingRepositoryEnumerator returns a new caching repository enumerator. | ||
func NewCachingRepositoryEnumerator(client client.RegistryClient, cache *cache.LRUExpireCache) RepositoryEnumerator { | ||
return &cachingRepositoryEnumerator{ | ||
client: client, | ||
cache: cache, | ||
} | ||
} | ||
|
||
type isHandlerFunc func(is *imageapiv1.ImageStream) error | ||
|
||
var errNoSpaceInSlice = errors.New("no space in slice") | ||
var errEnumerationFinished = errors.New("enumeration finished") | ||
|
||
func (re *cachingRepositoryEnumerator) EnumerateRepositories( | ||
ctx context.Context, | ||
repos []string, | ||
last string, | ||
) (n int, err error) { | ||
if len(repos) == 0 { | ||
// Client explicitly requested 0 results. Returning nil for error seems more appropriate but this is | ||
// more in line with upstream does. Returning nil actually makes the upstream code panic. | ||
// TODO: patch upstream? /_catalog?n=0 results in 500 | ||
return 0, errNoSpaceInSlice | ||
} | ||
|
||
err = re.enumerateImageStreams(ctx, int64(len(repos)), last, func(is *imageapiv1.ImageStream) error { | ||
name := fmt.Sprintf("%s/%s", is.Namespace, is.Name) | ||
repos[n] = name | ||
n++ | ||
|
||
if n >= len(repos) { | ||
return errEnumerationFinished | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
switch err { | ||
case errEnumerationFinished: | ||
err = nil | ||
case nil: | ||
err = io.EOF | ||
} | ||
|
||
return | ||
} | ||
|
||
func (r *cachingRepositoryEnumerator) enumerateImageStreams( | ||
ctx context.Context, | ||
limit int64, | ||
last string, | ||
handler isHandlerFunc, | ||
) error { | ||
var ( | ||
start string | ||
warned bool | ||
) | ||
|
||
client, ok := userClientFrom(ctx) | ||
if !ok { | ||
context.GetLogger(ctx).Warnf("user token not set, falling back to registry client") | ||
osClient, err := r.client.Client() | ||
if err != nil { | ||
return err | ||
} | ||
client = osClient | ||
} | ||
|
||
if len(last) > 0 { | ||
if c, ok := r.cache.Get(last); !ok { | ||
context.GetLogger(ctx).Warnf("failed to find opaque continue token for last repository=%q -> requesting the full image stream list instead of %d items", last, limit) | ||
warned = true | ||
limit = 0 | ||
} else { | ||
start = c.(string) | ||
} | ||
} | ||
|
||
iss, err := client.ImageStreams("").List(metav1.ListOptions{ | ||
Limit: limit, | ||
Continue: start, | ||
}) | ||
if apierrors.IsResourceExpired(err) { | ||
context.GetLogger(ctx).Warnf("continuation token expired (%v) -> requesting the full image stream list", err) | ||
iss, err = client.ImageStreams("").List(metav1.ListOptions{}) | ||
warned = true | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
warnBrokenPagination := func(msg string) { | ||
if !warned { | ||
context.GetLogger(ctx).Warnf("pagination not working: %s; the master API does not support chunking", msg) | ||
warned = true | ||
} | ||
} | ||
|
||
if limit > 0 && limit < int64(len(iss.Items)) { | ||
warnBrokenPagination(fmt.Sprintf("received %d image streams instead of requested maximum %d", len(iss.Items), limit)) | ||
} | ||
if len(iss.Items) > 0 && len(iss.ListMeta.Continue) > 0 { | ||
last := iss.Items[len(iss.Items)-1] | ||
r.cache.Add(fmt.Sprintf("%s/%s", last.Namespace, last.Name), iss.ListMeta.Continue, paginationEntryTTL) | ||
} | ||
|
||
for _, is := range iss.Items { | ||
name := fmt.Sprintf("%s/%s", is.Namespace, is.Name) | ||
if len(last) > 0 && name <= last { | ||
if !warned { | ||
warnBrokenPagination(fmt.Sprintf("received unexpected repository name %q -"+ | ||
" lexicographically preceding the requested %q", name, last)) | ||
} | ||
continue | ||
} | ||
err := handler(&is) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should set warned=true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary, but it will add to legibility and robustness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added