-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathimagestream.go
172 lines (146 loc) · 6.38 KB
/
imagestream.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
package server
import (
"fmt"
"github.com/docker/distribution/context"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/distribution/registry/api/errcode"
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"
"github.com/openshift/image-registry/pkg/dockerregistry/server/cache"
"github.com/openshift/image-registry/pkg/dockerregistry/server/client"
)
type imageStream struct {
namespace string
name string
registryOSClient client.Interface
// cachedImages contains images cached for the lifetime of the request being handled.
cachedImages map[digest.Digest]*imageapiv1.Image
// imageStreamGetter fetches and caches an image stream. The image stream stays cached for the entire time of handling single repository-scoped request.
imageStreamGetter *cachedImageStreamGetter
// cache is used to associate a digest with a repository name.
cache cache.RepositoryDigest
}
func (is *imageStream) Reference() string {
return fmt.Sprintf("%s/%s", is.namespace, is.name)
}
// createImageStream creates a new image stream and caches it.
func (is *imageStream) createImageStream(ctx context.Context) (*imageapiv1.ImageStream, error) {
stream := &imageapiv1.ImageStream{}
stream.Name = is.name
uclient, ok := userClientFrom(ctx)
if !ok {
errmsg := "error creating user client to auto provision image stream: user client to master API unavailable"
context.GetLogger(ctx).Errorf(errmsg)
return nil, errcode.ErrorCodeUnknown.WithDetail(errmsg)
}
stream, err := uclient.ImageStreams(is.namespace).Create(stream)
switch {
case kerrors.IsAlreadyExists(err), kerrors.IsConflict(err):
context.GetLogger(ctx).Infof("conflict while creating ImageStream: %v", err)
return is.imageStreamGetter.get()
case kerrors.IsForbidden(err), kerrors.IsUnauthorized(err), quotautil.IsErrorQuotaExceeded(err):
context.GetLogger(ctx).Errorf("denied creating ImageStream: %v", err)
return nil, errcode.ErrorCodeDenied.WithDetail(err)
case err != nil:
context.GetLogger(ctx).Errorf("error auto provisioning ImageStream: %s", err)
return nil, errcode.ErrorCodeUnknown.WithDetail(err)
}
is.imageStreamGetter.cacheImageStream(stream)
return stream, nil
}
// getImage retrieves the Image with digest `dgst`. No authorization check is done.
func (is *imageStream) getImage(ctx context.Context, dgst digest.Digest) (*imageapiv1.Image, error) {
if image, exists := is.cachedImages[dgst]; exists {
context.GetLogger(ctx).Infof("(*imageStream).getImage: returning cached copy of %s", image.Name)
return image, nil
}
image, err := is.registryOSClient.Images().Get(dgst.String(), metav1.GetOptions{})
if err != nil {
context.GetLogger(ctx).Errorf("failed to get image: %v", err)
return nil, wrapKStatusErrorOnGetImage(is.name, dgst, err)
}
context.GetLogger(ctx).Infof("(*imageStream).getImage: got image %s", image.Name)
if err := imageapiv1.ImageWithMetadata(image); err != nil {
return nil, err
}
is.cachedImages[dgst] = image
return image, nil
}
// getStoredImageOfImageStream retrieves the Image with digest `dgst` and
// ensures that the image belongs to the image stream `is`. It uses two
// queries to master API:
//
// 1st to get a corresponding image stream
// 2nd to get the image
//
// This allows us to cache the image stream for later use.
//
// If you need the image object to be modified according to image stream tag,
// please use getImageOfImageStream.
func (is *imageStream) getStoredImageOfImageStream(ctx context.Context, dgst digest.Digest) (*imageapiv1.Image, *imageapiv1.TagEvent, *imageapiv1.ImageStream, error) {
stream, err := is.imageStreamGetter.get()
if err != nil {
context.GetLogger(ctx).Errorf("failed to get ImageStream: %v", err)
return nil, nil, nil, wrapKStatusErrorOnGetImage(is.name, dgst, err)
}
tagEvent, err := imageapiv1.ResolveImageID(stream, dgst.String())
if err != nil {
context.GetLogger(ctx).Errorf("failed to resolve image %s in ImageStream %s: %v", dgst.String(), is.Reference(), err)
return nil, nil, nil, wrapKStatusErrorOnGetImage(is.name, dgst, err)
}
image, err := is.getImage(ctx, dgst)
if err != nil {
return nil, nil, nil, wrapKStatusErrorOnGetImage(is.name, dgst, err)
}
return image, tagEvent, stream, nil
}
// getImageOfImageStream retrieves the Image with digest `dgst` for the image
// stream. The image's field DockerImageReference is modified on the fly to
// pretend that we've got the image from the source from which the image was
// tagged to match tag's DockerImageReference.
//
// NOTE: due to on the fly modification, the returned image object should
// not be sent to the master API. If you need unmodified version of the
// image object, please use getStoredImageOfImageStream.
func (is *imageStream) getImageOfImageStream(ctx context.Context, dgst digest.Digest) (*imageapiv1.Image, *imageapiv1.ImageStream, error) {
image, tagEvent, stream, err := is.getStoredImageOfImageStream(ctx, dgst)
if err != nil {
return nil, nil, err
}
image.DockerImageReference = tagEvent.DockerImageReference
return image, stream, nil
}
// updateImage modifies the Image.
func (is *imageStream) updateImage(image *imageapiv1.Image) (*imageapiv1.Image, error) {
return is.registryOSClient.Images().Update(image)
}
// rememberLayersOfImage caches the layer digests of given image
func (is *imageStream) rememberLayersOfImage(ctx context.Context, image *imageapiv1.Image, cacheName string) {
if len(image.DockerImageLayers) > 0 {
for _, layer := range image.DockerImageLayers {
_ = is.cache.AddDigest(digest.Digest(layer.Name), cacheName)
}
meta, ok := image.DockerImageMetadata.Object.(*imageapi.DockerImage)
if !ok {
context.GetLogger(ctx).Errorf("image %s does not have metadata", image.Name)
return
}
// remember reference to manifest config as well for schema 2
if image.DockerImageManifestMediaType == schema2.MediaTypeManifest && len(meta.ID) > 0 {
_ = is.cache.AddDigest(digest.Digest(meta.ID), cacheName)
}
return
}
manifest, err := NewManifestFromImage(image)
if err != nil {
context.GetLogger(ctx).Errorf("cannot remember layers of image %s: %v", image.Name, err)
return
}
for _, ref := range manifest.References() {
_ = is.cache.AddDigest(ref.Digest, cacheName)
}
}