-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
newapp.go
1156 lines (1033 loc) · 37.8 KB
/
newapp.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cmd
import (
"fmt"
"io"
"reflect"
"strings"
"time"
"github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
kerrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/api/validation"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/errors"
authapi "github.com/openshift/origin/pkg/authorization/api"
buildapi "github.com/openshift/origin/pkg/build/api"
buildutil "github.com/openshift/origin/pkg/build/util"
"github.com/openshift/origin/pkg/client"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/dockerregistry"
"github.com/openshift/origin/pkg/generate/app"
"github.com/openshift/origin/pkg/generate/dockerfile"
"github.com/openshift/origin/pkg/generate/source"
imageapi "github.com/openshift/origin/pkg/image/api"
"github.com/openshift/origin/pkg/template"
outil "github.com/openshift/origin/pkg/util"
dockerfileutil "github.com/openshift/origin/pkg/util/docker/dockerfile"
)
const (
GeneratedByNamespace = "openshift.io/generated-by"
GeneratedForJob = "openshift.io/generated-job"
GeneratedForJobFor = "openshift.io/generated-job.for"
GeneratedByNewApp = "OpenShiftNewApp"
GeneratedByNewBuild = "OpenShiftNewBuild"
)
// ErrNoDockerfileDetected is the error returned when the requested build strategy is Docker
// and no Dockerfile is detected in the repository.
var ErrNoDockerfileDetected = fmt.Errorf("No Dockerfile was found in the repository and the requested build strategy is 'docker'")
// AppConfig contains all the necessary configuration for an application
type AppConfig struct {
SourceRepositories []string
ContextDir string
Components []string
ImageStreams []string
DockerImages []string
Templates []string
TemplateFiles []string
TemplateParameters []string
Groups []string
Environment []string
Labels map[string]string
AddEnvironmentToBuild bool
Dockerfile string
Name string
To string
Strategy string
InsecureRegistry bool
OutputDocker bool
NoOutput bool
ExpectToBuild bool
BinaryBuild bool
AllowMissingImages bool
Deploy bool
AsTestDeployment bool
SourceImage string
SourceImagePath string
SkipGeneration bool
AllowGenerationErrors bool
AllowSecretUse bool
SecretAccessor app.SecretAccessor
Secrets []string
AsSearch bool
AsList bool
DryRun bool
Out io.Writer
ErrOut io.Writer
KubeClient kclient.Interface
RefBuilder *app.ReferenceBuilder
DockerSearcher app.Searcher
ImageStreamSearcher app.Searcher
ImageStreamByAnnotationSearcher app.Searcher
TemplateSearcher app.Searcher
TemplateFileSearcher app.Searcher
Detector app.Detector
Typer runtime.ObjectTyper
Mapper meta.RESTMapper
ClientMapper resource.ClientMapper
OSClient client.Interface
OriginNamespace string
}
// UsageError is an interface for printing usage errors
type UsageError interface {
UsageError(commandName string) string
}
// TODO: replace with upstream converting [1]error to error
type errlist interface {
Errors() []error
}
type ErrRequiresExplicitAccess struct {
Match app.ComponentMatch
Input app.GeneratorInput
}
func (e ErrRequiresExplicitAccess) Error() string {
return fmt.Sprintf("the component %q is requesting access to run with your security credentials and install components - you must explicitly grant that access to continue", e.Match.String())
}
// ErrNoInputs is returned when no inputs are specified
var ErrNoInputs = fmt.Errorf("no inputs provided")
// AppResult contains the results of an application
type AppResult struct {
List *kapi.List
Name string
HasSource bool
Namespace string
GeneratedJobs bool
}
// QueryResult contains the results of a query (search or list)
type QueryResult struct {
Matches app.ComponentMatches
List *kapi.List
}
// NewAppConfig returns a new AppConfig, but you must set your typer, mapper, and clientMapper after the command has been run
// and flags have been parsed.
func NewAppConfig() *AppConfig {
return &AppConfig{
Detector: app.SourceRepositoryEnumerator{
Detectors: source.DefaultDetectors,
Tester: dockerfile.NewTester(),
},
RefBuilder: &app.ReferenceBuilder{},
}
}
func (c *AppConfig) DockerRegistrySearcher() app.Searcher {
return app.DockerRegistrySearcher{
Client: dockerregistry.NewClient(30*time.Second, true),
AllowInsecure: c.InsecureRegistry,
}
}
func (c *AppConfig) ensureDockerSearch() {
if c.DockerSearcher == nil {
c.DockerSearcher = c.DockerRegistrySearcher()
}
}
// SetOpenShiftClient sets the passed OpenShift client in the application configuration
func (c *AppConfig) SetOpenShiftClient(osclient client.Interface, OriginNamespace string, dockerclient *docker.Client) {
c.OSClient = osclient
c.OriginNamespace = OriginNamespace
namespaces := []string{OriginNamespace}
if openshiftNamespace := "openshift"; OriginNamespace != openshiftNamespace {
namespaces = append(namespaces, openshiftNamespace)
}
c.ImageStreamSearcher = app.ImageStreamSearcher{
Client: osclient,
ImageStreamImages: osclient,
Namespaces: namespaces,
}
c.ImageStreamByAnnotationSearcher = app.NewImageStreamByAnnotationSearcher(osclient, osclient, namespaces)
c.TemplateSearcher = app.TemplateSearcher{
Client: osclient,
TemplateConfigsNamespacer: osclient,
Namespaces: namespaces,
}
c.TemplateFileSearcher = &app.TemplateFileSearcher{
Typer: c.Typer,
Mapper: c.Mapper,
ClientMapper: c.ClientMapper,
Namespace: OriginNamespace,
}
// the hierarchy of docker searching is:
// 1) if we have an openshift client - query docker registries via openshift,
// if we're unable to query via openshift, query the docker registries directly(fallback),
// if we don't find a match there and a local docker daemon exists, look in the local registry.
// 2) if we don't have an openshift client - query the docker registries directly,
// if we don't find a match there and a local docker daemon exists, look in the local registry.
c.DockerSearcher = app.DockerClientSearcher{
Client: dockerclient,
Insecure: c.InsecureRegistry,
AllowMissingImages: c.AllowMissingImages,
RegistrySearcher: app.ImageImportSearcher{
Client: osclient.ImageStreams(OriginNamespace),
AllowInsecure: c.InsecureRegistry,
Fallback: c.DockerRegistrySearcher(),
},
}
}
// AddArguments converts command line arguments into the appropriate bucket based on what they look like
func (c *AppConfig) AddArguments(args []string) []string {
unknown := []string{}
for _, s := range args {
switch {
case cmdutil.IsEnvironmentArgument(s):
c.Environment = append(c.Environment, s)
case app.IsPossibleSourceRepository(s):
c.SourceRepositories = append(c.SourceRepositories, s)
case app.IsComponentReference(s):
c.Components = append(c.Components, s)
case app.IsPossibleTemplateFile(s):
c.Components = append(c.Components, s)
default:
if len(s) == 0 {
break
}
unknown = append(unknown, s)
}
}
return unknown
}
// individualSourceRepositories collects the list of SourceRepositories specified in the
// command line that are not associated with a builder using a '~'.
func (c *AppConfig) individualSourceRepositories() (app.SourceRepositories, error) {
for _, s := range c.SourceRepositories {
if repo, ok := c.RefBuilder.AddSourceRepository(s); ok {
repo.SetContextDir(c.ContextDir)
if c.Strategy == "docker" {
repo.BuildWithDocker()
}
}
}
if len(c.Dockerfile) > 0 {
if err := c.addDockerfile(); err != nil {
return nil, err
}
}
_, repos, errs := c.RefBuilder.Result()
return repos, errors.NewAggregate(errs)
}
// addDockerfile adds a Dockerfile passed in the command line to the reference
// builder.
func (c *AppConfig) addDockerfile() error {
if len(c.Strategy) != 0 && c.Strategy != "docker" {
return fmt.Errorf("when directly referencing a Dockerfile, the strategy must must be 'docker'")
}
_, repos, errs := c.RefBuilder.Result()
if err := errors.NewAggregate(errs); err != nil {
return err
}
switch len(repos) {
case 0:
// Create a new SourceRepository with the Dockerfile.
repo, err := app.NewSourceRepositoryForDockerfile(c.Dockerfile)
if err != nil {
return fmt.Errorf("provided Dockerfile is not valid: %v", err)
}
c.RefBuilder.AddExistingSourceRepository(repo)
case 1:
// Add the Dockerfile to the existing SourceRepository, so that
// eventually we generate a single BuildConfig with multiple
// sources.
if err := repos[0].AddDockerfile(c.Dockerfile); err != nil {
return fmt.Errorf("provided Dockerfile is not valid: %v", err)
}
default:
// Invalid.
return fmt.Errorf("--dockerfile cannot be used with multiple source repositories")
}
return nil
}
// set up the components to be used by the reference builder
func (c *AppConfig) addReferenceBuilderComponents(b *app.ReferenceBuilder) {
b.AddComponents(c.DockerImages, func(input *app.ComponentInput) app.ComponentReference {
input.Argument = fmt.Sprintf("--docker-image=%q", input.From)
input.Searcher = c.DockerSearcher
if c.DockerSearcher != nil {
resolver := app.PerfectMatchWeightedResolver{}
resolver = append(resolver, app.WeightedResolver{Searcher: c.DockerSearcher, Weight: 0.0})
if c.AllowMissingImages {
resolver = append(resolver, app.WeightedResolver{Searcher: app.MissingImageSearcher{}, Weight: 100.0})
}
input.Resolver = resolver
}
return input
})
b.AddComponents(c.ImageStreams, func(input *app.ComponentInput) app.ComponentReference {
input.Argument = fmt.Sprintf("--image-stream=%q", input.From)
input.Searcher = c.ImageStreamSearcher
if c.ImageStreamSearcher != nil {
input.Resolver = app.FirstMatchResolver{Searcher: c.ImageStreamSearcher}
}
return input
})
b.AddComponents(c.Templates, func(input *app.ComponentInput) app.ComponentReference {
input.Argument = fmt.Sprintf("--template=%q", input.From)
input.Searcher = c.TemplateSearcher
if c.TemplateSearcher != nil {
input.Resolver = app.HighestScoreResolver{Searcher: c.TemplateSearcher}
}
return input
})
b.AddComponents(c.TemplateFiles, func(input *app.ComponentInput) app.ComponentReference {
input.Argument = fmt.Sprintf("--file=%q", input.From)
input.Searcher = c.TemplateFileSearcher
if c.TemplateFileSearcher != nil {
input.Resolver = app.FirstMatchResolver{Searcher: c.TemplateFileSearcher}
}
return input
})
b.AddComponents(c.Components, func(input *app.ComponentInput) app.ComponentReference {
resolver := app.PerfectMatchWeightedResolver{}
searcher := app.MultiWeightedSearcher{}
if c.ImageStreamSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.ImageStreamSearcher, Weight: 0.0})
searcher = append(searcher, app.WeightedSearcher{Searcher: c.ImageStreamSearcher, Weight: 0.0})
}
if c.TemplateSearcher != nil && !input.ExpectToBuild {
resolver = append(resolver, app.WeightedResolver{Searcher: c.TemplateSearcher, Weight: 0.0})
searcher = append(searcher, app.WeightedSearcher{Searcher: c.TemplateSearcher, Weight: 0.0})
}
if c.TemplateFileSearcher != nil && !input.ExpectToBuild {
resolver = append(resolver, app.WeightedResolver{Searcher: c.TemplateFileSearcher, Weight: 0.0})
}
if c.DockerSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.DockerSearcher, Weight: 2.0})
searcher = append(searcher, app.WeightedSearcher{Searcher: c.DockerSearcher, Weight: 1.0})
}
if c.AllowMissingImages {
resolver = append(resolver, app.WeightedResolver{Searcher: app.MissingImageSearcher{}, Weight: 100.0})
}
input.Resolver = resolver
input.Searcher = searcher
return input
})
_, repos, _ := b.Result()
for _, repo := range repos {
repo.SetContextDir(c.ContextDir)
}
}
// validate converts all of the arguments on the config into references to objects, or returns an error
func (c *AppConfig) validate() (app.ComponentReferences, app.SourceRepositories, cmdutil.Environment, cmdutil.Environment, error) {
b := c.RefBuilder
c.addReferenceBuilderComponents(b)
b.AddGroups(c.Groups)
refs, repos, errs := b.Result()
if len(c.Strategy) != 0 && len(repos) == 0 {
errs = append(errs, fmt.Errorf("when --strategy is specified you must provide at least one source code location"))
}
if c.BinaryBuild && (len(repos) > 0 || refs.HasSource()) {
errs = append(errs, fmt.Errorf("specifying binary builds and source repositories at the same time is not allowed"))
}
env, duplicateEnv, envErrs := cmdutil.ParseEnvironmentArguments(c.Environment)
for _, s := range duplicateEnv {
glog.V(1).Infof("The environment variable %q was overwritten", s)
}
errs = append(errs, envErrs...)
parms, duplicateParms, parmsErrs := cmdutil.ParseEnvironmentArguments(c.TemplateParameters)
for _, s := range duplicateParms {
glog.V(1).Infof("The template parameter %q was overwritten", s)
}
errs = append(errs, parmsErrs...)
return refs, repos, env, parms, errors.NewAggregate(errs)
}
// componentsForRepos creates components for repositories that have not been previously associated by a builder
// these components have already gone through source code detection and have a SourceRepositoryInfo attached to them
func (c *AppConfig) componentsForRepos(repositories app.SourceRepositories) (app.ComponentReferences, error) {
b := c.RefBuilder
errs := []error{}
result := app.ComponentReferences{}
for _, repo := range repositories {
info := repo.Info()
switch {
case info == nil:
errs = append(errs, fmt.Errorf("source not detected for repository %q", repo))
continue
case info.Dockerfile != nil && (len(c.Strategy) == 0 || c.Strategy == "docker"):
node := info.Dockerfile.AST()
baseImage := dockerfileutil.LastBaseImage(node)
if baseImage == "" {
errs = append(errs, fmt.Errorf("the Dockerfile in the repository %q has no FROM instruction", info.Path))
continue
}
refs := b.AddComponents([]string{baseImage}, func(input *app.ComponentInput) app.ComponentReference {
resolver := app.PerfectMatchWeightedResolver{}
if c.ImageStreamSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.ImageStreamSearcher, Weight: 0.0})
}
if c.DockerSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.DockerSearcher, Weight: 1.0})
}
if c.AllowMissingImages {
resolver = append(resolver, app.WeightedResolver{Searcher: &app.MissingImageSearcher{}, Weight: 100.0})
}
input.Resolver = resolver
input.Use(repo)
input.ExpectToBuild = true
repo.UsedBy(input)
repo.BuildWithDocker()
return input
})
result = append(result, refs...)
default:
// TODO: Add support for searching for more than one language if len(info.Types) > 1
if len(info.Types) == 0 {
errs = append(errs, fmt.Errorf("no language was detected for repository at %q; please specify a builder image to use with your repository: [builder-image]~%s", repo, repo))
continue
}
refs := b.AddComponents([]string{info.Types[0].Term()}, func(input *app.ComponentInput) app.ComponentReference {
resolver := app.PerfectMatchWeightedResolver{}
if c.ImageStreamSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.ImageStreamSearcher, Weight: 0.0})
}
if c.ImageStreamByAnnotationSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.ImageStreamByAnnotationSearcher, Weight: 1.0})
}
if c.DockerSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.DockerSearcher, Weight: 2.0})
}
input.Resolver = resolver
input.ExpectToBuild = true
input.Use(repo)
repo.UsedBy(input)
return input
})
result = append(result, refs...)
}
}
return result, errors.NewAggregate(errs)
}
// Resolve the references to ensure they are all valid, and identify any images that don't match user input.
func Resolve(components app.ComponentReferences) 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 Search(components app.ComponentReferences) error {
errs := []error{}
for _, ref := range components {
if err := ref.Search(); err != nil {
errs = append(errs, err)
continue
}
}
return errors.NewAggregate(errs)
}
func (c *AppConfig) detectPartialMatches(components app.ComponentReferences) error {
errs := []error{}
for _, ref := range components {
input := ref.Input()
if input.ResolvedMatch.Score != 0.0 {
errs = append(errs, fmt.Errorf("component %q had only a partial match of %q - if this is the value you want to use, specify it explicitly", input.From, input.ResolvedMatch.Name))
}
}
return errors.NewAggregate(errs)
}
// inferBuildTypes infers build status and mismatches between source and docker builders
func (c *AppConfig) inferBuildTypes(components app.ComponentReferences) (app.ComponentReferences, error) {
errs := []error{}
for _, ref := range components {
input := ref.Input()
// identify whether the input is a builder and whether generation is requested
input.ResolvedMatch.Builder = app.IsBuilderMatch(input.ResolvedMatch)
generatorInput, err := app.GeneratorInputFromMatch(input.ResolvedMatch)
if err != nil && !c.AllowGenerationErrors {
errs = append(errs, err)
continue
}
input.ResolvedMatch.GeneratorInput = generatorInput
// if the strategy is explicitly Docker, all repos should assume docker
if c.Strategy == "docker" && input.Uses != nil {
input.Uses.BuildWithDocker()
}
// if we are expecting build inputs, or get a build input when strategy is not docker, expect to build
if c.ExpectToBuild || (input.ResolvedMatch.Builder && c.Strategy != "docker") {
input.ExpectToBuild = true
}
switch {
case input.ExpectToBuild && input.ResolvedMatch.IsTemplate():
// TODO: harder - break the template pieces and check if source code can be attached (look for a build config, build image, etc)
errs = append(errs, fmt.Errorf("template with source code explicitly attached is not supported - you must either specify the template and source code separately or attach an image to the source code using the '[image]~[code]' form"))
continue
case input.ExpectToBuild && !input.ResolvedMatch.Builder && input.Uses != nil && !input.Uses.IsDockerBuild():
if len(c.Strategy) == 0 {
errs = append(errs, fmt.Errorf("the resolved match %q for component %q cannot build source code - check whether this is the image you want to use, then use --strategy=source to build using source or --strategy=docker to treat this as a Docker base image and set up a layered Docker build", input.ResolvedMatch.Name, ref))
continue
}
}
}
if len(components) == 0 && c.BinaryBuild {
if len(c.Name) == 0 {
return nil, fmt.Errorf("you must provide a --name when you don't specify a source repository or base image")
}
ref := &app.ComponentInput{
From: "--binary",
Argument: "--binary",
Value: c.Name,
ScratchImage: true,
ExpectToBuild: true,
}
components = append(components, ref)
}
return components, errors.NewAggregate(errs)
}
// ensureHasSource ensure every builder component has source code associated with it. It takes a list of component references
// that are builders and have not been associated with source, and a set of source repositories that have not been associated
// with a builder
func (c *AppConfig) ensureHasSource(components app.ComponentReferences, repositories app.SourceRepositories) error {
if len(components) > 0 {
switch {
case len(repositories) > 1:
if len(components) == 1 {
component := components[0]
suggestions := ""
for _, repo := range repositories {
suggestions += fmt.Sprintf("%s~%s\n", component, repo)
}
return fmt.Errorf("there are multiple code locations provided - use one of the following suggestions to declare which code goes with the image:\n%s", suggestions)
}
return fmt.Errorf("the following images require source code: %s\n"+
" and the following repositories are not used: %s\nUse '[image]~[repo]' to declare which code goes with which image", components, repositories)
case len(repositories) == 1:
glog.V(2).Infof("Using %q as the source for build", repositories[0])
for _, component := range components {
glog.V(2).Infof("Pairing with component %v", component)
component.Input().Use(repositories[0])
repositories[0].UsedBy(component)
}
default:
switch {
case c.BinaryBuild && c.ExpectToBuild:
// create new "fake" binary repos for any component that doesn't already have a repo
// TODO: source repository should possibly be refactored to be an interface or a type that better reflects
// the different types of inputs
for _, component := range components {
input := component.Input()
if input.Uses != nil {
continue
}
repo := app.NewBinarySourceRepository()
if c.Strategy == "docker" || len(c.Strategy) == 0 {
repo.BuildWithDocker()
}
input.Use(repo)
repo.UsedBy(input)
input.ExpectToBuild = true
}
case c.ExpectToBuild:
return fmt.Errorf("you must specify at least one source repository URL, provide a Dockerfile, or indicate you wish to use binary builds")
default:
for _, component := range components {
component.Input().ExpectToBuild = false
}
}
}
}
return nil
}
// DetectSource runs a code detector on the passed in repositories to obtain a SourceRepositoryInfo
func (c *AppConfig) DetectSource(repositories []*app.SourceRepository) error {
errs := []error{}
for _, repo := range repositories {
err := repo.Detect(c.Detector, c.Strategy == "docker")
if err != nil {
if c.Strategy == "docker" && err == app.ErrNoLanguageDetected {
errs = append(errs, ErrNoDockerfileDetected)
} else {
errs = append(errs, err)
}
continue
}
}
return errors.NewAggregate(errs)
}
func validateEnforcedName(name string) error {
if ok, _ := validation.ValidateServiceName(name, false); !ok {
return fmt.Errorf("invalid name: %s. Must be an a lower case alphanumeric (a-z, and 0-9) string with a maximum length of 24 characters, where the first character is a letter (a-z), and the '-' character is allowed anywhere except the first or last character.", name)
}
return nil
}
func validateOutputImageReference(ref string) error {
if _, err := imageapi.ParseDockerImageReference(ref); err != nil {
return fmt.Errorf("invalid output image reference: %s", ref)
}
return nil
}
// buildPipelines converts a set of resolved, valid references into pipelines.
func (c *AppConfig) buildPipelines(components app.ComponentReferences, environment app.Environment) (app.PipelineGroup, error) {
pipelines := app.PipelineGroup{}
pipelineBuilder := app.NewPipelineBuilder(c.Name, c.GetBuildEnvironment(environment), c.OutputDocker).To(c.To)
for _, group := range components.Group() {
glog.V(4).Infof("found group: %v", group)
common := app.PipelineGroup{}
for _, ref := range group {
refInput := ref.Input()
from := refInput.String()
var (
pipeline *app.Pipeline
err error
)
switch {
case refInput.ExpectToBuild:
glog.V(4).Infof("will add %q secrets into a build for a source build of %q", strings.Join(c.Secrets, ","), refInput.Uses)
if err := refInput.Uses.AddBuildSecrets(c.Secrets); err != nil {
return nil, fmt.Errorf("unable to add build secrets %q: %v", strings.Join(c.Secrets, ","), err)
}
glog.V(4).Infof("will use %q as the base image for a source build of %q", ref, refInput.Uses)
if pipeline, err = pipelineBuilder.NewBuildPipeline(from, refInput.ResolvedMatch, refInput.Uses); err != nil {
return nil, fmt.Errorf("can't build %q: %v", refInput.Uses, err)
}
default:
glog.V(4).Infof("will include %q", ref)
if pipeline, err = pipelineBuilder.NewImagePipeline(from, refInput.ResolvedMatch); err != nil {
return nil, fmt.Errorf("can't include %q: %v", refInput, err)
}
}
if c.Deploy {
if err := pipeline.NeedsDeployment(environment, c.Labels, c.AsTestDeployment); err != nil {
return nil, fmt.Errorf("can't set up a deployment for %q: %v", refInput, err)
}
}
if c.NoOutput {
pipeline.Build.Output = nil
}
common = append(common, pipeline)
if err := common.Reduce(); err != nil {
return nil, fmt.Errorf("can't create a pipeline from %s: %v", common, err)
}
describeBuildPipelineWithImage(c.Out, ref, pipeline, c.OriginNamespace)
}
pipelines = append(pipelines, common...)
}
return pipelines, nil
}
// buildTemplates converts a set of resolved, valid references into references to template objects.
func (c *AppConfig) buildTemplates(components app.ComponentReferences, environment app.Environment) ([]runtime.Object, error) {
objects := []runtime.Object{}
for _, ref := range components {
tpl := ref.Input().ResolvedMatch.Template
glog.V(4).Infof("processing template %s/%s", c.OriginNamespace, tpl.Name)
for _, env := range environment.List() {
// only set environment values that match what's expected by the template.
if v := template.GetParameterByName(tpl, env.Name); v != nil {
v.Value = env.Value
v.Generate = ""
template.AddParameter(tpl, *v)
} else {
return nil, fmt.Errorf("unexpected parameter name %q", env.Name)
}
}
result, err := c.OSClient.TemplateConfigs(c.OriginNamespace).Create(tpl)
if err != nil {
return nil, fmt.Errorf("error processing template %s/%s: %v", c.OriginNamespace, tpl.Name, err)
}
errs := runtime.DecodeList(result.Objects, kapi.Codecs.UniversalDecoder())
if len(errs) > 0 {
err = errors.NewAggregate(errs)
return nil, fmt.Errorf("error processing template %s/%s: %v", c.OriginNamespace, tpl.Name, errs)
}
objects = append(objects, result.Objects...)
describeGeneratedTemplate(c.Out, ref, result, c.OriginNamespace)
}
return objects, nil
}
// fakeSecretAccessor is used during dry runs of installation
type fakeSecretAccessor struct {
token string
}
func (a *fakeSecretAccessor) Token() (string, error) {
return a.token, nil
}
func (a *fakeSecretAccessor) CACert() (string, error) {
return "", nil
}
// installComponents attempts to create pods to run installable images identified by the user. If an image
// is installable, we check whether it requires access to the user token. If so, the caller must have
// explicitly granted that access (because the token may be the user's).
func (c *AppConfig) installComponents(components app.ComponentReferences, env app.Environment) ([]runtime.Object, string, error) {
if c.SkipGeneration {
return nil, "", nil
}
jobs := components.InstallableComponentRefs()
switch {
case len(jobs) > 1:
return nil, "", fmt.Errorf("only one installable component may be provided: %s", jobs.HumanString(", "))
case len(jobs) == 0:
return nil, "", nil
}
job := jobs[0]
if len(components) > 1 {
return nil, "", fmt.Errorf("%q is installable and may not be specified with other components", job.Input().Value)
}
input := job.Input()
imageRef, err := app.InputImageFromMatch(input.ResolvedMatch)
if err != nil {
return nil, "", fmt.Errorf("can't include %q: %v", input, err)
}
glog.V(4).Infof("Resolved match for installer %#v", input.ResolvedMatch)
imageRef.AsImageStream = false
imageRef.AsResolvedImage = true
imageRef.Env = env
name := c.Name
if len(name) == 0 {
var ok bool
name, ok = imageRef.SuggestName()
if !ok {
return nil, "", fmt.Errorf("can't suggest a valid name, please specify a name with --name")
}
}
imageRef.ObjectName = name
glog.V(4).Infof("Proposed installable image %#v", imageRef)
secretAccessor := c.SecretAccessor
generatorInput := input.ResolvedMatch.GeneratorInput
token := generatorInput.Token
if token != nil && !c.AllowSecretUse || secretAccessor == nil {
if !c.DryRun {
return nil, "", ErrRequiresExplicitAccess{Match: *input.ResolvedMatch, Input: generatorInput}
}
secretAccessor = &fakeSecretAccessor{token: "FAKE_TOKEN"}
}
objects := []runtime.Object{}
serviceAccountName := "installer"
if token != nil && token.ServiceAccount {
if _, err := c.KubeClient.ServiceAccounts(c.OriginNamespace).Get(serviceAccountName); err != nil {
if kerrors.IsNotFound(err) {
objects = append(objects,
// create a new service account
&kapi.ServiceAccount{ObjectMeta: kapi.ObjectMeta{Name: serviceAccountName}},
// grant the service account the edit role on the project (TODO: installer)
&authapi.RoleBinding{
ObjectMeta: kapi.ObjectMeta{Name: "installer-role-binding"},
Subjects: []kapi.ObjectReference{{Kind: "ServiceAccount", Name: serviceAccountName}},
RoleRef: kapi.ObjectReference{Name: "edit"},
},
)
}
}
}
pod, secret, err := imageRef.InstallablePod(generatorInput, secretAccessor, serviceAccountName)
if err != nil {
return nil, "", err
}
objects = append(objects, pod)
if secret != nil {
objects = append(objects, secret)
}
for i := range objects {
outil.AddObjectAnnotations(objects[i], map[string]string{
GeneratedForJob: "true",
GeneratedForJobFor: input.String(),
})
}
describeGeneratedJob(c.Out, job, pod, secret, c.OriginNamespace)
return objects, name, nil
}
// Run executes the provided config to generate objects.
func (c *AppConfig) Run() (*AppResult, error) {
return c.run(app.Acceptors{app.NewAcceptUnique(c.Typer), app.AcceptNew})
}
// RunQuery executes the provided config and returns the result of the resolution.
func (c *AppConfig) RunQuery() (*QueryResult, error) {
c.ensureDockerSearch()
_, err := c.individualSourceRepositories()
if err != nil {
return nil, err
}
if c.AsList {
if c.AsSearch {
return nil, fmt.Errorf("--list and --search can't be used together")
}
if c.HasArguments() {
return nil, fmt.Errorf("--list can't be used with arguments")
}
c.Components = append(c.Components, "*")
}
components, repositories, environment, parameters, err := c.validate()
if err != nil {
return nil, err
}
if len(components) == 0 && !c.AsList {
return nil, ErrNoInputs
}
errs := []error{}
if len(repositories) > 0 {
errs = append(errs, fmt.Errorf("--search can't be used with source code"))
}
if len(environment) > 0 {
errs = append(errs, fmt.Errorf("--search can't be used with --env"))
}
if len(parameters) > 0 {
errs = append(errs, fmt.Errorf("--search can't be used with --param"))
}
if len(errs) > 0 {
return nil, errors.NewAggregate(errs)
}
if err := Search(components); err != nil {
return nil, err
}
glog.V(4).Infof("Code %v", repositories)
glog.V(4).Infof("Components %v", components)
matches := app.ComponentMatches{}
objects := app.Objects{}
for _, ref := range components {
for _, match := range ref.Input().SearchMatches {
matches = append(matches, match)
if match.IsTemplate() {
objects = append(objects, match.Template)
} else if match.IsImage() {
if match.ImageStream != nil {
objects = append(objects, match.ImageStream)
}
if match.Image != nil {
objects = append(objects, match.Image)
}
}
}
}
return &QueryResult{
Matches: matches,
List: &kapi.List{Items: objects},
}, nil
}
func (c *AppConfig) addImageSource(sourceRepos app.SourceRepositories) (app.ComponentReference, app.SourceRepositories, error) {
if len(c.SourceImage) == 0 {
return nil, sourceRepos, nil
}
paths := strings.SplitN(c.SourceImagePath, ":", 2)
var sourcePath, destPath string
switch len(paths) {
case 1:
sourcePath = paths[0]
case 2:
sourcePath = paths[0]
destPath = paths[1]
}
compRef, _, err := app.NewComponentInput(c.SourceImage)
if err != nil {
return nil, nil, err
}
resolver := app.PerfectMatchWeightedResolver{}
if c.ImageStreamByAnnotationSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.ImageStreamByAnnotationSearcher, Weight: 0.0})
}
if c.ImageStreamSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.ImageStreamSearcher, Weight: 1.0})
}
if c.DockerSearcher != nil {
resolver = append(resolver, app.WeightedResolver{Searcher: c.DockerSearcher, Weight: 2.0})
}
compRef.Resolver = resolver
switch len(sourceRepos) {
case 0:
sourceRepos = append(sourceRepos, app.NewImageSourceRepository(compRef, sourcePath, destPath))
case 1:
sourceRepos[0].SetSourceImage(compRef)
sourceRepos[0].SetSourceImagePath(sourcePath, destPath)
default:
return nil, nil, fmt.Errorf("--image-source cannot be used with multiple source repositories")
}
return compRef, sourceRepos, nil
}
// run executes the provided config applying provided acceptors.
func (c *AppConfig) run(acceptors app.Acceptors) (*AppResult, error) {
c.ensureDockerSearch()
repositories, err := c.individualSourceRepositories()
if err != nil {
return nil, err
}
err = c.DetectSource(repositories)
if err != nil {
return nil, err
}
components, repositories, environment, parameters, err := c.validate()
if err != nil {
return nil, err
}
var imageComp app.ComponentReference
imageComp, repositories, err = c.addImageSource(repositories)
if err != nil {
return nil, err
}
componentsIncludingImageComps := components
if imageComp != nil {
componentsIncludingImageComps = append(components, imageComp)
}
if err := Resolve(componentsIncludingImageComps); err != nil {
return nil, err
}
err = c.detectPartialMatches(componentsIncludingImageComps)
if err != nil {
return nil, err
}
components, err = c.inferBuildTypes(components)
if err != nil {
return nil, err
}
// Couple source with resolved builder components if possible
if err := c.ensureHasSource(components.NeedsSource(), repositories.NotUsed()); err != nil {
return nil, err
}
// For source repos that are not yet coupled with a component, create components
sourceComponents, err := c.componentsForRepos(repositories.NotUsed())
if err != nil {