forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_cluster_aks.go
265 lines (208 loc) · 6.68 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
package cmd
import (
"io"
"os"
"strings"
"github.com/Pallinder/go-randomdata"
"github.com/jenkins-x/jx/pkg/jx/cmd/log"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
)
// CreateClusterOptions the flags for running crest cluster
type CreateClusterAKSOptions struct {
CreateClusterOptions
Flags CreateClusterAKSFlags
}
type CreateClusterAKSFlags struct {
ClusterName string
ResourceName string
Location string
NodeCount string
KubeVersion string
PathToPublicKey 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 cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := CreateClusterAKSOptions{
CreateClusterOptions: CreateClusterOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
Provider: AKS,
InstallOptions: createInstallOptions(f, out, errOut),
},
}
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()
cmdutil.CheckErr(err)
},
}
options.addCreateClusterFlags(cmd)
cmd.Flags().StringVarP(&options.Flags.ResourceName, "resource group name", "n", "", "Name of the resource group")
cmd.Flags().StringVarP(&options.Flags.ClusterName, "clusterName", "c", "", "Name of the cluster")
cmd.Flags().StringVarP(&options.Flags.Location, "location", "l", "eastus", "location to run cluster in")
cmd.Flags().StringVarP(&options.Flags.NodeCount, "nodes", "o", "1", "node count")
cmd.Flags().StringVarP(&options.Flags.KubeVersion, "K8Version", "v", "1.8.2", "kubernetes version")
cmd.Flags().StringVarP(&options.Flags.PathToPublicKey, "PathToPublicRSAKey", "k", "", "pathToPublicRSAKey")
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 {
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 = resourceName + "-cluster"
log.Infof("No cluster name provided so using a generated one: %s", clusterName)
}
location := o.Flags.Location
if location == "" {
prompt := &survey.Input{
Message: "location",
Default: location,
Help: "location to run cluster",
}
survey.AskOne(prompt, &location, nil)
}
nodeCount := o.Flags.NodeCount
if nodeCount == "" {
prompt := &survey.Input{
Message: "nodes",
Default: nodeCount,
Help: "number of nodes",
}
survey.AskOne(prompt, &nodeCount, nil)
}
kubeVersion := o.Flags.KubeVersion
if kubeVersion == "" {
prompt := &survey.Input{
Message: "k8version",
Default: kubeVersion,
Help: "k8 version",
}
survey.AskOne(prompt, &kubeVersion, nil)
}
pathToPublicKey := o.Flags.PathToPublicKey
//First login
err := o.runCommand("az", "login")
if err != nil {
return err
}
//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
}
//create a resource group
createGroup := []string{"group", "create", "-l", location, "-n", resourceName}
err = o.runCommand("az", createGroup...)
if err != nil {
return err
}
createCluster := []string{"aks", "create", "-g", resourceName, "-n", clusterName, "-k", kubeVersion, "--node-count", nodeCount}
if pathToPublicKey != "" {
createCluster = append(createCluster, "--ssh-key-value", pathToPublicKey)
} else {
createCluster = append(createCluster, "--generate-ssh-keys")
}
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
}
/**
//create Container Registry on azure
registryName := resourceName + "Registry"+randomdata.StringNumber(2,"")
installContainerRegistry := []string{"acr","create","--resource-group",resourceName,"--name",registryName,"--sku Basic"}
err = o.runCommand("az",installContainerRegistry...)
if err != nil {
return err
}
//login to the registry
loginRegistry := []string{"acr","login","--name",registryName}
err = o.runCommand("az",loginRegistry...)
if err != nil {
return err
}
**/
// call jx init
initOpts := &InitOptions{
CommonOptions: o.CommonOptions,
Flags: InitFlags{
Provider: AKS,
},
}
err = initOpts.Run()
if err != nil {
return err
}
o.InstallOptions.Flags.Provider = AKS
// call jx install
installOpts := &o.InstallOptions
err = installOpts.Run()
if err != nil {
return err
}
return nil
}