/
cluster_gcp.go
168 lines (142 loc) · 4.28 KB
/
cluster_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
package create
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/joyent/triton-kubernetes/backend"
"github.com/joyent/triton-kubernetes/state"
"github.com/manifoldco/promptui"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
)
const (
gcpRancherKubernetesTerraformModulePath = "terraform/modules/gcp-rancher-k8s"
)
// This struct represents the definition of a Terraform .tf file.
// Marshalled into json this struct can be passed directly to Terraform.
type gcpClusterTerraformConfig struct {
baseClusterTerraformConfig
GCPPathToCredentials string `json:"gcp_path_to_credentials"`
GCPProjectID string `json:"gcp_project_id"`
GCPComputeRegion string `json:"gcp_compute_region"`
}
// Returns the name of the cluster that was created and the new state.
func newGCPCluster(remoteBackend backend.Backend, currentState state.State) (string, error) {
nonInteractiveMode := viper.GetBool("non-interactive")
baseConfig, err := getBaseClusterTerraformConfig(gcpRancherKubernetesTerraformModulePath)
if err != nil {
return "", err
}
cfg := gcpClusterTerraformConfig{
baseClusterTerraformConfig: baseConfig,
}
// GCP path_to_credentials
rawGCPPathToCredentials := ""
if viper.IsSet("gcp_path_to_credentials") {
rawGCPPathToCredentials = viper.GetString("gcp_path_to_credentials")
} else if nonInteractiveMode {
return "", errors.New("gcp_path_to_credentials must be specified")
} else {
prompt := promptui.Prompt{
Label: "Path to Google Cloud Platform Credentials File",
Validate: func(input string) error {
expandedPath, err := homedir.Expand(input)
if err != nil {
return err
}
_, err = os.Stat(expandedPath)
if err != nil {
if os.IsNotExist(err) {
return errors.New("File not found")
}
}
return nil
},
}
result, err := prompt.Run()
if err != nil {
return "", err
}
rawGCPPathToCredentials = result
}
expandedGCPPathToCredentials, err := homedir.Expand(rawGCPPathToCredentials)
if err != nil {
return "", err
}
cfg.GCPPathToCredentials = expandedGCPPathToCredentials
gcpCredentials, err := ioutil.ReadFile(cfg.GCPPathToCredentials)
if err != nil {
return "", err
}
jwtCfg, err := google.JWTConfigFromJSON(gcpCredentials, "https://www.googleapis.com/auth/compute.readonly")
if err != nil {
return "", err
}
// jwt.Config does not expose the project ID, so re-unmarshal to get it.
var pid struct {
ProjectID string `json:"project_id"`
}
if err := json.Unmarshal(gcpCredentials, &pid); err != nil {
return "", err
}
cfg.GCPProjectID = pid.ProjectID
service, err := compute.New(jwtCfg.Client(context.Background()))
if err != nil {
return "", err
}
regions, err := service.Regions.List(cfg.GCPProjectID).Do()
if err != nil {
return "", err
}
// GCP Compute Region
if viper.IsSet("gcp_compute_region") {
cfg.GCPComputeRegion = viper.GetString("gcp_compute_region")
found := false
for _, region := range regions.Items {
if region.Name == cfg.GCPComputeRegion {
found = true
break
}
}
if !found {
return "", fmt.Errorf("Selected GCP Compute Region '%s' does not exist.", cfg.GCPComputeRegion)
}
} else if nonInteractiveMode {
return "", errors.New("gcp_compute_region must be specified")
} else {
searcher := func(input string, index int) bool {
region := regions.Items[index]
name := strings.Replace(strings.ToLower(region.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "GCP Compute Region",
Items: regions.Items,
Templates: &promptui.SelectTemplates{
Label: "{{ .Name }}?",
Active: fmt.Sprintf(`%s {{ .Name | underline }}`, promptui.IconSelect),
Inactive: ` {{ .Name }}`,
Selected: fmt.Sprintf(`{{ "%s" | green }} {{ "GCP Compute Region:" | bold}} {{ .Name }}`, promptui.IconGood),
},
Searcher: searcher,
}
i, _, err := prompt.Run()
if err != nil {
return "", err
}
cfg.GCPComputeRegion = regions.Items[i].Name
}
// Add new cluster to terraform config
err = currentState.AddCluster("gcp", cfg.Name, &cfg)
if err != nil {
return "", err
}
return cfg.Name, nil
}