forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorblobstore.go
185 lines (155 loc) · 5.53 KB
/
errorblobstore.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
package server
import (
"fmt"
"net/http"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/reference"
)
// errorBlobStore wraps a distribution.BlobStore for a particular repo.
// before delegating, it ensures auth completed and there were no errors relevant to the repo.
type errorBlobStore struct {
store distribution.BlobStore
repo *repository
}
var _ distribution.BlobStore = &errorBlobStore{}
func (r *errorBlobStore) Stat(ctx context.Context, dgst digest.Digest) (distribution.Descriptor, error) {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return distribution.Descriptor{}, err
}
return r.store.Stat(withRepository(ctx, r.repo), dgst)
}
func (r *errorBlobStore) Get(ctx context.Context, dgst digest.Digest) ([]byte, error) {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return nil, err
}
return r.store.Get(withRepository(ctx, r.repo), dgst)
}
func (r *errorBlobStore) Open(ctx context.Context, dgst digest.Digest) (distribution.ReadSeekCloser, error) {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return nil, err
}
return r.store.Open(withRepository(ctx, r.repo), dgst)
}
func (r *errorBlobStore) Put(ctx context.Context, mediaType string, p []byte) (distribution.Descriptor, error) {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return distribution.Descriptor{}, err
}
return r.store.Put(withRepository(ctx, r.repo), mediaType, p)
}
func (r *errorBlobStore) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return nil, err
}
ctx = withRepository(ctx, r.repo)
opts, err := effectiveCreateOptions(options)
if err != nil {
return nil, err
}
err = checkPendingCrossMountErrors(ctx, opts)
if err != nil {
context.GetLogger(ctx).Infof("disabling cross-repo mount because of an error: %v", err)
options = append(options, guardCreateOptions{DisableCrossMount: true})
} else if !opts.Mount.ShouldMount {
options = append(options, guardCreateOptions{DisableCrossMount: true})
} else {
context.GetLogger(ctx).Debugf("attempting cross-repo mount")
options = append(options, statCrossMountCreateOptions{
ctx: ctx,
destRepo: r.repo,
})
}
return r.store.Create(ctx, options...)
}
func (r *errorBlobStore) Resume(ctx context.Context, id string) (distribution.BlobWriter, error) {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return nil, err
}
return r.store.Resume(withRepository(ctx, r.repo), id)
}
func (r *errorBlobStore) ServeBlob(ctx context.Context, w http.ResponseWriter, req *http.Request, dgst digest.Digest) error {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return err
}
return r.store.ServeBlob(withRepository(ctx, r.repo), w, req, dgst)
}
func (r *errorBlobStore) Delete(ctx context.Context, dgst digest.Digest) error {
if err := r.repo.checkPendingErrors(ctx); err != nil {
return err
}
return r.store.Delete(withRepository(ctx, r.repo), dgst)
}
// checkPendingCrossMountErrors returns true if a cross-repo mount has been requested with given create
// options. If requested and there are pending authorization errors for source repository, the error will be
// returned. Cross-repo mount must not be allowed in case of error.
func checkPendingCrossMountErrors(ctx context.Context, opts *distribution.CreateOptions) error {
if !opts.Mount.ShouldMount {
return nil
}
namespace, name, err := getNamespaceName(opts.Mount.From.Name())
if err != nil {
return err
}
return checkPendingErrors(ctx, context.GetLogger(ctx), namespace, name)
}
// guardCreateOptions ensures the expected options type is passed, and optionally disables cross mounting
type guardCreateOptions struct {
DisableCrossMount bool
}
var _ distribution.BlobCreateOption = guardCreateOptions{}
func (f guardCreateOptions) Apply(v interface{}) error {
opts, ok := v.(*distribution.CreateOptions)
if !ok {
return fmt.Errorf("Unexpected create options: %#v", v)
}
if f.DisableCrossMount {
opts.Mount.ShouldMount = false
}
return nil
}
// statCrossMountCreateOptions ensures the expected options type is passed, and optionally pre-fills the cross-mount stat info
type statCrossMountCreateOptions struct {
ctx context.Context
destRepo *repository
}
var _ distribution.BlobCreateOption = statCrossMountCreateOptions{}
func (f statCrossMountCreateOptions) Apply(v interface{}) error {
opts, ok := v.(*distribution.CreateOptions)
if !ok {
return fmt.Errorf("Unexpected create options: %#v", v)
}
if !opts.Mount.ShouldMount {
return nil
}
desc, err := statSourceRepository(f.ctx, f.destRepo, opts.Mount.From, opts.Mount.From.Digest())
if err != nil {
context.GetLogger(f.ctx).Infof("cannot mount blob %s from repository %s: %v - disabling cross-repo mount",
opts.Mount.From.Digest().String(),
opts.Mount.From.Name())
opts.Mount.ShouldMount = false
return nil
}
opts.Mount.Stat = &desc
return nil
}
func statSourceRepository(
ctx context.Context,
destRepo *repository,
sourceRepoName reference.Named,
dgst digest.Digest,
) (desc distribution.Descriptor, err error) {
upstreamRepo, err := dockerRegistry.Repository(ctx, sourceRepoName)
if err != nil {
return distribution.Descriptor{}, err
}
namespace, name, err := getNamespaceName(sourceRepoName.Name())
if err != nil {
return distribution.Descriptor{}, err
}
repo := *destRepo
repo.namespace = namespace
repo.name = name
repo.Repository = upstreamRepo
return repo.Blobs(ctx).Stat(ctx, dgst)
}