-
Notifications
You must be signed in to change notification settings - Fork 787
/
gc_gke.go
607 lines (499 loc) · 14.6 KB
/
gc_gke.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package gc
import (
"strings"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/spf13/cobra"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
)
// GCGKEOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type GCGKEOptions struct {
*opts.CommonOptions
Flags GCGKEFlags
RevisionHistoryLimit int
}
// GCGKEFlags contains the flags for the command
type GCGKEFlags struct {
ProjectID string
RunNow bool
}
var (
GCGKELong = templates.LongDesc(`
Garbage collect Google Container Engine resources that are not deleted when a delete cluster is performed
This command will generate the gcloud command to run and delete external loadbalancers and persistent disks
that are no longer in use.
`)
GCGKEExample = templates.Examples(`
jx garbage collect gke
jx gc gke
`)
ServiceAccountSuffixes = []string{"-vt", "-ko", "-tf", "-dn", "-ex", "-jb", "-st", "-tk", "-vo", "-bc", "-tekton"}
)
type Rules struct {
Rules []Rule
}
type Rule struct {
Name string `json:"name"`
TargetTags []string `json:"targetTags"`
}
type cluster struct {
Name string `json:"name"`
}
type address struct {
Name string `json:"name"`
Region string `json:"region"`
}
type zone struct {
Name string `json:"name"`
}
type disk struct {
Name string `json:"name"`
}
type serviceAccount struct {
DisplayName string `json:"displayName"`
Email string `json:"email"`
}
type iamPolicy struct {
Bindings []iamBinding `json:"bindings"`
}
type iamBinding struct {
Members []string `json:"members"`
Role string `json:"role"`
}
func (o *GCGKEOptions) addFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.Flags.ProjectID, "project", "p", "", "The google project id to create the GC script for")
cmd.Flags().BoolVarP(&o.Flags.RunNow, "run-now", "", false, "Execute the script")
}
// NewCmdGCGKE is a command object for the "step" command
func NewCmdGCGKE(commonOpts *opts.CommonOptions) *cobra.Command {
options := &GCGKEOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "gke",
Short: "garbage collection for gke",
Long: GCGKELong,
Example: GCGKEExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.CommonOptions.AddBaseFlags(cmd)
options.addFlags(cmd)
return cmd
}
// Run implements this command
func (o *GCGKEOptions) Run() error {
if o.Flags.ProjectID == "" {
if o.BatchMode {
projectID, err := o.getCurrentGoogleProjectId()
if err != nil {
return err
}
o.Flags.ProjectID = projectID
} else {
projectID, err := o.GetGoogleProjectID("")
if err != nil {
return err
}
o.Flags.ProjectID = projectID
}
}
dir, err := os.Getwd()
if err != nil {
return err
}
gkeSa := os.Getenv("GKE_SA_KEY_FILE")
if gkeSa != "" {
err = o.GCloud().Login(gkeSa, true)
if err != nil {
return err
}
}
path := util.UrlJoin(dir, "gc_gke.sh")
exists, err := util.FileExists(path)
if err != nil {
return errors.Wrapf(err, "checking if file %s exists", path)
}
if exists {
err = os.Remove(path)
if err != nil {
return err
}
}
message := `#!/bin/bash
set -euo pipefail
###################################################################################################
#
# WARNING: this command is experimental and the generated script should be executed at the users own risk. We use this
# generated command on the Jenkins X project itself but it has not been tested on other clusters.
#
###################################################################################################
# Project %s
%s
%s
%s
%s
`
log.Logger().Warn("This command is experimental and the generated script should be executed at the users own risk\n")
log.Logger().Warn("We will generate a script for you to review and execute, this command will not delete any resources by itself\n")
log.Logger().Info("It may take a few minutes to create the script\n")
fw, err := o.cleanUpFirewalls()
if err != nil {
return err
}
disks, err := o.cleanUpPersistentDisks()
if err != nil {
return err
}
addr, err := o.cleanUpAddresses()
if err != nil {
return err
}
serviceAccounts, err := o.cleanUpServiceAccounts()
if err != nil {
return err
}
data := fmt.Sprintf(message, o.Flags.ProjectID, fw, strings.Join(disks, "\n"), strings.Join(addr, "\n"), strings.Join(serviceAccounts, "\n"))
data = strings.Replace(data, "[", "", -1)
data = strings.Replace(data, "]", "", -1)
err = ioutil.WriteFile("gc_gke.sh", []byte(data), util.DefaultWritePermissions)
if err != nil {
return err
}
log.Logger().Info("Script 'gc_gke.sh' created!")
if o.Flags.RunNow {
log.Logger().Info("Executing 'gc_gke.sh'")
err = o.RunCommand("gc_gke.sh")
log.Logger().Info("Done")
}
return err
}
func (o *GCGKEOptions) cleanUpFirewalls() (string, error) {
co := &opts.CommonOptions{}
data, err := co.GetCommandOutput("", "gcloud", "compute", "firewall-rules", "list", "--format", "json", "--project", o.Flags.ProjectID)
if err != nil {
return "", err
}
var rules []Rule
err = json.Unmarshal([]byte(data), &rules)
if err != nil {
return "", err
}
out, err := co.GetCommandOutput("", "gcloud", "container", "clusters", "list", "--project", o.Flags.ProjectID)
if err != nil {
return "", err
}
lines := strings.Split(out, "\n")
var existingClusters []string
for _, l := range lines {
if strings.Contains(l, "NAME") {
continue
}
if strings.TrimSpace(l) == "" {
break
}
fields := strings.Fields(l)
existingClusters = append(existingClusters, fields[0])
}
var nameToDelete []string
for _, rule := range rules {
name := strings.TrimPrefix(rule.Name, "gke-")
if !contains(existingClusters, name) {
for _, tagFull := range rule.TargetTags {
tag := strings.TrimPrefix(tagFull, "gke-")
if !contains(existingClusters, tag) {
nameToDelete = append(nameToDelete, rule.Name)
}
}
}
}
if nameToDelete != nil {
args := "gcloud compute firewall-rules delete --quiet --project " + o.Flags.ProjectID
for _, name := range nameToDelete {
args = args + " " + name
}
args = args + " || true"
return args, nil
}
return "# No firewalls found for deletion", nil
}
func (o *GCGKEOptions) cleanUpPersistentDisks() ([]string, error) {
zones, err := o.getZones()
if err != nil {
return nil, err
}
var line []string
for _, z := range zones {
disks, err := o.getUnusedDisksForZone(z)
if err != nil {
return nil, err
}
for _, d := range disks {
if strings.HasPrefix(d.Name, "gke-") {
line = append(line, fmt.Sprintf("gcloud compute disks delete --zone=%s --quiet %s --project %s || true", z.Name, d.Name, o.Flags.ProjectID))
}
}
}
if len(line) == 0 {
line = append(line, "# No disks found for deletion\n")
}
return line, nil
}
func (o *GCGKEOptions) cleanUpAddresses() ([]string, error) {
cmd := "gcloud compute addresses list --filter=\"status:RESERVED\" --format=json --project " + o.Flags.ProjectID
data, err := o.GetCommandOutput("", "bash", "-c", cmd)
if err != nil {
return nil, err
}
var addresses []address
err = json.Unmarshal([]byte(data), &addresses)
if err != nil {
return nil, err
}
var line []string
if len(addresses) > 0 {
for _, address := range addresses {
var scope string
if address.Region != "" {
region := getLastString(strings.Split(address.Region, "/"))
scope = fmt.Sprintf("--region %s", region)
} else {
scope = "--global"
}
line = append(line, fmt.Sprintf("gcloud compute addresses delete %s %s --project %s || true", address.Name, scope, o.Flags.ProjectID))
}
return line, nil
}
if len(line) == 0 {
line = append(line, "# No addresses found for deletion\n")
}
return line, nil
}
func (o *GCGKEOptions) cleanUpServiceAccounts() ([]string, error) {
serviceAccounts, err := o.getServiceAccounts()
if err != nil {
return nil, err
}
clusters, err := o.getClusters()
if err != nil {
return nil, err
}
serviceAccounts, err = o.getFilteredServiceAccounts(serviceAccounts, clusters)
if err != nil {
return nil, err
}
var line []string
if len(serviceAccounts) > 0 {
for _, sa := range serviceAccounts {
log.Logger().Debugf("About to delete service account %s", sa)
line = append(line, fmt.Sprintf("gcloud iam service-accounts delete %s --quiet --project %s || true", sa.Email, o.Flags.ProjectID))
}
}
if len(line) == 0 {
line = append(line, "# No service accounts found for deletion\n")
}
policy, err := o.getIamPolicy()
if err != nil {
return nil, err
}
iam, err := o.determineUnusedIamBindings(policy)
if err != nil {
return nil, err
}
if len(iam) == 0 {
line = append(line, "# No iam policy bindings found for deletion\n")
}
line = append(line, iam...)
if len(line) == 0 {
line = append(line, "# No service accounts found for deletion\n")
}
return line, nil
}
func contains(s []string, e string) bool {
for _, a := range s {
if strings.HasPrefix(e, a) {
return true
}
}
return false
}
func getLastString(s []string) string {
return s[len(s)-1]
}
func (o *GCGKEOptions) getZones() ([]zone, error) {
cmd := "gcloud compute zones list --format=json --project " + o.Flags.ProjectID
data, err := o.GetCommandOutput("", "bash", "-c", cmd)
if err != nil {
return nil, err
}
var zones []zone
err = json.Unmarshal([]byte(data), &zones)
if err != nil {
return nil, err
}
return zones, nil
}
func (o *GCGKEOptions) getClusters() ([]cluster, error) {
cmd := "gcloud container clusters list --format=json --project " + o.Flags.ProjectID
data, err := o.GetCommandOutput("", "bash", "-c", cmd)
if err != nil {
return nil, err
}
var clusters []cluster
err = json.Unmarshal([]byte(data), &clusters)
if err != nil {
return nil, err
}
return clusters, nil
}
func (o *GCGKEOptions) getUnusedDisksForZone(z zone) ([]disk, error) {
diskCmd := fmt.Sprintf("gcloud compute disks list --filter=\"NOT users:* AND zone:(%s)\" --format=json --project %s", z.Name, o.Flags.ProjectID)
data, err := o.GetCommandOutput("", "bash", "-c", diskCmd)
if err != nil {
return nil, err
}
var disks []disk
err = json.Unmarshal([]byte(data), &disks)
if err != nil {
return nil, err
}
return disks, nil
}
func (o *GCGKEOptions) getFilteredServiceAccounts(serviceAccounts []serviceAccount, clusters []cluster) ([]serviceAccount, error) {
filteredServiceAccounts := []serviceAccount{}
for _, sa := range serviceAccounts {
if isServiceAccount(sa.DisplayName) {
if o.shouldRemoveServiceAccount(sa.DisplayName, clusters) {
log.Logger().Debugf("Adding service account to filtered service account list %s", sa.DisplayName)
filteredServiceAccounts = append(filteredServiceAccounts, sa)
}
}
}
return filteredServiceAccounts, nil
}
func (o *GCGKEOptions) shouldRemoveServiceAccount(saDisplayName string, clusters []cluster) bool {
sz := len(saDisplayName)
clusterName := saDisplayName[:sz-3]
if strings.HasPrefix(clusterName, "pr") {
// clusters with '-' in names such as BDD test clusters
// e.g. pr-331-170-gitop-vt
clusterNameParts := strings.Split(clusterName, "-")
if len(clusterNameParts) > 2 {
clusterNamePrefix := clusterNameParts[0] + "-" + clusterNameParts[1] + "-" + clusterNameParts[2]
if !o.clusterExistsWithPrefix(clusters, clusterNamePrefix) {
log.Logger().Debugf("cluster with prefix %s does not exist", clusterNamePrefix)
return true
}
}
} else {
if !o.clusterExists(clusters, clusterName) {
// clusters that don't start with pr
log.Logger().Debugf("cluster %s does not exist", clusterName)
return true
}
}
log.Logger().Debugf("cluster %s exists, excluding service account %s", clusterName, saDisplayName)
return false
}
func (o *GCGKEOptions) getServiceAccounts() ([]serviceAccount, error) {
cmd := "gcloud iam service-accounts list --format=json --project " + o.Flags.ProjectID
data, err := o.GetCommandOutput("", "bash", "-c", cmd)
if err != nil {
return nil, err
}
var serviceAccounts []serviceAccount
err = json.Unmarshal([]byte(data), &serviceAccounts)
if err != nil {
return nil, err
}
var modifiedSAs []serviceAccount
for _, sa := range serviceAccounts {
// If there isn't a display name or the display name contains spaces, which is the case for Terraform-created
// SAs, just split the email instead.
if sa.DisplayName == "" || strings.Contains(sa.DisplayName, " ") {
sa.DisplayName = sa.Email[:strings.IndexByte(sa.Email, '@')]
}
modifiedSAs = append(modifiedSAs, sa)
}
return modifiedSAs, nil
}
func (o *GCGKEOptions) clusterExistsWithPrefix(clusters []cluster, clusterNamePrefix string) bool {
for _, cluster := range clusters {
if strings.HasPrefix(cluster.Name, clusterNamePrefix) {
return true
}
}
return false
}
func (o *GCGKEOptions) clusterExists(clusters []cluster, clusterName string) bool {
for _, cluster := range clusters {
if cluster.Name == clusterName {
return true
}
}
return false
}
func (o *GCGKEOptions) getCurrentGoogleProjectId() (string, error) {
cmd := "gcloud config get-value core/project"
data, err := o.GetCommandOutput("", "bash", "-c", cmd)
if err != nil {
return "", err
}
return data, nil
}
func (o *GCGKEOptions) getIamPolicy() (iamPolicy, error) {
cmd := fmt.Sprintf("gcloud projects get-iam-policy %s --format=json", o.Flags.ProjectID)
data, err := o.GetCommandOutput("", "bash", "-c", cmd)
if err != nil {
return iamPolicy{}, err
}
var policy iamPolicy
err = json.Unmarshal([]byte(data), &policy)
if err != nil {
return iamPolicy{}, err
}
return policy, nil
}
func (o *GCGKEOptions) determineUnusedIamBindings(policy iamPolicy) ([]string, error) {
var line []string
clusters, err := o.getClusters()
if err != nil {
return line, err
}
for _, b := range policy.Bindings {
for _, m := range b.Members {
if strings.HasPrefix(m, "deleted:serviceAccount:") || strings.HasPrefix(m, "serviceAccount:") {
saName := strings.TrimPrefix(m, "deleted:")
saName = strings.TrimPrefix(saName, "serviceAccount:")
displayName := saName[:strings.IndexByte(saName, '@')]
if isServiceAccount(displayName) {
if o.shouldRemoveServiceAccount(displayName, clusters) {
cmd := fmt.Sprintf("gcloud projects remove-iam-policy-binding %s --member=%s --role=%s --quiet", o.Flags.ProjectID, m, b.Role)
line = append(line, cmd)
}
}
}
}
}
return line, nil
}
func isServiceAccount(sa string) bool {
for _, suffix := range ServiceAccountSuffixes {
if strings.HasSuffix(sa, suffix) {
return true
}
}
return false
}