blobs.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package distribution
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "time"
  8. "github.com/docker/distribution/context"
  9. "github.com/docker/distribution/digest"
  10. "github.com/docker/distribution/reference"
  11. )
  12. var (
  13. // ErrBlobExists returned when blob already exists
  14. ErrBlobExists = errors.New("blob exists")
  15. // ErrBlobDigestUnsupported when blob digest is an unsupported version.
  16. ErrBlobDigestUnsupported = errors.New("unsupported blob digest")
  17. // ErrBlobUnknown when blob is not found.
  18. ErrBlobUnknown = errors.New("unknown blob")
  19. // ErrBlobUploadUnknown returned when upload is not found.
  20. ErrBlobUploadUnknown = errors.New("blob upload unknown")
  21. // ErrBlobInvalidLength returned when the blob has an expected length on
  22. // commit, meaning mismatched with the descriptor or an invalid value.
  23. ErrBlobInvalidLength = errors.New("blob invalid length")
  24. )
  25. // ErrBlobInvalidDigest returned when digest check fails.
  26. type ErrBlobInvalidDigest struct {
  27. Digest digest.Digest
  28. Reason error
  29. }
  30. func (err ErrBlobInvalidDigest) Error() string {
  31. return fmt.Sprintf("invalid digest for referenced layer: %v, %v",
  32. err.Digest, err.Reason)
  33. }
  34. // ErrBlobMounted returned when a blob is mounted from another repository
  35. // instead of initiating an upload session.
  36. type ErrBlobMounted struct {
  37. From reference.Canonical
  38. Descriptor Descriptor
  39. }
  40. func (err ErrBlobMounted) Error() string {
  41. return fmt.Sprintf("blob mounted from: %v to: %v",
  42. err.From, err.Descriptor)
  43. }
  44. // Descriptor describes targeted content. Used in conjunction with a blob
  45. // store, a descriptor can be used to fetch, store and target any kind of
  46. // blob. The struct also describes the wire protocol format. Fields should
  47. // only be added but never changed.
  48. type Descriptor struct {
  49. // MediaType describe the type of the content. All text based formats are
  50. // encoded as utf-8.
  51. MediaType string `json:"mediaType,omitempty"`
  52. // Size in bytes of content.
  53. Size int64 `json:"size,omitempty"`
  54. // Digest uniquely identifies the content. A byte stream can be verified
  55. // against against this digest.
  56. Digest digest.Digest `json:"digest,omitempty"`
  57. // NOTE: Before adding a field here, please ensure that all
  58. // other options have been exhausted. Much of the type relationships
  59. // depend on the simplicity of this type.
  60. }
  61. // Descriptor returns the descriptor, to make it satisfy the Describable
  62. // interface. Note that implementations of Describable are generally objects
  63. // which can be described, not simply descriptors; this exception is in place
  64. // to make it more convenient to pass actual descriptors to functions that
  65. // expect Describable objects.
  66. func (d Descriptor) Descriptor() Descriptor {
  67. return d
  68. }
  69. // BlobStatter makes blob descriptors available by digest. The service may
  70. // provide a descriptor of a different digest if the provided digest is not
  71. // canonical.
  72. type BlobStatter interface {
  73. // Stat provides metadata about a blob identified by the digest. If the
  74. // blob is unknown to the describer, ErrBlobUnknown will be returned.
  75. Stat(ctx context.Context, dgst digest.Digest) (Descriptor, error)
  76. }
  77. // BlobDeleter enables deleting blobs from storage.
  78. type BlobDeleter interface {
  79. Delete(ctx context.Context, dgst digest.Digest) error
  80. }
  81. // BlobEnumerator enables iterating over blobs from storage
  82. type BlobEnumerator interface {
  83. Enumerate(ctx context.Context, ingester func(dgst digest.Digest) error) error
  84. }
  85. // BlobDescriptorService manages metadata about a blob by digest. Most
  86. // implementations will not expose such an interface explicitly. Such mappings
  87. // should be maintained by interacting with the BlobIngester. Hence, this is
  88. // left off of BlobService and BlobStore.
  89. type BlobDescriptorService interface {
  90. BlobStatter
  91. // SetDescriptor assigns the descriptor to the digest. The provided digest and
  92. // the digest in the descriptor must map to identical content but they may
  93. // differ on their algorithm. The descriptor must have the canonical
  94. // digest of the content and the digest algorithm must match the
  95. // annotators canonical algorithm.
  96. //
  97. // Such a facility can be used to map blobs between digest domains, with
  98. // the restriction that the algorithm of the descriptor must match the
  99. // canonical algorithm (ie sha256) of the annotator.
  100. SetDescriptor(ctx context.Context, dgst digest.Digest, desc Descriptor) error
  101. // Clear enables descriptors to be unlinked
  102. Clear(ctx context.Context, dgst digest.Digest) error
  103. }
  104. // ReadSeekCloser is the primary reader type for blob data, combining
  105. // io.ReadSeeker with io.Closer.
  106. type ReadSeekCloser interface {
  107. io.ReadSeeker
  108. io.Closer
  109. }
  110. // BlobProvider describes operations for getting blob data.
  111. type BlobProvider interface {
  112. // Get returns the entire blob identified by digest along with the descriptor.
  113. Get(ctx context.Context, dgst digest.Digest) ([]byte, error)
  114. // Open provides a ReadSeekCloser to the blob identified by the provided
  115. // descriptor. If the blob is not known to the service, an error will be
  116. // returned.
  117. Open(ctx context.Context, dgst digest.Digest) (ReadSeekCloser, error)
  118. }
  119. // BlobServer can serve blobs via http.
  120. type BlobServer interface {
  121. // ServeBlob attempts to serve the blob, identifed by dgst, via http. The
  122. // service may decide to redirect the client elsewhere or serve the data
  123. // directly.
  124. //
  125. // This handler only issues successful responses, such as 2xx or 3xx,
  126. // meaning it serves data or issues a redirect. If the blob is not
  127. // available, an error will be returned and the caller may still issue a
  128. // response.
  129. //
  130. // The implementation may serve the same blob from a different digest
  131. // domain. The appropriate headers will be set for the blob, unless they
  132. // have already been set by the caller.
  133. ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error
  134. }
  135. // BlobIngester ingests blob data.
  136. type BlobIngester interface {
  137. // Put inserts the content p into the blob service, returning a descriptor
  138. // or an error.
  139. Put(ctx context.Context, mediaType string, p []byte) (Descriptor, error)
  140. // Create allocates a new blob writer to add a blob to this service. The
  141. // returned handle can be written to and later resumed using an opaque
  142. // identifier. With this approach, one can Close and Resume a BlobWriter
  143. // multiple times until the BlobWriter is committed or cancelled.
  144. Create(ctx context.Context, options ...BlobCreateOption) (BlobWriter, error)
  145. // Resume attempts to resume a write to a blob, identified by an id.
  146. Resume(ctx context.Context, id string) (BlobWriter, error)
  147. }
  148. // BlobCreateOption is a general extensible function argument for blob creation
  149. // methods. A BlobIngester may choose to honor any or none of the given
  150. // BlobCreateOptions, which can be specific to the implementation of the
  151. // BlobIngester receiving them.
  152. // TODO (brianbland): unify this with ManifestServiceOption in the future
  153. type BlobCreateOption interface {
  154. Apply(interface{}) error
  155. }
  156. // BlobWriter provides a handle for inserting data into a blob store.
  157. // Instances should be obtained from BlobWriteService.Writer and
  158. // BlobWriteService.Resume. If supported by the store, a writer can be
  159. // recovered with the id.
  160. type BlobWriter interface {
  161. io.WriteCloser
  162. io.ReaderFrom
  163. // Size returns the number of bytes written to this blob.
  164. Size() int64
  165. // ID returns the identifier for this writer. The ID can be used with the
  166. // Blob service to later resume the write.
  167. ID() string
  168. // StartedAt returns the time this blob write was started.
  169. StartedAt() time.Time
  170. // Commit completes the blob writer process. The content is verified
  171. // against the provided provisional descriptor, which may result in an
  172. // error. Depending on the implementation, written data may be validated
  173. // against the provisional descriptor fields. If MediaType is not present,
  174. // the implementation may reject the commit or assign "application/octet-
  175. // stream" to the blob. The returned descriptor may have a different
  176. // digest depending on the blob store, referred to as the canonical
  177. // descriptor.
  178. Commit(ctx context.Context, provisional Descriptor) (canonical Descriptor, err error)
  179. // Cancel ends the blob write without storing any data and frees any
  180. // associated resources. Any data written thus far will be lost. Cancel
  181. // implementations should allow multiple calls even after a commit that
  182. // result in a no-op. This allows use of Cancel in a defer statement,
  183. // increasing the assurance that it is correctly called.
  184. Cancel(ctx context.Context) error
  185. }
  186. // BlobService combines the operations to access, read and write blobs. This
  187. // can be used to describe remote blob services.
  188. type BlobService interface {
  189. BlobStatter
  190. BlobProvider
  191. BlobIngester
  192. }
  193. // BlobStore represent the entire suite of blob related operations. Such an
  194. // implementation can access, read, write, delete and serve blobs.
  195. type BlobStore interface {
  196. BlobService
  197. BlobServer
  198. BlobDeleter
  199. }