-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
355 lines (299 loc) · 8.73 KB
/
main.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
package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os/exec"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"google.golang.org/api/container/v1"
"google.golang.org/api/cloudresourcemanager/v1"
)
type GKEConfig struct {
ProjectID string
Region string
Cluster string
Username string
}
func getProjects(ctx context.Context) ([]string, error) {
cloudResourceManagerService, err := cloudresourcemanager.NewService(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create cloud resource manager client: %v", err)
}
var projectIDs []string
resp, err := cloudResourceManagerService.Projects.List().Do()
if err != nil {
return nil, fmt.Errorf("failed to list projects: %v", err)
}
for _, project := range resp.Projects {
if project.LifecycleState == "ACTIVE" {
projectIDs = append(projectIDs, project.ProjectId)
}
}
return projectIDs, nil
}
func getClusters(ctx context.Context, projectID string) ([]*container.Cluster, error) {
containerService, err := container.NewService(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create container service client: %v", err)
}
parent := fmt.Sprintf("projects/%s/locations/-", projectID)
resp, err := containerService.Projects.Locations.Clusters.List(parent).Do()
if err != nil {
return nil, fmt.Errorf("failed to list clusters: %v", err)
}
return resp.Clusters, nil
}
func getCurrentPublicIP() (string, error) {
resp, err := http.Get("https://api.ipify.org")
if err != nil {
return "", fmt.Errorf("failed to get public IP: %v", err)
}
defer resp.Body.Close()
ip, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %v", err)
}
return string(ip), nil
}
func getGcloudUsername() (string, error) {
cmd := exec.Command("gcloud", "config", "get-value", "account")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get gcloud account: %v", err)
}
lines := strings.Split(string(output), "\n")
var email string
for _, line := range lines {
if strings.Contains(line, "@") {
email = strings.TrimSpace(line)
break
}
}
if email != "" {
username := strings.Split(email, "@")[0]
username = strings.ReplaceAll(username, ".", "-")
return username, nil
}
return "", fmt.Errorf("no valid email found in gcloud config")
}
func updateAuthorizedNetworks(ctx context.Context, config GKEConfig, cluster *container.Cluster) error {
containerService, err := container.NewService(ctx)
if err != nil {
return fmt.Errorf("failed to create container service client: %v", err)
}
publicIP, err := getCurrentPublicIP()
if err != nil {
return err
}
var currentNetworks []*container.CidrBlock
if cluster.MasterAuthorizedNetworksConfig.CidrBlocks != nil {
currentNetworks = cluster.MasterAuthorizedNetworksConfig.CidrBlocks
}
username := config.Username
userNetworkExists := false
for i, network := range currentNetworks {
if network.DisplayName == username {
currentNetworks[i].CidrBlock = publicIP + "/32"
userNetworkExists = true
break
}
}
if !userNetworkExists {
currentNetworks = append(currentNetworks, &container.CidrBlock{
DisplayName: username,
CidrBlock: publicIP + "/32",
})
}
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
DesiredMasterAuthorizedNetworksConfig: &container.MasterAuthorizedNetworksConfig{
Enabled: true,
CidrBlocks: currentNetworks,
GcpPublicCidrsAccessEnabled: cluster.MasterAuthorizedNetworksConfig.GcpPublicCidrsAccessEnabled,
},
},
}
name := fmt.Sprintf("projects/%s/locations/%s/clusters/%s",
config.ProjectID, config.Region, config.Cluster)
op, err := containerService.Projects.Locations.Clusters.Update(name, req).Do()
if err != nil {
return fmt.Errorf("failed to update authorized networks: %v", err)
}
return waitForOperation(ctx, containerService, op, config)
}
func waitForOperation(ctx context.Context, svc *container.Service, op *container.Operation, config GKEConfig) error {
opName := fmt.Sprintf("projects/%s/locations/%s/operations/%s",
config.ProjectID, config.Region, op.Name)
for {
result, err := svc.Projects.Locations.Operations.Get(opName).Do()
if err != nil {
return fmt.Errorf("failed to get operation status: %v", err)
}
if result.Status == "DONE" {
if result.Error != nil {
return fmt.Errorf("operation failed: %v", result.Error.Message)
}
return nil
}
time.Sleep(2 * time.Second)
}
}
func hasAuthorizedNetworks(cluster *container.Cluster) bool {
return cluster.MasterAuthorizedNetworksConfig != nil &&
cluster.MasterAuthorizedNetworksConfig.Enabled
}
func setClusterCredentials(ctx context.Context, config GKEConfig, cluster *container.Cluster) error {
fmt.Print("\n")
if hasAuthorizedNetworks(cluster) {
fmt.Printf("📡 Updating authorized networks...\n")
if err := updateAuthorizedNetworks(ctx, config, cluster); err != nil {
return fmt.Errorf("failed to update authorized networks: %v", err)
}
fmt.Printf("✨ Successfully updated authorized networks with your IP\n\n")
} else {
fmt.Printf("ℹ️ Cluster does not have authorized networks enabled, skipping IP update\n\n")
}
fmt.Printf("🔑 Configuring cluster credentials...\n")
cmd := exec.Command("gcloud", "container", "clusters", "get-credentials",
config.Cluster,
"--region", config.Region,
"--project", config.ProjectID)
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
if err := cmd.Run(); err != nil {
return err
}
fmt.Printf("✅ Testing cluster connection...\n")
testCmd := exec.Command("kubectl", "config", "current-context")
testCmd.Stdout = io.Discard
return testCmd.Run()
}
type model struct {
choices []string
cursor int
selected string
step string
projects []string
clusters []*container.Cluster
projectID string
loading bool
program *tea.Program
}
func initialModel() model {
return model{
step: "project",
}
}
func (m *model) Init() tea.Cmd {
return nil
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "enter":
if m.step == "project" {
m.projectID = m.projects[m.cursor]
m.step = "cluster"
m.cursor = 0
clusters, err := getClusters(context.Background(), m.projectID)
if err != nil {
log.Fatalf("Error getting clusters: %v", err)
}
m.clusters = clusters
var clusterNames []string
for _, cluster := range clusters {
clusterNames = append(clusterNames, cluster.Name)
}
m.choices = clusterNames
} else if m.step == "cluster" {
selectedCluster := m.clusters[m.cursor]
username, err := getGcloudUsername()
if err != nil {
log.Printf("Error getting gcloud username: %v", err)
return m, tea.Quit
}
config := GKEConfig{
ProjectID: m.projectID,
Region: selectedCluster.Location,
Cluster: selectedCluster.Name,
Username: username,
}
m.loading = true
m.step = "configuring"
go func() {
if err := setClusterCredentials(context.Background(), config, selectedCluster); err != nil {
log.Printf("Error setting cluster credentials: %v", err)
m.loading = false
m.program.Send(errMsg{err})
return
}
m.program.Send(successMsg{cluster: selectedCluster.Name})
}()
return m, nil
}
}
case errMsg:
return m, tea.Quit
case successMsg:
fmt.Printf("\n✨ Successfully configured credentials for cluster: %s\n", msg.cluster)
fmt.Printf("🚀 You can now use kubectl to interact with the cluster\n")
fmt.Printf("📝 Current context: %s\n\n", msg.cluster)
return m, tea.Quit
}
return m, nil
}
func (m *model) View() string {
if m.loading {
return "\n🔄 Configuring cluster access...\n"
}
var s strings.Builder
s.WriteString("Select using ↑/↓ arrows and enter to confirm\n\n")
if m.step == "project" {
s.WriteString("Choose a GCP project:\n\n")
} else {
s.WriteString("Choose a GKE cluster:\n\n")
}
for i, choice := range m.choices {
cursor := " "
if m.cursor == i {
cursor = ">"
}
s.WriteString(fmt.Sprintf("%s %s\n", cursor, choice))
}
s.WriteString("\n(press q to quit)\n")
return s.String()
}
type errMsg struct{ err error }
type successMsg struct{ cluster string }
func main() {
ctx := context.Background()
projects, err := getProjects(ctx)
if err != nil {
log.Fatalf("Error getting projects: %v", err)
}
m := &model{
step: "project",
projects: projects,
choices: projects,
}
p := tea.NewProgram(m)
m.program = p
if _, err := p.Run(); err != nil {
log.Fatalf("Error running program: %v", err)
}
}