-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathautolbclean.go
286 lines (242 loc) · 6.83 KB
/
autolbclean.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
package autolbclean
import (
"context"
"net/http"
"strings"
"github.com/pkg/errors"
compute "google.golang.org/api/compute/v1"
)
func New(project string, oauthClient *http.Client) (*App, error) {
s, err := compute.New(oauthClient)
if err != nil {
return nil, errors.Wrap(err, `failed to create compute.Service`)
}
return &App{
project: project,
service: s,
}, nil
}
// Lists HTTP(s) forwarding rules, whose names match "k8s-fw"
func (app *App) ListIngressForwardingRules() ([]*compute.ForwardingRule, error) {
l, err := app.service.ForwardingRules.AggregatedList(app.project).Do()
if err != nil {
return nil, errors.Wrap(err, `failed to list forwarding rules`)
}
var result []*compute.ForwardingRule
for _, scopedList := range l.Items {
for _, fr := range scopedList.ForwardingRules {
if strings.HasPrefix(fr.Name, "k8s-fw") {
result = append(result, fr)
}
}
}
return result, nil
}
func ParseTargetProxy(s string) (name string, region string, isHTTPs bool, err error) {
var pos int
if i := strings.Index(s, `/targetHttpProxies`); i >= 0 {
pos = i
} else if i := strings.Index(s, `/targetHttpsProxies`); i >= 0 {
isHTTPs = true
pos = i
} else {
err = errors.New(`failed to find keywords targetHttpProxies or targetHttpsProxies`)
return
}
// find the region region and the name
// /$region/targetHttp(s)Proxy/$name$
if i := strings.LastIndex(s[:pos], "/"); i >= 0 {
region = s[i+1 : pos]
} else {
err = errors.New(`failed to find region`)
return
}
if i := strings.LastIndex(s[pos:], "/"); i >= 0 {
name = s[pos+i+1:]
} else {
err = errors.New(`failed to find name`)
return
}
return
}
func (app *App) GetTargetHttpsProxy(name string) (*compute.TargetHttpsProxy, error) {
return app.service.TargetHttpsProxies.Get(app.project, name).Do()
}
func (app *App) GetTargetHttpProxy(name string) (*compute.TargetHttpProxy, error) {
return app.service.TargetHttpProxies.Get(app.project, name).Do()
}
func ParseUrlMap(s string) (name string, region string, err error) {
var pos int
if i := strings.Index(s, `/urlMaps`); i >= 0 {
pos = i
} else {
err = errors.New(`failed to find keyword urlMaps`)
return
}
// find the region region and the name
// /$region/urlMaps/$name$
if i := strings.LastIndex(s[:pos], "/"); i >= 0 {
region = s[i+1 : pos]
} else {
err = errors.New(`failed to find region`)
return
}
if i := strings.LastIndex(s[pos:], "/"); i >= 0 {
name = s[pos+i+1:]
} else {
err = errors.New(`failed to find name`)
return
}
return
}
func (app *App) GetUrlMap(name string) (*compute.UrlMap, error) {
return app.service.UrlMaps.Get(app.project, name).Do()
}
func parseURL(s, keyword string) (name string, region string, err error) {
var pos int
if i := strings.Index(s, `/`+keyword); i >= 0 {
pos = i
} else {
err = errors.Errorf(`failed to find keyword %s`, keyword)
return
}
// find the region region and the name
// /$region/backendServices/$name$
if i := strings.LastIndex(s[:pos], "/"); i >= 0 {
region = s[i+1 : pos]
} else {
err = errors.New(`failed to find region`)
return
}
if i := strings.LastIndex(s[pos:], "/"); i >= 0 {
name = s[pos+i+1:]
} else {
err = errors.New(`failed to find name`)
return
}
return
}
func ParseService(s string) (name string, region string, err error) {
return parseURL(s, `backendServices`)
}
func (app *App) FindBackendServices(um *compute.UrlMap) ([]*compute.BackendService, error) {
var list []*compute.BackendService
for _, pm := range um.PathMatchers {
for _, pr := range pm.PathRules {
sname, region, err := ParseService(pr.Service)
if err != nil {
return nil, errors.Wrap(err, `failed to parse backend service url`)
}
_ = region
s, err := app.service.BackendServices.Get(app.project, sname).Do()
if err != nil {
return nil, errors.Wrap(err, `failed to get backend service`)
}
list = append(list, s)
}
}
return list, nil
}
func ParseInstanceGroup(s string) (name string, zone string, err error) {
var pos int
if i := strings.Index(s, `/instanceGroups`); i >= 0 {
pos = i
} else {
err = errors.New(`failed to find keyword instanceGroups`)
return
}
// find the region region and the name
// /$zone/instanceGroups/$name$
if i := strings.LastIndex(s[:pos], "/"); i >= 0 {
zone = s[i+1 : pos]
} else {
err = errors.New(`failed to find zone`)
return
}
if i := strings.LastIndex(s[pos:], "/"); i >= 0 {
name = s[pos+i+1:]
} else {
err = errors.New(`failed to find name`)
return
}
return
}
func (app *App) ListInstancesForService(s *compute.BackendService) ([]string, error) {
var list []string
for _, backend := range s.Backends {
name, zone, err := ParseInstanceGroup(backend.Group)
if err != nil {
return nil, errors.Wrap(err, `failed to parse instance group url`)
}
instances, err := app.service.InstanceGroups.ListInstances(app.project, zone, name,
&compute.InstanceGroupsListInstancesRequest{
InstanceState: "ALL",
},
).Do()
// For this operation, we ignore errors
if err != nil {
continue
}
for _, instance := range instances.Items {
list = append(list, instance.Instance)
}
}
return list, nil
}
func ParseSslCertificates(s string) (name string, region string, err error) {
return parseURL(s, `sslCertificates`)
}
func ParseBackendServices(s string) (name string, region string, err error) {
return parseURL(s, `backendServices`)
}
func ParseHealthChecks(s string) (name string, region string, err error) {
return parseURL(s, `healthChecks`)
}
func (app *App) ListDanglingFirewalls(ctx context.Context) ([]*compute.Firewall, error) {
firewalls, err := app.service.Firewalls.List(app.project).Do()
if err != nil {
return nil, errors.Wrap(err, `failed to list firewall rules`)
}
tags2fws := make(map[string][]*compute.Firewall)
for _, fw := range firewalls.Items {
// We only care about gke-* tags
for _, tag := range fw.TargetTags {
if !strings.HasPrefix(tag, `gke-`) {
continue
}
tags2fws[tag] = append(tags2fws[tag], fw)
}
}
// Now we have the list of firewalls that are referenced by a particular tag
// next, find the list of gke nodes and their tags
// we need to know the zones
zones, err := app.service.Zones.List(app.project).Do()
if err != nil {
return nil, errors.Wrap(err, `faild to list zones`)
}
for _, zone := range zones.Items {
// if we don't have any more tags to check for, we're done
if len(tags2fws) == 0 {
break
}
instances, err := app.service.Instances.List(app.project, zone.Name).Do()
if err != nil {
return nil, errors.Wrap(err, `failed to list instances`)
}
for _, instance := range instances.Items {
for _, tag := range instance.Tags.Items {
if !strings.HasPrefix(tag, `gke-`) {
continue
}
delete(tags2fws, tag)
}
}
}
var ret []*compute.Firewall
for _, fws := range tags2fws {
for _, fw := range fws {
ret = append(ret, fw)
}
}
return ret, nil
}