forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_cluster_aks.go
218 lines (179 loc) · 5.71 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
package cmd
import (
"io"
"os"
"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 {
Name string
ResourceGroup 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,
},
}
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.Name, "name", "n", "jenkins-x-cluster", "Name of the cluster")
cmd.Flags().StringVarP(&options.Flags.ResourceGroup, "resource group", "g", "jenkins-x", "Name of the resource group")
cmd.Flags().StringVarP(&options.Flags.Location, "location", "l", "West Europe", "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", "p", "", "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 {
name := o.Flags.Name
prompt := &survey.Input{
Message: "name",
Default: name,
Help: "name of the cluster",
}
survey.AskOne(prompt, &name, nil)
location := o.Flags.Location
prompt = &survey.Input{
Message: "location",
Default: location,
Help: "location to run cluster",
}
survey.AskOne(prompt, &location, nil)
resourceGroup := o.Flags.ResourceGroup
prompt = &survey.Input{
Message: "resourceGroup",
Default: resourceGroup,
Help: "resource group",
}
survey.AskOne(prompt, &resourceGroup, nil)
nodeCount := o.Flags.NodeCount
prompt = &survey.Input{
Message: "nodes",
Default: nodeCount,
Help: "number of nodes",
}
survey.AskOne(prompt, &nodeCount, nil)
kubeVersion := o.Flags.KubeVersion
prompt = &survey.Input{
Message: "k8version",
Default: kubeVersion,
Help: "k8 version",
}
survey.AskOne(prompt, &kubeVersion, nil)
pathToPublicKey := o.Flags.PathToPublicKey
prompt = &survey.Input{
Message: "PathToPublicKey",
Default: "",
Help: "path to public RSA key",
}
survey.AskOne(prompt, &pathToPublicKey, nil)
//First login
err := o.runCommand("az", "login")
if err != nil {
return err
}
//create a resource group
createGroup := []string{"group", "create", "-l", location, "-n", resourceGroup}
err = o.runCommand("az", createGroup...)
if err != nil {
return err
}
createCluster := []string{"aks", "create", "-g", resourceGroup, "-n", name, "-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
}
// call jx init
initOpts := &InitOptions{
CommonOptions: o.CommonOptions,
}
err = initOpts.Run()
if err != nil {
return err
}
// call jx install
installOpts := &InstallOptions{
CommonOptions: o.CommonOptions,
CloudEnvRepository: DEFAULT_CLOUD_ENVIRONMENTS_URL,
KubernetesProvider: AKS, // TODO replace with context, maybe get from ~/.jx/gitAuth.yaml?
}
err = installOpts.Run()
if err != nil {
return err
}
return nil
}