-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_terraform.go
1147 lines (984 loc) · 33.2 KB
/
create_terraform.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 (
"io"
"strings"
"fmt"
os_user "os/user"
"strconv"
"errors"
"path/filepath"
"os"
"time"
"path"
"github.com/Pallinder/go-randomdata"
"github.com/jenkins-x/jx/pkg/cloud/gke"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/terraform"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// Cluster interface for Clusters
type Cluster interface {
Name() string
SetName(string) string
ClusterName() string
Provider() string
SetProvider(string) string
Context() string
CreateTfVarsFile(path string) error
}
// GKECluster implements Cluster interface for GKE
type GKECluster struct {
name string
provider string
Organisation string
ProjectID string
Zone string
MachineType string
MinNumOfNodes string
MaxNumOfNodes string
DiskSize string
AutoRepair bool
AutoUpgrade bool
ServiceAccount string
}
// Name Get name
func (g GKECluster) Name() string {
return g.name
}
// SetName Sets the name
func (g *GKECluster) SetName(name string) string {
g.name = name
return g.name
}
// ClusterName get cluster name
func (g GKECluster) ClusterName() string {
return fmt.Sprintf("%s-%s", g.Organisation, g.name)
}
// Provider get provider
func (g GKECluster) Provider() string {
return g.provider
}
// SetProvider Set the provider
func (g *GKECluster) SetProvider(provider string) string {
g.provider = provider
return g.provider
}
// Context Get the context
func (g GKECluster) Context() string {
return fmt.Sprintf("%s_%s_%s_%s", g.provider, g.ProjectID, g.Zone, g.ClusterName())
}
// Region Get the region
func (g GKECluster) Region() string {
return gke.GetRegionFromZone(g.Zone)
}
// CreateTfVarsFile create vars
func (g GKECluster) CreateTfVarsFile(path string) error {
user, err := os_user.Current()
var username string
if err != nil {
username = "unknown"
} else {
username = sanitizeLabel(user.Username)
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "created_by", username)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "created_timestamp", time.Now().Format("20060102150405"))
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "cluster_name", g.ClusterName())
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "organisation", g.Organisation)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "provider", g.provider)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "gcp_zone", g.Zone)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "gcp_project", g.ProjectID)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "min_node_count", g.MinNumOfNodes)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "max_node_count", g.MaxNumOfNodes)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "node_machine_type", g.MachineType)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "node_preemptible", "false")
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "node_disk_size", g.DiskSize)
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "auto_repair", strconv.FormatBool(g.AutoRepair))
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "auto_upgrade", strconv.FormatBool(g.AutoUpgrade))
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "enable_kubernetes_alpha", "false")
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "enable_legacy_abac", "true")
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "logging_service", "logging.googleapis.com")
if err != nil {
return err
}
err = terraform.WriteKeyValueToFileIfNotExists(path, "monitoring_service", "monitoring.googleapis.com")
if err != nil {
return err
}
return nil
}
// ParseTfVarsFile Parse vars file
func (g *GKECluster) ParseTfVarsFile(path string) {
g.Zone, _ = terraform.ReadValueFromFile(path, "gcp_zone")
g.Organisation, _ = terraform.ReadValueFromFile(path, "organisation")
g.provider, _ = terraform.ReadValueFromFile(path, "provider")
g.ProjectID, _ = terraform.ReadValueFromFile(path, "gcp_project")
g.MinNumOfNodes, _ = terraform.ReadValueFromFile(path, "min_node_count")
g.MaxNumOfNodes, _ = terraform.ReadValueFromFile(path, "max_node_count")
g.MachineType, _ = terraform.ReadValueFromFile(path, "node_machine_type")
g.DiskSize, _ = terraform.ReadValueFromFile(path, "node_disk_size")
autoRepair, _ := terraform.ReadValueFromFile(path, "auto_repair")
b, _ := strconv.ParseBool(autoRepair)
g.AutoRepair = b
autoUpgrade, _ := terraform.ReadValueFromFile(path, "auto_upgrade")
b, _ = strconv.ParseBool(autoUpgrade)
g.AutoUpgrade = b
}
// Flags for a cluster
type Flags struct {
Cluster []string
OrganisationName string
ForkOrganisationGitRepo string
SkipTerraformApply bool
IgnoreTerraformWarnings bool
JxEnvironment string
GKEProjectID string
GKESkipEnableApis bool
GKEZone string
GKEMachineType string
GKEMinNumOfNodes string
GKEMaxNumOfNodes string
GKEDiskSize string
GKEAutoRepair bool
GKEAutoUpgrade bool
GKEServiceAccount string
LocalOrganisationRepository string
}
// CreateTerraformOptions the options for the create spring command
type CreateTerraformOptions struct {
CreateOptions
InstallOptions InstallOptions
Flags Flags
Clusters []Cluster
GitRepositoryOptions gits.GitRepositoryOptions
}
var (
validTerraformClusterProviders = []string{"gke"}
createTerraformExample = templates.Examples(`
jx create terraform
# to specify the clusters via flags
jx create terraform -c dev=gke -c stage=gke -c prod=gke
`)
validTerraformVersions = "0.11.0"
gkeBucketConfiguration = `terraform {
required_version = ">= %s"
backend "gcs" {
bucket = "%s-%s-terraform-state"
prefix = "%s"
}
}`
)
const (
// Clusters constant
Clusters = "clusters"
// Terraform constant
Terraform = "terraform"
// TerraformTemplatesGKE constant
TerraformTemplatesGKE = "https://github.com/jenkins-x/terraform-jx-templates-gke.git"
)
// NewCmdCreateTerraform creates a command object for the "create" command
func NewCmdCreateTerraform(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateTerraformOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
InstallOptions: CreateInstallOptions(f, in, out, errOut),
}
cmd := &cobra.Command{
Use: "terraform",
Short: "Creates a Jenkins X Terraform plan",
Example: createTerraformExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.InstallOptions.addInstallFlags(cmd, true)
options.addCommonFlags(cmd)
options.addFlags(cmd)
cmd.Flags().StringVarP(&options.Flags.OrganisationName, "organisation-name", "o", "", "The organisation name that will be used as the Git repo containing cluster details, the repo will be organisation-<org name>")
cmd.Flags().StringVarP(&options.Flags.GKEServiceAccount, "gke-service-account", "", "", "The service account to use to connect to GKE")
cmd.Flags().StringVarP(&options.Flags.ForkOrganisationGitRepo, "fork-git-repo", "f", kube.DefaultOrganisationGitRepoURL, "The Git repository used as the fork when creating new Organisation Git repos")
return cmd
}
func (options *CreateTerraformOptions) addFlags(cmd *cobra.Command) {
// global flags
cmd.Flags().StringArrayVarP(&options.Flags.Cluster, "cluster", "c", []string{}, "Name and Kubernetes provider (gke, aks, eks) of clusters to be created in the form --cluster foo=gke")
cmd.Flags().BoolVarP(&options.Flags.SkipTerraformApply, "skip-terraform-apply", "", false, "Skip applying the generated Terraform plans")
cmd.Flags().BoolVarP(&options.Flags.IgnoreTerraformWarnings, "ignore-terraform-warnings", "", false, "Ignore any warnings about the Terraform plan being potentially destructive")
cmd.Flags().StringVarP(&options.Flags.JxEnvironment, "jx-environment", "", "dev", "The cluster name to install jx inside")
cmd.Flags().StringVarP(&options.Flags.LocalOrganisationRepository, "local-organisation-repository", "", "", "Rather than cloning from a remote Git server, the local directory to use for the organisational folder")
// GKE specific overrides
cmd.Flags().StringVarP(&options.Flags.GKEDiskSize, "gke-disk-size", "", "100", "Size in GB for node VM boot disks. Defaults to 100GB")
cmd.Flags().BoolVarP(&options.Flags.GKEAutoUpgrade, "gke-enable-autoupgrade", "", false, "Sets autoupgrade feature for a cluster's default node-pool(s)")
cmd.Flags().BoolVarP(&options.Flags.GKEAutoRepair, "gke-enable-autorepair", "", true, "Sets autorepair feature for a cluster's default node-pool(s)")
cmd.Flags().StringVarP(&options.Flags.GKEMachineType, "gke-machine-type", "", "", "The type of machine to use for nodes")
cmd.Flags().StringVarP(&options.Flags.GKEMinNumOfNodes, "gke-min-num-nodes", "", "", "The minimum number of nodes to be created in each of the cluster's zones")
cmd.Flags().StringVarP(&options.Flags.GKEMaxNumOfNodes, "gke-max-num-nodes", "", "", "The maximum number of nodes to be created in each of the cluster's zones")
cmd.Flags().StringVarP(&options.Flags.GKEProjectID, "gke-project-id", "", "", "Google Project ID to create cluster in")
cmd.Flags().StringVarP(&options.Flags.GKEZone, "gke-zone", "", "", "The compute zone (e.g. us-central1-a) for the cluster")
}
func stringInValidProviders(a string) bool {
for _, b := range validTerraformClusterProviders {
if b == a {
return true
}
}
return false
}
// Run implements this command
func (options *CreateTerraformOptions) Run() error {
options.InstallOptions.Flags.Prow = true
err := options.installRequirements(GKE, "terraform", options.InstallOptions.InitOptions.HelmBinary())
if err != nil {
return err
}
if len(options.Flags.Cluster) >= 1 {
err := options.ValidateClusterDetails()
if err != nil {
return err
}
}
if len(options.Flags.Cluster) == 0 {
err := options.ClusterDetailsWizard()
if err != nil {
return err
}
}
err = options.createOrganisationGitRepo()
if err != nil {
return err
}
return nil
}
// ClusterDetailsWizard cluster details wizard
func (options *CreateTerraformOptions) ClusterDetailsWizard() error {
surveyOpts := survey.WithStdio(options.In, options.Out, options.Err)
var numOfClusters int
numOfClustersStr := "1"
prompts := &survey.Input{
Message: fmt.Sprintf("How many clusters shall we create?"),
Default: numOfClustersStr,
}
err := survey.AskOne(prompts, &numOfClustersStr, nil, surveyOpts)
if err != nil {
return err
}
numOfClusters, err = strconv.Atoi(numOfClustersStr)
if err != nil {
return err
}
jxEnvironment := ""
for i := 1; i <= numOfClusters; i++ {
var name string
var provider string
defaultOption := ""
if i == 1 {
defaultOption = "dev"
}
prompts := &survey.Input{
Message: fmt.Sprintf("Cluster %v name:", i),
Default: defaultOption,
}
validator := survey.Required
err := survey.AskOne(prompts, &name, validator, surveyOpts)
if err != nil {
return err
}
prompts = &survey.Input{
Message: fmt.Sprintf("Cluster %v provider:", i),
Default: "gke",
}
validator = survey.Validator(
func(val interface{}) error {
if str, ok := val.(string); !ok || !stringInValidProviders(str) {
return fmt.Errorf("invalid cluster provider type %s, must be one of %v", str, validTerraformClusterProviders)
}
return nil
},
)
err = survey.AskOne(prompts, &provider, validator, surveyOpts)
if err != nil {
return err
}
if jxEnvironment == "" {
confirm := false
prompt := &survey.Confirm{
Message: fmt.Sprintf("Would you like to install Jenkins X in cluster %v", name),
Default: true,
}
survey.AskOne(prompt, &confirm, nil, surveyOpts)
if confirm {
jxEnvironment = name
}
}
c := &GKECluster{name: name, provider: provider}
options.Clusters = append(options.Clusters, c)
}
options.Flags.JxEnvironment = jxEnvironment
return nil
}
// ValidateClusterDetails validates the options for a cluster
func (options *CreateTerraformOptions) ValidateClusterDetails() error {
for _, p := range options.Flags.Cluster {
pair := strings.Split(p, "=")
if len(pair) != 2 {
return errors.New("need to provide cluster values as --cluster name=provider, e.g. --cluster production=gke")
}
if !stringInValidProviders(pair[1]) {
return fmt.Errorf("invalid cluster provider type %s, must be one of %v", p, validTerraformClusterProviders)
}
c := &GKECluster{name: pair[0], provider: pair[1]}
options.Clusters = append(options.Clusters, c)
}
return nil
}
func (options *CreateTerraformOptions) createOrganisationGitRepo() error {
organisationDir, err := util.OrganisationsDir()
if err != nil {
return err
}
authConfigSvc, err := options.CreateGitAuthConfigService()
if err != nil {
return err
}
if options.Flags.OrganisationName == "" {
options.Flags.OrganisationName = strings.ToLower(randomdata.SillyName())
}
defaultRepoName := fmt.Sprintf("organisation-%s", options.Flags.OrganisationName)
var dir string
if options.Flags.LocalOrganisationRepository != "" {
exists, err := util.FileExists(options.Flags.LocalOrganisationRepository)
if err != nil {
return err
}
if exists {
dir = options.Flags.LocalOrganisationRepository
} else {
return errors.New("unable to find local repository " + options.Flags.LocalOrganisationRepository)
}
} else {
details, err := gits.PickNewOrExistingGitRepository(options.BatchMode, authConfigSvc,
defaultRepoName, &options.GitRepositoryOptions, nil, nil, options.Git(), true, options.In, options.Out, options.Err)
if err != nil {
return err
}
org := details.Organisation
repoName := details.RepoName
owner := org
if owner == "" {
owner = details.User.Username
}
provider := details.GitProvider
repo, err := provider.GetRepository(owner, repoName)
remoteRepoExists := err == nil
if !remoteRepoExists {
fmt.Fprintf(options.Out, "Creating Git repository %s/%s\n", util.ColorInfo(owner), util.ColorInfo(repoName))
repo, err = details.CreateRepository()
if err != nil {
return err
}
dir, err = util.CreateUniqueDirectory(organisationDir, details.RepoName, util.MaximumNewDirectoryAttempts)
if err != nil {
return err
}
err = options.Git().Clone(options.Flags.ForkOrganisationGitRepo, dir)
if err != nil {
return err
}
pushGitURL, err := options.Git().CreatePushURL(repo.CloneURL, details.User)
if err != nil {
return err
}
err = options.Git().AddRemote(dir, "upstream", options.Flags.ForkOrganisationGitRepo)
if err != nil {
return err
}
err = options.Git().SetRemoteURL(dir, "origin", pushGitURL)
if err != nil {
return err
}
} else {
fmt.Fprintf(options.Out, "Git repository %s/%s already exists\n", util.ColorInfo(owner), util.ColorInfo(repoName))
dir = path.Join(organisationDir, details.RepoName)
localDirExists, err := util.FileExists(dir)
if err != nil {
return err
}
if localDirExists {
// if remote repo does exist & local does exist, git pull the local repo
fmt.Fprintf(options.Out, "local directory already exists\n")
err = options.Git().Pull(dir)
if err != nil {
return err
}
} else {
fmt.Fprintf(options.Out, "cloning repository locally\n")
err = os.MkdirAll(dir, os.FileMode(0755))
if err != nil {
return err
}
// if remote repo does exist & local directory does not exist, clone locally
pushGitURL, err := options.Git().CreatePushURL(repo.CloneURL, details.User)
if err != nil {
return err
}
err = options.Git().Clone(pushGitURL, dir)
if err != nil {
return err
}
}
fmt.Fprintf(options.Out, "Remote repository %s\n\n", util.ColorInfo(repo.HTMLURL))
}
}
// create directory structure
clusterDefinitions, err := options.CreateOrganisationFolderStructure(dir)
if err != nil {
return err
}
changes, err := options.commitClusters(dir)
if err != nil {
return err
}
if changes {
err = options.Git().PushMaster(dir)
if err != nil {
return err
}
fmt.Fprintf(options.Out, "Pushed Git repository %s\n", util.ColorInfo(dir))
}
fmt.Fprintf(options.Out, "Creating Clusters...\n")
err = options.createClusters(dir, clusterDefinitions)
if err != nil {
return err
}
if !options.Flags.SkipTerraformApply {
devCluster, err := options.findDevCluster(clusterDefinitions)
if err != nil {
fmt.Fprintf(options.Out, "Skipping jx install\n")
} else {
err = options.installJx(devCluster, clusterDefinitions)
if err != nil {
return err
}
}
} else {
fmt.Fprintf(options.Out, "Skipping jx install\n")
}
return nil
}
// CreateOrganisationFolderStructure creates an organisations folder structure
func (options *CreateTerraformOptions) CreateOrganisationFolderStructure(dir string) ([]Cluster, error) {
options.writeGitIgnoreFile(dir)
clusterDefinitions := []Cluster{}
for _, c := range options.Clusters {
fmt.Fprintf(options.Out, "Creating config for cluster %s\n\n", util.ColorInfo(c.Name()))
path := filepath.Join(dir, Clusters, c.Name(), Terraform)
exists, err := util.FileExists(path)
if err != nil {
return nil, fmt.Errorf("unable to check if existing folder exists for path %s: %v", path, err)
}
if !exists {
options.Debugf("cluster %s does not exist, creating...", c.Name())
os.MkdirAll(path, DefaultWritePermissions)
switch c.Provider() {
case "gke":
options.Git().Clone(TerraformTemplatesGKE, path)
g := c.(*GKECluster)
//g := &GKECluster{}
err := options.configureGKECluster(g, path)
if err != nil {
return nil, err
}
clusterDefinitions = append(clusterDefinitions, g)
case "aks":
// TODO add aks terraform templates URL
return nil, fmt.Errorf("creating an AKS cluster via Terraform is not currently supported")
case "eks":
// TODO add eks terraform templates URL
return nil, fmt.Errorf("creating an EKS cluster via Terraform is not currently supported")
default:
return nil, fmt.Errorf("unknown Kubernetes provider type %s must be one of %v", c.Provider(), validTerraformClusterProviders)
}
os.RemoveAll(filepath.Join(path, ".git"))
os.RemoveAll(filepath.Join(path, ".gitignore"))
} else {
// if the directory already exists, try to load its config
options.Debugf("cluster %s already exists, loading...", c.Name())
switch c.Provider() {
case "gke":
//g := &GKECluster{}
g := c.(*GKECluster)
terraformVars := filepath.Join(path, "terraform.tfvars")
fmt.Fprintf(options.Out, "loading config from %s\n", util.ColorInfo(terraformVars))
g.ParseTfVarsFile(terraformVars)
clusterDefinitions = append(clusterDefinitions, g)
case "aks":
// TODO add aks terraform templates URL
return nil, fmt.Errorf("creating an AKS cluster via Terraform is not currently supported")
case "eks":
// TODO add eks terraform templates URL
return nil, fmt.Errorf("creating an EKS cluster via Terraform is not currently supported")
default:
return nil, fmt.Errorf("unknown Kubernetes provider type %s must be one of %v", c.Provider(), validTerraformClusterProviders)
}
}
}
return clusterDefinitions, nil
}
func (options *CreateTerraformOptions) createClusters(dir string, clusterDefinitions []Cluster) error {
fmt.Printf("Creating/Updating %v clusters\n", util.ColorInfo(len(clusterDefinitions)))
for _, c := range clusterDefinitions {
switch v := c.(type) {
case *GKECluster:
path := filepath.Join(dir, Clusters, v.Name(), Terraform)
fmt.Fprintf(options.Out, "\n\nCreating/Updating cluster %s\n", util.ColorInfo(c.Name()))
err := options.applyTerraformGKE(v, path)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown Kubernetes provider type, must be one of %v, got %s", validTerraformClusterProviders, v)
}
}
return nil
}
func (options *CreateTerraformOptions) findDevCluster(clusterDefinitions []Cluster) (Cluster, error) {
for _, c := range clusterDefinitions {
if c.Name() == options.Flags.JxEnvironment {
return c, nil
}
}
return nil, fmt.Errorf("Unable to find jx environment %s", options.Flags.JxEnvironment)
}
func (options *CreateTerraformOptions) writeGitIgnoreFile(dir string) error {
gitignore := filepath.Join(dir, ".gitignore")
if _, err := os.Stat(gitignore); os.IsNotExist(err) {
file, err := os.OpenFile(gitignore, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString("**/*.key.json\n.terraform\n**/*.tfstate\njx\n")
if err != nil {
return err
}
}
return nil
}
func (options *CreateTerraformOptions) commitClusters(dir string) (bool, error) {
err := options.Git().Add(dir, "*")
if err != nil {
return false, err
}
changes, err := options.Git().HasChanges(dir)
if err != nil {
return false, err
}
if changes {
return true, options.Git().CommitDir(dir, "Add organisation clusters")
}
return false, nil
}
func (options *CreateTerraformOptions) configureGKECluster(g *GKECluster, path string) error {
surveyOpts := survey.WithStdio(options.In, options.Out, options.Err)
g.DiskSize = options.Flags.GKEDiskSize
g.AutoUpgrade = options.Flags.GKEAutoUpgrade
g.AutoRepair = options.Flags.GKEAutoRepair
g.MachineType = options.Flags.GKEMachineType
g.Zone = options.Flags.GKEZone
g.ProjectID = options.Flags.GKEProjectID
g.MinNumOfNodes = options.Flags.GKEMinNumOfNodes
g.MaxNumOfNodes = options.Flags.GKEMaxNumOfNodes
g.ServiceAccount = options.Flags.GKEServiceAccount
g.Organisation = options.Flags.OrganisationName
if g.ServiceAccount != "" {
options.Debugf("loading service account for cluster %s", g.Name())
err := gke.Login(g.ServiceAccount, false)
if err != nil {
return err
}
}
if g.ProjectID == "" {
options.Debugf("determining google project for cluster %s", g.Name())
projectID, err := options.getGoogleProjectID()
if err != nil {
return err
}
g.ProjectID = projectID
}
if !options.Flags.GKESkipEnableApis {
options.Debugf("enabling apis for %s", g.Name())
err := gke.EnableApis(g.ProjectID, "iam", "compute", "container")
if err != nil {
return err
}
}
if g.Name() == "" {
options.Debugf("generating a new name for cluster %s", g.Name())
g.name = strings.ToLower(randomdata.SillyName())
fmt.Fprintf(options.Out, "No cluster name provided so using a generated one: %s\n", util.ColorInfo(g.Name()))
}
if g.Zone == "" {
options.Debugf("getting available zones for cluster %s", g.Name())
availableZones, err := gke.GetGoogleZones(g.ProjectID)
if err != nil {
return err
}
prompts := &survey.Select{
Message: "Google Cloud Zone:",
Options: availableZones,
PageSize: 10,
Help: "The compute zone (e.g. us-central1-a) for the cluster",
}
err = survey.AskOne(prompts, &g.Zone, nil, surveyOpts)
if err != nil {
return err
}
}
if g.MachineType == "" {
prompts := &survey.Select{
Message: "Google Cloud Machine Type:",
Options: gke.GetGoogleMachineTypes(),
Help: "We recommend a minimum of n1-standard-2 for Jenkins X, a table of machine descriptions can be found here https://cloud.google.com/kubernetes-engine/docs/concepts/cluster-architecture",
PageSize: 10,
Default: "n1-standard-2",
}
err := survey.AskOne(prompts, &g.MachineType, nil, surveyOpts)
if err != nil {
return err
}
}
if g.MinNumOfNodes == "" {
prompt := &survey.Input{
Message: "Minimum number of Nodes",
Default: "3",
Help: "We recommend a minimum of 3 for Jenkins X, the minimum number of nodes to be created in each of the cluster's zones",
}
err := survey.AskOne(prompt, &g.MinNumOfNodes, nil, surveyOpts)
if err != nil {
return err
}
}
if g.MaxNumOfNodes == "" {
prompt := &survey.Input{
Message: "Maximum number of Nodes",
Default: "5",
Help: "We recommend at least 5 for Jenkins X, the maximum number of nodes to be created in each of the cluster's zones",
}
err := survey.AskOne(prompt, &g.MaxNumOfNodes, nil, surveyOpts)
if err != nil {
return err
}
}
terraformVars := filepath.Join(path, "terraform.tfvars")
err := g.CreateTfVarsFile(terraformVars)
if err != nil {
return err
}
storageBucket := fmt.Sprintf(gkeBucketConfiguration, validTerraformVersions, g.ProjectID, options.Flags.OrganisationName, g.Name())
options.Debugf("Using bucket configuration %s", storageBucket)
terraformTf := filepath.Join(path, "terraform.tf")
// file exists
if _, err := os.Stat(terraformTf); os.IsNotExist(err) {
file, err := os.OpenFile(terraformTf, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(storageBucket)
if err != nil {
return err
}
log.Infof("Created %s\n", terraformTf)
}
return nil
}
func (options *CreateTerraformOptions) applyTerraformGKE(g *GKECluster, path string) error {
surveyOpts := survey.WithStdio(options.In, options.Out, options.Err)
if g.ProjectID == "" {
return errors.New("Unable to apply terraform, projectId has not been set")
}
log.Info("Applying Terraform changes\n")
terraformVars := filepath.Join(path, "terraform.tfvars")
if g.ServiceAccount == "" {
if options.Flags.GKEServiceAccount != "" {
g.ServiceAccount = options.Flags.GKEServiceAccount
err := gke.Login(g.ServiceAccount, false)
if err != nil {
return err
}
options.Debugf("attempting to enable apis")
err = gke.EnableApis(g.ProjectID, "iam", "compute", "container")
if err != nil {
return err
}
}
}
var serviceAccountPath string
if g.ServiceAccount == "" {
serviceAccountName := fmt.Sprintf("jx-%s-%s", options.Flags.OrganisationName, g.Name())
fmt.Fprintf(options.Out, "No GCP service account provided, creating %s\n", util.ColorInfo(serviceAccountName))
_, err := gke.GetOrCreateServiceAccount(serviceAccountName, g.ProjectID, filepath.Dir(path))
if err != nil {
return err
}
serviceAccountPath = filepath.Join(filepath.Dir(path), fmt.Sprintf("%s.key.json", serviceAccountName))
fmt.Fprintf(options.Out, "Created GCP service account: %s\n", util.ColorInfo(serviceAccountPath))
} else {
serviceAccountPath = g.ServiceAccount
fmt.Fprintf(options.Out, "Using provided GCP service account: %s\n", util.ColorInfo(serviceAccountPath))
}
// create the bucket
bucketName := fmt.Sprintf("%s-%s-terraform-state", g.ProjectID, options.Flags.OrganisationName)
exists, err := gke.BucketExists(g.ProjectID, bucketName)
if err != nil {
return err
}
if !exists {
err = gke.CreateBucket(g.ProjectID, bucketName, g.Region())
if err != nil {
return err
}
fmt.Fprintf(options.Out, "Created GCS bucket: %s in region %s\n", util.ColorInfo(bucketName), util.ColorInfo(g.Region()))
}
err = terraform.Init(path, serviceAccountPath)
if err != nil {
return err
}
plan, err := terraform.Plan(path, terraformVars, serviceAccountPath)
if err != nil {
return err
}
fmt.Fprintf(options.Out, plan)
if !options.BatchMode {
confirm := false
prompt := &survey.Confirm{
Message: "Would you like to apply this plan?",
}
survey.AskOne(prompt, &confirm, nil, surveyOpts)
if !confirm {
// exit at this point
return nil
}
}
if !options.Flags.IgnoreTerraformWarnings {
if strings.Contains(plan, "forces new resource") {
fmt.Fprintf(options.Out, "%s\n", util.ColorError("It looks like this plan is destructive, aborting."))
fmt.Fprintf(options.Out, "Use --ignore-terraform-warnings to override\n")
return errors.New("aborting destructive plan")
}
}
if !options.Flags.SkipTerraformApply {
log.Info("Applying plan...\n")
err = terraform.Apply(path, terraformVars, serviceAccountPath, options.Out, options.Err)
if err != nil {
return err
}
output, err := options.getCommandOutput("", "gcloud", "container", "clusters", "get-credentials", g.ClusterName(), "--zone", g.Zone, "--project", g.ProjectID)
if err != nil {
return err
}
log.Info(output)
} else {
fmt.Fprintf(options.Out, "Skipping Terraform apply\n")
}
return nil
}
// asks to chose from existing projects or optionally creates one if none exist
func (options *CreateTerraformOptions) getGoogleProjectID() (string, error) {
surveyOpts := survey.WithStdio(options.In, options.Out, options.Err)
existingProjects, err := gke.GetGoogleProjects()
if err != nil {
return "", err
}
var projectID string
if len(existingProjects) == 0 {
confirm := &survey.Confirm{
Message: fmt.Sprintf("No existing Google Projects exist, create one now?"),
Default: true,