/
dockerimagelookup.go
305 lines (271 loc) · 9.08 KB
/
dockerimagelookup.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
package app
import (
"fmt"
"sort"
"strings"
docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
utilerrors "k8s.io/kubernetes/pkg/util/errors"
"github.com/openshift/origin/pkg/dockerregistry"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// DockerClient is the local interface for the docker client
type DockerClient interface {
ListImages(opts docker.ListImagesOptions) ([]docker.APIImages, error)
InspectImage(name string) (*docker.Image, error)
}
// DockerClientSearcher finds local docker images locally that match a search value
type DockerClientSearcher struct {
Client DockerClient
// Optional, will delegate resolution to the registry if no local
// exact matches are found.
RegistrySearcher Searcher
// Insecure, if true will add an annotation to generated ImageStream
// so that the image can be pulled properly
Insecure bool
// AllowingMissingImages will allow images that could not be found in the local or
// remote registry to be used anyway.
AllowMissingImages bool
}
// Search searches all images in local docker server for images that match terms
func (r DockerClientSearcher) Search(terms ...string) (ComponentMatches, error) {
componentMatches := ComponentMatches{}
errs := []error{}
for _, term := range terms {
ref, err := imageapi.ParseDockerImageReference(term)
if err != nil {
return nil, err
}
termMatches := ScoredComponentMatches{}
// first look for the image in the remote docker registry
if r.RegistrySearcher != nil {
glog.V(4).Infof("checking remote registry for %q", ref.String())
matches, err := r.RegistrySearcher.Search(term)
switch err.(type) {
case nil:
for i := range matches {
matches[i].LocalOnly = false
glog.V(5).Infof("Found remote match %v", matches[i].Value)
}
termMatches = append(termMatches, matches...)
case ErrNoMatch:
default:
glog.V(5).Infof("An error occurred searching remote registry for %q: %v", ref.String(), err)
}
}
// if we didn't find it exactly in a remote registry,
// try to find it as a local-only image.
if len(termMatches.Exact()) == 0 {
glog.V(4).Infof("checking local Docker daemon for %q", ref.String())
images, err := r.Client.ListImages(docker.ListImagesOptions{})
if err != nil {
return nil, err
}
if len(ref.Tag) == 0 {
ref.Tag = imageapi.DefaultImageTag
term = fmt.Sprintf("%s:%s", term, imageapi.DefaultImageTag)
}
for _, image := range images {
if tags := matchTag(image, term, ref.Registry, ref.Namespace, ref.Name, ref.Tag); len(tags) > 0 {
for i := range tags {
tags[i].LocalOnly = true
glog.V(5).Infof("Found local docker image match %q for %q", tags[i].Value, term)
}
termMatches = append(termMatches, tags...)
}
}
}
sort.Sort(termMatches)
for i, match := range termMatches {
if match.Image != nil {
continue
}
image, err := r.Client.InspectImage(match.Value)
if err != nil {
if err != docker.ErrNoSuchImage {
errs = append(errs, err)
}
continue
}
dockerImage := &imageapi.DockerImage{}
if err := kapi.Scheme.Convert(image, dockerImage); err != nil {
return nil, err
}
updated := &ComponentMatch{
Value: match.Value,
Argument: fmt.Sprintf("--docker-image=%q", match.Value),
Name: match.Value,
Description: descriptionFor(dockerImage, match.Value, ref.Registry, ""),
Score: match.Score,
Image: dockerImage,
ImageTag: ref.Tag,
Insecure: r.Insecure,
Meta: map[string]string{"registry": ref.Registry},
LocalOnly: match.LocalOnly,
}
termMatches[i] = updated
}
componentMatches = append(componentMatches, termMatches...)
}
if len(errs) != 0 {
return nil, utilerrors.NewAggregate(errs)
}
return componentMatches, nil
}
// MissingImageSearcher always returns an exact match for the item being searched for.
// It should be used with very high weight(weak priority) as a result of last resort when the
// user has indicated they want to allow missing images(not found in the docker registry
// or locally) to be used anyway.
type MissingImageSearcher struct {
}
// Search always returns an exact match for the search terms.
func (r MissingImageSearcher) Search(terms ...string) (ComponentMatches, error) {
componentMatches := ComponentMatches{}
for _, term := range terms {
componentMatches = append(componentMatches, &ComponentMatch{
Value: term,
Score: 0.0,
LocalOnly: true,
})
glog.V(4).Infof("Added missing match for %v", term)
}
return componentMatches, nil
}
// DockerRegistrySearcher searches for images in a given docker registry.
// Notice that it only matches exact searches - so a search for "rub" will
// not return images with the name "ruby".
// TODO: replace ImageByTag to allow partial matches
type DockerRegistrySearcher struct {
Client dockerregistry.Client
AllowInsecure bool
}
// Search searches in the Docker registry for images that match terms
func (r DockerRegistrySearcher) Search(terms ...string) (ComponentMatches, error) {
componentMatches := ComponentMatches{}
for _, term := range terms {
ref, err := imageapi.ParseDockerImageReference(term)
if err != nil {
return nil, err
}
glog.V(4).Infof("checking Docker registry for %q, allow-insecure=%v", ref.String(), r.AllowInsecure)
connection, err := r.Client.Connect(ref.Registry, r.AllowInsecure)
if err != nil {
if dockerregistry.IsRegistryNotFound(err) {
return nil, ErrNoMatch{value: term}
}
return nil, fmt.Errorf("can't connect to %q: %v", ref.Registry, err)
}
image, err := connection.ImageByTag(ref.Namespace, ref.Name, ref.Tag)
if err != nil {
if dockerregistry.IsNotFound(err) {
if dockerregistry.IsTagNotFound(err) {
glog.V(4).Infof("tag not found: %v", err)
}
continue
}
return nil, fmt.Errorf("can't connect to %q: %v", ref.Registry, err)
}
if len(ref.Tag) == 0 {
ref.Tag = imageapi.DefaultImageTag
}
if len(ref.Registry) == 0 {
ref.Registry = "Docker Hub"
}
glog.V(4).Infof("found image: %#v", image)
dockerImage := &imageapi.DockerImage{}
if err = kapi.Scheme.Convert(&image.Image, dockerImage); err != nil {
return nil, err
}
match := &ComponentMatch{
Value: term,
Argument: fmt.Sprintf("--docker-image=%q", term),
Name: term,
Description: descriptionFor(dockerImage, term, ref.Registry, ref.Tag),
Score: 0,
Image: dockerImage,
ImageTag: ref.Tag,
Insecure: r.AllowInsecure,
Meta: map[string]string{"registry": ref.Registry},
}
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
}
func descriptionFor(image *imageapi.DockerImage, value, from string, tag string) string {
shortID := imageapi.ShortDockerImageID(image, 7)
tagPart := ""
if len(tag) > 0 {
tagPart = fmt.Sprintf(" (tag %q)", tag)
}
parts := []string{fmt.Sprintf("Docker image %q%v", value, tagPart), shortID, fmt.Sprintf("from %s", from)}
if image.Size > 0 {
mb := float64(image.Size) / float64(1024*1024)
parts = append(parts, fmt.Sprintf("%f", mb))
}
if len(image.Author) > 0 {
parts = append(parts, fmt.Sprintf("author %s", image.Author))
}
if len(image.Comment) > 0 {
parts = append(parts, image.Comment)
}
return strings.Join(parts, ", ")
}
func matchTag(image docker.APIImages, value, registry, namespace, name, tag string) []*ComponentMatch {
matches := []*ComponentMatch{}
for _, s := range image.RepoTags {
if value == s {
glog.V(4).Infof("exact match on %q", s)
matches = append(matches, &ComponentMatch{
Value: s,
Score: 0.0,
})
continue
}
iRef, err := imageapi.ParseDockerImageReference(s)
if err != nil {
continue
}
if len(iRef.Tag) == 0 {
iRef.Tag = imageapi.DefaultImageTag
}
match := &ComponentMatch{}
ok, score := partialScorer(name, iRef.Name, true, 0.5, 1.0)
if !ok {
continue
}
match.Score += score
_, score = partialScorer(namespace, iRef.Namespace, false, 0.5, 1.0)
match.Score += score
_, score = partialScorer(registry, iRef.Registry, false, 0.5, 1.0)
match.Score += score
_, score = partialScorer(tag, iRef.Tag, false, 0.5, 1.0)
match.Score += score
if match.Score >= 4.0 {
continue
}
match.Score = match.Score / 4.0
glog.V(4).Infof("partial match on %q with %f", s, match.Score)
match.Value = s
match.Meta = map[string]string{"registry": registry}
matches = append(matches, match)
}
return matches
}
// PassThroughDockerSearcher returns a match with the value that was passed in
type PassThroughDockerSearcher struct{}
// Search always returns a match for every term passed in
func (r *PassThroughDockerSearcher) Search(terms ...string) (ComponentMatches, error) {
matches := ComponentMatches{}
for _, value := range terms {
matches = append(matches, &ComponentMatch{
Value: value,
Name: value,
Argument: fmt.Sprintf("--docker-image=%q", value),
Description: fmt.Sprintf("Docker image %q", value),
Score: 1.0,
})
}
return matches, nil
}