/
gcp.go
151 lines (134 loc) · 4.53 KB
/
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
package opts
import (
"fmt"
"strings"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cloud/gke"
"github.com/jenkins-x/jx/v2/pkg/kube/cluster"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
survey "gopkg.in/AlecAivazis/survey.v1"
)
// GkeClusterListHeader header name for GCP project ID when listing the GKE clusters
const GkeClusterListHeader = "PROJECT_ID"
// GetGoogleProjectID asks to chose from existing projects or optionally creates one if none exist
func (o *CommonOptions) GetGoogleProjectID(defaultProject string) (string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
out, err := o.GetCommandOutput("", "gcloud", "projects", "list")
if err != nil {
return "", err
}
lines := strings.Split(out, "\n")
var existingProjects []string
for _, l := range lines {
if strings.Contains(l, GkeClusterListHeader) {
continue
}
fields := strings.Fields(l)
existingProjects = append(existingProjects, fields[0])
}
var projectId string
if len(existingProjects) == 0 {
confirm := &survey.Confirm{
Message: fmt.Sprintf("No existing Google Projects exist, create one now?"),
Default: true,
}
flag := true
err = survey.AskOne(confirm, &flag, nil, surveyOpts)
if err != nil {
return "", err
}
if !flag {
return "", errors.New("no google project to create cluster in, please manual create one and rerun this wizard")
}
if flag {
return "", errors.New("auto creating projects not yet implemented, please manually create one and rerun the wizard")
}
} else if len(existingProjects) == 1 {
projectId = existingProjects[0]
log.Logger().Infof("Using the only Google Cloud Project %s to create the cluster", util.ColorInfo(projectId))
} else {
prompts := &survey.Select{
Message: "Google Cloud Project:",
Options: existingProjects,
Help: "Select a Google Project to create the cluster in",
}
if defaultProject != "" {
prompts.Default = defaultProject
} else if currentProject, err := gke.GetCurrentProject(); err == nil && currentProject != "" {
prompts.Default = currentProject
}
err := survey.AskOne(prompts, &projectId, nil, surveyOpts)
if err != nil {
return "", err
}
}
if projectId == "" {
return "", errors.New("no Google Cloud Project to create cluster in, please manual create one and rerun this wizard")
}
return projectId, nil
}
// GetGoogleZone returns the GCP zone
func (o *CommonOptions) GetGoogleZone(projectID string, defaultZone string) (string, error) {
if defaultZone == "" {
var err error
defaultZone, err = o.GetCommandOutput("", "gcloud", "config", "get-value", "compute/zone")
if err != nil {
return "", errors.Wrap(err, "getting google zone")
}
}
return o.GetGoogleZoneWithDefault(projectID, defaultZone)
}
// GetGoogleRegion returns the GCP region
func (o *CommonOptions) GetGoogleRegion(projectId string) (string, error) {
configuredRegion, err := o.GetCommandOutput("", "gcloud", "config", "get-value", "compute/region")
if err != nil {
return "", errors.Wrap(err, "getting google region")
}
return o.GetGoogleRegionWithDefault(projectId, configuredRegion)
}
// GetGoogleZoneWithDefault returns the GCP zone, if not zone is found returns the default zone
func (o *CommonOptions) GetGoogleZoneWithDefault(projectId string, defaultZone string) (string, error) {
availableZones, err := gke.GetGoogleZones(projectId)
if err != nil {
return "", err
}
prompts := &survey.Select{
Message: "Google Cloud Zone:",
Options: availableZones,
PageSize: 10,
Help: "The compute zone (e.g. us-central1-a) for the cluster",
Default: defaultZone,
}
zone := ""
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
err = survey.AskOne(prompts, &zone, nil, surveyOpts)
if err != nil {
return "", err
}
return zone, nil
}
func (o *CommonOptions) GetGoogleRegionWithDefault(projectId string, defaultRegion string) (string, error) {
availableRegions, err := gke.GetGoogleRegions(projectId)
if err != nil {
return "", err
}
prompts := &survey.Select{
Message: "Google Cloud Region:",
Options: availableRegions,
PageSize: 10,
Help: "The compute region (e.g. us-central1) for the cluster",
Default: defaultRegion,
}
region := ""
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
err = survey.AskOne(prompts, ®ion, nil, surveyOpts)
if err != nil {
return "", err
}
return region, nil
}
// GetGKEClusterNameFromContext returns the GKE cluster name from current Kubernetes context
func (o *CommonOptions) GetGKEClusterNameFromContext() (string, error) {
return cluster.ShortName(o.kuber)
}