diff options
author | Sebastiaan van Stijn <github@gone.nl> | 2022-02-27 22:11:13 +0100 |
---|---|---|
committer | Sebastiaan van Stijn <github@gone.nl> | 2022-04-21 23:12:02 +0200 |
commit | 566c8db66dd01f1848c8e38b9cdff75ae57d9028 (patch) | |
tree | 85fdeddc595aecdb9dd8509af26c85542d7ead7f /distribution | |
parent | 572c7e01847b3d1822c5c19ec15a72994ed860f3 (diff) |
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>
Diffstat (limited to 'distribution')
-rw-r--r-- | distribution/pull.go | 6 | ||||
-rw-r--r-- | distribution/pull_v2.go | 2 | ||||
-rw-r--r-- | distribution/pull_v2_test.go | 2 | ||||
-rw-r--r-- | distribution/push_v2.go | 2 | ||||
-rw-r--r-- | distribution/registry.go | 4 | ||||
-rw-r--r-- | distribution/registry_unit_test.go | 2 | ||||
-rw-r--r-- | distribution/repository.go | 34 |
7 files changed, 43 insertions, 9 deletions
diff --git a/distribution/pull.go b/distribution/pull.go index baba845264..cb60f62b6f 100644 --- a/distribution/pull.go +++ b/distribution/pull.go @@ -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)) } diff --git a/distribution/pull_v2.go b/distribution/pull_v2.go index 0495ab4146..37ced60d43 100644 --- a/distribution/pull_v2.go +++ b/distribution/pull_v2.go @@ -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 diff --git a/distribution/pull_v2_test.go b/distribution/pull_v2_test.go index a080c9eadf..a4824dff08 100644 --- a/distribution/pull_v2_test.go +++ b/distribution/pull_v2_test.go @@ -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) } diff --git a/distribution/push_v2.go b/distribution/push_v2.go index 24824709ee..f5168f836b 100644 --- a/distribution/push_v2.go +++ b/distribution/push_v2.go @@ -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) diff --git a/distribution/registry.go b/distribution/registry.go index 904ffff2be..7ff01b5d92 100644 --- a/distribution/registry.go +++ b/distribution/registry.go @@ -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) { diff --git a/distribution/registry_unit_test.go b/distribution/registry_unit_test.go index df9e12a99a..c803577173 100644 --- a/distribution/registry_unit_test.go +++ b/distribution/registry_unit_test.go @@ -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) } diff --git a/distribution/repository.go b/distribution/repository.go new file mode 100644 index 0000000000..f424c24b65 --- /dev/null +++ b/distribution/repository.go @@ -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 +} |