-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathrepository.go
239 lines (190 loc) · 6.44 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package server
import (
"fmt"
"net/http"
"strings"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
registrystorage "github.com/docker/distribution/registry/storage"
restclient "k8s.io/client-go/rest"
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
"github.com/openshift/image-registry/pkg/dockerregistry/server/audit"
"github.com/openshift/image-registry/pkg/dockerregistry/server/cache"
"github.com/openshift/image-registry/pkg/dockerregistry/server/metrics"
)
var (
// secureTransport is the transport pool used for pullthrough to remote registries marked as
// secure.
secureTransport http.RoundTripper
// insecureTransport is the transport pool that does not verify remote TLS certificates for use
// during pullthrough against registries marked as insecure.
insecureTransport http.RoundTripper
)
func init() {
secureTransport = http.DefaultTransport
var err error
insecureTransport, err = restclient.TransportFor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}})
if err != nil {
panic(fmt.Sprintf("Unable to configure a default transport for importing insecure images: %v", err))
}
}
// repository wraps a distribution.Repository and allows manifests to be served from the OpenShift image
// API.
type repository struct {
distribution.Repository
ctx context.Context
app *App
crossmount bool
imageStream *imageStream
// remoteBlobGetter is used to fetch blobs from remote registries if pullthrough is enabled.
remoteBlobGetter BlobGetterService
}
// Repository returns a new repository middleware.
func (app *App) Repository(ctx context.Context, repo distribution.Repository, crossmount bool) (distribution.Repository, distribution.BlobDescriptorServiceFactory, error) {
registryOSClient, err := app.registryClient.Client()
if err != nil {
return nil, nil, err
}
context.GetLogger(ctx).Infof("Using %q as Docker Registry URL", app.config.Server.Addr)
nameParts := strings.SplitN(repo.Named().Name(), "/", 2)
if len(nameParts) != 2 {
return nil, nil, fmt.Errorf("invalid repository name %q: it must be of the format <project>/<name>", repo.Named().Name())
}
namespace, name := nameParts[0], nameParts[1]
imageStreamGetter := &cachedImageStreamGetter{
ctx: ctx,
namespace: namespace,
name: name,
isNamespacer: registryOSClient,
}
r := &repository{
Repository: repo,
ctx: ctx,
app: app,
crossmount: crossmount,
imageStream: &imageStream{
namespace: nameParts[0],
name: nameParts[1],
registryOSClient: registryOSClient,
cachedImages: make(map[digest.Digest]*imageapiv1.Image),
imageStreamGetter: imageStreamGetter,
cache: &cache.RepoDigest{
Cache: app.cache,
},
},
}
if app.config.Pullthrough.Enabled {
r.remoteBlobGetter = NewBlobGetterService(
r.imageStream.namespace,
r.imageStream.name,
imageStreamGetter.get,
registryOSClient,
r.imageStream.cache)
}
bdsf := blobDescriptorServiceFactoryFunc(r.BlobDescriptorService)
return r, bdsf, nil
}
// Manifests returns r, which implements distribution.ManifestService.
func (r *repository) Manifests(ctx context.Context, options ...distribution.ManifestServiceOption) (distribution.ManifestService, error) {
// we do a verification of our own
// TODO: let upstream do the verification once they pass correct context object to their manifest handler
opts := append(options, registrystorage.SkipLayerVerification())
ms, err := r.Repository.Manifests(ctx, opts...)
if err != nil {
return nil, err
}
ms = &manifestService{
manifests: ms,
blobStore: r.Blobs(ctx),
serverAddr: r.app.config.Server.Addr,
imageStream: r.imageStream,
acceptSchema2: r.app.config.Compatibility.AcceptSchema2,
}
if r.app.config.Pullthrough.Enabled {
ms = &pullthroughManifestService{
ManifestService: ms,
imageStream: r.imageStream,
}
}
ms = newPendingErrorsManifestService(ms, r)
if audit.LoggerExists(ctx) {
ms = audit.NewManifestService(ctx, ms)
}
if r.app.config.Metrics.Enabled {
ms = metrics.NewManifestService(ms, r.Named().Name())
}
return ms, nil
}
// Blobs returns a blob store which can delegate to remote repositories.
func (r *repository) Blobs(ctx context.Context) distribution.BlobStore {
bs := r.Repository.Blobs(ctx)
if r.app.quotaEnforcing.enforcementEnabled {
bs = "aRestrictedBlobStore{
BlobStore: bs,
repo: r,
}
}
if r.app.config.Pullthrough.Enabled {
bs = &pullthroughBlobStore{
BlobStore: bs,
imageStream: r.imageStream,
remoteBlobGetter: r.remoteBlobGetter,
writeLimiter: r.app.writeLimiter,
mirror: r.app.config.Pullthrough.Mirror,
}
}
bs = newPendingErrorsBlobStore(bs, r)
if audit.LoggerExists(ctx) {
bs = audit.NewBlobStore(ctx, bs)
}
if r.app.config.Metrics.Enabled {
bs = metrics.NewBlobStore(bs, r.Named().Name())
}
return bs
}
// Tags returns a reference to this repository tag service.
func (r *repository) Tags(ctx context.Context) distribution.TagService {
ts := r.Repository.Tags(ctx)
ts = &tagService{
TagService: ts,
imageStream: r.imageStream,
pullthroughEnabled: r.app.config.Pullthrough.Enabled,
}
ts = newPendingErrorsTagService(ts, r)
if audit.LoggerExists(ctx) {
ts = audit.NewTagService(ctx, ts)
}
if r.app.config.Metrics.Enabled {
ts = metrics.NewTagService(ts, r.Named().Name())
}
return ts
}
func (r *repository) BlobDescriptorService(svc distribution.BlobDescriptorService) distribution.BlobDescriptorService {
svc = &cache.RepositoryScopedBlobDescriptor{
Repo: r.Named().String(),
Cache: r.app.cache,
Svc: svc,
}
svc = &blobDescriptorService{svc, r}
svc = newPendingErrorsBlobDescriptorService(svc, r)
return svc
}
func (r *repository) checkPendingErrors(ctx context.Context) error {
return checkPendingErrors(ctx, context.GetLogger(r.ctx), r.imageStream.namespace, r.imageStream.name)
}
func checkPendingErrors(ctx context.Context, logger context.Logger, namespace, name string) error {
if !authPerformed(ctx) {
return fmt.Errorf("openshift.auth.completed missing from context")
}
deferredErrors, haveDeferredErrors := deferredErrorsFrom(ctx)
if !haveDeferredErrors {
return nil
}
repoErr, haveRepoErr := deferredErrors.Get(namespace, name)
if !haveRepoErr {
return nil
}
logger.Debugf("Origin auth: found deferred error for %s/%s: %v", namespace, name, repoErr)
return repoErr
}