forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strategy.go
402 lines (351 loc) · 12.9 KB
/
strategy.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package imagestream
import (
"fmt"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
"github.com/golang/glog"
authorizationapi "github.com/openshift/origin/pkg/authorization/api"
"github.com/openshift/origin/pkg/authorization/registry/subjectaccessreview"
"github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/image/api/validation"
)
type ResourceGetter interface {
Get(kapi.Context, string) (runtime.Object, error)
}
// Strategy implements behavior for ImageStreams.
type Strategy struct {
runtime.ObjectTyper
kapi.NameGenerator
defaultRegistry DefaultRegistry
tagVerifier *TagVerifier
ImageStreamGetter ResourceGetter
}
// Strategy is the default logic that applies when creating and updating
// ImageStream objects via the REST API.
func NewStrategy(defaultRegistry DefaultRegistry, subjectAccessReviewClient subjectaccessreview.Registry) Strategy {
return Strategy{
ObjectTyper: kapi.Scheme,
NameGenerator: kapi.SimpleNameGenerator,
defaultRegistry: defaultRegistry,
tagVerifier: &TagVerifier{subjectAccessReviewClient},
}
}
// NamespaceScoped is true for image streams.
func (s Strategy) NamespaceScoped() bool {
return true
}
// PrepareForCreate clears fields that are not allowed to be set by end users on creation,
// and verifies the current user is authorized to access any image streams newly referenced
// in spec.tags.
func (s Strategy) PrepareForCreate(obj runtime.Object) {
stream := obj.(*api.ImageStream)
stream.Status = api.ImageStreamStatus{
DockerImageRepository: s.dockerImageRepository(stream),
Tags: make(map[string]api.TagEventList),
}
}
// Validate validates a new image stream.
func (s Strategy) Validate(ctx kapi.Context, obj runtime.Object) fielderrors.ValidationErrorList {
stream := obj.(*api.ImageStream)
user, ok := kapi.UserFrom(ctx)
if !ok {
return fielderrors.ValidationErrorList{kerrors.NewForbidden("imageStream", stream.Name, fmt.Errorf("unable to update an ImageStream without a user on the context"))}
}
errs := s.tagVerifier.Verify(nil, stream, user.GetName())
errs = append(errs, s.tagsChanged(nil, stream)...)
errs = append(errs, validation.ValidateImageStream(stream)...)
return errs
}
// AllowCreateOnUpdate is false for image streams.
func (s Strategy) AllowCreateOnUpdate() bool {
return false
}
// dockerImageRepository determines the docker image stream for stream.
// If stream.DockerImageRepository is set, that value is returned. Otherwise,
// if a default registry exists, the value returned is of the form
// <default registry>/<namespace>/<stream name>.
func (s Strategy) dockerImageRepository(stream *api.ImageStream) string {
if len(stream.Spec.DockerImageRepository) != 0 {
return stream.Spec.DockerImageRepository
}
registry, ok := s.defaultRegistry.DefaultRegistry()
if !ok {
return ""
}
if len(stream.Namespace) == 0 {
stream.Namespace = kapi.NamespaceDefault
}
ref := api.DockerImageReference{
Registry: registry,
Namespace: stream.Namespace,
Name: stream.Name,
}
return ref.String()
}
func parseFromReference(stream *api.ImageStream, from *kapi.ObjectReference) (string, string, error) {
splitChar := ""
refType := ""
switch from.Kind {
case "ImageStreamTag":
splitChar = ":"
refType = "tag"
case "ImageStreamImage":
splitChar = "@"
refType = "id"
default:
return "", "", fmt.Errorf("invalid from.kind %q - only ImageStreamTag and ImageStreamImage are allowed", from.Kind)
}
parts := strings.Split(from.Name, splitChar)
switch len(parts) {
case 1:
// <tag> or <id>
return stream.Name, from.Name, nil
case 2:
// <stream>:<tag> or <stream>@<id>
return parts[0], parts[1], nil
default:
return "", "", fmt.Errorf("invalid from.name %q - it must be of the form <%s> or <stream>%s<%s>", from.Name, refType, splitChar, refType)
}
}
// tagsChanged updates stream.Status.Tags based on the old and new image stream.
// if the old stream is nil, all tags are considered additions.
func (s Strategy) tagsChanged(old, stream *api.ImageStream) fielderrors.ValidationErrorList {
var errs fielderrors.ValidationErrorList
oldTags := map[string]api.TagReference{}
if old != nil && old.Spec.Tags != nil {
oldTags = old.Spec.Tags
}
for tag, tagRef := range stream.Spec.Tags {
if oldRef, ok := oldTags[tag]; ok && !tagRefChanged(oldRef, tagRef, stream.Namespace) {
continue
}
if len(tagRef.DockerImageReference) > 0 {
event, err := tagReferenceToTagEvent(stream, tagRef, "")
if err != nil {
errs = append(errs, fielderrors.NewFieldInvalid(fmt.Sprintf("spec.tags[%s].dockerImageReference", tag), tagRef.DockerImageReference, err.Error()))
continue
}
api.AddTagEventToImageStream(stream, tag, *event)
continue
}
if tagRef.From == nil {
continue
}
tagRefStreamName, tagOrID, err := parseFromReference(stream, tagRef.From)
if err != nil {
errs = append(errs, fielderrors.NewFieldInvalid(fmt.Sprintf("spec.tags[%s].from.name", tag), tagRef.From.Name, "must be of the form <tag>, <repo>:<tag>, <id>, or <repo>@<id>"))
continue
}
streamRef := stream
streamRefNamespace := tagRef.From.Namespace
if len(streamRefNamespace) == 0 {
streamRefNamespace = stream.Namespace
}
if streamRefNamespace != stream.Namespace || tagRefStreamName != stream.Name {
obj, err := s.ImageStreamGetter.Get(kapi.WithNamespace(kapi.NewContext(), streamRefNamespace), tagRefStreamName)
if err != nil {
errs = append(errs, fielderrors.NewFieldInvalid(fmt.Sprintf("spec.tags[%s].from.name", tag), tagRef.From.Name, fmt.Sprintf("error retrieving ImageStream %s/%s: %v", streamRefNamespace, tagRefStreamName, err)))
continue
}
streamRef = obj.(*api.ImageStream)
}
event, err := tagReferenceToTagEvent(streamRef, tagRef, tagOrID)
if err != nil {
errs = append(errs, fielderrors.NewFieldInvalid(fmt.Sprintf("spec.tags[%s].from.name", tag), tagRef.From.Name, fmt.Sprintf("error generating tag event: %v", err)))
continue
}
if event == nil {
glog.Errorf("unable to find tag event for %#v", tagRef.From)
continue
}
api.AddTagEventToImageStream(stream, tag, *event)
}
// use a consistent timestamp on creation
if old == nil && !stream.CreationTimestamp.IsZero() {
for tag, list := range stream.Status.Tags {
for _, event := range list.Items {
event.Created = stream.CreationTimestamp
}
stream.Status.Tags[tag] = list
}
}
return errs
}
func tagReferenceToTagEvent(stream *api.ImageStream, tagRef api.TagReference, tagOrID string) (*api.TagEvent, error) {
if len(tagRef.DockerImageReference) > 0 {
return &api.TagEvent{
Created: util.Now(),
DockerImageReference: tagRef.DockerImageReference,
}, nil
}
switch tagRef.From.Kind {
case "ImageStreamImage":
ref, err := api.DockerImageReferenceForStream(stream)
if err != nil {
return nil, err
}
ref.ID = tagOrID
return &api.TagEvent{
Created: util.Now(),
DockerImageReference: ref.String(),
Image: ref.ID,
}, nil
case "ImageStreamTag":
return api.LatestTaggedImage(stream, tagOrID), nil
default:
return nil, fmt.Errorf("invalid from.kind %q: it must be ImageStreamImage or ImageStreamTag", tagRef.From.Kind)
}
}
func tagRefChanged(old, next api.TagReference, streamNamespace string) bool {
if len(next.DockerImageReference) > 0 {
// DockerImageReference possibly changed
return next.DockerImageReference != old.DockerImageReference
}
if next.From == nil {
// both fields in next are empty
return false
}
if len(next.From.Kind) == 0 && len(next.From.Name) == 0 {
// invalid
return false
}
oldFrom := old.From
if oldFrom == nil {
oldFrom = &kapi.ObjectReference{}
}
oldNamespace := oldFrom.Namespace
if len(oldNamespace) == 0 {
oldNamespace = streamNamespace
}
nextNamespace := next.From.Namespace
if len(nextNamespace) == 0 {
nextNamespace = streamNamespace
}
if oldNamespace != nextNamespace {
return true
}
if oldFrom.Name != next.From.Name {
return true
}
return false
}
type TagVerifier struct {
subjectAccessReviewClient subjectaccessreview.Registry
}
func (v *TagVerifier) Verify(old, stream *api.ImageStream, user string) fielderrors.ValidationErrorList {
var errors fielderrors.ValidationErrorList
oldTags := map[string]api.TagReference{}
if old != nil && old.Spec.Tags != nil {
oldTags = old.Spec.Tags
}
for tag, tagRef := range stream.Spec.Tags {
if tagRef.From == nil {
continue
}
if len(tagRef.From.Namespace) == 0 {
continue
}
if stream.Namespace == tagRef.From.Namespace {
continue
}
if oldRef, ok := oldTags[tag]; ok && !tagRefChanged(oldRef, tagRef, stream.Namespace) {
continue
}
glog.Infof("validating access for %s to %v", user, tagRef.From)
streamName, _, err := parseFromReference(stream, tagRef.From)
if err != nil {
errors = append(errors, fielderrors.NewFieldInvalid(fmt.Sprintf("spec.tags[%s].from.name", tag), tagRef.From.Name, "must be of the form <tag>, <repo>:<tag>, <id>, or <repo>@<id>"))
continue
}
subjectAccessReview := authorizationapi.SubjectAccessReview{
Verb: "get",
Resource: "imageStream",
User: user,
ResourceName: streamName,
}
ctx := kapi.WithNamespace(kapi.NewContext(), tagRef.From.Namespace)
glog.V(1).Infof("Performing SubjectAccessReview for user %s to %s/%s", user, tagRef.From.Namespace, streamName)
resp, err := v.subjectAccessReviewClient.CreateSubjectAccessReview(ctx, &subjectAccessReview)
if err != nil || resp == nil || (resp != nil && !resp.Allowed) {
errors = append(errors, fielderrors.NewFieldForbidden(fmt.Sprintf("spec.tags[%s].from", tag), fmt.Sprintf("%s/%s", tagRef.From.Namespace, streamName)))
continue
}
}
return errors
}
func (s Strategy) PrepareForUpdate(obj, old runtime.Object) {
oldStream := old.(*api.ImageStream)
stream := obj.(*api.ImageStream)
stream.Status = oldStream.Status
stream.Status.DockerImageRepository = s.dockerImageRepository(stream)
}
// ValidateUpdate is the default update validation for an end user.
func (s Strategy) ValidateUpdate(ctx kapi.Context, obj, old runtime.Object) fielderrors.ValidationErrorList {
stream := obj.(*api.ImageStream)
user, ok := kapi.UserFrom(ctx)
if !ok {
return fielderrors.ValidationErrorList{kerrors.NewForbidden("imageStream", stream.Name, fmt.Errorf("unable to update an ImageStream without a user on the context"))}
}
oldStream := old.(*api.ImageStream)
errs := s.tagVerifier.Verify(oldStream, stream, user.GetName())
errs = append(errs, s.tagsChanged(oldStream, stream)...)
errs = append(errs, validation.ValidateImageStreamUpdate(stream, oldStream)...)
return errs
}
// Decorate decorates stream.Status.DockerImageRepository using the logic from
// dockerImageRepository().
func (s Strategy) Decorate(obj runtime.Object) error {
ir := obj.(*api.ImageStream)
ir.Status.DockerImageRepository = s.dockerImageRepository(ir)
return nil
}
type StatusStrategy struct {
Strategy
}
// NewStatusStrategy creates a status update strategy around an existing stream
// strategy.
func NewStatusStrategy(strategy Strategy) StatusStrategy {
return StatusStrategy{strategy}
}
func (StatusStrategy) PrepareForUpdate(obj, old runtime.Object) {
}
func (StatusStrategy) ValidateUpdate(ctx kapi.Context, obj, old runtime.Object) fielderrors.ValidationErrorList {
// TODO: merge valid fields after update
return validation.ValidateImageStreamStatusUpdate(obj.(*api.ImageStream), old.(*api.ImageStream))
}
// MatchImageStream returns a generic matcher for a given label and field selector.
func MatchImageStream(label labels.Selector, field fields.Selector) generic.Matcher {
return generic.MatcherFunc(func(obj runtime.Object) (bool, error) {
ir, ok := obj.(*api.ImageStream)
if !ok {
return false, fmt.Errorf("not an ImageStream")
}
fields := ImageStreamToSelectableFields(ir)
return label.Matches(labels.Set(ir.Labels)) && field.Matches(fields), nil
})
}
// ImageStreamToSelectableFields returns a label set that represents the object.
func ImageStreamToSelectableFields(ir *api.ImageStream) labels.Set {
return labels.Set{
"name": ir.Name,
"spec.dockerImageRepository": ir.Spec.DockerImageRepository,
"status.dockerImageRepository": ir.Status.DockerImageRepository,
}
}
// DefaultRegistry returns the default Docker registry (host or host:port), or false if it is not available.
type DefaultRegistry interface {
DefaultRegistry() (string, bool)
}
// DefaultRegistryFunc implements DefaultRegistry for a simple function.
type DefaultRegistryFunc func() (string, bool)
// DefaultRegistry implements the DefaultRegistry interface for a function.
func (fn DefaultRegistryFunc) DefaultRegistry() (string, bool) {
return fn()
}