-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_cluster_iks.go
539 lines (488 loc) · 17 KB
/
create_cluster_iks.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
package cmd
import (
"io"
"math"
"os"
"regexp"
"strconv"
"strings"
"time"
ibmcloud "github.com/IBM-Cloud/bluemix-go"
"github.com/IBM-Cloud/bluemix-go/api/container/containerv1"
"github.com/IBM-Cloud/bluemix-go/session"
randomdata "github.com/Pallinder/go-randomdata"
"github.com/jenkins-x/jx/pkg/cloud/iks"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
survey "gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// CreateClusterOptions the flags for running create cluster
type CreateClusterIKSOptions struct {
CreateClusterOptions
Flags CreateClusterIKSFlags
}
type CreateClusterIKSFlags struct {
Username string
Password string
Account string
SSOCode bool
APIKey string
Region string
ClusterName string
KubeVersion string
Zone string
MachineType string
PrivateVLAN string
CreatePrivateVLAN bool
PublicVLAN string
CreatePublicVLAN bool
PrivateOnly bool
Workers string
Isolation string
NoSubnet bool
DiskEncrypt bool
Trusted bool
SkipLogin bool
}
const (
iKSSubDomain = ".containers.appdomain.cloud"
dockerRegistryhost = "docker-registry.jx."
DEFAULT_IBMREPO_URL = "https://registry.bluemix.net/helm/ibm"
)
var (
createClusterIKSLong = templates.LongDesc(`
This command creates a new kubernetes cluster on IKS, installing required local dependencies and provisions the
Jenkins X platform
IBM® Cloud Kubernetes Service delivers powerful tools by combining Docker containers, the Kubernetes technology,
an intuitive user experience, and built-in security and isolation to automate the deployment, operation, scaling,
and monitoring of containerized apps in a cluster of compute hosts.
Important: In order to create a "standard cluster" required for jenkins-x, you must have a Trial, Pay-As-You-Go,
or Subscription IBM Cloud account (https://console.bluemix.net/registration/). "Free cluster"s are currently not
supported.
`)
createClusterIKSExample = templates.Examples(`
jx create cluster iks
`)
re = regexp.MustCompile("^([0-9]+)")
)
type byNumberIndex []string
func (s byNumberIndex) Len() int {
return len(s)
}
func (s byNumberIndex) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byNumberIndex) Less(i, j int) bool {
var left, right int = 0, 0
left, _ = strconv.Atoi(re.FindAllString(s[i], -1)[0])
right, _ = strconv.Atoi(re.FindAllString(s[j], -1)[0])
return left < right
}
// NewCmdGet creates a command object for the generic "init" action, which
// installs the dependencies required to run the jenkins-x platform on a kubernetes cluster.
func NewCmdCreateClusterIKS(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := CreateClusterIKSOptions{
CreateClusterOptions: createCreateClusterOptions(f, in, out, errOut, OKE),
}
cmd := &cobra.Command{
Use: "iks",
Short: "Create a new kubernetes cluster on IBM Cloud Kubernetes Services",
Long: createClusterIKSLong,
Example: createClusterIKSExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCreateClusterFlags(cmd)
cmd.Flags().StringVarP(&options.Flags.Username, "login", "u", "", "Username")
cmd.Flags().StringVarP(&options.Flags.Password, "password", "p", "", "Password")
cmd.Flags().StringVarP(&options.Flags.Account, "account", "c", "", "Account")
cmd.Flags().BoolVarP(&options.Flags.SSOCode, "sso", "", false, "SSO Passcode. See run 'ibmcloud login --sso'")
cmd.Flags().StringVarP(&options.Flags.APIKey, "apikey", "", "", "The IBM Cloud API Key.")
cmd.Flags().StringVarP(&options.Flags.Region, "region", "r", "", "The IBM Cloud Region. Default is 'us-east'")
cmd.Flags().StringVarP(&options.Flags.ClusterName, "name", "n", "", "Set the name of the cluster that will be created.")
cmd.Flags().StringVarP(&options.Flags.KubeVersion, "kube-version", "k", "", "Specify the Kubernetes version, including at least the major.minor version. If you do not include this flag, the default version is used. To see available versions, run ‘ibmcloud ks kube-versions’.")
cmd.Flags().StringVarP(&options.Flags.Zone, "zone", "z", "", "Specify the zone where you want to create the cluster, the options depend on what region that you are logged in to. To see available zones, run 'ibmcloud ks zones'. Default is 'wdc07'")
cmd.Flags().StringVarP(&options.Flags.MachineType, "machine-type", "m", "", "The machine type of the worker node. To see available machine types, run 'ibmcloud ks machine-types --zone <zone name>'. Default is 'b2c.4x16', 4 cores CPU, 16GB Memory")
cmd.Flags().StringVarP(&options.Flags.PrivateVLAN, "private-vlan", "", "", "Conditional: Specify the ID of the private VLAN. To see available VLANs, run 'ibmcloud ks vlans --zone <zone name>'. If you do not have a private VLAN yet, do not specify this option because one will be automatically created for you. When you specify a private VLAN, you must also specify either the ‘--public-vlan’ flag or the ‘--private-only’ flag.")
cmd.Flags().BoolVarP(&options.Flags.CreatePrivateVLAN, "create-private-vlan", "", false, "Automatically create private vlan (default 'true')")
cmd.Flags().StringVarP(&options.Flags.PublicVLAN, "public-vlan", "", "", "Conditional: Specify the ID of the public VLAN. To see available VLANs, run 'ibmcloud ks vlans --zone <zone name>'. If you do not have a public VLAN yet, do not specify this option because one will be automatically created for you.")
cmd.Flags().BoolVarP(&options.Flags.CreatePublicVLAN, "create-public-vlan", "", false, "Automatically create public vlan (default 'true')")
cmd.Flags().BoolVarP(&options.Flags.PrivateOnly, "private-only", "", false, "Use this flag to prevent a public VLAN from being created. Required only when you specify the ‘--private-vlan’ flag without specifying the ‘--public-vlan’ flag.")
cmd.Flags().StringVarP(&options.Flags.Workers, "workers", "", "", "The number of cluster worker nodes. Defaults to 3.")
cmd.Flags().StringVarP(&options.Flags.Isolation, "isolation", "", "public", "The level of hardware isolation for your worker node. Use 'private' to have available physical resources dedicated to you only, or 'public' to allow physical resources to be shared with other IBM customers. For IBM Cloud Public accounts, the default value is public.")
cmd.Flags().BoolVarP(&options.Flags.NoSubnet, "no-subnet", "", false, "Optional: Prevent the creation of a portable subnet when creating the cluster. By default, both a public and a private portable subnet are created on the associated VLAN, and this flag prevents that behavior. To add a subnet to the cluster later, run 'ibmcloud ks cluster-subnet-add'.")
cmd.Flags().BoolVarP(&options.Flags.DiskEncrypt, "disk-encrypt", "", true, "Optional: Disable encryption on a worker node.")
cmd.Flags().BoolVarP(&options.Flags.Trusted, "trusted", "", false, "Optional: Enable trusted cluster feature.")
cmd.Flags().BoolVarP(&options.Flags.SkipLogin, "skip-login", "", false, "Skip login if already logged in using `ibmcloud login`")
return cmd
}
func (o *CreateClusterIKSOptions) Run() error {
var deps []string
d := binaryShouldBeInstalled("ibmcloud")
if d != "" {
deps = append(deps, d)
}
err := o.installMissingDependencies(deps)
if err != nil {
log.Errorf("%v\nPlease fix the error or install manually then try again", err)
os.Exit(-1)
}
err = o.createClusterIKS()
if err != nil {
log.Errorf("error creating cluster %v", err)
os.Exit(-1)
}
return nil
}
func (o *CreateClusterIKSOptions) createClusterIKS() error {
var err error
var c *ibmcloud.Config
var s *session.Session
var regions iks.Regions
var region *iks.Region
var zones iks.Zones
var zone *iks.Zone
var machineTypes iks.MachineTypes
var machineType *iks.MachineType
var vLANs iks.VLANs
var clusters containerv1.Clusters
var config iks.Clusters
var kubeVersion, privateVLAN, publicVLAN, accountGUID string
userName := o.Flags.Username
password := o.Flags.Password
aPIKey := o.Flags.APIKey
sSO := o.Flags.SSOCode
accountGUID = o.Flags.Account
c = new(ibmcloud.Config)
s, err = session.New(c)
if err != nil {
return err
}
if o.Flags.Region == "" {
regionsarr, err := iks.GetAuthRegions(c)
if err != nil {
return err
}
prompt := &survey.Select{
Message: "Region",
Options: regionsarr,
Default: "us-east",
PageSize: 10,
Help: "IBM Cloud Region to authenticate with and create the cluster in:",
}
var regionstr string
err = survey.AskOne(prompt, ®ionstr, nil)
c.Region = regionstr
if err != nil {
return err
}
} else {
c.Region = o.Flags.Region
}
if !o.Flags.SkipLogin {
// [-a API_ENDPOINT] [--sso] [-u USERNAME] [-p PASSWORD] [--apikey KEY | @KEY_FILE] [--no-iam] [-c ACCOUNT_ID | --no-account] [-g RESOURCE_GROUP] [-o ORG] [-s SPACE]
var ibmLogin []string
if c.Region == "us-south" {
ibmLogin = []string{"login", "-a", "api.ng.bluemix.net"}
} else {
ibmLogin = []string{"login", "-a", "api." + c.Region + ".bluemix.net"}
}
if aPIKey != "" {
ibmLogin = append(ibmLogin, "--apikey", aPIKey)
} else if sSO {
ibmLogin = append(ibmLogin, "--sso")
} else {
ibmLogin = append(ibmLogin, "-u", userName, "-p", password)
}
if o.Flags.Account != "" {
ibmLogin = append(ibmLogin, "-c", o.Flags.Account)
}
err = o.runCommandInteractive(true, "ibmcloud", ibmLogin...)
}
accountGUID, err = iks.ConfigFromJSON(c)
if err != nil {
return err
}
c.Endpoint = nil
sessionlessAPI, err := iks.NewSessionless(s)
regions = sessionlessAPI.Regions()
region, err = regions.GetRegion(c.Region)
if err != nil {
return err
}
// Authenticated at this point, grab a session
sessionedAPI, err := iks.New(s)
if err != nil {
return err
}
clusterAPI, err := containerv1.New(s)
if err != nil {
return err
}
zones = sessionlessAPI.Zones()
machineTypes = sessionedAPI.MachineTypes()
vLANs = sessionedAPI.VLANs()
clusters = clusterAPI.Clusters()
config = sessionedAPI.Clusters()
target := containerv1.ClusterTargetHeader{
Region: c.Region,
AccountID: accountGUID,
}
var validClusterName = false
clusterName := o.Flags.ClusterName
// +1 for the dot
clusterMaxLength := 64 - (len(iKSSubDomain) + len(dockerRegistryhost) + len(c.Region) + 1)
if clusterName != "" {
if len(clusterName) > clusterMaxLength {
log.Infof("Cluster name too can only be %d bytes for %s\n", clusterMaxLength, c.Region)
} else {
validClusterName = true
}
} else {
clusterName = strings.ToLower(randomdata.SillyName())
if len(clusterName) > clusterMaxLength {
clusterName = clusterName[:clusterMaxLength]
}
}
for !validClusterName {
for !validClusterName {
cluster, err := clusters.Find(clusterName, target)
if err != nil || cluster.ID == "" {
validClusterName = true
} else {
clusterName = strings.ToLower(randomdata.SillyName())
if len(clusterName) > clusterMaxLength {
clusterName = clusterName[:clusterMaxLength]
}
}
}
prompt := &survey.Input{
Message: "Cluster Name:",
Default: clusterName,
}
validator := survey.ComposeValidators(survey.Required, survey.MaxLength(clusterMaxLength))
err := survey.AskOne(prompt, &clusterName, validator)
if err != nil {
return err
}
}
if o.Flags.Zone == "" {
zonesarr, err := iks.GetZones(*region, zones)
if err != nil {
return err
}
prompts := &survey.Select{
Message: "Zone (location):",
Options: zonesarr,
Help: "A single zone cluster will be create by default in the washington (us-east) data center 7",
PageSize: 10,
Default: "wdc07",
}
var zonestr string
err = survey.AskOne(prompts, &zonestr, nil)
if err != nil {
return err
}
zone, err = zones.GetZone(zonestr, *region)
if err != nil {
return err
}
} else {
zone, err = zones.GetZone(o.Flags.Zone, *region)
if err != nil {
return err
}
}
clusterClient, err := containerv1.New(s)
if err != nil {
return err
}
if o.Flags.KubeVersion == "" {
versionarr, defversion, err := iks.GetKubeVersions(clusterClient.KubeVersions())
prompts := &survey.Select{
Message: "Kubernetes Version:",
Options: versionarr,
Help: "Version of Kubernetes cluster to create",
PageSize: 10,
Default: defversion,
}
err = survey.AskOne(prompts, &kubeVersion, nil)
if err != nil {
return err
}
} else {
kubeVersion = o.Flags.KubeVersion
}
if o.Flags.MachineType == "" {
machinetypearr, err := iks.GetMachineTypes(*zone, machineTypes)
prompts := &survey.Select{
Message: "Kubernetes Node Machine Type:",
Options: machinetypearr,
Help: "Machine type to use for kubernetes nodes;",
PageSize: 10,
Default: "b2c.4x16",
}
var machineTypeStr string
err = survey.AskOne(prompts, &machineTypeStr, nil)
if err != nil {
return err
}
machineType, err = machineTypes.GetMachineType(machineTypeStr, *zone)
if err != nil {
return err
}
} else {
machineType, err = machineTypes.GetMachineType(o.Flags.MachineType, *zone)
if err != nil {
return err
}
}
var workers *int
if o.Flags.Workers != "" {
i, err := strconv.Atoi(o.Flags.Workers)
if err != nil {
o.Flags.Workers = ""
} else {
workers = &i
}
}
if workers == nil {
prompt := &survey.Input{
Message: "Number of kubernetes workers:",
Default: "3",
}
workers = new(int)
err := survey.AskOne(prompt, workers, survey.Required)
if err != nil {
return err
}
}
privatearr, err := iks.GetPrivateVLANs(*zone, vLANs)
if privatearr != nil && len(privatearr) > 0 && err == nil && !o.Flags.CreatePrivateVLAN && o.Flags.PrivateVLAN == "" {
if len(privatearr) > 1 {
prompts := &survey.Select{
Message: "Private VLAN for workers:",
Options: privatearr,
Help: "Private VLAN to use for kubernetes nodes.",
PageSize: 10,
Default: "",
}
err = survey.AskOne(prompts, &privateVLAN, nil)
if err != nil {
return err
}
} else {
privateVLAN = privatearr[0]
}
} else {
privateVLAN = o.Flags.PrivateVLAN
}
if privateVLAN == "" {
log.Info("Creating Private VLAN.")
} else {
log.Infof("Chosen Private VLAN is %s\n", util.ColorInfo(privateVLAN))
}
publicarr, err := iks.GetPublicVLANs(*zone, vLANs)
if publicarr != nil && len(publicarr) > 0 && err == nil && !o.Flags.CreatePublicVLAN && o.Flags.PublicVLAN == "" && !o.Flags.PrivateOnly {
if len(publicarr) > 1 {
prompts := &survey.Select{
Message: "Public VLAN for workers:",
Options: publicarr,
Help: "Public VLAN to use for kubernetes nodes.",
PageSize: 10,
Default: "",
}
err = survey.AskOne(prompts, &publicVLAN, nil)
if err != nil {
return err
}
} else {
publicVLAN = publicarr[0]
}
} else {
publicVLAN = o.Flags.PublicVLAN
}
if publicVLAN == "" {
if o.Flags.PrivateOnly {
log.Info("Private only -> No Public VLAN")
} else {
log.Info("Creating Public VLAN.")
}
} else {
log.Infof("Chosen Public VLAN is %s\n", util.ColorInfo(publicVLAN))
}
var clusterInfo = containerv1.ClusterCreateRequest{
Name: clusterName,
Datacenter: zone.ID,
MachineType: machineType.Name,
WorkerNum: *workers,
PrivateVlan: privateVLAN,
PublicVlan: publicVLAN,
Isolation: o.Flags.Isolation,
NoSubnet: o.Flags.NoSubnet,
MasterVersion: kubeVersion,
DiskEncryption: o.Flags.DiskEncrypt,
EnableTrusted: o.Flags.Trusted,
}
log.Infof("Creating cluster named %s\n", clusterName)
// fmt.Println(clusterInfo)
createResponse, err := clusters.Create(clusterInfo, target)
if err != nil {
return err
}
cluster, err := clusters.Find(createResponse.ID, target)
if err != nil {
return err
}
/* cluster, err := clusters.Find("sagenickel", target)
if err != nil {
return err
}*/
log.Infof("Waiting for cluster named %s, can take around 15 minutes (time out in 1h) ...\n", cluster.Name)
timeout := time.After(time.Hour)
tick := time.Tick(time.Second)
start := time.Now()
L:
for {
select {
case <-timeout:
return errors.New("timed out")
case <-tick:
cluster, err = clusters.Find(createResponse.ID, target)
//cluster, err = clusters.Find("sagenickel", target)
if err != nil {
return err
}
if cluster.State != "normal" {
duration := time.Since(start)
seconds := int64(math.Mod(duration.Seconds(), 60))
secondsString := strconv.FormatInt(seconds, 10)
if len(secondsString) < 2 {
secondsString = "0" + secondsString
}
log.Infof("\rTime elapsed: %dm%ss", int(duration.Minutes()), secondsString)
} else {
break L
}
}
}
//setup the kube context
log.Infof("\nCreated cluster named %s\n", cluster.Name)
kubeconfig, err := config.GetClusterConfig(cluster.Name, target)
if err != nil {
return err
}
log.Info("Setting kube config file\n")
log.Infof("export KUBECONFIG=\"%s\"\n", kubeconfig)
os.Setenv("KUBECONFIG", kubeconfig)
log.Info("Initialising cluster ...\n")
return o.initAndInstall(IKS)
}