-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_delete.go
508 lines (421 loc) · 11.8 KB
/
deep_delete.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
/*
Copyright 2018 Turbine Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"strings"
"github.com/turbinelabs/api"
"github.com/turbinelabs/api/service"
"github.com/turbinelabs/cli/terminal"
"github.com/turbinelabs/nonstdlib/log/console"
tbnos "github.com/turbinelabs/nonstdlib/os"
)
type proxyMod struct {
proxy api.Proxy
domainsToRemove map[api.DomainKey]api.Domain
}
type deleter struct {
svc *unifiedSvc
zone api.Zone
clusters map[api.ClusterKey]api.Cluster
domains map[api.DomainKey]api.Domain
routes map[api.RouteKey]api.Route
srs map[api.SharedRulesKey]api.SharedRules
proxies map[api.ProxyKey]api.Proxy
proxyMods map[api.ProxyKey]*proxyMod
listeners map[api.ListenerKey]api.Listener
domainsForReport map[api.DomainKey]api.Domain
}
func newDeleter(svc *unifiedSvc) *deleter {
return &deleter{
svc: svc,
clusters: make(map[api.ClusterKey]api.Cluster),
domains: make(map[api.DomainKey]api.Domain),
routes: make(map[api.RouteKey]api.Route),
srs: make(map[api.SharedRulesKey]api.SharedRules),
proxies: make(map[api.ProxyKey]api.Proxy),
proxyMods: make(map[api.ProxyKey]*proxyMod),
listeners: make(map[api.ListenerKey]api.Listener),
domainsForReport: make(map[api.DomainKey]api.Domain),
}
}
func (d *deleter) zoneIsSet() bool {
return !d.zone.Equals(api.Zone{})
}
func (d *deleter) addZoneKey(zk api.ZoneKey) error {
if d.zoneIsSet() {
return fmt.Errorf("cannot delete more than one zone")
}
z, err := d.svc.Zone().Get(zk)
if err != nil {
return err
}
if (api.Zone{}.Equals(z)) {
return fmt.Errorf("no zone found for key %s", zk)
}
d.zone = z
clusters, err := d.svc.Cluster().Index(service.ClusterFilter{ZoneKey: zk})
if err != nil {
return err
}
for _, c := range clusters {
d.clusters[c.ClusterKey] = c
}
routes, err := d.svc.Route().Index(service.RouteFilter{ZoneKey: zk})
if err != nil {
return err
}
for _, r := range routes {
d.routes[r.RouteKey] = r
}
srs, err := d.svc.SharedRules().Index(service.SharedRulesFilter{ZoneKey: zk})
if err != nil {
return err
}
for _, sr := range srs {
d.srs[sr.SharedRulesKey] = sr
}
domains, err := d.svc.Domain().Index(service.DomainFilter{ZoneKey: zk})
if err != nil {
return err
}
for _, dom := range domains {
d.domains[dom.DomainKey] = dom
}
proxies, err := d.svc.Proxy().Index(service.ProxyFilter{ZoneKey: zk})
if err != nil {
return err
}
for _, p := range proxies {
d.proxies[p.ProxyKey] = p
}
return nil
}
func (d *deleter) addDomainKey(dk api.DomainKey) error {
if _, ok := d.domains[dk]; ok {
return nil
}
dom, err := d.svc.Domain().Get(dk)
if err != nil {
return err
}
if (api.Domain{}.Equals(dom)) {
return fmt.Errorf("no domain found for key %s", dk)
}
return d.addDomain(dom)
}
func (d *deleter) addDomain(dom api.Domain) error {
dk := dom.DomainKey
if _, ok := d.domains[dk]; ok {
return nil
}
d.domains[dk] = dom
// add any routes that may also need to be deleted
rs, err := d.svc.Route().Index(service.RouteFilter{DomainKey: dk})
if err != nil {
return err
}
for _, r := range rs {
if err := d.addRoute(r); err != nil {
return err
}
}
// find proxies for which the domain needs to be removed
ps, err := d.svc.Proxy().Index(service.ProxyFilter{DomainKeys: []api.DomainKey{dk}})
if err != nil {
return err
}
for _, p := range ps {
if _, ok := d.proxyMods[p.ProxyKey]; !ok {
d.proxyMods[p.ProxyKey] = &proxyMod{p, map[api.DomainKey]api.Domain{dom.DomainKey: dom}}
} else {
d.proxyMods[p.ProxyKey].domainsToRemove[dom.DomainKey] = dom
}
}
return nil
}
func (d *deleter) addRouteKey(rk api.RouteKey) error {
if _, ok := d.routes[rk]; ok {
return nil
}
r, err := d.svc.Route().Get(rk)
if err != nil {
return err
}
if (api.Route{}.Equals(r)) {
return fmt.Errorf("no route found for key %s", rk)
}
return d.addRoute(r)
}
func (d *deleter) addRoute(r api.Route) error {
rk := r.RouteKey
if _, ok := d.routes[rk]; !ok {
d.routes[rk] = r
}
if dom, ok := d.domains[r.DomainKey]; ok {
d.domainsForReport[dom.DomainKey] = dom
return nil
}
dom, err := d.svc.Domain().Get(r.DomainKey)
if err != nil {
return err
}
d.domainsForReport[dom.DomainKey] = dom
return nil
}
func (d *deleter) addSharedRulesKey(srk api.SharedRulesKey) error {
if _, ok := d.srs[srk]; ok {
return nil
}
sr, err := d.svc.SharedRules().Get(srk)
if err != nil {
return err
}
if (api.SharedRules{}.Equals(sr)) {
return fmt.Errorf("no shared_rules found for key %s", srk)
}
return d.addSharedRules(sr)
}
func (d *deleter) addSharedRules(sr api.SharedRules) error {
srk := sr.SharedRulesKey
if _, ok := d.srs[srk]; ok {
return nil
}
d.srs[srk] = sr
rs, err := d.svc.Route().Index(service.RouteFilter{SharedRulesKey: srk})
if err != nil {
return err
}
for _, r := range rs {
d.addRoute(r)
}
return nil
}
func (d *deleter) addOrphans() error {
// collect shared rules keys from routes
candidates := map[api.SharedRulesKey]bool{}
for _, r := range d.routes {
candidates[r.SharedRulesKey] = true
}
if len(candidates) == 0 {
return nil
}
// look up routes with those shared rules keys
var filters []service.RouteFilter
for srk := range candidates {
filters = append(filters, service.RouteFilter{SharedRulesKey: srk})
}
rs, err := d.svc.Route().Index(filters...)
if err != nil {
return err
}
// if any of these routes is not amongst the routes to be deleted,
// remove the SR from the candidates
for _, r := range rs {
if _, ok := d.routes[r.RouteKey]; !ok && candidates[r.SharedRulesKey] {
delete(candidates, r.SharedRulesKey)
}
}
if len(candidates) == 0 {
return nil
}
for srk := range candidates {
if err := d.addSharedRulesKey(srk); err != nil {
return err
}
}
return nil
}
func clusterStr(c api.Cluster) string {
return fmt.Sprintf("Cluster(%s:%s)", c.ClusterKey, c.Name)
}
func routeStr(r api.Route, d api.Domain) string {
return fmt.Sprintf("Route(%s:%s:%d%s)", r.RouteKey, d.Name, d.Port, r.Path)
}
func srStr(sr api.SharedRules) string {
return fmt.Sprintf("SharedRules(%s:%s)", sr.SharedRulesKey, sr.Name)
}
func domainStr(d api.Domain) string {
return fmt.Sprintf("Domain(%s:%s:%d)", d.DomainKey, d.Name, d.Port)
}
func proxyStr(p api.Proxy) string {
return fmt.Sprintf("Proxy(%s:%s)", p.ProxyKey, p.Name)
}
func zoneStr(z api.Zone) string {
return fmt.Sprintf("Zone(%s:%s)", z.ZoneKey, z.Name)
}
func proxyModStr(pm *proxyMod, indent, verb string) string {
dNames := []string{}
for dk, dom := range pm.domainsToRemove {
dNames = append(dNames, fmt.Sprintf("%s:%s:%d", dk, dom.Name, dom.Port))
}
noun := "Domain"
if len(dNames) > 1 {
noun += "s"
}
return fmt.Sprintf(
"%sProxy(%s:%s):\n%s %s %s: %s",
indent,
pm.proxy.ProxyKey,
pm.proxy.Name,
indent,
verb,
noun,
strings.Join(dNames, ", "),
)
}
func (d *deleter) report() string {
str := "Deep deletion will delete the following objects:\n"
for _, r := range d.routes {
dom := d.domainsForReport[r.DomainKey]
str += " " + routeStr(r, dom) + "\n"
}
for _, sr := range d.srs {
str += " " + srStr(sr) + "\n"
}
for _, p := range d.proxies {
str += " " + proxyStr(p) + "\n"
}
for _, dom := range d.domains {
str += " " + domainStr(dom) + "\n"
}
for _, c := range d.clusters {
str += " " + clusterStr(c) + "\n"
}
if d.zoneIsSet() {
str += " " + zoneStr(d.zone) + "\n"
}
if len(d.proxyMods) != 0 {
str += "Additionally, the following proxies will be modified:\n"
for _, pm := range d.proxyMods {
str += proxyModStr(pm, " ", "Remove") + "\n"
}
}
str += "Proceed?"
return str
}
func (d *deleter) execute() error {
if !d.zoneIsSet() {
d.addOrphans()
}
if ok, err := terminal.Ask(tbnos.New(), d.report()); err != nil {
return err
} else if !ok {
return fmt.Errorf("canceled deep deletion")
}
for _, r := range d.routes {
dom := d.domainsForReport[r.DomainKey]
console.Info().Printf("Deleting %s", routeStr(r, dom))
if err := d.svc.Route().Delete(r.RouteKey, r.Checksum); err != nil {
return err
}
console.Info().Printf("Deleted %s", routeStr(r, dom))
}
for _, sr := range d.srs {
console.Info().Printf("Deleting %s", srStr(sr))
if err := d.svc.SharedRules().Delete(sr.SharedRulesKey, sr.Checksum); err != nil {
return err
}
console.Info().Printf("Deleted %s", srStr(sr))
}
for _, p := range d.proxies {
console.Info().Printf("Deleting %s", proxyStr(p))
if err := d.svc.Proxy().Delete(p.ProxyKey, p.Checksum); err != nil {
return err
}
console.Info().Printf("Deleted %s", proxyStr(p))
}
for _, pm := range d.proxyMods {
p := pm.proxy
var dks []api.DomainKey
for _, dk := range p.DomainKeys {
if _, ok := pm.domainsToRemove[dk]; !ok {
// only add back in if not to be removed
dks = append(dks, dk)
}
}
p.DomainKeys = dks
console.Info().Println(proxyModStr(pm, "", "Deleting"))
if _, err := d.svc.Proxy().Modify(p); err != nil {
return err
}
console.Info().Println(proxyModStr(pm, "", "Deleted"))
}
for _, dom := range d.domains {
console.Info().Printf("Deleting %s", domainStr(dom))
if err := d.svc.Domain().Delete(dom.DomainKey, dom.Checksum); err != nil {
return err
}
console.Info().Printf("Deleted %s", domainStr(dom))
}
for _, c := range d.clusters {
console.Info().Printf("Deleting %s", clusterStr(c))
if err := d.svc.Cluster().Delete(c.ClusterKey, c.Checksum); err != nil {
return err
}
console.Info().Printf("Deleted %s", clusterStr(c))
}
if d.zoneIsSet() {
console.Info().Printf("Deleting %s", zoneStr(d.zone))
if err := d.svc.Zone().Delete(d.zone.ZoneKey, d.zone.Checksum); err != nil {
return err
}
console.Info().Printf("Deleted %s", zoneStr(d.zone))
}
return nil
}
func (a clusterAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
console.Error().Println("warning: --deep ignored for cluster delete")
return a.Delete(k, cs)
}
func (a domainAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
d := newDeleter(svc)
if err := d.addDomainKey(api.DomainKey(k)); err != nil {
return err
}
return d.execute()
}
func (a proxyAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
console.Error().Println("warning: --deep ignored for proxy delete")
return a.Delete(k, cs)
}
// per #5630 we should figure out what to do with listeners in a deep delete
func (a listenerAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
console.Error().Println("warning: --deep ignored for listener delete")
return a.Delete(k, cs)
}
func (a routeAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
d := newDeleter(svc)
if err := d.addRouteKey(api.RouteKey(k)); err != nil {
return err
}
return d.execute()
}
func (a sharedRulesAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
d := newDeleter(svc)
if err := d.addSharedRulesKey(api.SharedRulesKey(k)); err != nil {
return err
}
return d.execute()
}
func (a userAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
console.Error().Println("warning: --deep ignored for user delete")
return a.Delete(k, cs)
}
func (a zoneAdapter) DeepDelete(k string, cs api.Checksum, svc *unifiedSvc) error {
d := newDeleter(svc)
if err := d.addZoneKey(api.ZoneKey(k)); err != nil {
return err
}
return d.execute()
}