-
Notifications
You must be signed in to change notification settings - Fork 11
/
install.go
931 lines (767 loc) · 32.9 KB
/
install.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
package kuberoCli
import (
"bytes"
"encoding/base64"
"fmt"
"log"
"math/rand"
"os"
"os/exec"
"strconv"
"time"
"encoding/json"
"github.com/go-resty/resty/v2"
"github.com/i582/cfmt/cmd/cfmt"
"github.com/leaanthony/spinner"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/crypto/bcrypt"
"gopkg.in/yaml.v3"
"k8s.io/client-go/tools/clientcmd"
)
// installCmd represents the install command
var installCmd = &cobra.Command{
Use: "install",
Short: "Create a Kubernetes cluster and install all required components for kubero",
Long: `This command will create a kubernetes cluster and install all required components
for kubero on any kubernetes cluster.
required binaries:
- kubectl
- kind (optional)`,
Run: func(cmd *cobra.Command, args []string) {
rand.Seed(time.Now().UnixNano())
checkAllBinaries()
switch arg_component {
case "metrics":
installMetrics()
return
case "certmanager":
installCertManager()
return
case "olm":
installOLM()
return
case "kubero-operator":
installKuberoOperator()
return
case "kubero-ui":
installKuberoUi()
return
case "ingress":
installIngress()
return
case "kubernetes":
installKubernetes()
checkCluster()
return
case "":
printInstallSteps()
installKubernetes()
checkCluster()
installOLM()
installIngress()
installMetrics()
installCertManager()
installKuberoOperator()
installKuberoUi()
writeCLIconfig()
printDNSinfo()
finalMessage()
return
default:
return
}
},
}
var arg_adminPassword string
var arg_adminUser string
var arg_domain string
var arg_apiToken string
var arg_port string
var arg_portSecure string
var clusterType string
var arg_component string
var install_olm bool
var ingressControllerVersion = "v1.7.0" // https://github.com/kubernetes/ingress-nginx/tags -> controller-v1.5.1
// var clusterTypeSelection = "[scaleway,linode,gke,digitalocean,kind]"
var clusterTypeList = []string{"kind", "linode", "scaleway", "gke", "digitalocean"}
func init() {
installCmd.Flags().StringVarP(&arg_component, "component", "c", "", "install component (kubernetes,olm,ingress,metrics,certmanager,kubero-operator,kubero-ui)")
installCmd.Flags().StringVarP(&arg_adminUser, "user", "u", "", "Admin username for the kubero UI")
installCmd.Flags().StringVarP(&arg_adminPassword, "user-password", "U", "", "Password for the admin user")
installCmd.Flags().StringVarP(&arg_apiToken, "apitoken", "a", "", "API token for the admin user")
installCmd.Flags().StringVarP(&arg_port, "port", "p", "", "Kubero UI HTTP port")
installCmd.Flags().StringVarP(&arg_portSecure, "secureport", "P", "", "Kubero UI HTTPS port")
installCmd.Flags().StringVarP(&arg_domain, "domain", "d", "", "Domain name for the kubero UI")
rootCmd.AddCommand(installCmd)
install_olm = false
}
func checkAllBinaries() {
cfmt.Println("\n Check for required binaries")
if !checkBinary("kubectl") {
cfmt.Println("{{✗ kubectl is not installed}}::red")
} else {
cfmt.Println("{{✓ kubectl is installed}}::lightGreen")
}
if !checkBinary("kind") {
cfmt.Println("{{⚠ kind is not installed}}::yellow (only required if you want to install a local kind cluster)")
} else {
cfmt.Println("{{✓ kind is installed}}::lightGreen")
}
if !checkBinary("gcloud") {
cfmt.Println("{{⚠ gcloud is not installed}}::yellow (only required if you want to install a GKE cluster)")
} else {
cfmt.Println("{{✓ gcloud is installed}}::lightGreen")
}
}
func printInstallSteps() {
cfmt.Print(`
Steps to install kubero:
1. Create a kubernetes cluster {{(optional)}}::gray
2. Install the OLM {{(optional)}}::gray
3. Install the ingress controller {{(required)}}::gray
4. Install the metrics server {{(optional, but recommended)}}::gray
5. Install the cert-manager {{(optional)}}::gray
6. Install the kubero operator {{(required)}}::gray
7. Install the kubero UI {{(optional, but highly recommended)}}::gray
8. Write the kubero CLI config
`)
}
func checkBinary(binary string) bool {
_, err := exec.LookPath(binary)
return err == nil
}
func installKubernetes() {
kubernetesInstall := promptLine("1) Create a kubernetes cluster", "[y,n]", "y")
if kubernetesInstall != "y" {
return
}
clusterType = selectFromList("Select a Kubernetes provider", clusterTypeList, "")
switch clusterType {
case "scaleway":
installScaleway()
case "linode":
installLinode()
case "gke":
installGKE()
case "digitalocean":
installDigitalOcean()
case "kind":
installKind()
default:
cfmt.Println("{{✗ Unknown cluster type}}::red")
os.Exit(1)
}
}
func tellAChucknorrisJoke() {
jokesapi := resty.New().
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "kubero-cli/"+kuberoCliVersion).
SetBaseURL("https://api.chucknorris.io/jokes/random")
joke, _ := jokesapi.R().Get("?category=dev")
var jokeResponse JokeResponse
json.Unmarshal(joke.Body(), &jokeResponse)
cfmt.Println("\r{{ " + jokeResponse.Value + " }}::gray")
}
func mergeKubeconfig(kubeconfig []byte) error {
new := clientcmd.NewDefaultPathOptions()
config1, _ := new.GetStartingConfig()
config2, err := clientcmd.Load(kubeconfig)
if err != nil {
return err
}
// append the second config to the first
for k, v := range config2.Clusters {
config1.Clusters[k] = v
}
for k, v := range config2.AuthInfos {
config1.AuthInfos[k] = v
}
for k, v := range config2.Contexts {
config1.Contexts[k] = v
}
config1.CurrentContext = config2.CurrentContext
clientcmd.ModifyConfig(clientcmd.DefaultClientConfig.ConfigAccess(), *config1, true)
return nil
}
func checkCluster() {
var outb, errb bytes.Buffer
clusterInfo := exec.Command("kubectl", "cluster-info")
clusterInfo.Stdout = &outb
clusterInfo.Stderr = &errb
err := clusterInfo.Run()
if err != nil {
fmt.Println(errb.String())
fmt.Println(outb.String())
log.Fatal("command failed : kubectl cluster-info")
}
fmt.Println(outb.String())
out, _ := exec.Command("kubectl", "config", "get-contexts").Output()
fmt.Println(string(out))
clusterselect := promptLine("Is the CURRENT cluster the one you wish to install Kubero?", "[y,n]", "y")
if clusterselect == "n" {
os.Exit(0)
}
}
func installOLM() {
openshiftInstalled, _ := exec.Command("kubectl", "get", "deployment", "olm-operator", "-n", "openshift-operator-lifecycle-manager").Output()
if len(openshiftInstalled) > 0 {
cfmt.Println("{{✓ OLM is allredy installed}}::lightGreen")
return
}
//namespace := promptLine("Install OLM in which namespace?", "[openshift-operator-lifecycle-manager,olm]", "olm")
namespace := "olm"
olmInstalled, _ := exec.Command("kubectl", "get", "deployment", "olm-operator", "-n", namespace).Output()
if len(olmInstalled) > 0 {
cfmt.Println("{{✓ OLM is allredy installed}}::lightGreen")
return
}
olmInstall := promptLine("2) Install OLM", "[y,n]", "n")
if olmInstall != "y" {
install_olm = false
return
} else {
install_olm = true
}
olmRelease := promptLine("Install OLM from which release?", "[0.20.0,0.21.0,0.22.0,0.23.1]", "0.23.1")
olmURL := "https://github.com/operator-framework/operator-lifecycle-manager/releases/download/v" + olmRelease
olmSpinner := spinner.New("Install OLM")
olmCRDInstalled, _ := exec.Command("kubectl", "get", "crd", "subscriptions.operators.coreos.com").Output()
if len(olmCRDInstalled) > 0 {
cfmt.Println("{{✓ OLM CRD's allredy installed}}::lightGreen")
} else {
olmSpinner.Start("run command : kubectl create -f " + olmURL + "/olm.yaml")
_, olmCRDErr := exec.Command("kubectl", "create", "-f", olmURL+"/crds.yaml").Output()
if olmCRDErr != nil {
fmt.Println("")
olmSpinner.Error("OLM CRD installation failed. Try runnig this command manually: kubectl create -f " + olmURL + "/crds.yaml")
log.Fatal(olmCRDErr)
} else {
olmSpinner.Success("OLM CRDs installed sucessfully")
}
}
olmSpinner.Start("run command : kubectl create -f " + olmURL + "/olm.yaml")
_, olmOLMErr := exec.Command("kubectl", "create", "-f", olmURL+"/olm.yaml").Output()
if olmOLMErr != nil {
fmt.Println("")
olmSpinner.Error("Failed to run command. Try runnig this command manually: kubectl create -f " + olmURL + "/olm.yaml")
log.Fatal(olmOLMErr)
}
olmSpinner.Success("OLM installed sucessfully")
olmWaitSpinner := spinner.New("Wait for OLM to be ready")
olmWaitSpinner.Start("run command : kubectl wait --for=condition=available deployment/olm-operator -n " + namespace + " --timeout=180s")
_, olmWaitErr := exec.Command("kubectl", "wait", "--for=condition=available", "deployment/olm-operator", "-n", namespace, "--timeout=180s").Output()
if olmWaitErr != nil {
olmWaitSpinner.Error("Failed to run command. Try runnig this command manually: kubectl wait --for=condition=available deployment/olm-operator -n " + namespace + " --timeout=180s")
log.Fatal(olmWaitErr)
}
olmWaitSpinner.Success("OLM is ready")
olmWaitCatalogSpinner := spinner.New("Wait for OLM Catalog to be ready")
olmWaitCatalogSpinner.Start("run command : kubectl wait --for=condition=available deployment/catalog-operator -n " + namespace + " --timeout=180s")
_, olmWaitCatalogErr := exec.Command("kubectl", "wait", "--for=condition=available", "deployment/catalog-operator", "-n", namespace, "--timeout=180s").Output()
if olmWaitCatalogErr != nil {
olmWaitCatalogSpinner.Error("Failed to run command. Try runnig this command manually: kubectl wait --for=condition=available deployment/catalog-operator -n " + namespace + " --timeout=180s")
log.Fatal(olmWaitCatalogErr)
}
olmWaitCatalogSpinner.Success("OLM Catalog is ready")
}
func installMetrics() {
installed, _ := exec.Command("kubectl", "get", "deployments.apps", "metrics-server", "-n", "kube-system").Output()
if len(installed) > 0 {
cfmt.Println("{{✓ Metrics is allredy enabled}}::lightGreen")
return
}
install := promptLine("4) Install Kubernetes internal metrics service (required for HPA and stats)", "[y,n]", "y")
if install != "y" {
return
}
//components := "https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml"
components := "https://raw.githubusercontent.com/kubero-dev/kubero-operator/main/deploy/metrics-server.yaml"
_, installErr := exec.Command("kubectl", "apply", "-f", components).Output()
if installErr != nil {
fmt.Println("failed to install metrics server")
log.Fatal(installErr)
}
cfmt.Println("{{✓ Metrics server installed}}::lightGreen")
}
func installIngress() {
ingressInstalled, _ := exec.Command("kubectl", "get", "ns", "ingress-nginx").Output()
if len(ingressInstalled) > 0 {
cfmt.Println("{{✓ Ingress is allredy installed}}::lightGreen")
return
}
ingressInstall := promptLine("3) Install Ingress", "[y,n]", "y")
if ingressInstall != "y" {
return
} else {
if clusterType == "" {
clusterType = selectFromList("Which cluster type have you insalled?", clusterTypeList, "")
}
prefill := "baremetal"
switch clusterType {
case "kind":
prefill = "kind"
case "linode":
prefill = "cloud"
case "gke":
prefill = "cloud"
case "scaleway":
prefill = "scw"
case "digitalocean":
prefill = "do"
}
ingressProviderList := []string{"kind", "aws", "baremetal", "cloud", "do", "exoscale", "scw"}
ingressProvider := selectFromList("Provider [kind, aws, baremetal, cloud(Azure,Google,Oracle,Linode), do(digital ocean), exoscale, scw(scaleway)]", ingressProviderList, prefill)
ingressSpinner := spinner.New("Install Ingress")
URL := "https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-" + ingressControllerVersion + "/deploy/static/provider/" + ingressProvider + "/deploy.yaml"
ingressSpinner.Start("run command : kubectl apply -f " + URL)
_, ingressErr := exec.Command("kubectl", "apply", "-f", URL).Output()
if ingressErr != nil {
ingressSpinner.Error("Failed to run command. Try runnig this command manually: kubectl apply -f " + URL)
log.Fatal(ingressErr)
}
ingressSpinner.Success("Ingress installed sucessfully")
}
}
func installKuberoOperator() {
cfmt.Println("\n {{6) Install Kubero Operator}}::bold")
kuberoInstalled, _ := exec.Command("kubectl", "get", "operator", "kubero-operator.operators").Output()
if len(kuberoInstalled) > 0 {
cfmt.Println("{{✓ Kubero Operator is allredy installed}}::lightGreen")
return
}
if install_olm {
installKuberoOLMOperator()
} else {
installKuberoOperatorSlim()
}
}
func installKuberoOLMOperator() {
kuberoSpinner := spinner.New("Install Kubero Operator")
kuberoSpinner.Start("run command : kubectl apply -f https://operatorhub.io/install/kubero-operator.yaml")
_, kuberoErr := exec.Command("kubectl", "apply", "-f", "https://operatorhub.io/install/kubero-operator.yaml").Output()
if kuberoErr != nil {
fmt.Println("")
kuberoSpinner.Error("Failed to run command to install the Operator. Try runnig this command manually: kubectl apply -f https://operatorhub.io/install/kubero-operator.yaml")
log.Fatal(kuberoErr)
}
kuberoSpinner.UpdateMessage("Wait for Kubero Operator to be ready")
var kuberoWait []byte
for len(kuberoWait) == 0 {
// kubectl api-resources --api-group=application.kubero.dev --no-headers=true
kuberoWait, _ = exec.Command("kubectl", "api-resources", "--api-group=application.kubero.dev", "--no-headers=true").Output()
time.Sleep(1 * time.Second)
}
kuberoSpinner.Success("Kubero Operator installed sucessfully")
}
func installKuberoOperatorSlim() {
kuberoSpinner := spinner.New("Install Kubero Operator")
kuberoSpinner.Start("run command : kubectl apply -f https://raw.githubusercontent.com/kubero-dev/kubero-operator/main/deploy/operator.yaml")
_, kuberoErr := exec.Command("kubectl", "apply", "-f", "https://raw.githubusercontent.com/kubero-dev/kubero-operator/main/deploy/operator.yaml").Output()
if kuberoErr != nil {
fmt.Println("")
kuberoSpinner.Error("Failed to run command to install the Operator. Try runnig this command manually: kubectl apply -f https://raw.githubusercontent.com/kubero-dev/kubero-operator/main/deploy/operator.yaml")
log.Fatal(kuberoErr)
}
kuberoSpinner.UpdateMessage("Wait for Kubero Operator to be ready")
var kuberoWait []byte
for len(kuberoWait) == 0 {
// kubectl api-resources --api-group=application.kubero.dev --no-headers=true
kuberoWait, _ = exec.Command("kubectl", "api-resources", "--api-group=application.kubero.dev", "--no-headers=true").Output()
time.Sleep(1 * time.Second)
}
kuberoSpinner.Success("Kubero Operator installed sucessfully")
}
func installKuberoUi() {
ingressInstall := promptLine("7) Install Kubero UI", "[y,n]", "y")
if ingressInstall != "y" {
return
}
kuberoNSinstalled, _ := exec.Command("kubectl", "get", "ns", "kubero").Output()
if len(kuberoNSinstalled) > 0 {
cfmt.Println("{{✓ Kubero Namespace exists}}::lightGreen")
} else {
_, kuberoNSErr := exec.Command("kubectl", "create", "namespace", "kubero").Output()
if kuberoNSErr != nil {
fmt.Println("Failed to run command to create the namespace. Try runnig this command manually: kubectl create namespace kubero")
log.Fatal(kuberoNSErr)
} else {
cfmt.Println("{{✓ Kubero Namespace created}}::lightGreen")
}
}
kuberoSecretInstalled, _ := exec.Command("kubectl", "get", "secret", "kubero-secrets", "-n", "kubero").Output()
if len(kuberoSecretInstalled) > 0 {
cfmt.Println("{{✓ Kubero Secret exists}}::lightGreen")
} else {
webhookSecret := promptLine("Random string for your webhook secret", "", generateRandomString(20, ""))
sessionKey := promptLine("Random string for your session key", "", generateRandomString(20, ""))
if arg_adminUser == "" {
arg_adminUser = promptLine("Admin User", "", "admin")
}
if arg_adminPassword == "" {
arg_adminPassword = promptLine("Admin Password", "", generateRandomString(12, ""))
}
if arg_apiToken == "" {
arg_apiToken = promptLine("Random string for admin API token", "", generateRandomString(20, ""))
}
var userDB []User
userDB = append(userDB, User{Username: arg_adminUser, Password: arg_adminPassword, Insecure: true, Apitoken: arg_apiToken})
userDBjson, _ := json.Marshal(userDB)
userDBencoded := base64.StdEncoding.EncodeToString(userDBjson)
createSecretCommand := exec.Command("kubectl", "create", "secret", "generic", "kubero-secrets",
"--from-literal=KUBERO_WEBHOOK_SECRET="+webhookSecret,
"--from-literal=KUBERO_SESSION_KEY="+sessionKey,
"--from-literal=KUBERO_USERS="+userDBencoded,
)
githubConfigure := promptLine("Configure Github", "[y,n]", "y")
githubPersonalAccessToken := ""
if githubConfigure == "y" {
githubPersonalAccessToken = promptLine("Github personal access token", "", "")
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GITHUB_PERSONAL_ACCESS_TOKEN="+githubPersonalAccessToken)
}
giteaConfigure := promptLine("Configure Gitea", "[y,n]", "n")
giteaPersonalAccessToken := ""
giteaBaseUrl := ""
if giteaConfigure == "y" {
giteaPersonalAccessToken = promptLine("Gitea personal access token", "", "")
giteaBaseUrl = promptLine("Gitea URL", "http://localhost:3000", "")
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GITEA_PERSONAL_ACCESS_TOKEN="+giteaPersonalAccessToken)
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GITEA_BASEURL="+giteaBaseUrl)
}
gogsConfigure := promptLine("Configure Gogs", "[y,n]", "n")
gogsPersonalAccessToken := ""
gogsBaseUrl := ""
if gogsConfigure == "y" {
gogsPersonalAccessToken = promptLine("Gogs personal access token", "", "")
gogsBaseUrl = promptLine("Gogs URL", "http://localhost:3000", "")
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GOGS_PERSONAL_ACCESS_TOKEN="+gogsPersonalAccessToken)
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GOGS_BASEURL="+gogsBaseUrl)
}
gitlabConfigure := promptLine("Configure Gitlab", "[y,n]", "n")
gitlabPersonalAccessToken := ""
gitlabBaseUrl := ""
if gitlabConfigure == "y" {
gitlabPersonalAccessToken = promptLine("Gitlab personal access token", "", "")
gitlabBaseUrl = promptLine("Gitlab URL", "http://localhost:3080", "")
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GITLAB_PERSONAL_ACCESS_TOKEN="+gitlabPersonalAccessToken)
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=GITLAB_BASEURL="+gitlabBaseUrl)
}
bitbucketConfigure := promptLine("Configure Bitbucket", "[y,n]", "n")
bitbucketUsername := ""
bitbucketAppPassword := ""
if bitbucketConfigure == "y" {
bitbucketUsername = promptLine("Bitbucket Username", "", "")
bitbucketAppPassword = promptLine("Bitbucket App Password", "", "")
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=BITBUCKET_USERNAME="+bitbucketUsername)
createSecretCommand.Args = append(createSecretCommand.Args, "--from-literal=BITBUCKET_APP_PASSWORD="+bitbucketAppPassword)
}
createSecretCommand.Args = append(createSecretCommand.Args, "-n", "kubero")
_, kuberoErr := createSecretCommand.Output()
if kuberoErr != nil {
cfmt.Println("{{✗ Failed to run command to create the secrets.}}::red")
log.Fatal(kuberoErr)
} else {
cfmt.Println("{{✓ Kubero Secret created}}::lightGreen")
}
}
kuberoUIInstalled, _ := exec.Command("kubectl", "get", "kuberoes.application.kubero.dev", "-n", "kubero").Output()
if len(kuberoUIInstalled) > 0 {
cfmt.Println("{{✓ Kubero UI allready installed}}::lightGreen")
} else {
installer := resty.New()
installer.SetBaseURL("https://raw.githubusercontent.com")
kf, _ := installer.R().Get("kubero-dev/kubero-operator/main/config/samples/application_v1alpha1_kubero.yaml")
var kuberoUIConfig KuberoUIConfig
yaml.Unmarshal(kf.Body(), &kuberoUIConfig)
if arg_domain == "" {
arg_domain = promptLine("Kuberi UI Domain", "", "kubero.lacolhost.com")
}
kuberoUIConfig.Spec.Ingress.Hosts[0].Host = arg_domain
webhookURL := promptLine("URL to which the webhooks should be sent", "", "https://"+arg_domain+"/api/repo/webhooks")
kuberoUIConfig.Spec.Kubero.WebhookURL = webhookURL
kuberoUIssl := promptLine("Enable SSL for the Kubero UI", "[y/n]", "y")
if kuberoUIssl == "y" {
clusterissuer := promptLine("Kubero UI Clusterissuer", "", "letsencrypt-prod")
kuberoUIConfig.Spec.Ingress.Annotations.KubernetesIoIngressClass = clusterissuer
kuberoUIConfig.Spec.Ingress.Annotations.KubernetesIoTLSacme = "true"
kuberoUIConfig.Spec.Ingress.TLS = []KuberoUItls{
{
Hosts: []string{arg_domain},
SecretName: "kubero-tls",
},
}
}
kuberoUIRegistry := promptLine("Enable Buildpipeline for Kubero (BETA)", "[y/n]", "n")
if kuberoUIRegistry == "y" {
kuberoUIConfig.Spec.Registry.Enabled = true
kuberoUICreateRegistry := promptLine("Create a local Registry for Kubero", "[y/n]", "n")
if kuberoUICreateRegistry == "y" {
kuberoUIConfig.Spec.Registry.Create = true
}
kuberoUIRegistryPort := promptLine("Registry port", "", "443")
kuberoUIConfig.Spec.Registry.Port, _ = strconv.Atoi(kuberoUIRegistryPort)
kuberoUIRegistryHost := promptLine("Registry domain", "", "registry."+arg_domain)
kuberoUIConfig.Spec.Registry.Host = kuberoUIRegistryHost
kuberoUIRegistryUsername := promptLine("Registry username", "", "admin")
kuberoUIConfig.Spec.Registry.Account.Username = kuberoUIRegistryUsername
kuberoUIRegistryPassword := promptLine("Registry password", "", generateRandomString(12, ""))
kuberoUIConfig.Spec.Registry.Account.Password = kuberoUIRegistryPassword
kuberoUIRegistryPasswordBytes, _ := bcrypt.GenerateFromPassword([]byte(kuberoUIRegistryPassword), 14)
kuberoUIConfig.Spec.Registry.Account.Hash = string(kuberoUIRegistryPasswordBytes)
kuberoUIRegistryStorage := promptLine("Registry storage size", "", "10Gi")
kuberoUIConfig.Spec.Registry.Storage = kuberoUIRegistryStorage
storageClassList := getAvailableStorageClasses()
kuberoUIRegistryStorageClassName := selectFromList("Registry storage class", storageClassList, "")
kuberoUIConfig.Spec.Registry.StorageClassName = kuberoUIRegistryStorageClassName
}
kuberoUIAudit := promptLine("Enable Audit Logging", "[y/n]", "n")
if kuberoUIAudit == "y" {
kuberoUIConfig.Spec.Kubero.AuditLogs.Enabled = true
storageClassList := getAvailableStorageClasses()
kuberoUIRegistryStorageClassName := selectFromList("Registry storage class", storageClassList, "")
kuberoUIConfig.Spec.Kubero.AuditLogs.StorageClassName = kuberoUIRegistryStorageClassName
}
kuberoUIconsole := promptLine("Enable Console Access to running contianers", "[y/n]", "n")
if kuberoUIconsole == "y" {
kuberoUIConfig.Spec.Kubero.Config.Kubero.Console.Enabled = true
}
//kuberoUIConfig.Spec.Image.Tag = "v2.0.0-rc.8"
if clusterType == "" {
clusterType = selectFromList("Which cluster type have you insalled?", clusterTypeList, "")
}
if clusterType == "linode" ||
clusterType == "digitalocean" ||
clusterType == "scaleway" ||
clusterType == "gke" {
kuberoUIConfig.Spec.Ingress.ClassName = "nginx"
}
kuberiUIYaml, _ := yaml.Marshal(kuberoUIConfig)
kuberiUIErr := os.WriteFile("kuberoUI.yaml", kuberiUIYaml, 0644)
if kuberiUIErr != nil {
fmt.Println(kuberiUIErr)
return
}
_, olminstallErr := exec.Command("kubectl", "apply", "-f", "kuberoUI.yaml", "-n", "kubero").Output()
if olminstallErr != nil {
cfmt.Println("{{✗ Failed to run command to install Kubero UI. Try runnig this command manually: kubectl apply -f kuberoUI.yaml -n kubero}}::red")
return
} else {
e := os.Remove("kuberoUI.yaml")
if e != nil {
log.Fatal(e)
}
cfmt.Println("{{✓ Kubero UI installed}}::lightGreen")
}
kuberoUISpinner := spinner.New("Wait for Kubero UI to be created")
kuberoUISpinner.Start("Wait for Kubero UI to be created")
var kuberoWait []byte
for len(kuberoWait) == 0 {
// kubectl get --ignore-not-found deployment kubero
kuberoWait, _ = exec.Command("kubectl", "get", "--ignore-not-found", "deployment", "kubero", "-n", "kubero").Output()
kuberoUISpinner.UpdateMessage("Waiting for Kubero UI to be created")
time.Sleep(1 * time.Second)
}
kuberoUISpinner.UpdateMessage("Waiting for Kubero UI to be ready")
time.Sleep(5 * time.Second)
// kubectl wait --for=condition=available deployment/kubero -n kubero --timeout=180s
_, olmWaitErr := exec.Command("kubectl", "wait", "--for=condition=available", "deployment/kubero", "-n", "kubero", "--timeout=180s").Output()
if olmWaitErr != nil {
kuberoUISpinner.Error("Failed to wait for Kubero UI to become ready")
log.Fatal(olmWaitErr)
}
kuberoUISpinner.Success("Kubero UI is ready")
}
}
func installCertManager() {
install := promptLine("5) Install SSL Certmanager", "[y,n]", "y")
if install != "y" {
return
}
if install_olm {
installOLMCertManager()
} else {
installCertManagerSlim()
}
}
func installCertManagerSlim() {
kuberoUIInstalled, _ := exec.Command("kubectl", "get", "crd", "certificates.cert-manager.io").Output()
if len(kuberoUIInstalled) > 0 {
cfmt.Println("{{✓ Certmanager already installed}}::lightGreen")
return
}
certManagerSpinner := spinner.New("Install Cert Manager")
//certmanagerUrl := "https://github.com/cert-manager/cert-manager/releases/download/v1.11.0/cert-manager.yaml"
certmanagerUrl := "https://github.com/cert-manager/cert-manager/releases/latest/download/cert-manager.yaml"
certManagerSpinner.Start("run command : kubectl create -f " + certmanagerUrl)
_, certManagerErr := exec.Command("kubectl", "create", "-f", certmanagerUrl).Output()
if certManagerErr != nil {
certManagerSpinner.Error("Failed to run command. Try runnig this command manually: kubectl create -f " + certmanagerUrl)
log.Fatal(certManagerErr)
}
certManagerSpinner.UpdateMessage("Waiting for Cert Manager to be ready")
time.Sleep(5 * time.Second)
_, certManagerWaitErr := exec.Command("kubectl", "wait", "--for=condition=available", "deployment/cert-manager-webhook", "-n", "cert-manager", "--timeout=180s", "-n", "cert-manager").Output()
if certManagerWaitErr != nil {
certManagerSpinner.Error("Failed to run command. Try runnig it manually: kubectl wait --for=condition=available deployment/cert-manager-webhook -n cert-manager --timeout=180s -n cert-manager")
log.Fatal(certManagerWaitErr)
}
certManagerSpinner.Success("Cert Manager installed")
installCertManagerClusterissuer()
}
func installCertManagerClusterissuer() {
installer := resty.New()
installer.SetBaseURL("https://raw.githubusercontent.com")
kf, _ := installer.R().Get("kubero-dev/kubero-cli/main/templates/certmanagerClusterIssuer.prod.yaml")
var certmanagerClusterIssuer CertmanagerClusterIssuer
yaml.Unmarshal(kf.Body(), &certmanagerClusterIssuer)
arg_certmanagerContact := promptLine("Letsencrypt ACME contact email", "", "noreply@yourdomain.com")
certmanagerClusterIssuer.Spec.Acme.Email = arg_certmanagerContact
clusterissuer := promptLine("Clusterissuer Name", "", "letsencrypt-prod")
certmanagerClusterIssuer.Metadata.Name = clusterissuer
certmanagerClusterIssuerYaml, _ := yaml.Marshal(certmanagerClusterIssuer)
certmanagerClusterIssuerYamlErr := os.WriteFile("kuberoCertmanagerClusterIssuer.yaml", certmanagerClusterIssuerYaml, 0644)
if certmanagerClusterIssuerYamlErr != nil {
fmt.Println(certmanagerClusterIssuerYamlErr)
return
}
_, certmanagerClusterIssuerErr := exec.Command("kubectl", "apply", "-f", "kuberoCertmanagerClusterIssuer.yaml", "-n", "cert-manager").Output()
if certmanagerClusterIssuerErr != nil {
cfmt.Println("{{✗ Failed to create Certmanager Clusterissuer. Try runnig this command manually: kubectl apply -f kuberoCertmanagerClusterIssuer.yaml -n cert-manager}}::red")
return
} else {
e := os.Remove("kuberoCertmanagerClusterIssuer.yaml")
if e != nil {
log.Fatal(e)
}
cfmt.Println("{{✓ Cert Manager Cluster Issuer created}}::lightGreen")
}
}
func installOLMCertManager() {
certManagerInstalled, _ := exec.Command("kubectl", "get", "deployment", "cert-manager-webhook", "-n", "operators").Output()
if len(certManagerInstalled) > 0 {
cfmt.Println("{{✓ Cert Manager allready installed}}::lightGreen")
return
}
certManagerSpinner := spinner.New("Install Cert Manager")
certManagerSpinner.Start("run command : kubectl create -f https://operatorhub.io/install/cert-manager.yaml")
_, certManagerErr := exec.Command("kubectl", "create", "-f", "https://operatorhub.io/install/cert-manager.yaml").Output()
if certManagerErr != nil {
certManagerSpinner.Error("Failed to run command. Try runnig this command manually: kubectl create -f https://operatorhub.io/install/cert-manager.yaml")
log.Fatal(certManagerErr)
}
certManagerSpinner.Success("Cert Manager installed")
time.Sleep(2 * time.Second)
certManagerSpinner = spinner.New("Wait for Cert Manager to be ready")
certManagerSpinner.Start("run command : kubectl wait --for=condition=available deployment/cert-manager-webhook -n cert-manager --timeout=180s -n operators")
_, certManagerWaitErr := exec.Command("kubectl", "wait", "--for=condition=available", "deployment/cert-manager-webhook", "-n", "cert-manager", "--timeout=180s", "-n", "operators").Output()
if certManagerWaitErr != nil {
certManagerSpinner.Error("Failed to run command. Try runnig it manually: kubectl wait --for=condition=available deployment/cert-manager-webhook -n cert-manager --timeout=180s -n operators")
log.Fatal(certManagerWaitErr)
}
certManagerSpinner.Success("Cert Manager is ready")
}
func writeCLIconfig() {
ingressInstall := promptLine("8) Write the Kubero CLI config", "[y,n]", "y")
if ingressInstall != "y" {
return
}
//TODO consider using SSL here.
url := promptLine("Kubero Host adress", "", "http://"+arg_domain+":"+arg_port)
viper.Set("api.url", url)
token := promptLine("Kubero Token", "", arg_apiToken)
viper.Set("api.token", token)
var config Config
if err := viper.Unmarshal(&config); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", config)
viper.WriteConfig()
}
func printDNSinfo() {
ingressInstalled, err := exec.Command("kubectl", "get", "ingress", "-n", "kubero", "-o", "json").Output()
if err != nil {
cfmt.Println("{{✗ Failed to fetch DNS informations}}::red")
return
}
var kuberoIngress KuberoIngress
json.Unmarshal(ingressInstalled, &kuberoIngress)
cfmt.Println("{{⚠ make sure your DNS is pointing to your Kubernetes cluster}}::yellow")
//TODO this should be replaces by the default reviewapp domain
if len(kuberoIngress.Items) > 0 &&
len(kuberoIngress.Items[0].Spec.Rules[0].Host) > 0 &&
len(kuberoIngress.Items[0].Status.LoadBalancer.Ingress) > 0 &&
len(kuberoIngress.Items[0].Status.LoadBalancer.Ingress[0].IP) > 0 {
cfmt.Printf("{{ %s. IN A %s}}::lightBlue\n", kuberoIngress.Items[0].Spec.Rules[0].Host, kuberoIngress.Items[0].Status.LoadBalancer.Ingress[0].IP)
cfmt.Printf("{{ *.review.example.com. IN A %s}}::lightBlue", kuberoIngress.Items[0].Status.LoadBalancer.Ingress[0].IP)
}
}
func finalMessage() {
cfmt.Println(`
,--. ,--. ,--.
| .' /,--.,--.| |-. ,---. ,--.--. ,---.
| . ' | || || .-. '| .-. :| .--'| .-. |
| |\ \' '' '| '-' |\ --.| | ' '-' '
'--' '--' '----' '---' '----''--' '---'
Documentation:
https://docs.kubero.dev
`)
if arg_domain != "" && arg_port != "" && arg_apiToken != "" && arg_adminPassword != "" {
cfmt.Println(`
Your Kubero UI :{{
URL : ` + arg_domain + `:` + arg_port + `
User: ` + arg_adminUser + `
Pass: ` + arg_adminPassword + `}}::lightBlue
`)
} else {
cfmt.Println("\n\n {{Done - you can now login to your Kubero UI}}::lightGreen\n\n ")
}
}
func generateRandomString(length int, chars string) string {
if chars == "" {
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!+?._-%"
}
var letterRunes = []rune(chars)
b := make([]rune, length)
rand.Seed(time.Now().UnixNano())
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func getAvailableStorageClasses() []string {
var storageClasses []string
storageClassesRaw, _ := exec.Command("kubectl", "get", "storageclasses", "-o", "json").Output()
var storageClassesList StorageClassesList
json.Unmarshal(storageClassesRaw, &storageClassesList)
for _, storageClass := range storageClassesList.Items {
storageClasses = append(storageClasses, storageClass.Metadata.Name)
}
return storageClasses
}
type StorageClassesList struct {
APIVersion string `json:"apiVersion"`
Items []struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata struct {
Annotations struct {
KubectlKubernetesIoLastAppliedConfiguration string `json:"kubectl.kubernetes.io/last-applied-configuration"`
StorageclassKubernetesIoIsDefaultClass string `json:"storageclass.kubernetes.io/is-default-class"`
} `json:"annotations"`
CreationTimestamp time.Time `json:"creationTimestamp"`
Name string `json:"name"`
ResourceVersion string `json:"resourceVersion"`
UID string `json:"uid"`
} `json:"metadata"`
Provisioner string `json:"provisioner"`
ReclaimPolicy string `json:"reclaimPolicy"`
VolumeBindingMode string `json:"volumeBindingMode"`
} `json:"items"`
Kind string `json:"kind"`
Metadata struct {
ResourceVersion string `json:"resourceVersion"`
SelfLink string `json:"selfLink"`
} `json:"metadata"`
}