mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Cleanup v2 session to require endpoint
Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
parent
750b41ced4
commit
9c6f8e1439
3 changed files with 61 additions and 45 deletions
|
@ -379,26 +379,30 @@ type downloadInfo struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool) error {
|
func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool) error {
|
||||||
auth, err := r.GetV2Authorization(repoInfo.RemoteName, true)
|
endpoint, err := r.V2RegistryEndpoint(repoInfo.Index)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting registry endpoint: %s", err)
|
||||||
|
}
|
||||||
|
auth, err := r.GetV2Authorization(endpoint, repoInfo.RemoteName, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting authorization: %s", err)
|
return fmt.Errorf("error getting authorization: %s", err)
|
||||||
}
|
}
|
||||||
var layersDownloaded bool
|
var layersDownloaded bool
|
||||||
if tag == "" {
|
if tag == "" {
|
||||||
log.Debugf("Pulling tag list from V2 registry for %s", repoInfo.CanonicalName)
|
log.Debugf("Pulling tag list from V2 registry for %s", repoInfo.CanonicalName)
|
||||||
tags, err := r.GetV2RemoteTags(repoInfo.RemoteName, auth)
|
tags, err := r.GetV2RemoteTags(endpoint, repoInfo.RemoteName, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, t := range tags {
|
for _, t := range tags {
|
||||||
if downloaded, err := s.pullV2Tag(eng, r, out, repoInfo, t, sf, parallel, auth); err != nil {
|
if downloaded, err := s.pullV2Tag(eng, r, out, endpoint, repoInfo, t, sf, parallel, auth); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if downloaded {
|
} else if downloaded {
|
||||||
layersDownloaded = true
|
layersDownloaded = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if downloaded, err := s.pullV2Tag(eng, r, out, repoInfo, tag, sf, parallel, auth); err != nil {
|
if downloaded, err := s.pullV2Tag(eng, r, out, endpoint, repoInfo, tag, sf, parallel, auth); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if downloaded {
|
} else if downloaded {
|
||||||
layersDownloaded = true
|
layersDownloaded = true
|
||||||
|
@ -413,9 +417,9 @@ func (s *TagStore) pullV2Repository(eng *engine.Engine, r *registry.Session, out
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Writer, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool, auth *registry.RequestAuthorization) (bool, error) {
|
func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Writer, endpoint *registry.Endpoint, repoInfo *registry.RepositoryInfo, tag string, sf *utils.StreamFormatter, parallel bool, auth *registry.RequestAuthorization) (bool, error) {
|
||||||
log.Debugf("Pulling tag from V2 registry: %q", tag)
|
log.Debugf("Pulling tag from V2 registry: %q", tag)
|
||||||
manifestBytes, err := r.GetV2ImageManifest(repoInfo.RemoteName, tag, auth)
|
manifestBytes, err := r.GetV2ImageManifest(endpoint, repoInfo.RemoteName, tag, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -479,7 +483,7 @@ func (s *TagStore) pullV2Tag(eng *engine.Engine, r *registry.Session, out io.Wri
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
r, l, err := r.GetV2ImageBlobReader(repoInfo.RemoteName, sumType, checksum, auth)
|
r, l, err := r.GetV2ImageBlobReader(endpoint, repoInfo.RemoteName, sumType, checksum, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -260,7 +260,11 @@ func (s *TagStore) pushV2Repository(r *registry.Session, eng *engine.Engine, out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auth, err := r.GetV2Authorization(repoInfo.RemoteName, false)
|
endpoint, err := r.V2RegistryEndpoint(repoInfo.Index)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error getting registry endpoint: %s", err)
|
||||||
|
}
|
||||||
|
auth, err := r.GetV2Authorization(endpoint, repoInfo.RemoteName, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting authorization: %s", err)
|
return fmt.Errorf("error getting authorization: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -330,13 +334,13 @@ func (s *TagStore) pushV2Repository(r *registry.Session, eng *engine.Engine, out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call mount blob
|
// Call mount blob
|
||||||
exists, err := r.PostV2ImageMountBlob(repoInfo.RemoteName, sumParts[0], manifestSum, auth)
|
exists, err := r.HeadV2ImageBlob(endpoint, repoInfo.RemoteName, sumParts[0], manifestSum, auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out.Write(sf.FormatProgress(utils.TruncateID(img.ID), "Image push failed", nil))
|
out.Write(sf.FormatProgress(utils.TruncateID(img.ID), "Image push failed", nil))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
err = r.PutV2ImageBlob(repoInfo.RemoteName, sumParts[0], manifestSum, utils.ProgressReader(arch, int(img.Size), out, sf, false, utils.TruncateID(img.ID), "Pushing"), auth)
|
err = r.PutV2ImageBlob(endpoint, repoInfo.RemoteName, sumParts[0], manifestSum, utils.ProgressReader(arch, int(img.Size), out, sf, false, utils.TruncateID(img.ID), "Pushing"), auth)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
out.Write(sf.FormatProgress(utils.TruncateID(img.ID), "Image push failed", nil))
|
out.Write(sf.FormatProgress(utils.TruncateID(img.ID), "Image push failed", nil))
|
||||||
return err
|
return err
|
||||||
|
@ -348,7 +352,7 @@ func (s *TagStore) pushV2Repository(r *registry.Session, eng *engine.Engine, out
|
||||||
}
|
}
|
||||||
|
|
||||||
// push the manifest
|
// push the manifest
|
||||||
return r.PutV2ImageManifest(repoInfo.RemoteName, tag, bytes.NewReader([]byte(manifestBytes)), auth)
|
return r.PutV2ImageManifest(endpoint, repoInfo.RemoteName, tag, bytes.NewReader([]byte(manifestBytes)), auth)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Allow to interrupt current push when new push of same image is done.
|
// FIXME: Allow to interrupt current push when new push of same image is done.
|
||||||
|
|
|
@ -19,33 +19,41 @@ func getV2Builder(e *Endpoint) *v2.URLBuilder {
|
||||||
return e.URLBuilder
|
return e.URLBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Session) V2RegistryEndpoint(index *IndexInfo) (ep *Endpoint, err error) {
|
||||||
|
// TODO check if should use Mirror
|
||||||
|
if index.Official {
|
||||||
|
ep, err = newEndpoint(REGISTRYSERVER, true)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = validateEndpoint(ep)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if r.indexEndpoint.String() == index.GetAuthConfigKey() {
|
||||||
|
ep = r.indexEndpoint
|
||||||
|
} else {
|
||||||
|
ep, err = NewEndpoint(index)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ep.URLBuilder = v2.NewURLBuilder(ep.URL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetV2Authorization gets the authorization needed to the given image
|
// GetV2Authorization gets the authorization needed to the given image
|
||||||
// If readonly access is requested, then only the authorization may
|
// If readonly access is requested, then only the authorization may
|
||||||
// only be used for Get operations.
|
// only be used for Get operations.
|
||||||
func (r *Session) GetV2Authorization(imageName string, readOnly bool) (auth *RequestAuthorization, err error) {
|
func (r *Session) GetV2Authorization(ep *Endpoint, imageName string, readOnly bool) (auth *RequestAuthorization, err error) {
|
||||||
scopes := []string{"pull"}
|
scopes := []string{"pull"}
|
||||||
if !readOnly {
|
if !readOnly {
|
||||||
scopes = append(scopes, "push")
|
scopes = append(scopes, "push")
|
||||||
}
|
}
|
||||||
|
|
||||||
var registry *Endpoint
|
|
||||||
if r.indexEndpoint.String() == IndexServerAddress() {
|
|
||||||
registry, err = newEndpoint(REGISTRYSERVER, true)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = validateEndpoint(registry)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
registry = r.indexEndpoint
|
|
||||||
}
|
|
||||||
registry.URLBuilder = v2.NewURLBuilder(registry.URL)
|
|
||||||
r.indexEndpoint = registry
|
|
||||||
|
|
||||||
log.Debugf("Getting authorization for %s %s", imageName, scopes)
|
log.Debugf("Getting authorization for %s %s", imageName, scopes)
|
||||||
return NewRequestAuthorization(r.GetAuthConfig(true), registry, "repository", imageName, scopes), nil
|
return NewRequestAuthorization(r.GetAuthConfig(true), ep, "repository", imageName, scopes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -55,8 +63,8 @@ func (r *Session) GetV2Authorization(imageName string, readOnly bool) (auth *Req
|
||||||
// 1.c) if anything else, err
|
// 1.c) if anything else, err
|
||||||
// 2) PUT the created/signed manifest
|
// 2) PUT the created/signed manifest
|
||||||
//
|
//
|
||||||
func (r *Session) GetV2ImageManifest(imageName, tagName string, auth *RequestAuthorization) ([]byte, error) {
|
func (r *Session) GetV2ImageManifest(ep *Endpoint, imageName, tagName string, auth *RequestAuthorization) ([]byte, error) {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildManifestURL(imageName, tagName)
|
routeURL, err := getV2Builder(ep).BuildManifestURL(imageName, tagName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -92,11 +100,11 @@ func (r *Session) GetV2ImageManifest(imageName, tagName string, auth *RequestAut
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// - Succeeded to mount for this image scope
|
// - Succeeded to head image blob (already exists)
|
||||||
// - Failed with no error (So continue to Push the Blob)
|
// - Failed with no error (continue to Push the Blob)
|
||||||
// - Failed with error
|
// - Failed with error
|
||||||
func (r *Session) PostV2ImageMountBlob(imageName, sumType, sum string, auth *RequestAuthorization) (bool, error) {
|
func (r *Session) HeadV2ImageBlob(ep *Endpoint, imageName, sumType, sum string, auth *RequestAuthorization) (bool, error) {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobURL(imageName, sumType+":"+sum)
|
routeURL, err := getV2Builder(ep).BuildBlobURL(imageName, sumType+":"+sum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -127,8 +135,8 @@ func (r *Session) PostV2ImageMountBlob(imageName, sumType, sum string, auth *Req
|
||||||
return false, fmt.Errorf("Failed to mount %q - %s:%s : %d", imageName, sumType, sum, res.StatusCode)
|
return false, fmt.Errorf("Failed to mount %q - %s:%s : %d", imageName, sumType, sum, res.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Session) GetV2ImageBlob(imageName, sumType, sum string, blobWrtr io.Writer, auth *RequestAuthorization) error {
|
func (r *Session) GetV2ImageBlob(ep *Endpoint, imageName, sumType, sum string, blobWrtr io.Writer, auth *RequestAuthorization) error {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobURL(imageName, sumType+":"+sum)
|
routeURL, err := getV2Builder(ep).BuildBlobURL(imageName, sumType+":"+sum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -158,8 +166,8 @@ func (r *Session) GetV2ImageBlob(imageName, sumType, sum string, blobWrtr io.Wri
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Session) GetV2ImageBlobReader(imageName, sumType, sum string, auth *RequestAuthorization) (io.ReadCloser, int64, error) {
|
func (r *Session) GetV2ImageBlobReader(ep *Endpoint, imageName, sumType, sum string, auth *RequestAuthorization) (io.ReadCloser, int64, error) {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobURL(imageName, sumType+":"+sum)
|
routeURL, err := getV2Builder(ep).BuildBlobURL(imageName, sumType+":"+sum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
@ -195,8 +203,8 @@ func (r *Session) GetV2ImageBlobReader(imageName, sumType, sum string, auth *Req
|
||||||
// Push the image to the server for storage.
|
// Push the image to the server for storage.
|
||||||
// 'layer' is an uncompressed reader of the blob to be pushed.
|
// 'layer' is an uncompressed reader of the blob to be pushed.
|
||||||
// The server will generate it's own checksum calculation.
|
// The server will generate it's own checksum calculation.
|
||||||
func (r *Session) PutV2ImageBlob(imageName, sumType, sumStr string, blobRdr io.Reader, auth *RequestAuthorization) error {
|
func (r *Session) PutV2ImageBlob(ep *Endpoint, imageName, sumType, sumStr string, blobRdr io.Reader, auth *RequestAuthorization) error {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildBlobUploadURL(imageName)
|
routeURL, err := getV2Builder(ep).BuildBlobUploadURL(imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -245,8 +253,8 @@ func (r *Session) PutV2ImageBlob(imageName, sumType, sumStr string, blobRdr io.R
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally Push the (signed) manifest of the blobs we've just pushed
|
// Finally Push the (signed) manifest of the blobs we've just pushed
|
||||||
func (r *Session) PutV2ImageManifest(imageName, tagName string, manifestRdr io.Reader, auth *RequestAuthorization) error {
|
func (r *Session) PutV2ImageManifest(ep *Endpoint, imageName, tagName string, manifestRdr io.Reader, auth *RequestAuthorization) error {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildManifestURL(imageName, tagName)
|
routeURL, err := getV2Builder(ep).BuildManifestURL(imageName, tagName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -283,8 +291,8 @@ type remoteTags struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a repository name, returns a json array of string tags
|
// Given a repository name, returns a json array of string tags
|
||||||
func (r *Session) GetV2RemoteTags(imageName string, auth *RequestAuthorization) ([]string, error) {
|
func (r *Session) GetV2RemoteTags(ep *Endpoint, imageName string, auth *RequestAuthorization) ([]string, error) {
|
||||||
routeURL, err := getV2Builder(r.indexEndpoint).BuildTagsURL(imageName)
|
routeURL, err := getV2Builder(ep).BuildTagsURL(imageName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue