2018-02-05 16:05:59 -05:00
|
|
|
package distribution // import "github.com/docker/docker/distribution"
|
2016-02-11 17:08:49 -05:00
|
|
|
|
|
|
|
import (
|
2017-07-19 10:20:13 -04:00
|
|
|
"fmt"
|
2016-02-11 17:08:49 -05:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2016-11-29 16:13:42 -05:00
|
|
|
"github.com/docker/distribution"
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-02-11 17:08:49 -05:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
|
|
|
"github.com/docker/distribution/registry/api/v2"
|
|
|
|
"github.com/docker/distribution/registry/client"
|
2016-03-12 12:01:01 -05:00
|
|
|
"github.com/docker/distribution/registry/client/auth"
|
2016-02-11 17:08:49 -05:00
|
|
|
"github.com/docker/docker/distribution/xfer"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2018-08-03 09:41:05 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-02-11 17:08:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoSupport is an error type used for errors indicating that an operation
|
|
|
|
// is not supported. It encapsulates a more specific error.
|
|
|
|
type ErrNoSupport struct{ Err error }
|
|
|
|
|
|
|
|
func (e ErrNoSupport) Error() string {
|
|
|
|
if e.Err == nil {
|
|
|
|
return "not supported"
|
|
|
|
}
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallbackError wraps an error that can possibly allow fallback to a different
|
|
|
|
// endpoint.
|
|
|
|
type fallbackError struct {
|
|
|
|
// err is the error being wrapped.
|
|
|
|
err error
|
|
|
|
// confirmedV2 is set to true if it was confirmed that the registry
|
|
|
|
// supports the v2 protocol. This is used to limit fallbacks to the v1
|
|
|
|
// protocol.
|
|
|
|
confirmedV2 bool
|
2016-02-11 18:45:29 -05:00
|
|
|
// transportOK is set to true if we managed to speak HTTP with the
|
|
|
|
// registry. This confirms that we're using appropriate TLS settings
|
|
|
|
// (or lack of TLS).
|
|
|
|
transportOK bool
|
2016-02-11 17:08:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error renders the FallbackError as a string.
|
|
|
|
func (f fallbackError) Error() string {
|
2016-07-25 17:52:27 -04:00
|
|
|
return f.Cause().Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fallbackError) Cause() error {
|
|
|
|
return f.err
|
2016-02-11 17:08:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// shouldV2Fallback returns true if this error is a reason to fall back to v1.
|
|
|
|
func shouldV2Fallback(err errcode.Error) bool {
|
|
|
|
switch err.Code {
|
|
|
|
case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
type notFoundError struct {
|
|
|
|
cause errcode.Error
|
|
|
|
ref reference.Named
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e notFoundError) Error() string {
|
|
|
|
switch e.cause.Code {
|
|
|
|
case errcode.ErrorCodeDenied:
|
|
|
|
// ErrorCodeDenied is used when access to the repository was denied
|
2018-08-03 09:41:05 -04:00
|
|
|
return errors.Wrapf(e.cause, "pull access denied for %s, repository does not exist or may require 'docker login'", reference.FamiliarName(e.ref)).Error()
|
2017-07-19 10:20:13 -04:00
|
|
|
case v2.ErrorCodeManifestUnknown:
|
2018-08-03 09:41:05 -04:00
|
|
|
return errors.Wrapf(e.cause, "manifest for %s not found", reference.FamiliarString(e.ref)).Error()
|
2017-07-19 10:20:13 -04:00
|
|
|
case v2.ErrorCodeNameUnknown:
|
2018-08-03 09:41:05 -04:00
|
|
|
return errors.Wrapf(e.cause, "repository %s not found", reference.FamiliarName(e.ref)).Error()
|
2017-07-19 10:20:13 -04:00
|
|
|
}
|
|
|
|
// Shouldn't get here, but this is better than returning an empty string
|
|
|
|
return e.cause.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e notFoundError) NotFound() {}
|
|
|
|
|
|
|
|
func (e notFoundError) Cause() error {
|
|
|
|
return e.cause
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:06:48 -05:00
|
|
|
// TranslatePullError is used to convert an error from a registry pull
|
|
|
|
// operation to an error representing the entire pull operation. Any error
|
|
|
|
// information which is not used by the returned error gets output to
|
|
|
|
// log at info level.
|
|
|
|
func TranslatePullError(err error, ref reference.Named) error {
|
2016-11-10 18:14:33 -05:00
|
|
|
switch v := err.(type) {
|
|
|
|
case errcode.Errors:
|
|
|
|
if len(v) != 0 {
|
|
|
|
for _, extra := range v[1:] {
|
|
|
|
logrus.Infof("Ignoring extra error returned from registry: %v", extra)
|
|
|
|
}
|
2016-11-15 18:06:48 -05:00
|
|
|
return TranslatePullError(v[0], ref)
|
2016-11-10 18:14:33 -05:00
|
|
|
}
|
|
|
|
case errcode.Error:
|
|
|
|
switch v.Code {
|
2017-07-19 10:20:13 -04:00
|
|
|
case errcode.ErrorCodeDenied, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
|
|
|
|
return notFoundError{v, ref}
|
2016-11-10 18:14:33 -05:00
|
|
|
}
|
|
|
|
case xfer.DoNotRetry:
|
2016-11-15 18:06:48 -05:00
|
|
|
return TranslatePullError(v.Err, ref)
|
2016-11-10 18:14:33 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.Unknown(err)
|
2016-11-10 18:14:33 -05:00
|
|
|
}
|
|
|
|
|
2016-02-11 17:08:49 -05:00
|
|
|
// continueOnError returns true if we should fallback to the next endpoint
|
|
|
|
// as a result of this error.
|
2017-11-14 19:06:17 -05:00
|
|
|
func continueOnError(err error, mirrorEndpoint bool) bool {
|
2016-02-11 17:08:49 -05:00
|
|
|
switch v := err.(type) {
|
|
|
|
case errcode.Errors:
|
|
|
|
if len(v) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2017-11-14 19:06:17 -05:00
|
|
|
return continueOnError(v[0], mirrorEndpoint)
|
2016-02-11 17:08:49 -05:00
|
|
|
case ErrNoSupport:
|
2017-11-14 19:06:17 -05:00
|
|
|
return continueOnError(v.Err, mirrorEndpoint)
|
2016-02-11 17:08:49 -05:00
|
|
|
case errcode.Error:
|
2017-11-14 19:06:17 -05:00
|
|
|
return mirrorEndpoint || shouldV2Fallback(v)
|
2016-02-11 17:08:49 -05:00
|
|
|
case *client.UnexpectedHTTPResponseError:
|
|
|
|
return true
|
|
|
|
case ImageConfigPullError:
|
2017-11-14 19:06:17 -05:00
|
|
|
// ImageConfigPullError only happens with v2 images, v1 fallback is
|
|
|
|
// unnecessary.
|
|
|
|
// Failures from a mirror endpoint should result in fallback to the
|
|
|
|
// canonical repo.
|
|
|
|
return mirrorEndpoint
|
2016-02-11 17:08:49 -05:00
|
|
|
case error:
|
2017-05-23 10:22:32 -04:00
|
|
|
return !strings.Contains(err.Error(), strings.ToLower(syscall.ESRCH.Error()))
|
2016-02-11 17:08:49 -05:00
|
|
|
}
|
|
|
|
// let's be nice and fallback if the error is a completely
|
|
|
|
// unexpected one.
|
|
|
|
// If new errors have to be handled in some way, please
|
|
|
|
// add them to the switch above.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
|
|
|
|
// operation after this error.
|
|
|
|
func retryOnError(err error) error {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case errcode.Errors:
|
2016-03-14 16:11:35 -04:00
|
|
|
if len(v) != 0 {
|
|
|
|
return retryOnError(v[0])
|
|
|
|
}
|
2016-02-11 17:08:49 -05:00
|
|
|
case errcode.Error:
|
|
|
|
switch v.Code {
|
2016-07-11 20:06:23 -04:00
|
|
|
case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
|
2016-02-11 17:08:49 -05:00
|
|
|
return xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
case *url.Error:
|
2016-03-18 13:54:05 -04:00
|
|
|
switch v.Err {
|
|
|
|
case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
|
2016-03-12 12:01:01 -05:00
|
|
|
return xfer.DoNotRetry{Err: v.Err}
|
|
|
|
}
|
2016-02-11 17:08:49 -05:00
|
|
|
return retryOnError(v.Err)
|
|
|
|
case *client.UnexpectedHTTPResponseError:
|
|
|
|
return xfer.DoNotRetry{Err: err}
|
|
|
|
case error:
|
2016-11-29 16:13:42 -05:00
|
|
|
if err == distribution.ErrBlobUnknown {
|
|
|
|
return xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2016-02-11 17:08:49 -05:00
|
|
|
if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
|
|
|
|
return xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// let's be nice and fallback if the error is a completely
|
|
|
|
// unexpected one.
|
|
|
|
// If new errors have to be handled in some way, please
|
|
|
|
// add them to the switch above.
|
|
|
|
return err
|
|
|
|
}
|
2017-07-19 10:20:13 -04:00
|
|
|
|
|
|
|
type invalidManifestClassError struct {
|
|
|
|
mediaType string
|
|
|
|
class string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidManifestClassError) Error() string {
|
|
|
|
return fmt.Sprintf("Encountered remote %q(%s) when fetching", e.mediaType, e.class)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidManifestClassError) InvalidParameter() {}
|
|
|
|
|
|
|
|
type invalidManifestFormatError struct{}
|
|
|
|
|
|
|
|
func (invalidManifestFormatError) Error() string {
|
|
|
|
return "unsupported manifest format"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (invalidManifestFormatError) InvalidParameter() {}
|
|
|
|
|
|
|
|
type reservedNameError string
|
|
|
|
|
|
|
|
func (e reservedNameError) Error() string {
|
|
|
|
return "'" + string(e) + "' is a reserved name"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e reservedNameError) Forbidden() {}
|