/
node_gcp.go
387 lines (331 loc) · 11.3 KB
/
node_gcp.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
package create
import (
"context"
"errors"
"fmt"
"io/ioutil"
"sort"
"strings"
"github.com/joyent/triton-kubernetes/backend"
"github.com/joyent/triton-kubernetes/state"
"github.com/manifoldco/promptui"
"github.com/spf13/viper"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
)
const (
gcpRancherKubernetesHostTerraformModulePath = "terraform/modules/gcp-rancher-k8s-host"
)
type gcpNodeTerraformConfig struct {
baseNodeTerraformConfig
GCPPathToCredentials string `json:"gcp_path_to_credentials"`
GCPProjectID string `json:"gcp_project_id"`
GCPComputeRegion string `json:"gcp_compute_region"`
GCPComputeNetworkName string `json:"gcp_compute_network_name"`
GCPComputeFirewallHostTag string `json:"gcp_compute_firewall_host_tag"`
GCPMachineType string `json:"gcp_machine_type"`
GCPInstanceZone string `json:"gcp_instance_zone"`
GCPImage string `json:"gcp_image"`
GCPDiskType string `json:"gcp_disk_type"`
GCPDiskSize string `json:"gcp_disk_size"`
GCPDiskMountPath string `json:"gcp_disk_mount_path"`
}
// Adds new GCP nodes to the given cluster and manager.
// Returns:
// - a slice of the hostnames added
// - the new state
// - error or nil
func newGCPNode(selectedClusterManager, selectedCluster string, remoteBackend backend.Backend, currentState state.State) ([]string, error) {
nonInteractiveMode := viper.GetBool("non-interactive")
baseConfig, err := getBaseNodeTerraformConfig(gcpRancherKubernetesHostTerraformModulePath, selectedCluster, currentState)
if err != nil {
return []string{}, err
}
baseConfig.NodeCount, err = getNodeCount(baseConfig)
if err != nil {
return []string{}, err
}
baseConfig.Hostname, err = getNodeHostnamePrefix()
if err != nil {
return []string{}, err
}
cfg := gcpNodeTerraformConfig{
baseNodeTerraformConfig: baseConfig,
// Grab variables from cluster config
GCPPathToCredentials: currentState.Get(fmt.Sprintf("module.%s.gcp_path_to_credentials", selectedCluster)),
GCPProjectID: currentState.Get(fmt.Sprintf("module.%s.gcp_project_id", selectedCluster)),
GCPComputeRegion: currentState.Get(fmt.Sprintf("module.%s.gcp_compute_region", selectedCluster)),
// Reference terraform output variables from cluster module
GCPComputeNetworkName: fmt.Sprintf("${module.%s.gcp_compute_network_name}", selectedCluster),
GCPComputeFirewallHostTag: fmt.Sprintf("${module.%s.gcp_compute_firewall_host_tag}", selectedCluster),
}
gcpCredentials, err := ioutil.ReadFile(cfg.GCPPathToCredentials)
if err != nil {
return []string{}, err
}
jwtCfg, err := google.JWTConfigFromJSON(gcpCredentials, "https://www.googleapis.com/auth/compute.readonly")
if err != nil {
return []string{}, err
}
service, err := compute.New(jwtCfg.Client(context.Background()))
if err != nil {
return []string{}, err
}
zones, err := service.Zones.List(cfg.GCPProjectID).Filter(fmt.Sprintf("region eq https://www.googleapis.com/compute/v1/projects/%s/regions/%s", cfg.GCPProjectID, cfg.GCPComputeRegion)).Do()
if err != nil {
return []string{}, err
}
// GCP Instance Zone
if viper.IsSet("gcp_instance_zone") {
cfg.GCPInstanceZone = viper.GetString("gcp_instance_zone")
found := false
for _, zone := range zones.Items {
if zone.Name == cfg.GCPInstanceZone {
found = true
break
}
}
if !found {
return []string{}, fmt.Errorf("Selected GCP Instance Zone '%s' does not exist.", cfg.GCPInstanceZone)
}
} else if nonInteractiveMode {
return []string{}, errors.New("gcp_instance_zone must be specified")
} else {
searcher := func(input string, index int) bool {
zone := zones.Items[index]
name := strings.Replace(strings.ToLower(zone.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "GCP Instance Zone",
Items: zones.Items,
Templates: &promptui.SelectTemplates{
Label: "{{ .Name }}?",
Active: fmt.Sprintf(`%s {{ .Name | underline }}`, promptui.IconSelect),
Inactive: ` {{ .Name }}`,
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "GCP Instance Zone:" | bold}} {{ .Name }}`, promptui.IconGood),
},
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
return []string{}, err
}
cfg.GCPInstanceZone = zones.Items[i].Name
}
machineTypes, err := service.MachineTypes.List(cfg.GCPProjectID, cfg.GCPInstanceZone).Do()
if err != nil {
return []string{}, err
}
// GCP Machine Type
if viper.IsSet("gcp_machine_type") {
cfg.GCPMachineType = viper.GetString("gcp_machine_type")
found := false
for _, machineType := range machineTypes.Items {
if machineType.Name == cfg.GCPMachineType {
found = true
break
}
}
if !found {
return []string{}, fmt.Errorf("Selected GCP Machine Type '%s' does not exist.", cfg.GCPMachineType)
}
} else if nonInteractiveMode {
return []string{}, errors.New("gcp_machine_type must be specified")
} else {
searcher := func(input string, index int) bool {
machineType := machineTypes.Items[index]
name := strings.Replace(strings.ToLower(machineType.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "GCP Machine Type",
Items: machineTypes.Items,
Templates: &promptui.SelectTemplates{
Label: "{{ .Name }}?",
Active: fmt.Sprintf(`%s {{ .Name | underline }}`, promptui.IconSelect),
Inactive: ` {{ .Name }}`,
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "GCP Machine Type:" | bold}} {{ .Name }}`, promptui.IconGood),
},
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
return []string{}, err
}
cfg.GCPMachineType = machineTypes.Items[i].Name
}
images, err := service.Images.List("ubuntu-os-cloud").Do()
if err != nil {
return []string{}, err
}
// Sort images by created timestamp in reverse chronological order
sort.SliceStable(images.Items, func(i, j int) bool {
return images.Items[i].CreationTimestamp > images.Items[j].CreationTimestamp
})
// GCP Image
if viper.IsSet("gcp_image") {
cfg.GCPImage = viper.GetString("gcp_image")
found := false
for _, image := range images.Items {
if image.Name == cfg.GCPImage {
found = true
break
}
}
if !found {
return []string{}, fmt.Errorf("Selected GCP Image '%s' does not exist.", cfg.GCPImage)
}
} else if nonInteractiveMode {
return []string{}, errors.New("gcp_image must be specified")
} else {
searcher := func(input string, index int) bool {
image := images.Items[index]
name := strings.Replace(strings.ToLower(image.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "GCP Image",
Items: images.Items,
Templates: &promptui.SelectTemplates{
Label: "{{ .Name }}?",
Active: fmt.Sprintf(`%s {{ .Name | underline }}`, promptui.IconSelect),
Inactive: ` {{ .Name }}`,
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "GCP Image:" | bold}} {{ .Name }}`, promptui.IconGood),
},
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
return []string{}, err
}
cfg.GCPImage = images.Items[i].Name
}
// Get list of GCP permanent disk types
// diskTypesResponse, err := service.DiskTypes.List(cfg.GCPProjectID, cfg.GCPInstanceZone).Do()
// if err != nil {
// return nil, err
// }
// permanentDiskTypes := []*compute.DiskType{}
// for _, diskType := range diskTypesResponse.Items {
// if strings.HasPrefix(diskType.Name, "pd-") {
// permanentDiskTypes = append(permanentDiskTypes, diskType)
// }
// }
// // GCP Disk
// diskTypeIsSet := viper.IsSet("gcp_disk_type")
// diskSizeIsSet := viper.IsSet("gcp_disk_size")
// diskMountPathIsSet := viper.IsSet("gcp_disk_mount_path")
// if nonInteractiveMode {
// // If disk type is defined, assume the user's intent is to add
// // a disk to the host and throw an error if neither size nor mount path is set.
// if diskTypeIsSet && !(diskSizeIsSet || diskMountPathIsSet) {
// return nil, errors.New("If gcp_disk_type is set, gcp_disk_size and gcp_disk_mount must also be set.")
// } else if diskTypeIsSet {
// cfg.GCPDiskType = viper.GetString("gcp_disk_type")
// if !isValidDiskType(permanentDiskTypes, cfg.GCPDiskType) {
// return nil, fmt.Errorf("gcp_disk_type must be valid. Found '%s'.", cfg.GCPDiskType)
// }
// cfg.GCPDiskSize = viper.GetString("gcp_disk_size")
// cfg.GCPDiskMountPath = viper.GetString("gcp_disk_mount_path")
// }
// } else {
// shouldCreateDisk, err := util.PromptForConfirmation("Create a disk for this node", "Disk created")
// if err != nil {
// return nil, err
// }
// if shouldCreateDisk {
// // GCP Disk Type
// if diskTypeIsSet {
// cfg.GCPDiskType = viper.GetString("gcp_disk_type")
// if !isValidDiskType(permanentDiskTypes, cfg.GCPDiskType) {
// return nil, fmt.Errorf("gcp_disk_type must be valid. Found '%s'.", cfg.GCPDiskType)
// }
// } else {
// prompt := promptui.Select{
// Label: "GCP Disk Type",
// Items: permanentDiskTypes,
// Templates: &promptui.SelectTemplates{
// Label: "{{ . }}?",
// Active: fmt.Sprintf("%s {{ .Name | underline }}", promptui.IconSelect),
// Inactive: " {{.Name}}",
// Selected: " GCP Disk Type? {{.Name}}",
// },
// }
// i, _, err := prompt.Run()
// if err != nil {
// return nil, err
// }
// cfg.GCPDiskType = permanentDiskTypes[i].Name
// }
// // GCP Disk Size
// if diskSizeIsSet {
// cfg.GCPDiskSize = viper.GetString("gcp_disk_size")
// } else {
// prompt := promptui.Prompt{
// Label: "GCP Disk Size in GB",
// Validate: func(input string) error {
// num, err := strconv.ParseInt(input, 10, 64)
// if err != nil {
// return errors.New("Invalid number")
// }
// if num <= 0 {
// return errors.New("Number must be greater than 0")
// }
// return nil
// },
// }
// result, err := prompt.Run()
// if err != nil {
// return nil, err
// }
// cfg.GCPDiskSize = result
// }
// // GCP Disk Mount path
// if diskMountPathIsSet {
// cfg.GCPDiskMountPath = viper.GetString("gcp_disk_mount_path")
// } else {
// prompt := promptui.Prompt{
// Label: "GCP Disk Mount Path",
// }
// result, err := prompt.Run()
// if err != nil {
// return nil, err
// }
// cfg.GCPDiskMountPath = result
// }
// }
// }
// Get existing node names
nodes, err := currentState.Nodes(selectedCluster)
if err != nil {
return []string{}, err
}
existingNames := []string{}
for nodeName := range nodes {
existingNames = append(existingNames, nodeName)
}
// Determine what the hostnames should be for the new node(s)
newHostnames := getNewHostnames(existingNames, cfg.Hostname, cfg.NodeCount)
// Add new node to terraform config with the new hostnames
for _, newHostname := range newHostnames {
cfgCopy := cfg
cfgCopy.Hostname = newHostname
err = currentState.AddNode(selectedCluster, newHostname, cfgCopy)
if err != nil {
return []string{}, err
}
}
return newHostnames, nil
}
func isValidDiskType(validDiskTypes []*compute.DiskType, diskTypeName string) bool {
for _, diskType := range validDiskTypes {
if diskType.Name == diskTypeName {
return true
}
}
return false
}