2015-07-29 18:12:01 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-07-29 18:12:01 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
2020-08-28 00:07:35 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/opencontainers/go-digest"
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
2023-10-24 17:16:58 +00:00
|
|
|
"github.com/distribution/distribution/v3/internal/dcontext"
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/registry/proxy/scheduler"
|
2023-08-30 15:50:56 +00:00
|
|
|
"github.com/distribution/reference"
|
2015-07-29 18:12:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type proxyBlobStore struct {
|
2016-01-27 00:42:10 +00:00
|
|
|
localStore distribution.BlobStore
|
|
|
|
remoteStore distribution.BlobService
|
|
|
|
scheduler *scheduler.TTLExpirationScheduler
|
2020-08-28 00:07:35 +00:00
|
|
|
ttl *time.Duration
|
2016-01-27 00:42:10 +00:00
|
|
|
repositoryName reference.Named
|
2016-02-11 02:07:28 +00:00
|
|
|
authChallenger authChallenger
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
var _ distribution.BlobStore = &proxyBlobStore{}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
// inflight tracks currently downloading blobs
|
2015-09-18 23:11:35 +00:00
|
|
|
var inflight = make(map[digest.Digest]struct{})
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
// mu protects inflight
|
|
|
|
var mu sync.Mutex
|
|
|
|
|
2024-01-26 23:56:45 +00:00
|
|
|
func setResponseHeaders(h http.Header, length int64, mediaType string, digest digest.Digest) {
|
|
|
|
h.Set("Content-Length", strconv.FormatInt(length, 10))
|
|
|
|
h.Set("Content-Type", mediaType)
|
|
|
|
h.Set("Docker-Content-Digest", digest.String())
|
|
|
|
h.Set("Etag", digest.String())
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 23:56:45 +00:00
|
|
|
func (pbs *proxyBlobStore) copyContent(ctx context.Context, dgst digest.Digest, writer io.Writer, h http.Header) (distribution.Descriptor, error) {
|
2015-09-18 23:11:35 +00:00
|
|
|
desc, err := pbs.remoteStore.Stat(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 23:56:45 +00:00
|
|
|
setResponseHeaders(h, desc.Size, desc.MediaType, dgst)
|
2015-07-29 18:12:01 +00:00
|
|
|
|
|
|
|
remoteReader, err := pbs.remoteStore.Open(ctx, dgst)
|
|
|
|
if err != nil {
|
2015-09-18 23:11:35 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 03:10:10 +00:00
|
|
|
defer remoteReader.Close()
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
_, err = io.CopyN(writer, remoteReader, desc.Size)
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
2015-09-18 23:11:35 +00:00
|
|
|
return distribution.Descriptor{}, err
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 13:47:54 +00:00
|
|
|
proxyMetrics.BlobPull(uint64(desc.Size))
|
|
|
|
proxyMetrics.BlobPush(uint64(desc.Size), false)
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-09-18 23:11:35 +00: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 18:12:01 +00:00
|
|
|
if err != nil {
|
2015-09-18 23:11:35 +00: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 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 13:47:54 +00:00
|
|
|
proxyMetrics.BlobPush(uint64(localDesc.Size), true)
|
2020-07-22 02:54:46 +00:00
|
|
|
return true, pbs.localStore.ServeBlob(ctx, w, r, dgst)
|
2015-09-18 23:11:35 +00:00
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-09-18 23:11:35 +00: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 18:12:01 +00:00
|
|
|
if err != nil {
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ctx).Errorf("Error serving blob from local storage: %s", err.Error())
|
2015-09-18 23:11:35 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if served {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-11 02:07:28 +00:00
|
|
|
if err := pbs.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
mu.Lock()
|
|
|
|
_, ok := inflight[dgst]
|
|
|
|
if ok {
|
2022-01-20 03:52:34 +00:00
|
|
|
// If the blob has been serving in other requests.
|
|
|
|
// Will return the blob from the remote store directly.
|
|
|
|
// TODO Maybe we could reuse the these blobs are serving remotely and caching locally.
|
2015-09-18 23:11:35 +00:00
|
|
|
mu.Unlock()
|
2024-01-26 23:56:45 +00:00
|
|
|
_, err := pbs.copyContent(ctx, dgst, w, w.Header())
|
2015-07-29 18:12:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-18 23:11:35 +00:00
|
|
|
inflight[dgst] = struct{}{}
|
|
|
|
mu.Unlock()
|
|
|
|
|
2022-01-20 03:52:34 +00:00
|
|
|
defer func() {
|
|
|
|
mu.Lock()
|
|
|
|
delete(inflight, dgst)
|
|
|
|
mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
bw, err := pbs.localStore.Create(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serving client and storing locally over same fetching request.
|
|
|
|
// This can prevent a redundant blob fetching.
|
|
|
|
multiWriter := io.MultiWriter(w, bw)
|
2024-01-26 23:56:45 +00:00
|
|
|
desc, err := pbs.copyContent(ctx, dgst, multiWriter, w.Header())
|
2022-01-20 03:52:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = bw.Commit(ctx, desc)
|
2015-09-18 23:11:35 +00:00
|
|
|
if err != nil {
|
2015-07-29 18:12:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-20 03:52:34 +00:00
|
|
|
|
|
|
|
blobRef, err := reference.WithDigest(pbs.repositoryName, dgst)
|
|
|
|
if err != nil {
|
|
|
|
dcontext.GetLogger(ctx).Errorf("Error creating reference: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if pbs.scheduler != nil && pbs.ttl != nil {
|
2023-11-18 06:50:40 +00:00
|
|
|
if err := pbs.scheduler.AddBlob(blobRef, *pbs.ttl); err != nil {
|
|
|
|
dcontext.GetLogger(ctx).Errorf("Error adding blob: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 03:52:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
return nil
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
func (pbs *proxyBlobStore) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
|
2015-07-29 18:12:01 +00: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 02:07:28 +00:00
|
|
|
if err := pbs.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return distribution.Descriptor{}, err
|
|
|
|
}
|
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
return pbs.remoteStore.Stat(ctx, dgst)
|
|
|
|
}
|
|
|
|
|
2016-02-18 00:32:23 +00: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 18:12:01 +00:00
|
|
|
// Unsupported functions
|
2015-09-18 23:11:35 +00:00
|
|
|
func (pbs *proxyBlobStore) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
|
2015-07-29 18:12:01 +00:00
|
|
|
return distribution.Descriptor{}, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2016-01-13 19:44:42 +00:00
|
|
|
func (pbs *proxyBlobStore) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) {
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
func (pbs *proxyBlobStore) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-12-15 22:35:23 +00:00
|
|
|
func (pbs *proxyBlobStore) Mount(ctx context.Context, sourceRepo reference.Named, dgst digest.Digest) (distribution.Descriptor, error) {
|
2015-12-15 02:34:18 +00:00
|
|
|
return distribution.Descriptor{}, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:15:53 +00:00
|
|
|
func (pbs *proxyBlobStore) Open(ctx context.Context, dgst digest.Digest) (io.ReadSeekCloser, error) {
|
2015-07-29 18:12:01 +00:00
|
|
|
return nil, distribution.ErrUnsupported
|
|
|
|
}
|
|
|
|
|
2015-09-18 23:11:35 +00:00
|
|
|
func (pbs *proxyBlobStore) Delete(ctx context.Context, dgst digest.Digest) error {
|
2015-07-29 18:12:01 +00:00
|
|
|
return distribution.ErrUnsupported
|
|
|
|
}
|