-
Notifications
You must be signed in to change notification settings - Fork 786
/
common.go
885 lines (783 loc) · 24.9 KB
/
common.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
package cmd
import (
"fmt"
"io"
"net/url"
"os"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/expose"
"github.com/jenkins-x/jx/pkg/certmanager"
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
vaultoperatorclient "github.com/banzaicloud/bank-vaults/operator/pkg/client/clientset/versioned"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/log"
buildclient "github.com/knative/build/pkg/client/clientset/versioned"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/config"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/table"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
gitcfg "gopkg.in/src-d/go-git.v4/config"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
optionServerName = "name"
optionServerURL = "url"
optionBatchMode = "batch-mode"
optionVerbose = "verbose"
optionLogLevel = "log-level"
optionHeadless = "headless"
optionNoBrew = "no-brew"
optionInstallDeps = "install-dependencies"
optionSkipAuthSecMerge = "skip-auth-secrets-merge"
optionPullSecrets = "pull-secrets"
)
// ModifyDevEnvironmentFn a callback to create/update the development Environment
type ModifyDevEnvironmentFn func(callback func(env *jenkinsv1.Environment) error) error
// ModifyEnvironmentFn a callback to create/update an Environment
type ModifyEnvironmentFn func(name string, callback func(env *jenkinsv1.Environment) error) error
// CommonOptions contains common options and helper methods
type CommonOptions struct {
Factory Factory
In terminal.FileReader
Out terminal.FileWriter
Err io.Writer
Cmd *cobra.Command
Args []string
BatchMode bool
Verbose bool
LogLevel string
Headless bool
NoBrew bool
InstallDependencies bool
SkipAuthSecretsMerge bool
ServiceAccount string
Username string
ExternalJenkinsBaseURL string
PullSecrets string
// common cached clients
KubeClientCached kubernetes.Interface
apiExtensionsClient apiextensionsclientset.Interface
currentNamespace string
devNamespace string
jxClient versioned.Interface
knbClient buildclient.Interface
jenkinsClient gojenkins.JenkinsClient
GitClient gits.Gitter
helm helm.Helmer
Kuber kube.Kuber
vaultOperatorClient vaultoperatorclient.Interface
modifyDevEnvironmentFn ModifyDevEnvironmentFn
modifyEnvironmentFn ModifyEnvironmentFn
Prow
}
type ServerFlags struct {
ServerName string
ServerURL string
}
// IsEmpty returns true if the server flags and server URL are tempry
func (f *ServerFlags) IsEmpty() bool {
return f.ServerName == "" && f.ServerURL == ""
}
// CreateTable creates a new Table
func (o *CommonOptions) CreateTable() table.Table {
return o.Factory.CreateTable(o.Out)
}
// NewCommonOptions a helper method to create a new CommonOptions instance
// pre configured in a specific devNamespace
func NewCommonOptions(devNamespace string, factory Factory) CommonOptions {
return CommonOptions{
Factory: factory,
Out: os.Stdout,
Err: os.Stderr,
currentNamespace: devNamespace,
devNamespace: devNamespace,
}
}
// SetDevNamespace configures the current dev namespace
func (o *CommonOptions) SetDevNamespace(ns string) {
o.devNamespace = ns
o.currentNamespace = ns
o.KubeClientCached = nil
}
// Debugf outputs the given text to the console if verbose mode is enabled
func (o *CommonOptions) Debugf(format string, a ...interface{}) {
if o.Verbose {
log.Infof(format, a...)
}
}
func (options *CommonOptions) addCommonFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&options.BatchMode, optionBatchMode, "b", false, "In batch mode the command never prompts for user input")
cmd.Flags().BoolVarP(&options.Verbose, optionVerbose, "", false, "Enable verbose logging")
cmd.Flags().StringVarP(&options.LogLevel, optionLogLevel, "", logrus.InfoLevel.String(), "Logging level. Possible values - panic, fatal, error, warning, info, debug.")
cmd.Flags().BoolVarP(&options.Headless, optionHeadless, "", false, "Enable headless operation if using browser automation")
cmd.Flags().BoolVarP(&options.NoBrew, optionNoBrew, "", false, "Disables the use of brew on macOS to install or upgrade command line dependencies")
cmd.Flags().BoolVarP(&options.InstallDependencies, optionInstallDeps, "", false, "Should any required dependencies be installed automatically")
cmd.Flags().BoolVarP(&options.SkipAuthSecretsMerge, optionSkipAuthSecMerge, "", false, "Skips merging a local git auth yaml file with any pipeline secrets that are found")
cmd.Flags().StringVarP(&options.PullSecrets, optionPullSecrets, "", "", "The pull secrets the service account created should have (useful when deploying to your own private registry): provide multiple pull secrets by providing them in a singular block of quotes e.g. --pull-secrets \"foo, bar, baz\"")
options.Cmd = cmd
}
func (o *CommonOptions) CreateApiExtensionsClient() (apiextensionsclientset.Interface, error) {
var err error
if o.apiExtensionsClient == nil {
o.apiExtensionsClient, err = o.Factory.CreateApiExtensionsClient()
if err != nil {
return nil, err
}
}
return o.apiExtensionsClient, nil
}
func (o *CommonOptions) KubeClient() (kubernetes.Interface, string, error) {
if o.KubeClientCached == nil {
kubeClient, currentNs, err := o.Factory.CreateKubeClient()
if err != nil {
return nil, "", err
}
o.KubeClientCached = kubeClient
o.currentNamespace = currentNs
}
return o.KubeClientCached, o.currentNamespace, nil
}
// KubeClientAndDevNamespace returns a kube client and the development namespace
func (o *CommonOptions) KubeClientAndDevNamespace() (kubernetes.Interface, string, error) {
kubeClient, curNs, err := o.KubeClient()
if err != nil {
return nil, "", err
}
if o.devNamespace == "" {
o.devNamespace, _, err = kube.GetDevNamespace(kubeClient, curNs)
}
return kubeClient, o.devNamespace, err
}
func (o *CommonOptions) JXClient() (versioned.Interface, string, error) {
if o.Factory == nil {
return nil, "", errors.New("command factory is not initialized")
}
if o.jxClient == nil {
jxClient, ns, err := o.Factory.CreateJXClient()
if err != nil {
return nil, ns, err
}
o.jxClient = jxClient
if o.currentNamespace == "" {
o.currentNamespace = ns
}
}
return o.jxClient, o.currentNamespace, nil
}
func (o *CommonOptions) KnativeBuildClient() (buildclient.Interface, string, error) {
if o.Factory == nil {
return nil, "", errors.New("command factory is not initialized")
}
if o.knbClient == nil {
knbClient, ns, err := o.Factory.CreateKnativeBuildClient()
if err != nil {
return nil, ns, err
}
o.knbClient = knbClient
if o.currentNamespace == "" {
o.currentNamespace = ns
}
}
return o.knbClient, o.currentNamespace, nil
}
func (o *CommonOptions) JXClientAndAdminNamespace() (versioned.Interface, string, error) {
kubeClient, _, err := o.KubeClient()
if err != nil {
return nil, "", err
}
jxClient, devNs, err := o.JXClientAndDevNamespace()
if err != nil {
return nil, "", err
}
ns, err := kube.GetAdminNamespace(kubeClient, devNs)
return jxClient, ns, err
}
func (o *CommonOptions) JXClientAndDevNamespace() (versioned.Interface, string, error) {
if o.jxClient == nil {
jxClient, ns, err := o.JXClient()
if err != nil {
return nil, ns, err
}
o.jxClient = jxClient
if o.currentNamespace == "" {
o.currentNamespace = ns
}
}
if o.devNamespace == "" {
client, ns, err := o.KubeClient()
if err != nil {
return nil, "", err
}
devNs, _, err := kube.GetDevNamespace(client, ns)
if err != nil {
return nil, "", err
}
o.devNamespace = devNs
}
return o.jxClient, o.devNamespace, nil
}
// SetJenkinsClient sets the JenkinsClient - usually used in testing
func (o *CommonOptions) SetJenkinsClient(jenkinsClient gojenkins.JenkinsClient) {
o.jenkinsClient = jenkinsClient
}
func (o *CommonOptions) JenkinsClient() (gojenkins.JenkinsClient, error) {
if o.jenkinsClient == nil {
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, err
}
o.Factory.SetBatch(o.BatchMode)
jenkins, err := o.Factory.CreateJenkinsClient(kubeClient, ns, o.In, o.Out, o.Err)
if err != nil {
return nil, err
}
o.jenkinsClient = jenkins
}
return o.jenkinsClient, nil
}
func (o *CommonOptions) GetJenkinsURL() (string, error) {
kubeClient, ns, err := o.KubeClient()
if err != nil {
return "", err
}
return o.Factory.GetJenkinsURL(kubeClient, ns)
}
func (o *CommonOptions) Git() gits.Gitter {
if o.GitClient == nil {
o.GitClient = gits.NewGitCLI()
}
return o.GitClient
}
func (o *CommonOptions) Helm() helm.Helmer {
if o.helm == nil {
helmBinary, noTiller, helmTemplate, _ := o.TeamHelmBin()
o.helm = o.Factory.GetHelm(o.Verbose, helmBinary, noTiller, helmTemplate)
}
return o.helm
}
// SetHelm sets the helmer used for this object
func (o *CommonOptions) SetHelm(helmer helm.Helmer) {
o.helm = helmer
}
func (o *CommonOptions) Kube() kube.Kuber {
if o.Kuber == nil {
o.Kuber = kube.NewKubeConfig()
}
return o.Kuber
}
func (o *CommonOptions) TeamAndEnvironmentNames() (string, string, error) {
kubeClient, currentNs, err := o.KubeClient()
if err != nil {
return "", "", err
}
return kube.GetDevNamespace(kubeClient, currentNs)
}
func (o *CommonOptions) GetImagePullSecrets() []string {
pullSecrets := strings.Fields(o.PullSecrets)
return pullSecrets
}
func (o *ServerFlags) addGitServerFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.ServerName, optionServerName, "n", "", "The name of the Git server to add a user")
cmd.Flags().StringVarP(&o.ServerURL, optionServerURL, "u", "", "The URL of the Git server to add a user")
}
// findGitServer finds the Git server from the given flags or returns an error
func (o *CommonOptions) findGitServer(config *auth.AuthConfig, serverFlags *ServerFlags) (*auth.AuthServer, error) {
return o.findServer(config, serverFlags, "git", "Try creating one via: jx create git server", false)
}
// findIssueTrackerServer finds the issue tracker server from the given flags or returns an error
func (o *CommonOptions) findIssueTrackerServer(config *auth.AuthConfig, serverFlags *ServerFlags) (*auth.AuthServer, error) {
return o.findServer(config, serverFlags, "issues", "Try creating one via: jx create tracker server", false)
}
// findChatServer finds the chat server from the given flags or returns an error
func (o *CommonOptions) findChatServer(config *auth.AuthConfig, serverFlags *ServerFlags) (*auth.AuthServer, error) {
return o.findServer(config, serverFlags, "chat", "Try creating one via: jx create chat server", false)
}
// findAddonServer finds the addon server from the given flags or returns an error
func (o *CommonOptions) findAddonServer(config *auth.AuthConfig, serverFlags *ServerFlags, kind string) (*auth.AuthServer, error) {
return o.findServer(config, serverFlags, kind, "Try creating one via: jx create addon", true)
}
func (o *CommonOptions) findServer(config *auth.AuthConfig, serverFlags *ServerFlags, defaultKind string, missingServerDescription string, lazyCreate bool) (*auth.AuthServer, error) {
kind := defaultKind
var server *auth.AuthServer
if serverFlags.ServerURL != "" {
server = config.GetServer(serverFlags.ServerURL)
if server == nil {
if lazyCreate {
return config.GetOrCreateServerName(serverFlags.ServerURL, serverFlags.ServerName, kind), nil
}
return nil, util.InvalidOption(optionServerURL, serverFlags.ServerURL, config.GetServerURLs())
}
}
if server == nil && serverFlags.ServerName != "" {
name := serverFlags.ServerName
if lazyCreate {
server = config.GetOrCreateServerName(serverFlags.ServerURL, name, kind)
} else {
server = config.GetServerByName(name)
}
if server == nil {
return nil, util.InvalidOption(optionServerName, name, config.GetServerNames())
}
}
if server == nil {
name := config.CurrentServer
if name != "" && o.BatchMode {
server = config.GetServerByName(name)
if server == nil {
log.Warnf("Current server %s no longer exists\n", name)
}
}
}
if server == nil && len(config.Servers) == 1 {
server = config.Servers[0]
}
if server == nil && len(config.Servers) > 1 {
if o.BatchMode {
return nil, fmt.Errorf("Multiple servers found. Please specify one via the %s option", optionServerName)
}
defaultServerName := ""
if config.CurrentServer != "" {
s := config.GetServer(config.CurrentServer)
if s != nil {
defaultServerName = s.Name
}
}
name, err := util.PickNameWithDefault(config.GetServerNames(), "Pick server to use: ", defaultServerName, "", o.In, o.Out, o.Err)
if err != nil {
return nil, err
}
server = config.GetServerByName(name)
if server == nil {
return nil, fmt.Errorf("Could not find the server for name %s", name)
}
}
if server == nil {
return nil, fmt.Errorf("Could not find a %s. %s", kind, missingServerDescription)
}
return server, nil
}
func (o *CommonOptions) findService(name string) (string, error) {
client, ns, err := o.KubeClient()
if err != nil {
return "", err
}
devNs, _, err := kube.GetDevNamespace(client, ns)
if err != nil {
return "", err
}
url, err := services.FindServiceURL(client, ns, name)
if url == "" {
url, err = services.FindServiceURL(client, devNs, name)
}
if url == "" {
names, err := services.GetServiceNames(client, ns, name)
if err != nil {
return "", err
}
if len(names) > 1 {
name, err = util.PickName(names, "Pick service to open: ", "", o.In, o.Out, o.Err)
if err != nil {
return "", err
}
if name != "" {
url, err = services.FindServiceURL(client, ns, name)
}
} else if len(names) == 1 {
// must have been a filter
url, err = services.FindServiceURL(client, ns, names[0])
}
if url == "" {
return "", fmt.Errorf("Could not find URL for service %s in namespace %s", name, ns)
}
}
return url, nil
}
func (o *CommonOptions) findEnvironmentNamespace(envName string) (string, error) {
client, ns, err := o.KubeClient()
if err != nil {
return "", err
}
jxClient, _, err := o.JXClient()
if err != nil {
return "", err
}
devNs, _, err := kube.GetDevNamespace(client, ns)
if err != nil {
return "", err
}
envMap, envNames, err := kube.GetEnvironments(jxClient, devNs)
if err != nil {
return "", err
}
env := envMap[envName]
if env == nil {
return "", util.InvalidOption(optionEnvironment, envName, envNames)
}
answer := env.Spec.Namespace
if answer == "" {
return "", fmt.Errorf("Environment %s does not have a Namespace!", envName)
}
return answer, nil
}
func (o *CommonOptions) findServiceInNamespace(name string, ns string) (string, error) {
client, curNs, err := o.KubeClient()
if err != nil {
return "", err
}
if ns == "" {
ns = curNs
}
url, err := services.FindServiceURL(client, ns, name)
if url == "" {
names, err := services.GetServiceNames(client, ns, name)
if err != nil {
return "", err
}
if len(names) > 1 {
name, err = util.PickName(names, "Pick service to open: ", "", o.In, o.Out, o.Err)
if err != nil {
return "", err
}
if name != "" {
url, err = services.FindServiceURL(client, ns, name)
}
} else if len(names) == 1 {
// must have been a filter
url, err = services.FindServiceURL(client, ns, names[0])
}
if url == "" {
return "", fmt.Errorf("Could not find URL for service %s in namespace %s", name, ns)
}
}
return url, nil
}
func (o *CommonOptions) retry(attempts int, sleep time.Duration, call func() error) (err error) {
for i := 0; ; i++ {
err = call()
if err == nil {
return
}
if i >= (attempts - 1) {
break
}
time.Sleep(sleep)
log.Infof("retrying after error:%s\n", err)
}
return fmt.Errorf("after %d attempts, last error: %s", attempts, err)
}
func (o *CommonOptions) retryQuiet(attempts int, sleep time.Duration, call func() error) (err error) {
lastMessage := ""
dot := false
for i := 0; ; i++ {
err = call()
if err == nil {
if dot {
log.Blank()
}
return
}
if i >= (attempts - 1) {
break
}
time.Sleep(sleep)
message := fmt.Sprintf("retrying after error: %s", err)
if lastMessage == message {
log.Info(".")
dot = true
} else {
lastMessage = message
if dot {
dot = false
log.Blank()
}
log.Infof("%s\n", lastMessage)
}
}
return fmt.Errorf("after %d attempts, last error: %s", attempts, err)
}
func (o *CommonOptions) retryQuietlyUntilTimeout(timeout time.Duration, sleep time.Duration, call func() error) (err error) {
timeoutTime := time.Now().Add(timeout)
lastMessage := ""
dot := false
for i := 0; ; i++ {
err = call()
if err == nil {
if dot {
log.Blank()
}
return
}
if time.Now().After(timeoutTime) {
return fmt.Errorf("Timed out after %s, last error: %s", timeout.String(), err)
}
time.Sleep(sleep)
message := fmt.Sprintf("retrying after error: %s", err)
if lastMessage == message {
log.Info(".")
dot = true
} else {
lastMessage = message
if dot {
dot = false
log.Blank()
}
log.Infof("%s\n", lastMessage)
}
}
}
// retryUntilTrueOrTimeout waits until complete is true, an error occurs or the timeout
func (o *CommonOptions) retryUntilTrueOrTimeout(timeout time.Duration, sleep time.Duration, call func() (bool, error)) (err error) {
timeoutTime := time.Now().Add(timeout)
for i := 0; ; i++ {
complete, err := call()
if complete || err != nil {
return err
}
if time.Now().After(timeoutTime) {
return fmt.Errorf("Timed out after %s, last error: %s", timeout.String(), err)
}
time.Sleep(sleep)
}
}
func (o *CommonOptions) getJobMap(filter string) (map[string]gojenkins.Job, error) {
jobMap := map[string]gojenkins.Job{}
jenkins, err := o.JenkinsClient()
if err != nil {
return jobMap, err
}
jobs, err := jenkins.GetJobs()
if err != nil {
return jobMap, err
}
o.addJobs(&jobMap, filter, "", jobs)
return jobMap, nil
}
func (o *CommonOptions) addJobs(jobMap *map[string]gojenkins.Job, filter string, prefix string, jobs []gojenkins.Job) {
jenkins, err := o.JenkinsClient()
if err != nil {
return
}
for _, j := range jobs {
name := jobName(prefix, &j)
if IsPipeline(&j) {
if filter == "" || strings.Contains(name, filter) {
(*jobMap)[name] = j
continue
}
}
if j.Jobs != nil {
o.addJobs(jobMap, filter, name, j.Jobs)
} else {
job, err := jenkins.GetJob(name)
if err == nil && job.Jobs != nil {
o.addJobs(jobMap, filter, name, job.Jobs)
}
}
}
}
func (o *CommonOptions) tailBuild(jobName string, build *gojenkins.Build) error {
jenkins, err := o.JenkinsClient()
if err != nil {
return nil
}
u, err := url.Parse(build.Url)
if err != nil {
return err
}
buildPath := u.Path
log.Infof("%s %s\n", "tailing the log of", fmt.Sprintf("%s #%d", jobName, build.Number))
// TODO Logger
return jenkins.TailLog(buildPath, o.Out, time.Second, time.Hour*100)
}
func (o *CommonOptions) pickRemoteURL(config *gitcfg.Config) (string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
urls := []string{}
if config.Remotes != nil {
for _, r := range config.Remotes {
if r.URLs != nil {
for _, u := range r.URLs {
urls = append(urls, u)
}
}
}
}
if len(urls) == 1 {
return urls[0], nil
}
url := ""
if len(urls) > 1 {
prompt := &survey.Select{
Message: "Choose a remote git URL:",
Options: urls,
}
err := survey.AskOne(prompt, &url, nil, surveyOpts)
if err != nil {
return "", err
}
}
return url, nil
}
// todo switch to using expose as a jx plugin
// get existing config from the devNamespace and run expose in the target environment
func (o *CommonOptions) expose(devNamespace, targetNamespace, password string) error {
return expose.Expose(devNamespace, targetNamespace, password, o.KubeClientCached, o.Helm(), defaultInstallTimeout)
}
func (o *CommonOptions) runExposecontroller(devNamespace, targetNamespace string, ic kube.IngressConfig, services ...string) error {
return expose.RunExposecontroller(devNamespace, targetNamespace, ic, o.KubeClientCached, o.Helm(),
defaultInstallTimeout)
}
// CleanExposecontrollerReources cleans expose controller resources
func (o *CommonOptions) CleanExposecontrollerReources(ns string) {
expose.CleanExposecontrollerReources(o.KubeClientCached, ns)
}
func (o *CommonOptions) getDefaultAdminPassword(devNamespace string) (string, error) {
client, _, err := o.KubeClient() // cache may not have been created yet...
if err != nil {
return "", fmt.Errorf("cannot obtain k8s client %v", err)
}
basicAuth, err := client.CoreV1().Secrets(devNamespace).Get(JXInstallConfig, v1.GetOptions{})
if err != nil {
return "", fmt.Errorf("cannot find secret %s in namespace %s: %v", kube.SecretBasicAuth, devNamespace, err)
}
adminSecrets := basicAuth.Data[AdminSecretsFile]
adminConfig := config.AdminSecretsConfig{}
err = yaml.Unmarshal(adminSecrets, &adminConfig)
if err != nil {
return "", err
}
return adminConfig.Jenkins.JenkinsSecret.Password, nil
}
func (o *CommonOptions) ensureAddonServiceAvailable(serviceName string) (string, error) {
present, err := services.IsServicePresent(o.KubeClientCached, serviceName, o.currentNamespace)
if err != nil {
return "", fmt.Errorf("no %s provider service found, are you in your teams dev environment? Type `jx ns` to switch.", serviceName)
}
if present {
url, err := services.GetServiceURLFromName(o.KubeClientCached, serviceName, o.currentNamespace)
if err != nil {
return "", fmt.Errorf("no %s provider service found, are you in your teams dev environment? Type `jx ns` to switch.", serviceName)
}
return url, nil
}
// todo ask if user wants to install addon?
return "", nil
}
func (o *CommonOptions) copyCertmanagerResources(targetNamespace string, ic kube.IngressConfig) error {
return certmanager.CopyCertmanagerResources(targetNamespace, ic, o.KubeClientCached)
}
func (o *CommonOptions) getJobName() string {
owner := os.Getenv("REPO_OWNER")
repo := os.Getenv("REPO_NAME")
branch := os.Getenv("BRANCH_NAME")
if owner != "" && repo != "" && branch != "" {
return fmt.Sprintf("%s/%s/%s", owner, repo, branch)
}
job := os.Getenv("JOB_NAME")
if job != "" {
return job
}
return ""
}
func (o *CommonOptions) getBuildNumber() string {
buildNumber := os.Getenv("JX_BUILD_NUMBER")
if buildNumber != "" {
return buildNumber
}
buildNumber = os.Getenv("BUILD_NUMBER")
if buildNumber != "" {
return buildNumber
}
buildID := os.Getenv("BUILD_ID")
if buildID != "" {
return buildID
}
return ""
}
func (o *CommonOptions) VaultOperatorClient() (vaultoperatorclient.Interface, error) {
if o.Factory == nil {
return nil, errors.New("command factory is not initialized")
}
if o.vaultOperatorClient == nil {
vaultOperatorClient, err := o.Factory.CreateVaultOperatorClient()
if err != nil {
return nil, err
}
o.vaultOperatorClient = vaultOperatorClient
}
return o.vaultOperatorClient, nil
}
func (o *CommonOptions) GetWebHookEndpoint() (string, error) {
_, _, err := o.JXClient()
if err != nil {
return "", errors.Wrap(err, "failed to get jxclient")
}
_, _, err = o.KubeClient()
if err != nil {
return "", errors.Wrap(err, "failed to get kube client")
}
isProwEnabled, err := o.isProw()
if err != nil {
return "", err
}
ns, _, err := kube.GetDevNamespace(o.KubeClientCached, o.currentNamespace)
if err != nil {
return "", err
}
var webHookUrl string
if isProwEnabled {
baseURL, err := services.GetServiceURLFromName(o.KubeClientCached, "hook", ns)
if err != nil {
return "", err
}
webHookUrl = util.UrlJoin(baseURL, "hook")
} else {
baseURL, err := services.GetServiceURLFromName(o.KubeClientCached, "jenkins", ns)
if err != nil {
return "", err
}
webHookUrl = util.UrlJoin(baseURL, "github-webhook/")
}
return webHookUrl, nil
}
//ChangeNamespace switches the current jx/K8S namespace to the one specified.
//This is analogous to running `jx namespace cheese`.
func (o *CommonOptions) ChangeNamespace(ns string) {
nsOptions := &NamespaceOptions{
CommonOptions: *o,
}
nsOptions.BatchMode = true
nsOptions.Args = []string{ns}
err := nsOptions.Run()
if err != nil {
log.Warnf("Failed to set context to namespace %s: %s", ns, err)
}
//Reset all the cached clients & namespace values when switching so that they can be properly recalculated for
//the new namespace.
o.KubeClientCached = nil
o.jxClient = nil
o.currentNamespace = ""
o.devNamespace = ""
}
func (o *CommonOptions) GetIn() terminal.FileReader {
return o.In
}
func (o *CommonOptions) GetOut() terminal.FileWriter {
return o.Out
}
func (o *CommonOptions) GetErr() io.Writer {
return o.Err
}