forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_cluster_aks.go
373 lines (308 loc) · 13.2 KB
/
create_cluster_aks.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
package cmd
import (
"io"
"os"
"strings"
"github.com/Pallinder/go-randomdata"
"github.com/jenkins-x/jx/pkg/cloud/aks"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
// CreateClusterOptions the flags for running create cluster
type CreateClusterAKSOptions struct {
CreateClusterOptions
Flags CreateClusterAKSFlags
}
type CreateClusterAKSFlags struct {
UserName string
Password string
ClusterName string
ResourceName string
Location string
NodeVMSize string
NodeOSDiskSize string
NodeCount string
KubeVersion string
PathToPublicKey string
ClientSecret string
ServicePrincipal string
Subscription string
AADClientAppID string
AADServerAppID string
AADServerAppSecret string
AADTenantID string
AdminUsername string
DNSNamePrefix string
DNSServiceIP string
DockerBridgeAddress string
PodCIDR string
ServiceCIDR string
VnetSubnetID string
WorkspaceResourceID string
SkipLogin bool
SkipProviderRegistration bool
SkipResourceGroupCreation bool
Tags string
}
var (
createClusterAKSLong = templates.LongDesc(`
This command creates a new Kubernetes cluster on AKS, installing required local dependencies and provisions the
Jenkins X platform
Azure Container Service (AKS) manages your hosted Kubernetes environment, making it quick and easy to deploy and
manage containerized applications without container orchestration expertise. It also eliminates the burden of
ongoing operations and maintenance by provisioning, upgrading, and scaling resources on demand, without taking
your applications offline.
Please use a location local to you: you can retrieve this from the Azure portal or by
running "az provider list" in your terminal.
Important: You will need an account on Azure, with a storage account (https://portal.azure.com/#create/Microsoft.StorageAccount-ARM)
and network (https://portal.azure.com/#create/Microsoft.VirtualNetwork-ARM) - both linked to the resource group you use
to create the cluster in.
`)
createClusterAKSExample = templates.Examples(`
jx create cluster aks
`)
)
// 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 NewCmdCreateClusterAKS(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := CreateClusterAKSOptions{
CreateClusterOptions: createCreateClusterOptions(f, in, out, errOut, AKS),
}
cmd := &cobra.Command{
Use: "aks",
Short: "Create a new Kubernetes cluster on AKS: Runs on Azure",
Long: createClusterAKSLong,
Example: createClusterAKSExample,
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, "user-name", "u", "", "Azure user name")
cmd.Flags().StringVarP(&options.Flags.Password, "password", "p", "", "Azure password")
cmd.Flags().StringVarP(&options.Flags.ResourceName, "resource-group-name", "n", "", "Name of the resource group")
cmd.Flags().StringVarP(&options.Flags.ClusterName, "cluster-name", "c", "", "Name of the cluster")
cmd.Flags().StringVarP(&options.Flags.Location, "location", "l", "", "Location to run cluster in")
cmd.Flags().StringVarP(&options.Flags.NodeVMSize, "node-vm-size", "s", "", "Size of Virtual Machines to create as Kubernetes nodes")
cmd.Flags().StringVarP(&options.Flags.NodeOSDiskSize, "disk-size", "", "", "Size in GB of the OS disk for each node in the node pool.")
cmd.Flags().StringVarP(&options.Flags.NodeCount, "nodes", "o", "", "Number of node")
cmd.Flags().StringVarP(&options.Flags.KubeVersion, optionKubernetesVersion, "v", "", "Version of Kubernetes to use for creating the cluster, such as '1.8.11' or '1.9.6'. Values from: `az aks get-versions`.")
cmd.Flags().StringVarP(&options.Flags.PathToPublicKey, "path-To-public-rsa-key", "k", "", "Path to public RSA key")
cmd.Flags().StringVarP(&options.Flags.ClientSecret, "client-secret", "", "", "Azure AD client secret to use an existing SP")
cmd.Flags().StringVarP(&options.Flags.ServicePrincipal, "service-principal", "", "", "Azure AD service principal to use an existing SP")
cmd.Flags().StringVarP(&options.Flags.Subscription, "subscription", "", "", "Azure subscription to be used if not default one")
cmd.Flags().StringVarP(&options.Flags.AADClientAppID, "aad-client-app-id", "", "", "The ID of an Azure Active Directory client application")
cmd.Flags().StringVarP(&options.Flags.AADServerAppID, "aad-server-app-id", "", "", "The ID of an Azure Active Directory server application")
cmd.Flags().StringVarP(&options.Flags.AADServerAppSecret, "aad-server-app-secret", "", "", "The secret of an Azure Active Directory server application")
cmd.Flags().StringVarP(&options.Flags.AADTenantID, "aad-tenant-id", "", "", "The ID of an Azure Active Directory tenant")
cmd.Flags().StringVarP(&options.Flags.AdminUsername, "admin-username", "", "", "User account to create on node VMs for SSH access")
cmd.Flags().StringVarP(&options.Flags.DNSNamePrefix, "dns-name-prefix", "", "", "Prefix for hostnames that are created")
cmd.Flags().StringVarP(&options.Flags.DNSServiceIP, "dns-service-ip", "", "", "IP address assigned to the Kubernetes DNS service")
cmd.Flags().StringVarP(&options.Flags.DockerBridgeAddress, "docker-bridge-address", "", "", "An IP address and netmask assigned to the Docker bridge")
cmd.Flags().StringVarP(&options.Flags.PodCIDR, "pod-cidr", "", "", "A CIDR notation IP range from which to assign pod IPs")
cmd.Flags().StringVarP(&options.Flags.ServiceCIDR, "service-cidr", "", "", "A CIDR notation IP range from which to assign service cluster IPs")
cmd.Flags().StringVarP(&options.Flags.VnetSubnetID, "vnet-subnet-id", "", "", "The ID of a subnet in an existing VNet into which to deploy the cluster")
cmd.Flags().StringVarP(&options.Flags.WorkspaceResourceID, "workspace-resource-id", "", "", "The resource ID of an existing Log Analytics Workspace")
cmd.Flags().BoolVarP(&options.Flags.SkipLogin, "skip-login", "", false, "Skip login if already logged in using `az login`")
cmd.Flags().BoolVarP(&options.Flags.SkipProviderRegistration, "skip-provider-registration", "", false, "Skip provider registration")
cmd.Flags().BoolVarP(&options.Flags.SkipResourceGroupCreation, "skip-resource-group-creation", "", false, "Skip resource group creation")
cmd.Flags().StringVarP(&options.Flags.Tags, "tags", "", "", "Space-separated tags in 'key[=value]' format. Use '' to clear existing tags.")
return cmd
}
func (o *CreateClusterAKSOptions) Run() error {
var deps []string
d := binaryShouldBeInstalled("az")
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.createClusterAKS()
if err != nil {
log.Errorf("error creating cluster %v", err)
os.Exit(-1)
}
return nil
}
func (o *CreateClusterAKSOptions) createClusterAKS() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
resourceName := o.Flags.ResourceName
if resourceName == "" {
resourceName = strings.ToLower(randomdata.SillyName())
log.Infof("No resource name provided so using a generated one: %s", resourceName)
}
clusterName := o.Flags.ClusterName
if clusterName == "" {
clusterName = strings.ToLower(randomdata.SillyName())
log.Infof("No cluster name provided so using a generated one: %s", clusterName)
}
location := o.Flags.Location
if location == "" {
prompt := &survey.Select{
Message: "Location",
Options: aks.GetResourceGroupLocation(),
Default: "eastus",
PageSize: 10,
Help: "location to run cluster",
}
err := survey.AskOne(prompt, &location, nil, surveyOpts)
if err != nil {
return err
}
}
nodeVMSize := o.Flags.NodeVMSize
if nodeVMSize == "" {
prompts := &survey.Select{
Message: "Virtual Machine Size:",
Options: aks.GetSizes(),
Help: "We recommend a minimum of Standard_D2s_v3 for Jenkins X.\nA table of machine descriptions can be found here https://azure.microsoft.com/en-us/pricing/details/virtual-machines/linux/",
PageSize: 10,
Default: "Standard_D2s_v3",
}
err := survey.AskOne(prompts, &nodeVMSize, nil, surveyOpts)
if err != nil {
return err
}
}
nodeCount := o.Flags.NodeCount
if nodeCount == "" {
prompt := &survey.Input{
Message: "Number of Nodes",
Default: "3",
Help: "We recommend a minimum of 3 nodes for Jenkins X",
}
survey.AskOne(prompt, &nodeCount, nil, surveyOpts)
}
pathToPublicKey := o.Flags.PathToPublicKey
userName := o.Flags.UserName
password := o.Flags.Password
var err error
if !o.Flags.SkipLogin {
//First login
if userName != "" && password != "" {
log.Info("Logging in to Azure using provider username and password...\n")
err = o.RunCommand("az", "login", "-u", userName, "-p", password)
if err != nil {
return err
}
} else {
log.Info("Logging in to Azure interactively...\n")
err = o.runCommandVerbose("az", "login")
if err != nil {
return err
}
}
}
if !o.Flags.SkipProviderRegistration {
//register for Microsoft Compute and Containers
err = o.RunCommand("az", "provider", "register", "-n", "Microsoft.Compute")
if err != nil {
return err
}
err = o.RunCommand("az", "provider", "register", "-n", "Microsoft.ContainerService")
if err != nil {
return err
}
}
if !o.Flags.SkipResourceGroupCreation {
//create a resource group
createGroup := []string{"group", "create", "-l", location, "-n", resourceName}
err = o.RunCommand("az", createGroup...)
if err != nil {
return err
}
}
subscription := o.Flags.Subscription
if subscription != "" {
log.Info("Changing subscription...\n")
err = o.runCommandVerbose("az", "account", "set", "--subscription", subscription)
if err != nil {
return err
}
}
createCluster := []string{"aks", "create", "-g", resourceName, "-n", clusterName}
if o.Flags.KubeVersion != "" {
createCluster = append(createCluster, "--kubernetes-version", o.Flags.KubeVersion)
}
if nodeVMSize != "" {
createCluster = append(createCluster, "--node-vm-size", nodeVMSize)
}
if o.Flags.NodeOSDiskSize != "" {
createCluster = append(createCluster, "--node-osdisk-size", o.Flags.NodeOSDiskSize)
}
if nodeCount != "" {
createCluster = append(createCluster, "--node-count", nodeCount)
}
if pathToPublicKey != "" {
createCluster = append(createCluster, "--ssh-key-value", pathToPublicKey)
} else {
createCluster = append(createCluster, "--generate-ssh-keys")
}
if o.Flags.ClientSecret != "" {
createCluster = append(createCluster, "--client-secret", o.Flags.ClientSecret)
}
if o.Flags.ServicePrincipal != "" {
createCluster = append(createCluster, "--service-principal", o.Flags.ServicePrincipal)
}
if o.Flags.AADClientAppID != "" {
createCluster = append(createCluster, "--aad-client-app-id", o.Flags.AADClientAppID)
}
if o.Flags.AADServerAppID != "" {
createCluster = append(createCluster, "--aad-server-app-id", o.Flags.AADServerAppID)
}
if o.Flags.AADServerAppSecret != "" {
createCluster = append(createCluster, "--aad-server-app-secret", o.Flags.AADServerAppSecret)
}
if o.Flags.AADTenantID != "" {
createCluster = append(createCluster, "--aad-tenant-id", o.Flags.AADTenantID)
}
if o.Flags.AdminUsername != "" {
createCluster = append(createCluster, "--admin-username", o.Flags.AdminUsername)
}
if o.Flags.DNSNamePrefix != "" {
createCluster = append(createCluster, "--dns-name-prefix", o.Flags.DNSNamePrefix)
}
if o.Flags.DNSServiceIP != "" {
createCluster = append(createCluster, "--dns-service-ip", o.Flags.DNSServiceIP)
}
if o.Flags.DockerBridgeAddress != "" {
createCluster = append(createCluster, "--docker-bridge-address", o.Flags.DockerBridgeAddress)
}
if o.Flags.PodCIDR != "" {
createCluster = append(createCluster, "--pod-cidr", o.Flags.PodCIDR)
}
if o.Flags.ServiceCIDR != "" {
createCluster = append(createCluster, "--service-cidr", o.Flags.ServiceCIDR)
}
if o.Flags.VnetSubnetID != "" {
createCluster = append(createCluster, "--vnet-subnet-id", o.Flags.VnetSubnetID)
}
if o.Flags.WorkspaceResourceID != "" {
createCluster = append(createCluster, "--workspace-resource-id", o.Flags.WorkspaceResourceID)
}
if o.Flags.Tags != "" {
createCluster = append(createCluster, "--tags", o.Flags.Tags)
}
log.Infof("Creating cluster named %s in resource group %s...\n", clusterName, resourceName)
err = o.RunCommand("az", createCluster...)
if err != nil {
return err
}
//setup the kube context
getCredentials := []string{"aks", "get-credentials", "--resource-group", resourceName, "--name", clusterName}
err = o.RunCommand("az", getCredentials...)
if err != nil {
return err
}
log.Info("Initialising cluster ...\n")
return o.initAndInstall(AKS)
}