forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerimagelookup.go
268 lines (237 loc) · 7.48 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
package app
import (
"fmt"
"sort"
"strings"
docker "github.com/fsouza/go-dockerclient"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
utilerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
"github.com/golang/glog"
"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
}
// 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
}
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.Registry) == 0 {
ref.Registry = "local Docker"
}
if len(ref.Tag) == 0 {
ref.Tag = imageapi.DefaultImageTag
}
termMatches := ScoredComponentMatches{}
for _, image := range images {
if tags := matchTag(image, term, ref.Registry, ref.Namespace, ref.Name, ref.Tag); len(tags) > 0 {
termMatches = append(termMatches, tags...)
}
}
sort.Sort(termMatches)
if r.RegistrySearcher != nil && len(termMatches.Exact()) == 0 {
matches, err := r.RegistrySearcher.Search(term)
switch err.(type) {
case nil:
termMatches = append(termMatches, matches...)
case ErrNoMatch:
default:
return nil, err
}
}
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, ""),
Builder: IsBuilderImage(dockerImage),
Score: match.Score,
Image: dockerImage,
ImageTag: ref.Tag,
Insecure: r.Insecure,
Meta: map[string]string{"registry": ref.Registry},
}
termMatches[i] = updated
}
componentMatches = append(componentMatches, termMatches...)
}
if len(errs) != 0 {
return nil, utilerrors.NewAggregate(errs)
}
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", ref.String())
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) {
return nil, ErrNoMatch{value: term, qualifier: err.Error()}
}
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, dockerImage); err != nil {
return nil, err
}
componentMatches = append(componentMatches, &ComponentMatch{
Value: term,
Argument: fmt.Sprintf("--docker-image=%q", term),
Name: term,
Description: descriptionFor(dockerImage, term, ref.Registry, ref.Tag),
Builder: IsBuilderImage(dockerImage),
Score: 0,
Image: dockerImage,
ImageTag: ref.Tag,
Meta: map[string]string{"registry": ref.Registry},
})
}
return componentMatches, nil
}
func descriptionFor(image *imageapi.DockerImage, value, from string, tag string) string {
shortID := image.ID
if len(shortID) > 7 {
shortID = shortID[: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 {
if len(tag) == 0 {
tag = imageapi.DefaultImageTag
}
matches := []*ComponentMatch{}
for _, s := range image.RepoTags {
if value == 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
}