This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
repositorymiddleware.go
286 lines (243 loc) · 8.09 KB
/
repositorymiddleware.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
284
285
286
package server
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
log "github.com/Sirupsen/logrus"
"github.com/docker/distribution"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest"
repomw "github.com/docker/distribution/registry/middleware/repository"
"github.com/docker/libtrust"
"github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
"golang.org/x/net/context"
)
func init() {
repomw.Register("openshift", repomw.InitFunc(newRepository))
}
type repository struct {
distribution.Repository
registryClient *client.Client
registryAddr string
namespace string
name string
}
// newRepository returns a new repository middleware.
func newRepository(repo distribution.Repository, options map[string]interface{}) (distribution.Repository, error) {
registryAddr := os.Getenv("REGISTRY_URL")
if len(registryAddr) == 0 {
return nil, errors.New("REGISTRY_URL is required")
}
registryClient, err := NewRegistryOpenShiftClient()
if err != nil {
return nil, err
}
nameParts := strings.SplitN(repo.Name(), "/", 2)
if len(nameParts) != 2 {
return nil, fmt.Errorf("invalid repository name %q: it must be of the format <project>/<name>", repo.Name())
}
return &repository{
Repository: repo,
registryClient: registryClient,
registryAddr: registryAddr,
namespace: nameParts[0],
name: nameParts[1],
}, nil
}
// Manifests returns r, which implements distribution.ManifestService.
func (r *repository) Manifests() distribution.ManifestService {
return r
}
// Tags lists the tags under the named repository.
func (r *repository) Tags(ctx context.Context) ([]string, error) {
imageStream, err := r.getImageStream(ctx)
if err != nil {
return []string{}, nil
}
tags := []string{}
for tag := range imageStream.Status.Tags {
tags = append(tags, tag)
}
return tags, nil
}
// Exists returns true if the manifest specified by dgst exists.
func (r *repository) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
image, err := r.getImage(dgst)
if err != nil {
return false, err
}
return image != nil, nil
}
// ExistsByTag returns true if the manifest with tag `tag` exists.
func (r *repository) ExistsByTag(ctx context.Context, tag string) (bool, error) {
imageStream, err := r.getImageStream(ctx)
if err != nil {
return false, err
}
_, found := imageStream.Status.Tags[tag]
return found, nil
}
// Get retrieves the manifest with digest `dgst`.
func (r *repository) Get(ctx context.Context, dgst digest.Digest) (*manifest.SignedManifest, error) {
if _, err := r.getImageStreamImage(ctx, dgst); err != nil {
log.Errorf("Error retrieving ImageStreamImage %s/%s@%s: %v", r.namespace, r.name, dgst.String(), err)
return nil, err
}
image, err := r.getImage(dgst)
if err != nil {
log.Errorf("Error retrieving image %s: %v", dgst.String(), err)
return nil, err
}
return r.manifestFromImage(image)
}
// GetByTag retrieves the named manifest with the provided tag
func (r *repository) GetByTag(ctx context.Context, tag string) (*manifest.SignedManifest, error) {
imageStreamTag, err := r.getImageStreamTag(ctx, tag)
if err != nil {
log.Errorf("Error getting ImageStreamTag %q: %v", tag, err)
return nil, err
}
image := &imageStreamTag.Image
dgst, err := digest.ParseDigest(imageStreamTag.Image.Name)
if err != nil {
log.Errorf("Error parsing digest %q: %v", imageStreamTag.Image.Name, err)
return nil, err
}
image, err = r.getImage(dgst)
if err != nil {
log.Errorf("Error getting image %q: %v", dgst.String(), err)
return nil, err
}
return r.manifestFromImage(image)
}
// Put creates or updates the named manifest.
func (r *repository) Put(ctx context.Context, manifest *manifest.SignedManifest) error {
// Resolve the payload in the manifest.
payload, err := manifest.Payload()
if err != nil {
return err
}
// Calculate digest
dgst, err := digest.FromBytes(payload)
if err != nil {
return err
}
// Upload to openshift
ism := imageapi.ImageStreamMapping{
ObjectMeta: kapi.ObjectMeta{
Namespace: r.namespace,
Name: r.name,
},
Tag: manifest.Tag,
Image: imageapi.Image{
ObjectMeta: kapi.ObjectMeta{
Name: dgst.String(),
Annotations: map[string]string{
imageapi.ManagedByOpenShiftAnnotation: "true",
},
},
DockerImageReference: fmt.Sprintf("%s/%s/%s@%s", r.registryAddr, r.namespace, r.name, dgst.String()),
DockerImageManifest: string(payload),
},
}
if err := r.registryClient.ImageStreamMappings(r.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 {
log.Errorf("Error creating ImageStreamMapping: %s", err)
return err
}
status := statusErr.ErrStatus
if status.Code != http.StatusNotFound || status.Details.Kind != "imageStream" || status.Details.Name != r.name {
log.Errorf("Error creating ImageStreamMapping: %s", err)
return err
}
stream := imageapi.ImageStream{
ObjectMeta: kapi.ObjectMeta{
Name: r.name,
},
}
client, ok := UserClientFrom(ctx)
if !ok {
log.Errorf("Error creating user client to auto provision ImageStream: OpenShift user client unavailable")
return statusErr
}
if _, err := client.ImageStreams(r.namespace).Create(&stream); err != nil {
log.Errorf("Error auto provisioning ImageStream: %s", err)
return statusErr
}
// try to create the ISM again
if err := r.registryClient.ImageStreamMappings(r.namespace).Create(&ism); err != nil {
log.Errorf("Error creating ImageStreamMapping: %s", err)
return err
}
}
// Grab each json signature and store them.
signatures, err := manifest.Signatures()
if err != nil {
return err
}
for _, signature := range signatures {
if err := r.Signatures().Put(dgst, signature); err != nil {
log.Errorf("Error storing signature: %s", err)
return err
}
}
return nil
}
// Delete deletes the manifest with digest `dgst`. Note: Image resources
// in OpenShift are deleted via 'oadm prune images'. This function deletes
// the content related to the manifest in the registry's storage (signatures).
func (r *repository) Delete(ctx context.Context, dgst digest.Digest) error {
return r.Repository.Manifests().Delete(ctx, dgst)
}
// getImageStream retrieves the ImageStream for r.
func (r *repository) getImageStream(ctx context.Context) (*imageapi.ImageStream, error) {
return r.registryClient.ImageStreams(r.namespace).Get(r.name)
}
// getImage retrieves the Image with digest `dgst`.
func (r *repository) getImage(dgst digest.Digest) (*imageapi.Image, error) {
return r.registryClient.Images().Get(dgst.String())
}
// getImageStreamTag retrieves the Image with tag `tag` for the ImageStream
// associated with r.
func (r *repository) getImageStreamTag(ctx context.Context, tag string) (*imageapi.ImageStreamTag, error) {
return r.registryClient.ImageStreamTags(r.namespace).Get(r.name, tag)
}
// getImageStreamImage retrieves the Image with digest `dgst` for the ImageStream
// associated with r. This ensures the image belongs to the image stream.
func (r *repository) getImageStreamImage(ctx context.Context, dgst digest.Digest) (*imageapi.ImageStreamImage, error) {
return r.registryClient.ImageStreamImages(r.namespace).Get(r.name, dgst.String())
}
// manifestFromImage converts an Image to a SignedManifest.
func (r *repository) manifestFromImage(image *imageapi.Image) (*manifest.SignedManifest, error) {
dgst, err := digest.ParseDigest(image.Name)
if err != nil {
return nil, err
}
// Fetch the signatures for the manifest
signatures, err := r.Signatures().Get(dgst)
if err != nil {
return nil, err
}
jsig, err := libtrust.NewJSONSignature([]byte(image.DockerImageManifest), signatures...)
if err != nil {
return nil, err
}
// Extract the pretty JWS
raw, err := jsig.PrettySignature("signatures")
if err != nil {
return nil, err
}
var sm manifest.SignedManifest
if err := json.Unmarshal(raw, &sm); err != nil {
return nil, err
}
return &sm, err
}