forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
130 lines (109 loc) · 3.55 KB
/
rest.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
package imagestreamtag
import (
"fmt"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/image/registry/image"
"github.com/openshift/origin/pkg/image/registry/imagestream"
)
// REST implements the RESTStorage interface for ImageStreamTag
// It only supports the Get method and is used to simplify retrieving an Image by tag from an ImageStream
type REST struct {
imageRegistry image.Registry
imageStreamRegistry imagestream.Registry
}
// NewREST returns a new REST.
func NewREST(imageRegistry image.Registry, imageStreamRegistry imagestream.Registry) *REST {
return &REST{imageRegistry, imageStreamRegistry}
}
// New is only implemented to make REST implement RESTStorage
func (r *REST) New() runtime.Object {
return &api.ImageStreamTag{}
}
// nameAndTag splits a string into its name component and tag component, and returns an error
// if the string is not in the right form.
func nameAndTag(id string) (name string, tag string, err error) {
segments := strings.Split(id, ":")
switch len(segments) {
case 2:
name = segments[0]
tag = segments[1]
if len(name) == 0 || len(tag) == 0 {
err = errors.NewBadRequest("imageStreamTags must be retrieved with <name>:<tag>")
}
default:
err = errors.NewBadRequest("imageStreamTags must be retrieved with <name>:<tag>")
}
return
}
// Get retrieves an image that has been tagged by stream and tag. `id` is of the format
// <stream name>:<tag>.
func (r *REST) Get(ctx kapi.Context, id string) (runtime.Object, error) {
name, tag, err := nameAndTag(id)
if err != nil {
return nil, err
}
stream, err := r.imageStreamRegistry.GetImageStream(ctx, name)
if err != nil {
return nil, err
}
event := api.LatestTaggedImage(stream, tag)
if event == nil || len(event.Image) == 0 {
return nil, errors.NewNotFound("imageStreamTag", id)
}
image, err := r.imageRegistry.GetImage(ctx, event.Image)
if err != nil {
return nil, err
}
// if the stream has Spec.Tags[tag].Annotations[k] = v, copy it to the image's annotations
if stream.Spec.Tags != nil {
if tagRef, ok := stream.Spec.Tags[tag]; ok {
if image.Annotations == nil {
image.Annotations = make(map[string]string)
}
for k, v := range tagRef.Annotations {
image.Annotations[k] = v
}
}
}
imageWithMetadata, err := api.ImageWithMetadata(*image)
if err != nil {
return nil, err
}
ist := api.ImageStreamTag{
Image: *imageWithMetadata,
ImageName: imageWithMetadata.Name,
}
ist.Namespace = kapi.NamespaceValue(ctx)
ist.Name = id
return &ist, nil
}
// Delete removes a tag from a stream. `id` is of the format <stream name>:<tag>.
// The associated image that the tag points to is *not* deleted.
// The tag history remains intact and is not deleted.
func (r *REST) Delete(ctx kapi.Context, id string) (runtime.Object, error) {
name, tag, err := nameAndTag(id)
if err != nil {
return nil, err
}
stream, err := r.imageStreamRegistry.GetImageStream(ctx, name)
if err != nil {
return nil, err
}
if stream.Spec.Tags == nil {
return nil, errors.NewNotFound("imageStreamTag", tag)
}
_, ok := stream.Spec.Tags[tag]
if !ok {
return nil, errors.NewNotFound("imageStreamTag", tag)
}
delete(stream.Spec.Tags, tag)
_, err = r.imageStreamRegistry.UpdateImageStream(ctx, stream)
if err != nil {
return nil, fmt.Errorf("Error removing tag from image stream: %s", err)
}
return &kapi.Status{Status: kapi.StatusSuccess}, nil
}