-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmanifestservice.go
283 lines (238 loc) · 9.18 KB
/
manifestservice.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package server
import (
"fmt"
"net/http"
"strings"
"sync"
"github.com/docker/distribution"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/registry/api/errcode"
regapi "github.com/docker/distribution/registry/api/v2"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
quotautil "github.com/openshift/origin/pkg/quota/util"
)
// ErrManifestBlobBadSize is returned when the blob size in a manifest does
// not match the actual size. The docker/distribution does not check this and
// therefore does not provide an error for this.
type ErrManifestBlobBadSize struct {
Digest digest.Digest
ActualSize int64
SizeInManifest int64
}
func (err ErrManifestBlobBadSize) Error() string {
return fmt.Sprintf("the blob %s has the size (%d) different from the one specified in the manifest (%d)",
err.Digest, err.ActualSize, err.SizeInManifest)
}
var _ distribution.ManifestService = &manifestService{}
type manifestService struct {
manifests distribution.ManifestService
blobStore distribution.BlobStore
serverAddr string
imageStream *imageStream
// acceptSchema2 allows to refuse the manifest schema version 2
acceptSchema2 bool
}
// Exists returns true if the manifest specified by dgst exists.
func (m *manifestService) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
context.GetLogger(ctx).Debugf("(*manifestService).Exists")
image, _, err := m.imageStream.getImageOfImageStream(ctx, dgst)
if err != nil {
return false, err
}
return image != nil, nil
}
// Get retrieves the manifest with digest `dgst`.
func (m *manifestService) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
context.GetLogger(ctx).Debugf("(*manifestService).Get")
image, _, _, err := m.imageStream.getStoredImageOfImageStream(ctx, dgst)
if err != nil {
return nil, err
}
ref := imageapi.DockerImageReference{
Registry: m.serverAddr,
Namespace: m.imageStream.namespace,
Name: m.imageStream.name,
}
if isImageManaged(image) {
// Reference without a registry part refers to repository containing locally managed images.
// Such an entry is retrieved, checked and set by blobDescriptorService operating only on local blobs.
ref.Registry = ""
} else {
// Repository with a registry points to remote repository. This is used by pullthrough middleware.
ref = ref.DockerClientDefaults().AsRepository()
}
manifest, err := m.manifests.Get(ctx, dgst, options...)
if err == nil {
m.imageStream.rememberLayersOfImage(ctx, image, ref.Exact())
m.migrateManifest(ctx, image, dgst, manifest, true)
return manifest, nil
} else if _, ok := err.(distribution.ErrManifestUnknownRevision); !ok {
context.GetLogger(ctx).Errorf("unable to get manifest from storage: %v", err)
return nil, err
}
manifest, err = NewManifestFromImage(image)
if err == nil {
m.imageStream.rememberLayersOfImage(ctx, image, ref.Exact())
m.migrateManifest(ctx, image, dgst, manifest, false)
return manifest, nil
} else {
context.GetLogger(ctx).Errorf("unable to get manifest from image object: %v", err)
}
return nil, distribution.ErrManifestUnknownRevision{
Name: m.imageStream.Reference(),
Revision: dgst,
}
}
// Put creates or updates the named manifest.
func (m *manifestService) Put(ctx context.Context, manifest distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
context.GetLogger(ctx).Debugf("(*manifestService).Put")
mh, err := NewManifestHandler(m.serverAddr, m.blobStore, manifest)
if err != nil {
return "", regapi.ErrorCodeManifestInvalid.WithDetail(err)
}
mediaType, payload, _, err := mh.Payload()
if err != nil {
return "", regapi.ErrorCodeManifestInvalid.WithDetail(err)
}
// this is fast to check, let's do it before verification
if !m.acceptSchema2 && mediaType == schema2.MediaTypeManifest {
return "", regapi.ErrorCodeManifestInvalid.WithDetail(fmt.Errorf("manifest V2 schema 2 not allowed"))
}
// in order to stat the referenced blobs, repository need to be set on the context
if err := mh.Verify(ctx, false); err != nil {
return "", err
}
_, err = m.manifests.Put(ctx, manifest, options...)
if err != nil {
return "", err
}
config, err := mh.Config(ctx)
if err != nil {
return "", err
}
dgst, err := mh.Digest()
if err != nil {
return "", err
}
layerOrder, layers, err := mh.Layers(ctx)
if err != nil {
return "", err
}
// Upload to openshift
ism := imageapiv1.ImageStreamMapping{
ObjectMeta: metav1.ObjectMeta{
Namespace: m.imageStream.namespace,
Name: m.imageStream.name,
},
Image: imageapiv1.Image{
ObjectMeta: metav1.ObjectMeta{
Name: dgst.String(),
Annotations: map[string]string{
imageapi.ManagedByOpenShiftAnnotation: "true",
imageapi.ImageManifestBlobStoredAnnotation: "true",
imageapi.DockerImageLayersOrderAnnotation: layerOrder,
},
},
DockerImageReference: fmt.Sprintf("%s/%s/%s@%s", m.serverAddr, m.imageStream.namespace, m.imageStream.name, dgst.String()),
DockerImageManifest: string(payload),
DockerImageManifestMediaType: mediaType,
DockerImageConfig: string(config),
DockerImageLayers: layers,
},
}
for _, option := range options {
if opt, ok := option.(distribution.WithTagOption); ok {
ism.Tag = opt.Tag
break
}
}
if _, err = m.imageStream.registryOSClient.ImageStreamMappings(m.imageStream.namespace).Create(&ism); err != nil {
// if the error was that the image stream wasn't found, try to auto provision it
statusErr, ok := err.(*kerrors.StatusError)
if !ok {
context.GetLogger(ctx).Errorf("error creating ImageStreamMapping: %s", err)
return "", err
}
if quotautil.IsErrorQuotaExceeded(statusErr) {
context.GetLogger(ctx).Errorf("denied creating ImageStreamMapping: %v", statusErr)
return "", distribution.ErrAccessDenied
}
status := statusErr.ErrStatus
kind := strings.ToLower(status.Details.Kind)
isValidKind := kind == "imagestream" /*pre-1.2*/ || kind == "imagestreams" /*1.2 to 1.6*/ || kind == "imagestreammappings" /*1.7+*/
if !isValidKind || status.Code != http.StatusNotFound || status.Details.Name != m.imageStream.name {
context.GetLogger(ctx).Errorf("error creating ImageStreamMapping: %s", err)
return "", err
}
if _, err := m.imageStream.createImageStream(ctx); err != nil {
if e, ok := err.(errcode.Error); ok && e.ErrorCode() == errcode.ErrorCodeUnknown {
// TODO: convert statusErr to distribution error
return "", statusErr
}
return "", err
}
// try to create the ISM again
if _, err := m.imageStream.registryOSClient.ImageStreamMappings(m.imageStream.namespace).Create(&ism); err != nil {
if quotautil.IsErrorQuotaExceeded(err) {
context.GetLogger(ctx).Errorf("denied a creation of ImageStreamMapping: %v", err)
return "", distribution.ErrAccessDenied
}
context.GetLogger(ctx).Errorf("error creating ImageStreamMapping: %s", err)
return "", err
}
}
return dgst, nil
}
// Delete deletes the manifest with digest `dgst`. Note: Image resources
// in OpenShift are deleted via 'oc adm prune images'. This function deletes
// the content related to the manifest in the registry's storage (signatures).
func (m *manifestService) Delete(ctx context.Context, dgst digest.Digest) error {
context.GetLogger(ctx).Debugf("(*manifestService).Delete")
return m.manifests.Delete(ctx, dgst)
}
// manifestInflight tracks currently downloading manifests
var manifestInflight = make(map[digest.Digest]struct{})
// manifestInflightSync protects manifestInflight
var manifestInflightSync sync.Mutex
func (m *manifestService) migrateManifest(ctx context.Context, image *imageapiv1.Image, dgst digest.Digest, manifest distribution.Manifest, isLocalStored bool) {
// Everything in its place and nothing to do.
if isLocalStored && len(image.DockerImageManifest) == 0 {
return
}
manifestInflightSync.Lock()
if _, ok := manifestInflight[dgst]; ok {
manifestInflightSync.Unlock()
return
}
manifestInflight[dgst] = struct{}{}
manifestInflightSync.Unlock()
go m.storeManifestLocally(ctx, image, dgst, manifest, isLocalStored)
}
func (m *manifestService) storeManifestLocally(ctx context.Context, image *imageapiv1.Image, dgst digest.Digest, manifest distribution.Manifest, isLocalStored bool) {
defer func() {
manifestInflightSync.Lock()
delete(manifestInflight, dgst)
manifestInflightSync.Unlock()
}()
if !isLocalStored {
if _, err := m.manifests.Put(ctx, manifest); err != nil {
context.GetLogger(ctx).Errorf("unable to put manifest to storage: %v", err)
return
}
}
if len(image.DockerImageManifest) == 0 || image.Annotations[imageapi.ImageManifestBlobStoredAnnotation] == "true" {
return
}
if image.Annotations == nil {
image.Annotations = make(map[string]string)
}
image.Annotations[imageapi.ImageManifestBlobStoredAnnotation] = "true"
if _, err := m.imageStream.updateImage(image); err != nil {
context.GetLogger(ctx).Errorf("error updating Image: %v", err)
}
}