forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagestreamlookup.go
257 lines (233 loc) · 7.78 KB
/
imagestreamlookup.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
package app
import (
"fmt"
"strings"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// ImageStreamSearcher searches the openshift server image streams for images matching a particular name
type ImageStreamSearcher struct {
Client client.ImageStreamsNamespacer
ImageStreamImages client.ImageStreamImagesNamespacer
Namespaces []string
}
// Search will attempt to find imagestreams with names that matches the passed in value
func (r ImageStreamSearcher) Search(terms ...string) (ComponentMatches, error) {
componentMatches := ComponentMatches{}
for _, term := range terms {
ref, err := imageapi.ParseDockerImageReference(term)
if err != nil || len(ref.Registry) != 0 {
return nil, fmt.Errorf("image streams must be of the form [<namespace>/]<name>[:<tag>|@<digest>]")
}
namespaces := r.Namespaces
if len(ref.Namespace) != 0 {
namespaces = []string{ref.Namespace}
}
searchTag := ref.Tag
if len(searchTag) == 0 {
searchTag = imageapi.DefaultImageTag
}
for _, namespace := range namespaces {
glog.V(4).Infof("checking ImageStreams %s/%s with ref %q", namespace, ref.Name, searchTag)
streams, err := r.Client.ImageStreams(namespace).List(labels.Everything(), fields.Everything())
if err != nil {
if errors.IsNotFound(err) || errors.IsForbidden(err) {
continue
}
return nil, err
}
ref.Namespace = namespace
for i := range streams.Items {
stream := &streams.Items[i]
score, scored := imageStreamScorer(*stream, ref.Name)
if !scored {
glog.V(2).Infof("unscored %s: %v", stream.Name, score)
} else {
imageref, _ := imageapi.ParseDockerImageReference(term)
imageref.Name = stream.Name
latest := imageapi.LatestTaggedImage(stream, searchTag)
if latest == nil || len(latest.Image) == 0 {
glog.V(2).Infof("no image recorded for %s/%s:%s", stream.Namespace, stream.Name, searchTag)
componentMatches = append(componentMatches, &ComponentMatch{
Value: imageref.Exact(),
Argument: fmt.Sprintf("--image-stream=%q", imageref.Exact()),
Name: imageref.Name,
Description: fmt.Sprintf("Image stream %s in project %s", stream.Name, stream.Namespace),
Score: 0.5 + score,
ImageStream: stream,
ImageTag: searchTag,
})
continue
}
imageStreamImage, err := r.ImageStreamImages.ImageStreamImages(namespace).Get(stream.Name, latest.Image)
if err != nil {
if errors.IsNotFound(err) {
// continue searching
glog.V(2).Infof("tag %q is set, but image %q has been removed", searchTag, latest.Image)
continue
}
return nil, err
}
imageData := imageStreamImage.Image
imageref.Registry = ""
match := &ComponentMatch{
Value: imageref.Exact(),
Argument: fmt.Sprintf("--image-stream=%q", imageref.Exact()),
Name: imageref.Name,
Description: fmt.Sprintf("Image stream %q (tag %q) in project %q", stream.Name, searchTag, stream.Namespace),
Score: score,
ImageStream: stream,
Image: &imageData.DockerImageMetadata,
ImageTag: searchTag,
}
glog.V(2).Infof("Adding %s as component match for %q with score %v", match.Description, term, match.Score)
componentMatches = append(componentMatches, match)
}
}
}
}
return componentMatches, nil
}
// InputImageFromMatch returns an image reference from a component match.
// The component match will either be an image stream or an image.
func InputImageFromMatch(match *ComponentMatch) (*ImageRef, error) {
g := NewImageRefGenerator()
switch {
case match.ImageStream != nil:
input, err := g.FromStream(match.ImageStream, match.ImageTag)
if err != nil {
return nil, err
}
input.AsImageStream = true
input.Info = match.Image
return input, nil
case match.Image != nil:
input, err := g.FromName(match.Value)
if err != nil {
return nil, err
}
input.AsImageStream = !match.LocalOnly
input.Info = match.Image
input.Insecure = match.Insecure
return input, nil
default:
input, err := g.FromName(match.Value)
if err != nil {
return nil, err
}
return input, nil
}
}
// ImageStreamByAnnotationSearcher searches for image streams based on 'supports' annotations
// found in tagged images belonging to the stream
type ImageStreamByAnnotationSearcher struct {
Client client.ImageStreamsNamespacer
ImageStreamImages client.ImageStreamImagesNamespacer
Namespaces []string
imageStreams map[string]*imageapi.ImageStreamList
}
const supportsAnnotationKey = "supports"
// NewImageStreamByAnnotationSearcher creates a new ImageStreamByAnnotationSearcher
func NewImageStreamByAnnotationSearcher(streamClient client.ImageStreamsNamespacer, imageClient client.ImageStreamImagesNamespacer, namespaces []string) Searcher {
return &ImageStreamByAnnotationSearcher{
Client: streamClient,
ImageStreamImages: imageClient,
Namespaces: namespaces,
imageStreams: make(map[string]*imageapi.ImageStreamList),
}
}
func (r *ImageStreamByAnnotationSearcher) getImageStreams(namespace string) ([]imageapi.ImageStream, error) {
imageStreamList, ok := r.imageStreams[namespace]
if !ok {
var err error
imageStreamList, err = r.Client.ImageStreams(namespace).List(labels.Everything(), fields.Everything())
if err != nil {
return nil, err
}
r.imageStreams[namespace] = imageStreamList
}
return imageStreamList.Items, nil
}
func matchSupportsAnnotation(value, annotation string) (float32, bool) {
valueBase := strings.Split(value, ":")[0]
parts := strings.Split(annotation, ",")
// attempt an exact match first
for _, p := range parts {
if value == p {
return 0.0, true
}
}
// attempt a partial match
for _, p := range parts {
partBase := strings.Split(p, ":")[0]
if valueBase == partBase {
return 0.5, true
}
}
return 0, false
}
func (r *ImageStreamByAnnotationSearcher) annotationMatches(stream *imageapi.ImageStream, value string) []*ComponentMatch {
if stream.Spec.Tags == nil {
glog.Infof("No tags found on image, returning nil")
return nil
}
matches := []*ComponentMatch{}
for tag, tagref := range stream.Spec.Tags {
if tagref.Annotations == nil {
continue
}
supports, ok := tagref.Annotations[supportsAnnotationKey]
if !ok {
continue
}
score, ok := matchSupportsAnnotation(value, supports)
if !ok {
continue
}
latest := imageapi.LatestTaggedImage(stream, tag)
if latest == nil {
continue
}
imageStream, err := r.ImageStreamImages.ImageStreamImages(stream.Namespace).Get(stream.Name, latest.Image)
if err != nil {
glog.V(2).Infof("Could not retrieve image stream image for stream %q, tag %q: %v", stream.Name, tag, err)
continue
}
if imageStream == nil {
continue
}
imageData := imageStream.Image
match := &ComponentMatch{
Value: value,
Name: stream.Name,
Argument: fmt.Sprintf("--image-stream=%q", value),
Description: fmt.Sprintf("Image stream %s in project %s", stream.Name, stream.Namespace),
Score: score,
ImageStream: stream,
Image: &imageData.DockerImageMetadata,
ImageTag: tag,
}
matches = append(matches, match)
}
return matches
}
// Search finds image stream images using their 'supports' annotation
func (r *ImageStreamByAnnotationSearcher) Search(terms ...string) (ComponentMatches, error) {
matches := ComponentMatches{}
for _, namespace := range r.Namespaces {
streams, err := r.getImageStreams(namespace)
if err != nil {
return nil, err
}
for i := range streams {
for _, term := range terms {
matches = append(matches, r.annotationMatches(&streams[i], term)...)
}
}
}
return matches, nil
}