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
|
|
|
"time"
|
|
|
|
|
2020-08-28 00:07:35 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
|
|
|
|
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 proxyManifestStore struct {
|
|
|
|
ctx context.Context
|
|
|
|
localManifests distribution.ManifestService
|
|
|
|
remoteManifests distribution.ManifestService
|
2015-12-15 22:35:23 +00:00
|
|
|
repositoryName reference.Named
|
2015-07-29 18:12:01 +00:00
|
|
|
scheduler *scheduler.TTLExpirationScheduler
|
2020-08-28 00:07:35 +00:00
|
|
|
ttl *time.Duration
|
2016-02-11 02:07:28 +00:00
|
|
|
authChallenger authChallenger
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ distribution.ManifestService = &proxyManifestStore{}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (pms proxyManifestStore) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
|
|
|
|
exists, err := pms.localManifests.Exists(ctx, dgst)
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return true, nil
|
|
|
|
}
|
2016-02-11 02:07:28 +00:00
|
|
|
if err := pms.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-08-21 04:50:15 +00:00
|
|
|
return pms.remoteManifests.Exists(ctx, dgst)
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (pms proxyManifestStore) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
|
|
|
|
// At this point `dgst` was either specified explicitly, or returned by the
|
|
|
|
// tagstore with the most recent association.
|
|
|
|
var fromRemote bool
|
|
|
|
manifest, err := pms.localManifests.Get(ctx, dgst, options...)
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
2016-02-11 02:07:28 +00:00
|
|
|
if err := pms.authChallenger.tryEstablishChallenges(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
manifest, err = pms.remoteManifests.Get(ctx, dgst, options...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fromRemote = true
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
_, payload, err := manifest.Payload()
|
2015-07-29 18:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-05 13:47:54 +00:00
|
|
|
proxyMetrics.ManifestPush(uint64(len(payload)), !fromRemote)
|
2015-08-21 04:50:15 +00:00
|
|
|
if fromRemote {
|
|
|
|
proxyMetrics.ManifestPull(uint64(len(payload)))
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
_, err = pms.localManifests.Put(ctx, manifest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2016-01-27 00:42:10 +00:00
|
|
|
// Schedule the manifest blob for removal
|
|
|
|
repoBlob, err := reference.WithDigest(pms.repositoryName, dgst)
|
|
|
|
if err != nil {
|
2017-08-11 22:31:16 +00:00
|
|
|
dcontext.GetLogger(ctx).Errorf("Error creating reference: %s", err)
|
2016-01-27 00:42:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-29 18:12:01 +00:00
|
|
|
|
2020-08-28 00:07:35 +00:00
|
|
|
if pms.scheduler != nil && pms.ttl != nil {
|
2023-11-18 06:50:40 +00:00
|
|
|
if err := pms.scheduler.AddManifest(repoBlob, *pms.ttl); err != nil {
|
|
|
|
dcontext.GetLogger(ctx).Errorf("Error adding manifest: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-28 00:07:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
// Ensure the manifest blob is cleaned up
|
2022-11-02 21:05:45 +00:00
|
|
|
// pms.scheduler.AddBlob(blobRef, repositoryTTL)
|
2016-01-27 00:42:10 +00:00
|
|
|
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
return manifest, err
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (pms proxyManifestStore) Put(ctx context.Context, manifest distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
|
|
|
|
var d digest.Digest
|
|
|
|
return d, distribution.ErrUnsupported
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|
|
|
|
|
2015-08-21 04:50:15 +00:00
|
|
|
func (pms proxyManifestStore) Delete(ctx context.Context, dgst digest.Digest) error {
|
2015-08-11 18:00:30 +00:00
|
|
|
return distribution.ErrUnsupported
|
2015-07-29 18:12:01 +00:00
|
|
|
}
|