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
/
imagestreamlookup.go
367 lines (333 loc) · 11.2 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
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
package app
import (
"fmt"
"strings"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"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
AllowMissingTags bool
}
// Search will attempt to find imagestreams with names that match the passed in value
func (r ImageStreamSearcher) Search(precise bool, terms ...string) (ComponentMatches, []error) {
componentMatches := ComponentMatches{}
var errs []error
for _, term := range terms {
ref, err := imageapi.ParseDockerImageReference(term)
if err != nil || len(ref.Registry) != 0 {
glog.V(2).Infof("image streams must be of the form [<namespace>/]<name>[:<tag>|@<digest>], term %q did not qualify", term)
continue
}
if term == "__imagestream_fail" {
errs = append(errs, fmt.Errorf("unable to find the specified image: %s", term))
continue
}
namespaces := r.Namespaces
if len(ref.Namespace) != 0 {
namespaces = []string{ref.Namespace}
}
followTag := false
searchTag := ref.Tag
if len(searchTag) == 0 {
searchTag = imageapi.DefaultImageTag
followTag = true
}
for _, namespace := range namespaces {
glog.V(4).Infof("checking ImageStreams %s/%s with ref %q", namespace, ref.Name, searchTag)
exact := false
streams, err := r.Client.ImageStreams(namespace).List(kapi.ListOptions{})
if err != nil {
if errors.IsNotFound(err) || errors.IsForbidden(err) {
continue
}
errs = append(errs, err)
continue
}
original := ref
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)
continue
}
// indicate the server knows how to directly import image stream tags
var meta map[string]string
if stream.Generation > 0 {
meta = map[string]string{"direct-tag": "1"}
}
imageref := original
imageref.Name = stream.Name
imageref.Registry = ""
matchName := fmt.Sprintf("%s/%s", stream.Namespace, stream.Name)
addMatch := func(tag string, matchScore float32, image *imageapi.DockerImage, notFound bool) {
name := matchName
var description, argument string
if len(tag) > 0 {
name = fmt.Sprintf("%s:%s", name, tag)
argument = fmt.Sprintf("--image-stream=%q", name)
description = fmt.Sprintf("Image stream %q (tag %q) in project %q", stream.Name, tag, stream.Namespace)
} else {
argument = fmt.Sprintf("--image-stream=%q --allow-missing-imagestream-tags", name)
description = fmt.Sprintf("Image stream %q in project %q", stream.Name, stream.Namespace)
}
match := &ComponentMatch{
Value: term,
Argument: argument,
Name: name,
Description: description,
Score: matchScore,
ImageStream: stream,
Image: image,
ImageTag: tag,
Meta: meta,
NoTagsFound: notFound,
}
glog.V(2).Infof("Adding %s as component match for %q with score %v", match.Description, term, matchScore)
componentMatches = append(componentMatches, match)
}
// When an image stream contains a tag that references another local tag, and the user has not
// provided a tag themselves (i.e. they asked for mysql and we defaulted to mysql:latest), walk
// the chain of references to the end. This ensures that applications can default to using a "stable"
// branch by giving the control over version to the image stream author.
finalTag := searchTag
if specTag, ok := stream.Spec.Tags[searchTag]; ok && followTag {
if specTag.From != nil && specTag.From.Kind == "ImageStreamTag" && !strings.Contains(specTag.From.Name, ":") {
if imageapi.LatestTaggedImage(stream, specTag.From.Name) != nil {
finalTag = specTag.From.Name
}
}
}
latest := imageapi.LatestTaggedImage(stream, finalTag)
if latest == nil || len(latest.Image) == 0 {
glog.V(2).Infof("no image recorded for %s/%s:%s", stream.Namespace, stream.Name, finalTag)
if r.AllowMissingTags {
addMatch(finalTag, score, nil, false)
continue
}
// Find tags that do exist and return those as partial matches
foundOtherTags := false
for tag := range stream.Status.Tags {
latest := imageapi.LatestTaggedImage(stream, tag)
if latest == nil || len(latest.Image) == 0 {
continue
}
foundOtherTags = true
// If the user specified a tag in their search string (followTag == false),
// then score this match lower.
tagScore := score
if !followTag {
tagScore += 0.5
}
addMatch(tag, tagScore, nil, false)
}
if !foundOtherTags {
addMatch("", 0.5+score, nil, true)
}
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", finalTag, latest.Image)
continue
}
errs = append(errs, err)
continue
}
addMatch(finalTag, score, &imageStreamImage.Image.DockerImageMetadata, false)
if score == 0.0 {
exact = true
}
}
// If we found one or more exact matches in this namespace, do not continue looking at
// other namespaces
if exact && precise {
break
}
}
}
return componentMatches, errs
}
// 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
}
if match.Meta["direct-tag"] == "1" {
input.TagDirectly = true
}
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
}
if match.Meta["direct-tag"] == "1" {
input.TagDirectly = true
input.AsResolvedImage = true
}
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(kapi.ListOptions{})
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
}
// indicate the server knows how to directly tag images
var meta map[string]string
if imageStream.Generation > 0 {
meta = map[string]string{"direct-tag": "1"}
}
imageData := imageStream.Image
matchName := fmt.Sprintf("%s/%s", stream.Namespace, stream.Name)
description := fmt.Sprintf("Image stream %q in project %q", stream.Name, stream.Namespace)
if len(tag) > 0 {
matchName = fmt.Sprintf("%s:%s", matchName, tag)
description = fmt.Sprintf("Image stream %q (tag %q) in project %q", stream.Name, tag, stream.Namespace)
}
glog.V(5).Infof("ImageStreamAnnotationSearcher match found: %s for %s with score %f", matchName, value, score)
match := &ComponentMatch{
Value: value,
Name: fmt.Sprintf("%s", matchName),
Argument: fmt.Sprintf("--image-stream=%q", matchName),
Description: description,
Score: score,
ImageStream: stream,
Image: &imageData.DockerImageMetadata,
ImageTag: tag,
Meta: meta,
}
matches = append(matches, match)
}
return matches
}
// Search finds image stream images using their 'supports' annotation
func (r *ImageStreamByAnnotationSearcher) Search(precise bool, terms ...string) (ComponentMatches, []error) {
matches := ComponentMatches{}
var errs []error
for _, namespace := range r.Namespaces {
streams, err := r.getImageStreams(namespace)
if err != nil {
errs = append(errs, err)
continue
}
for i := range streams {
for _, term := range terms {
if term == "__imagestreamannotation_fail" {
errs = append(errs, fmt.Errorf("unable to find the specified image: %s", term))
continue
}
glog.V(5).Infof("Checking imagestream %s/%s for supports annotation %q", namespace, streams[i].Name, term)
matches = append(matches, r.annotationMatches(&streams[i], term)...)
}
}
if precise {
for _, m := range matches {
if m.Score == 0.0 {
return matches, errs
}
}
}
}
return matches, errs
}