...
1 package proxy
2
3 import (
4 "context"
5 "time"
6
7 "github.com/distribution/reference"
8 "github.com/docker/distribution"
9 dcontext "github.com/docker/distribution/context"
10 "github.com/docker/distribution/registry/proxy/scheduler"
11 "github.com/opencontainers/go-digest"
12 )
13
14
15 const repositoryTTL = 24 * 7 * time.Hour
16
17 type proxyManifestStore struct {
18 ctx context.Context
19 localManifests distribution.ManifestService
20 remoteManifests distribution.ManifestService
21 repositoryName reference.Named
22 scheduler *scheduler.TTLExpirationScheduler
23 authChallenger authChallenger
24 }
25
26 var _ distribution.ManifestService = &proxyManifestStore{}
27
28 func (pms proxyManifestStore) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
29 exists, err := pms.localManifests.Exists(ctx, dgst)
30 if err != nil {
31 return false, err
32 }
33 if exists {
34 return true, nil
35 }
36 if err := pms.authChallenger.tryEstablishChallenges(ctx); err != nil {
37 return false, err
38 }
39 return pms.remoteManifests.Exists(ctx, dgst)
40 }
41
42 func (pms proxyManifestStore) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
43
44
45 var fromRemote bool
46 manifest, err := pms.localManifests.Get(ctx, dgst, options...)
47 if err != nil {
48 if err := pms.authChallenger.tryEstablishChallenges(ctx); err != nil {
49 return nil, err
50 }
51
52 manifest, err = pms.remoteManifests.Get(ctx, dgst, options...)
53 if err != nil {
54 return nil, err
55 }
56 fromRemote = true
57 }
58
59 _, payload, err := manifest.Payload()
60 if err != nil {
61 return nil, err
62 }
63
64 proxyMetrics.ManifestPush(uint64(len(payload)))
65 if fromRemote {
66 proxyMetrics.ManifestPull(uint64(len(payload)))
67
68 _, err = pms.localManifests.Put(ctx, manifest)
69 if err != nil {
70 return nil, err
71 }
72
73
74 repoBlob, err := reference.WithDigest(pms.repositoryName, dgst)
75 if err != nil {
76 dcontext.GetLogger(ctx).Errorf("Error creating reference: %s", err)
77 return nil, err
78 }
79
80 pms.scheduler.AddManifest(repoBlob, repositoryTTL)
81
82
83
84 }
85
86 return manifest, err
87 }
88
89 func (pms proxyManifestStore) Put(ctx context.Context, manifest distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
90 var d digest.Digest
91 return d, distribution.ErrUnsupported
92 }
93
94 func (pms proxyManifestStore) Delete(ctx context.Context, dgst digest.Digest) error {
95 return distribution.ErrUnsupported
96 }
97
View as plain text