-
Notifications
You must be signed in to change notification settings - Fork 787
/
helper.go
153 lines (130 loc) · 2.97 KB
/
helper.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
package gke
import (
"sort"
"strings"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/util"
)
var PROJECT_LIST_HEADER = "PROJECT_ID"
func GetGoogleZones(project string) ([]string, error) {
var zones []string
args := []string{"compute", "zones", "list"}
if "" != project {
args = append(args, "--project")
args = append(args, project)
}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
out, err := cmd.RunWithoutRetry()
if err != nil {
return nil, err
}
for _, item := range strings.Split(out, "\n") {
zone := strings.Split(item, " ")[0]
if strings.Contains(zone, "-") {
zones = append(zones, zone)
}
sort.Strings(zones)
}
return zones, nil
}
func GetGoogleRegions(project string) ([]string, error) {
var regions []string
args := []string{"compute", "regions", "list"}
if "" != project {
args = append(args, "--project")
args = append(args, project)
}
cmd := util.Command{
Name: "gcloud",
Args: args,
}
out, err := cmd.RunWithoutRetry()
if err != nil {
return nil, err
}
regions = append(regions, "none")
for _, item := range strings.Split(out, "\n") {
region := strings.Split(item, " ")[0]
if strings.Contains(region, "-") {
regions = append(regions, region)
}
sort.Strings(regions)
}
return regions, nil
}
func GetGoogleProjects() ([]string, error) {
cmd := util.Command{
Name: "gcloud",
Args: []string{"projects", "list"},
}
out, err := cmd.RunWithoutRetry()
if err != nil {
return nil, err
}
if out == "Listed 0 items." {
return []string{}, nil
}
lines := strings.Split(out, "\n")
var existingProjects []string
for _, l := range lines {
if strings.Contains(l, PROJECT_LIST_HEADER) {
continue
}
fields := strings.Fields(l)
existingProjects = append(existingProjects, fields[0])
}
return existingProjects, nil
}
func GetCurrentProject() (string, error) {
cmd := util.Command{
Name: "gcloud",
Args: []string{"config", "get-value", "project"},
}
out, err := cmd.RunWithoutRetry()
if err != nil {
return "", err
}
index := strings.LastIndex(out, "\n")
if index >= 0 {
return out[index+1:], nil
}
return out, nil
}
func GetGoogleMachineTypes() []string {
return []string{
"g1-small",
"n1-standard-1",
"n1-standard-2",
"n1-standard-4",
"n1-standard-8",
"n1-standard-16",
"n1-standard-32",
"n1-standard-64",
"n1-standard-96",
"n1-highmem-2",
"n1-highmem-4",
"n1-highmem-8",
"n1-highmem-16",
"n1-highmem-32",
"n1-highmem-64",
"n1-highmem-96",
"n1-highcpu-2",
"n1-highcpu-4",
"n1-highcpu-8",
"n1-highcpu-16",
"n1-highcpu-32",
"n1-highcpu-64",
"n1-highcpu-96",
}
}
// ParseContext parses the context string for GKE and gets the GKE project, GKE zone and cluster name
func ParseContext(context string) (string, string, string, error) {
parts := strings.Split(context, "_")
if len(parts) != 4 {
return "", "", "", errors.Errorf("unable to parse %s as <project id>_<zone>_<cluster name>", context)
}
return parts[1], parts[2], parts[3], nil
}