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
/
componentref.go
358 lines (318 loc) · 9.85 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
package app
import (
"fmt"
"sort"
"strings"
"github.com/openshift/origin/pkg/generate"
"k8s.io/apimachinery/pkg/util/errors"
)
// IsComponentReference returns an error if the provided string does not appear 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) error {
if len(s) == 0 {
return fmt.Errorf("empty string provided to component reference check")
}
all := strings.Split(s, "+")
_, _, _, err := componentWithSource(all[0])
return err
}
// 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
}
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
// Search sets the search matches in input
Search() error
// NeedsSource indicates if the component needs source code
NeedsSource() bool
}
// ComponentReferences is a set of components
type ComponentReferences []ComponentReference
func (r ComponentReferences) filter(filterFunc func(ref ComponentReference) bool) ComponentReferences {
refs := ComponentReferences{}
for _, ref := range r {
if filterFunc(ref) {
refs = append(refs, ref)
}
}
return refs
}
// HasSource returns true if there is more than one component that has a repo associated
func (r ComponentReferences) HasSource() bool {
return len(r.filter(func(ref ComponentReference) bool { return ref.Input().Uses != nil })) > 0
}
// NeedsSource returns all the components that need source code in order to build
func (r ComponentReferences) NeedsSource() (refs ComponentReferences) {
return r.filter(func(ref ComponentReference) bool {
return ref.NeedsSource()
})
}
// UseSource returns all the components that use source repositories
func (r ComponentReferences) UseSource() (refs ComponentReferences) {
return r.filter(func(ref ComponentReference) bool {
return ref.Input().Uses != nil
})
}
// ImageComponentRefs returns the list of component references to images
func (r ComponentReferences) ImageComponentRefs() (refs ComponentReferences) {
return r.filter(func(ref ComponentReference) bool {
if ref.Input().ScratchImage {
return true
}
return ref.Input() != nil && ref.Input().ResolvedMatch != nil && ref.Input().ResolvedMatch.IsImage()
})
}
// TemplateComponentRefs returns the list of component references to templates
func (r ComponentReferences) TemplateComponentRefs() (refs ComponentReferences) {
return r.filter(func(ref ComponentReference) bool {
return ref.Input() != nil && ref.Input().ResolvedMatch != nil && ref.Input().ResolvedMatch.IsTemplate()
})
}
// InstallableComponentRefs returns the list of component references to templates
func (r ComponentReferences) InstallableComponentRefs() (refs ComponentReferences) {
return r.filter(func(ref ComponentReference) bool {
return ref.Input() != nil && ref.Input().ResolvedMatch != nil && ref.Input().ResolvedMatch.GeneratorInput.Job
})
}
func (r ComponentReferences) String() string {
return r.HumanString(",")
}
func (r ComponentReferences) HumanString(separator string) string {
components := []string{}
for _, ref := range r {
components = append(components, ref.Input().Value)
}
return strings.Join(components, separator)
}
// 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
}
// Resolve the references to ensure they are all valid, and identify any images that don't match user input.
func (components ComponentReferences) Resolve() error {
errs := []error{}
for _, ref := range components {
if err := ref.Resolve(); err != nil {
errs = append(errs, err)
continue
}
}
return errors.NewAggregate(errs)
}
// Search searches on all references
func (components ComponentReferences) Search() error {
errs := []error{}
for _, ref := range components {
if err := ref.Search(); err != nil {
errs = append(errs, err)
continue
}
}
return errors.NewAggregate(errs)
}
// GeneratorJobReference is a reference that should be treated as a job execution,
// not a direct app creation.
type GeneratorJobReference struct {
Ref ComponentReference
Input GeneratorInput
Err error
}
// ReferenceBuilder is used for building all the necessary object references
// for an application
type ReferenceBuilder struct {
refs ComponentReferences
repos SourceRepositories
errs []error
groupID int
}
// AddComponents turns all provided component inputs into component references
func (r *ReferenceBuilder) AddComponents(inputs []string, fn func(*ComponentInput) ComponentReference) ComponentReferences {
refs := ComponentReferences{}
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, generate.StrategySource)
if !ok {
continue
}
input.Use(repository)
repository.UsedBy(ref)
}
refs = append(refs, ref)
}
r.groupID++
}
r.refs = append(r.refs, refs...)
return refs
}
// 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, strategy generate.Strategy) (*SourceRepository, bool) {
for _, existing := range r.repos {
if input == existing.location {
return existing, true
}
}
source, err := NewSourceRepository(input, strategy)
if err != nil {
r.errs = append(r.errs, err)
return nil, false
}
r.repos = append(r.repos, source)
return source, true
}
func (r *ReferenceBuilder) AddExistingSourceRepository(source *SourceRepository) {
r.repos = append(r.repos, source)
}
// Result returns the result of the config conversion to object references
func (r *ReferenceBuilder) Result() (ComponentReferences, SourceRepositories, []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
ScratchImage bool
Uses *SourceRepository
ResolvedMatch *ComponentMatch
SearchMatches ComponentMatches
Resolver
Searcher
}
// 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 unique 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.ResolvedMatch = match
return nil
}
// Search sets the search matches in input
func (i *ComponentInput) Search() error {
if i.Searcher == nil {
return ErrNoMatch{Value: i.Value, Qualifier: "no searcher defined"}
}
matches, err := i.Searcher.Search(false, i.Value)
if matches != nil {
i.SearchMatches = matches
}
return errors.NewAggregate(err)
}
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
}