errors.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package distribution
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. "syscall"
  7. "github.com/docker/distribution"
  8. "github.com/docker/distribution/reference"
  9. "github.com/docker/distribution/registry/api/errcode"
  10. "github.com/docker/distribution/registry/api/v2"
  11. "github.com/docker/distribution/registry/client"
  12. "github.com/docker/distribution/registry/client/auth"
  13. "github.com/docker/docker/distribution/xfer"
  14. "github.com/docker/docker/errdefs"
  15. "github.com/sirupsen/logrus"
  16. )
  17. // ErrNoSupport is an error type used for errors indicating that an operation
  18. // is not supported. It encapsulates a more specific error.
  19. type ErrNoSupport struct{ Err error }
  20. func (e ErrNoSupport) Error() string {
  21. if e.Err == nil {
  22. return "not supported"
  23. }
  24. return e.Err.Error()
  25. }
  26. // fallbackError wraps an error that can possibly allow fallback to a different
  27. // endpoint.
  28. type fallbackError struct {
  29. // err is the error being wrapped.
  30. err error
  31. // confirmedV2 is set to true if it was confirmed that the registry
  32. // supports the v2 protocol. This is used to limit fallbacks to the v1
  33. // protocol.
  34. confirmedV2 bool
  35. // transportOK is set to true if we managed to speak HTTP with the
  36. // registry. This confirms that we're using appropriate TLS settings
  37. // (or lack of TLS).
  38. transportOK bool
  39. }
  40. // Error renders the FallbackError as a string.
  41. func (f fallbackError) Error() string {
  42. return f.Cause().Error()
  43. }
  44. func (f fallbackError) Cause() error {
  45. return f.err
  46. }
  47. // shouldV2Fallback returns true if this error is a reason to fall back to v1.
  48. func shouldV2Fallback(err errcode.Error) bool {
  49. switch err.Code {
  50. case errcode.ErrorCodeUnauthorized, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
  51. return true
  52. }
  53. return false
  54. }
  55. type notFoundError struct {
  56. cause errcode.Error
  57. ref reference.Named
  58. }
  59. func (e notFoundError) Error() string {
  60. switch e.cause.Code {
  61. case errcode.ErrorCodeDenied:
  62. // ErrorCodeDenied is used when access to the repository was denied
  63. return fmt.Sprintf("pull access denied for %s, repository does not exist or may require 'docker login'", reference.FamiliarName(e.ref))
  64. case v2.ErrorCodeManifestUnknown:
  65. return fmt.Sprintf("manifest for %s not found", reference.FamiliarString(e.ref))
  66. case v2.ErrorCodeNameUnknown:
  67. return fmt.Sprintf("repository %s not found", reference.FamiliarName(e.ref))
  68. }
  69. // Shouldn't get here, but this is better than returning an empty string
  70. return e.cause.Message
  71. }
  72. func (e notFoundError) NotFound() {}
  73. func (e notFoundError) Cause() error {
  74. return e.cause
  75. }
  76. // TranslatePullError is used to convert an error from a registry pull
  77. // operation to an error representing the entire pull operation. Any error
  78. // information which is not used by the returned error gets output to
  79. // log at info level.
  80. func TranslatePullError(err error, ref reference.Named) error {
  81. switch v := err.(type) {
  82. case errcode.Errors:
  83. if len(v) != 0 {
  84. for _, extra := range v[1:] {
  85. logrus.Infof("Ignoring extra error returned from registry: %v", extra)
  86. }
  87. return TranslatePullError(v[0], ref)
  88. }
  89. case errcode.Error:
  90. switch v.Code {
  91. case errcode.ErrorCodeDenied, v2.ErrorCodeManifestUnknown, v2.ErrorCodeNameUnknown:
  92. return notFoundError{v, ref}
  93. }
  94. case xfer.DoNotRetry:
  95. return TranslatePullError(v.Err, ref)
  96. }
  97. return errdefs.Unknown(err)
  98. }
  99. // continueOnError returns true if we should fallback to the next endpoint
  100. // as a result of this error.
  101. func continueOnError(err error, mirrorEndpoint bool) bool {
  102. switch v := err.(type) {
  103. case errcode.Errors:
  104. if len(v) == 0 {
  105. return true
  106. }
  107. return continueOnError(v[0], mirrorEndpoint)
  108. case ErrNoSupport:
  109. return continueOnError(v.Err, mirrorEndpoint)
  110. case errcode.Error:
  111. return mirrorEndpoint || shouldV2Fallback(v)
  112. case *client.UnexpectedHTTPResponseError:
  113. return true
  114. case ImageConfigPullError:
  115. // ImageConfigPullError only happens with v2 images, v1 fallback is
  116. // unnecessary.
  117. // Failures from a mirror endpoint should result in fallback to the
  118. // canonical repo.
  119. return mirrorEndpoint
  120. case error:
  121. return !strings.Contains(err.Error(), strings.ToLower(syscall.ESRCH.Error()))
  122. }
  123. // let's be nice and fallback if the error is a completely
  124. // unexpected one.
  125. // If new errors have to be handled in some way, please
  126. // add them to the switch above.
  127. return true
  128. }
  129. // retryOnError wraps the error in xfer.DoNotRetry if we should not retry the
  130. // operation after this error.
  131. func retryOnError(err error) error {
  132. switch v := err.(type) {
  133. case errcode.Errors:
  134. if len(v) != 0 {
  135. return retryOnError(v[0])
  136. }
  137. case errcode.Error:
  138. switch v.Code {
  139. case errcode.ErrorCodeUnauthorized, errcode.ErrorCodeUnsupported, errcode.ErrorCodeDenied, errcode.ErrorCodeTooManyRequests, v2.ErrorCodeNameUnknown:
  140. return xfer.DoNotRetry{Err: err}
  141. }
  142. case *url.Error:
  143. switch v.Err {
  144. case auth.ErrNoBasicAuthCredentials, auth.ErrNoToken:
  145. return xfer.DoNotRetry{Err: v.Err}
  146. }
  147. return retryOnError(v.Err)
  148. case *client.UnexpectedHTTPResponseError:
  149. return xfer.DoNotRetry{Err: err}
  150. case error:
  151. if err == distribution.ErrBlobUnknown {
  152. return xfer.DoNotRetry{Err: err}
  153. }
  154. if strings.Contains(err.Error(), strings.ToLower(syscall.ENOSPC.Error())) {
  155. return xfer.DoNotRetry{Err: err}
  156. }
  157. }
  158. // let's be nice and fallback if the error is a completely
  159. // unexpected one.
  160. // If new errors have to be handled in some way, please
  161. // add them to the switch above.
  162. return err
  163. }
  164. type invalidManifestClassError struct {
  165. mediaType string
  166. class string
  167. }
  168. func (e invalidManifestClassError) Error() string {
  169. return fmt.Sprintf("Encountered remote %q(%s) when fetching", e.mediaType, e.class)
  170. }
  171. func (e invalidManifestClassError) InvalidParameter() {}
  172. type invalidManifestFormatError struct{}
  173. func (invalidManifestFormatError) Error() string {
  174. return "unsupported manifest format"
  175. }
  176. func (invalidManifestFormatError) InvalidParameter() {}
  177. type reservedNameError string
  178. func (e reservedNameError) Error() string {
  179. return "'" + string(e) + "' is a reserved name"
  180. }
  181. func (e reservedNameError) Forbidden() {}