1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

distribution: add GetRepository(), un-export NewV2Repository, ValidateRepoName

These were only exported to facilitate ImageService.GetRepository() (used for
the `GET /distribution/{name:.*}/json` endpoint.

Moving the core functionality of that to the distribution package makes it
more consistent with (e.g.) "pull" operations, and allows us to keep more things
internal.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-27 22:11:13 +01:00
parent 572c7e0184
commit 566c8db66d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
8 changed files with 49 additions and 38 deletions

View file

@ -133,35 +133,12 @@ func (i *ImageService) pullImageWithReference(ctx context.Context, ref reference
// GetRepository returns a repository from the registry.
func (i *ImageService) GetRepository(ctx context.Context, ref reference.Named, authConfig *types.AuthConfig) (dist.Repository, error) {
// get repository info
repoInfo, err := i.registryService.ResolveRepository(ref)
if err != nil {
return nil, errdefs.InvalidParameter(err)
}
// makes sure name is not empty or `scratch`
if err := distribution.ValidateRepoName(repoInfo.Name); err != nil {
return nil, errdefs.InvalidParameter(err)
}
// get endpoints
endpoints, err := i.registryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
if err != nil {
return nil, err
}
// retrieve repository
var (
repository dist.Repository
lastError error
)
for _, endpoint := range endpoints {
repository, lastError = distribution.NewV2Repository(ctx, repoInfo, endpoint, nil, authConfig, "pull")
if lastError == nil {
break
}
}
return repository, lastError
return distribution.GetRepository(ctx, ref, &distribution.ImagePullConfig{
Config: distribution.Config{
AuthConfig: authConfig,
RegistryService: i.registryService,
},
})
}
func tempLease(ctx context.Context, mgr leases.Manager) (context.Context, func(context.Context) error, error) {

View file

@ -38,7 +38,7 @@ func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, loc
}
// makes sure name is not `scratch`
if err := ValidateRepoName(repoInfo.Name); err != nil {
if err := validateRepoName(repoInfo.Name); err != nil {
return err
}
@ -112,8 +112,8 @@ func writeStatus(requestedTag string, out progress.Output, layersDownloaded bool
}
}
// ValidateRepoName validates the name of a repository.
func ValidateRepoName(name reference.Named) error {
// validateRepoName validates the name of a repository.
func validateRepoName(name reference.Named) error {
if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
}

View file

@ -63,7 +63,7 @@ type puller struct {
func (p *puller) pull(ctx context.Context, ref reference.Named) (err error) {
// TODO(tiborvass): was ReceiveTimeout
p.repo, err = NewV2Repository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
p.repo, err = newRepository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
if err != nil {
logrus.Warnf("Error getting v2 registry: %v", err)
return err

View file

@ -359,7 +359,7 @@ func testNewPuller(t *testing.T, rawurl string) *puller {
}
p := newPuller(endpoint, repoInfo, imagePullConfig, nil)
p.repo, err = NewV2Repository(context.Background(), p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
p.repo, err = newRepository(context.Background(), p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
if err != nil {
t.Fatal(err)
}

View file

@ -60,7 +60,7 @@ type pushState struct {
func (p *pusher) push(ctx context.Context) (err error) {
p.pushState.remoteLayers = make(map[layer.DiffID]distribution.Descriptor)
p.repo, err = NewV2Repository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "push", "pull")
p.repo, err = newRepository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "push", "pull")
p.pushState.hasAuthInfo = p.config.AuthConfig.RegistryToken != "" || (p.config.AuthConfig.Username != "" && p.config.AuthConfig.Password != "")
if err != nil {
logrus.Debugf("Error getting v2 registry: %v", err)

View file

@ -53,10 +53,10 @@ func init() {
}
}
// NewV2Repository returns a repository (v2 only). It creates an HTTP transport
// newRepository returns a repository (v2 only). It creates an HTTP transport
// providing timeout settings and authentication support, and also verifies the
// remote API version.
func NewV2Repository(
func newRepository(
ctx context.Context, repoInfo *registry.RepositoryInfo, endpoint registry.APIEndpoint,
metaHeaders http.Header, authConfig *types.AuthConfig, actions ...string,
) (repo distribution.Repository, err error) {

View file

@ -70,7 +70,7 @@ func testTokenPassThru(t *testing.T, ts *httptest.Server) {
}
p := newPuller(endpoint, repoInfo, imagePullConfig, nil)
ctx := context.Background()
p.repo, err = NewV2Repository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
p.repo, err = newRepository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
if err != nil {
t.Fatal(err)
}

View file

@ -0,0 +1,34 @@
package distribution
import (
"context"
"github.com/docker/distribution"
"github.com/docker/distribution/reference"
"github.com/docker/docker/errdefs"
)
// GetRepository returns a repository from the registry.
func GetRepository(ctx context.Context, ref reference.Named, config *ImagePullConfig) (repository distribution.Repository, lastError error) {
repoInfo, err := config.RegistryService.ResolveRepository(ref)
if err != nil {
return nil, errdefs.InvalidParameter(err)
}
// makes sure name is not empty or `scratch`
if err := validateRepoName(repoInfo.Name); err != nil {
return nil, errdefs.InvalidParameter(err)
}
endpoints, err := config.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
if err != nil {
return nil, err
}
for _, endpoint := range endpoints {
repository, lastError = newRepository(ctx, repoInfo, endpoint, nil, config.AuthConfig, "pull")
if lastError == nil {
break
}
}
return repository, lastError
}