2015-07-29 20:12:01 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2017-08-12 00:31:16 +02:00
|
|
|
"context"
|
2015-07-29 20:12:01 +02:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext "github.com/docker/distribution/context"
|
2015-12-15 23:35:23 +01:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-07-29 20:12:01 +02:00
|
|
|
"github.com/docker/distribution/registry/proxy/scheduler"
|
2016-12-17 01:28:34 +01:00
|
|
|
"github.com/opencontainers/go-digest"
|
2015-07-29 20:12:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// todo(richardscothern): from cache control header or config file
|
|
|
|
const blobTTL = time.Duration(24 * 7 * time.Hour)
|
|
|
|
|
|
|
|
type proxyBlobStore struct {
|
2016-01-27 01:42:10 +01:00
|
|
|
localStore distribution.BlobStore
|
|
|
|
remoteStore distribution.BlobService
|
|
|
|
scheduler *scheduler.TTLExpirationScheduler
|
|
|
|
repositoryName reference.Named
|
2016-02-11 03:07:28 +01:00
|
|
|
authChallenger authChallenger
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
var _ distribution.BlobStore = &proxyBlobStore{}
|
2015-07-29 20:12:01 +02:00
|
|
|
|
|
|
|
// inflight tracks currently downloading blobs
|
2015-09-19 01:11:35 +02:00
|
|
|
var inflight = make(map[digest.Digest]struct{})
|
2015-07-29 20:12:01 +02:00
|
|
|
|
|
|
|
// mu protects inflight
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
|
|
|
func setResponseHeaders(w http.ResponseWriter, length int64, mediaType string, digest digest.Digest) {
|
|
|
|
w.Header().Set("Content-Length", strconv.FormatInt(length, 10))
|
|
|
|
w.Header().Set("Content-Type", mediaType)
|
|
|
|
w.Header().Set("Docker-Content-Digest", digest.String())
|
|
|
|
w.Header().Set("Etag", digest.String())
|
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) copyContent(ctx context.Context, dgst digest.Digest, writer io.Writer) (distribution.Descriptor, error) {
|
|
|
|
desc, err := pbs.remoteStore.Stat(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
if w, ok := writer.(http.ResponseWriter); ok {
|
|
|
|
setResponseHeaders(w, desc.Size, desc.MediaType, dgst)
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteReader, err := pbs.remoteStore.Open(ctx, dgst)
|
|
|
|
if err != nil {
|
2015-09-19 01:11:35 +02:00
|
|
|
return distribution.Descriptor{}, err
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2016-08-02 05:10:10 +02:00
|
|
|
defer remoteReader.Close()
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
_, err = io.CopyN(writer, remoteReader, desc.Size)
|
2015-07-29 20:12:01 +02:00
|
|
|
if err != nil {
|
2015-09-19 01:11:35 +02:00
|
|
|
return distribution.Descriptor{}, err
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
proxyMetrics.BlobPush(uint64(desc.Size))
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
return desc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pbs *proxyBlobStore) serveLocal(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) (bool, error) {
|
|
|
|
localDesc, err := pbs.localStore.Stat(ctx, dgst)
|
2015-07-29 20:12:01 +02:00
|
|
|
if err != nil {
|
2015-09-19 01:11:35 +02:00
|
|
|
// Stat can report a zero sized file here if it's checked between creation
|
|
|
|
// and population. Return nil error, and continue
|
|
|
|
return false, nil
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
if err == nil {
|
|
|
|
proxyMetrics.BlobPush(uint64(localDesc.Size))
|
|
|
|
return true, pbs.localStore.ServeBlob(ctx, w, r, dgst)
|
|
|
|
}
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
return false, nil
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
}
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) storeLocal(ctx context.Context, dgst digest.Digest) error {
|
|
|
|
defer func() {
|
|
|
|
mu.Lock()
|
|
|
|
delete(inflight, dgst)
|
|
|
|
mu.Unlock()
|
|
|
|
}()
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
var desc distribution.Descriptor
|
|
|
|
var err error
|
2015-07-29 20:12:01 +02:00
|
|
|
var bw distribution.BlobWriter
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
bw, err = pbs.localStore.Create(ctx)
|
2015-07-29 20:12:01 +02:00
|
|
|
if err != nil {
|
2015-09-19 01:11:35 +02:00
|
|
|
return err
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
desc, err = pbs.copyContent(ctx, dgst, bw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-29 20:12:01 +02:00
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
_, err = bw.Commit(ctx, desc)
|
2015-07-29 20:12:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) ServeBlob(ctx context.Context, w http.ResponseWriter, r *http.Request, dgst digest.Digest) error {
|
|
|
|
served, err := pbs.serveLocal(ctx, w, r, dgst)
|
2015-07-29 20:12:01 +02:00
|
|
|
if err != nil {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ctx).Errorf("Error serving blob from local storage: %s", err.Error())
|
2015-09-19 01:11:35 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if served {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:07:28 +01:00
|
|
|
if err := pbs.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
mu.Lock()
|
|
|
|
_, ok := inflight[dgst]
|
|
|
|
if ok {
|
|
|
|
mu.Unlock()
|
|
|
|
_, err := pbs.copyContent(ctx, dgst, w)
|
2015-07-29 20:12:01 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-09-19 01:11:35 +02:00
|
|
|
inflight[dgst] = struct{}{}
|
|
|
|
mu.Unlock()
|
|
|
|
|
|
|
|
go func(dgst digest.Digest) {
|
|
|
|
if err := pbs.storeLocal(ctx, dgst); err != nil {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ctx).Errorf("Error committing to storage: %s", err.Error())
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
2016-01-27 01:42:10 +01:00
|
|
|
|
|
|
|
blobRef, err := reference.WithDigest(pbs.repositoryName, dgst)
|
|
|
|
if err != nil {
|
2017-08-12 00:31:16 +02:00
|
|
|
dcontext.GetLogger(ctx).Errorf("Error creating reference: %s", err)
|
2016-01-27 01:42:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pbs.scheduler.AddBlob(blobRef, repositoryTTL)
|
2015-09-19 01:11:35 +02:00
|
|
|
}(dgst)
|
|
|
|
|
|
|
|
_, err = pbs.copyContent(ctx, dgst, w)
|
|
|
|
if err != nil {
|
2015-07-29 20:12:01 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-09-19 01:11:35 +02:00
|
|
|
return nil
|
2015-07-29 20:12:01 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
2015-07-29 20:12:01 +02:00
|
|
|
desc, err := pbs.localStore.Stat(ctx, dgst)
|
|
|
|
if err == nil {
|
|
|
|
return desc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != distribution.ErrBlobUnknown {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:07:28 +01:00
|
|
|
if err := pbs.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
2015-07-29 20:12:01 +02:00
|
|
|
return pbs.remoteStore.Stat(ctx, dgst)
|
|
|
|
}
|
|
|
|
|
2016-02-18 01:32:23 +01:00
|
|
|
func (pbs *proxyBlobStore) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) {
|
|
|
|
blob, err := pbs.localStore.Get(ctx, dgst)
|
|
|
|
if err == nil {
|
|
|
|
return blob, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pbs.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, err = pbs.remoteStore.Get(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = pbs.localStore.Put(ctx, "", blob)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return blob, nil
|
|
|
|
}
|
|
|
|
|
2015-07-29 20:12:01 +02:00
|
|
|
// Unsupported functions
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
|
2015-07-29 20:12:01 +02:00
|
|
|
return distribution.Descriptor{}, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2016-01-13 20:44:42 +01:00
|
|
|
func (pbs *proxyBlobStore) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) {
|
2015-07-29 20:12:01 +02:00
|
|
|
return nil, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
|
2015-07-29 20:12:01 +02:00
|
|
|
return nil, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-12-15 23:35:23 +01:00
|
|
|
func (pbs *proxyBlobStore) Mount(ctx context.Context, sourceRepo reference.Named, dgst digest.Digest) (distribution.Descriptor, error) {
|
2015-12-15 03:34:18 +01:00
|
|
|
return distribution.Descriptor{}, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) {
|
2015-07-29 20:12:01 +02:00
|
|
|
return nil, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-09-19 01:11:35 +02:00
|
|
|
func (pbs *proxyBlobStore) Delete(ctx context.Context, dgst digest.Digest) error {
|
2015-07-29 20:12:01 +02:00
|
|
|
return distribution.ErrUnsupported
|
|
|
|
}
|