forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
componentref.go
423 lines (380 loc) · 11.6 KB
/
componentref.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package app
import (
"fmt"
"sort"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
imageapi "github.com/openshift/origin/pkg/image/api"
templateapi "github.com/openshift/origin/pkg/template/api"
)
// IsComponentReference returns true if the provided string appears to be a reference to a source repository
// on disk, at a URL, a docker image name (which might be on a Docker registry or an OpenShift image stream),
// or a template.
func IsComponentReference(s string) bool {
if len(s) == 0 {
return false
}
all := strings.Split(s, "+")
_, _, _, err := componentWithSource(all[0])
return err == nil
}
// componentWithSource parses the provided string and returns an image component
// and optionally a repository on success
func componentWithSource(s string) (component, repo string, builder bool, err error) {
if strings.Contains(s, "~") {
segs := strings.SplitN(s, "~", 2)
if len(segs) == 2 {
builder = true
switch {
case len(segs[0]) == 0:
err = fmt.Errorf("when using '[image]~[code]' form for %q, you must specify a image name", s)
return
case len(segs[1]) == 0:
component = segs[0]
default:
component = segs[0]
repo = segs[1]
}
}
} else {
component = s
}
// component must be of the form compatible with a pull spec *or* <namespace>/<name>
if _, err := imageapi.ParseDockerImageReference(component); err != nil {
return "", "", false, fmt.Errorf("%q is not a valid Docker pull specification: %s", component, err)
}
return
}
// ComponentReference defines an interface for components
type ComponentReference interface {
// Input contains the input of the component
Input() *ComponentInput
// Resolve sets the match in input
Resolve() error
// NeedsSource indicates if the component needs source code
NeedsSource() bool
}
// ComponentReferences is a set of components
type ComponentReferences []ComponentReference
// NeedsSource returns all the components that need source code in order to build
func (r ComponentReferences) NeedsSource() (refs ComponentReferences) {
for _, ref := range r {
if ref.NeedsSource() {
refs = append(refs, ref)
}
}
return
}
func (r ComponentReferences) String() string {
components := []string{}
for _, ref := range r {
components = append(components, ref.Input().Value)
}
return strings.Join(components, ",")
}
// GroupedComponentReferences is a set of components that can be grouped
// by their group id
type GroupedComponentReferences ComponentReferences
func (m GroupedComponentReferences) Len() int { return len(m) }
func (m GroupedComponentReferences) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m GroupedComponentReferences) Less(i, j int) bool {
return m[i].Input().GroupID < m[j].Input().GroupID
}
// Group groups components based on their group ids
func (r ComponentReferences) Group() (refs []ComponentReferences) {
sorted := make(GroupedComponentReferences, len(r))
copy(sorted, r)
sort.Sort(sorted)
groupID := -1
for _, ref := range sorted {
if ref.Input().GroupID != groupID {
refs = append(refs, ComponentReferences{})
}
groupID = ref.Input().GroupID
refs[len(refs)-1] = append(refs[len(refs)-1], ref)
}
return
}
// ComponentMatch is a match to a provided component
type ComponentMatch struct {
Value string
Argument string
Name string
Description string
Score float32
Builder bool
Image *imageapi.DockerImage
ImageStream *imageapi.ImageStream
ImageTag string
Template *templateapi.Template
}
func (m *ComponentMatch) String() string {
return m.Argument
}
// IsImage returns whether or not the component match is an
// image or image stream
func (m *ComponentMatch) IsImage() bool {
return m.Image != nil || m.ImageStream != nil
}
// IsTemplate returns whether or not the component match is
// a template
func (m *ComponentMatch) IsTemplate() bool {
return m.Template != nil
}
// Resolver is an interface for resolving provided input to component matches.
// A Resolver should return ErrMultipleMatches when more than one result can
// be constructed as a match. It should also set the score to 0.0 if this is a
// perfect match, and to higher values the less adequate the match is.
type Resolver interface {
Resolve(value string) (*ComponentMatch, error)
}
// ScoredComponentMatches is a set of component matches grouped by score
type ScoredComponentMatches []*ComponentMatch
func (m ScoredComponentMatches) Len() int { return len(m) }
func (m ScoredComponentMatches) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m ScoredComponentMatches) Less(i, j int) bool { return m[i].Score < m[j].Score }
// Exact returns all the exact component matches
func (m ScoredComponentMatches) Exact() []*ComponentMatch {
out := []*ComponentMatch{}
for _, match := range m {
if match.Score == 0.0 {
out = append(out, match)
}
}
return out
}
// WeightedResolver is a resolver identified as exact or not, depending on its weight
type WeightedResolver struct {
Resolver
Weight float32
}
// PerfectMatchWeightedResolver returns only matches from resolvers that are identified as exact
// (weight 0.0), and only matches from those resolvers that qualify as exact (score = 0.0). If no
// perfect matches exist, an ErrMultipleMatches is returned indicating the remaining candidate(s).
// Note that this method may resolve ErrMultipleMatches with a single match, indicating an error
// (no perfect match) but with only one candidate.
type PerfectMatchWeightedResolver []WeightedResolver
// Resolve resolves the provided input and returns only exact matches
func (r PerfectMatchWeightedResolver) Resolve(value string) (*ComponentMatch, error) {
imperfect := []*ComponentMatch{}
group := []WeightedResolver{}
for i, resolver := range r {
if len(group) == 0 || resolver.Weight == group[0].Weight {
group = append(group, resolver)
if i != len(r)-1 && r[i+1].Weight == group[0].Weight {
continue
}
}
exact, inexact, err := resolveExact(WeightedResolvers(group), value)
switch {
case exact != nil:
if exact.Score == 0.0 {
return exact, nil
}
if resolver.Weight != 0.0 {
exact.Score = resolver.Weight * exact.Score
}
imperfect = append(imperfect, exact)
case len(inexact) > 0:
sort.Sort(ScoredComponentMatches(inexact))
if inexact[0].Score == 0.0 && (len(inexact) == 1 || inexact[1].Score != 0.0) {
return inexact[0], nil
}
for _, m := range inexact {
if resolver.Weight != 0.0 {
m.Score = resolver.Weight * m.Score
}
imperfect = append(imperfect, m)
}
case err != nil:
return nil, err
}
group = nil
}
switch len(imperfect) {
case 0:
return nil, ErrNoMatch{value: value}
case 1:
return imperfect[0], nil
default:
return nil, ErrMultipleMatches{value, imperfect}
}
}
func resolveExact(resolver Resolver, value string) (exact *ComponentMatch, inexact []*ComponentMatch, err error) {
match, err := resolver.Resolve(value)
if err != nil {
switch t := err.(type) {
case ErrNoMatch:
return nil, nil, nil
case ErrMultipleMatches:
return nil, t.Matches, nil
default:
return nil, nil, err
}
}
return match, nil, nil
}
// WeightedResolvers is a set of weighted resolvers
type WeightedResolvers []WeightedResolver
// Resolve resolves the provided input and returns both exact and inexact matches
func (r WeightedResolvers) Resolve(value string) (*ComponentMatch, error) {
candidates := []*ComponentMatch{}
errs := []error{}
for _, resolver := range r {
exact, inexact, err := resolveExact(resolver.Resolver, value)
switch {
case exact != nil:
candidates = append(candidates, exact)
case len(inexact) > 0:
candidates = append(candidates, inexact...)
case err != nil:
errs = append(errs, err)
}
}
if len(errs) != 0 {
return nil, errors.NewAggregate(errs)
}
switch len(candidates) {
case 0:
return nil, ErrNoMatch{value: value}
case 1:
return candidates[0], nil
default:
return nil, ErrMultipleMatches{value, candidates}
}
}
// ReferenceBuilder is used for building all the necessary object references
// for an application
type ReferenceBuilder struct {
refs ComponentReferences
repos []*SourceRepository
errs []error
groupID int
}
// AddComponents turns all provided component inputs into component references
func (r *ReferenceBuilder) AddComponents(inputs []string, fn func(*ComponentInput) ComponentReference) {
for _, s := range inputs {
for _, s := range strings.Split(s, "+") {
input, repo, err := NewComponentInput(s)
if err != nil {
r.errs = append(r.errs, err)
continue
}
input.GroupID = r.groupID
ref := fn(input)
if len(repo) != 0 {
repository, ok := r.AddSourceRepository(repo)
if !ok {
continue
}
input.Use(repository)
repository.UsedBy(ref)
}
r.refs = append(r.refs, ref)
}
r.groupID++
}
}
// AddGroups adds group ids to groups of components
func (r *ReferenceBuilder) AddGroups(inputs []string) {
for _, s := range inputs {
groups := strings.Split(s, "+")
if len(groups) == 1 {
r.errs = append(r.errs, fmt.Errorf("group %q only contains a single name", s))
continue
}
to := -1
for _, group := range groups {
var match ComponentReference
for _, ref := range r.refs {
if group == ref.Input().Value {
match = ref
break
}
}
if match == nil {
r.errs = append(r.errs, fmt.Errorf("the name %q from the group definition is not in use, and can't be used", group))
break
}
if to == -1 {
to = match.Input().GroupID
} else {
match.Input().GroupID = to
}
}
}
}
// AddSourceRepository resolves the input to an actual source repository
func (r *ReferenceBuilder) AddSourceRepository(input string) (*SourceRepository, bool) {
for _, existing := range r.repos {
if input == existing.location {
return existing, true
}
}
source, err := NewSourceRepository(input)
if err != nil {
r.errs = append(r.errs, err)
return nil, false
}
r.repos = append(r.repos, source)
return source, true
}
// Result returns the result of the config conversion to object references
func (r *ReferenceBuilder) Result() (ComponentReferences, []*SourceRepository, []error) {
return r.refs, r.repos, r.errs
}
// NewComponentInput returns a new ComponentInput by checking for image using [image]~
// (to indicate builder) or [image]~[code] (builder plus code)
func NewComponentInput(input string) (*ComponentInput, string, error) {
component, repo, builder, err := componentWithSource(input)
if err != nil {
return nil, "", err
}
return &ComponentInput{
From: input,
Argument: input,
Value: component,
ExpectToBuild: builder,
}, repo, nil
}
// ComponentInput is the necessary input for creating a component
type ComponentInput struct {
GroupID int
From string
Argument string
Value string
ExpectToBuild bool
Uses *SourceRepository
Match *ComponentMatch
Resolver
}
// Input returns the component input
func (i *ComponentInput) Input() *ComponentInput {
return i
}
// NeedsSource indicates if the component input needs source code
func (i *ComponentInput) NeedsSource() bool {
return i.ExpectToBuild && i.Uses == nil
}
// Resolve sets the match in input
func (i *ComponentInput) Resolve() error {
if i.Resolver == nil {
return ErrNoMatch{value: i.Value, qualifier: "no resolver defined"}
}
match, err := i.Resolver.Resolve(i.Value)
if err != nil {
return err
}
i.Value = match.Value
i.Argument = match.Argument
i.Match = match
return nil
}
func (i *ComponentInput) String() string {
return i.Value
}
// Use adds the provided source repository as the used one
// by the component input
func (i *ComponentInput) Use(repo *SourceRepository) {
i.Uses = repo
}