-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ali.go
814 lines (686 loc) · 22.5 KB
/
ali.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/*
Copyright 2019 The Kubernetes Authors.
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 ali
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"k8s.io/klog/v2"
common "github.com/denverdino/aliyungo/common"
ecs "github.com/denverdino/aliyungo/ecs"
ess "github.com/denverdino/aliyungo/ess"
ram "github.com/denverdino/aliyungo/ram"
slb "github.com/denverdino/aliyungo/slb"
"k8s.io/kops/pkg/resources"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/alitasks"
"k8s.io/kops/upup/pkg/fi/cloudup/aliup"
)
type aliListFn func() ([]*resources.Resource, error)
const (
typeScalingGroup = "ScalingGroup"
typeLoadBalancer = "LoadBalancer"
typeSecurityGroup = "SecurityGroup"
typeSecurityGroupRole = "SecurityGroupRole"
typeVswitch = "Vswitch"
typeRamRole = "RamRole"
typeVolume = "Volume"
typeSSHKey = "SSHKey"
typeVPC = "VPC"
typeNatGateway = "NatGateway"
typeEIP = "EIP"
)
type clusterDiscoveryALI struct {
aliCloud aliup.ALICloud
clusterName string
scalingGroup map[string]string
}
func ListResourcesALI(aliCloud aliup.ALICloud, clusterName string, region string) (map[string]*resources.Resource, error) {
resources := make(map[string]*resources.Resource)
d := clusterDiscoveryALI{
aliCloud: aliCloud,
clusterName: clusterName,
}
d.scalingGroup = make(map[string]string)
// SecurityGroup and VPC should be deleted after ScalingGroup.
// We list ScalingGroups first to configure dependencies' blocked parameter.
resourceTrackers, err := d.ListScalingGroups()
if err != nil {
return nil, err
}
for _, t := range resourceTrackers {
d.scalingGroup[t.Name] = t.ID
resources[t.Type+":"+t.ID] = t
}
listFunctions := []aliListFn{
d.ListLoadBalancer,
d.ListRam,
d.ListSecurityGroup,
d.ListSSHKey,
d.ListVPC,
d.ListVolume,
}
for _, fn := range listFunctions {
resourceTrackers, err := fn()
if err != nil {
return nil, err
}
for _, t := range resourceTrackers {
resources[t.Type+":"+t.ID] = t
}
}
for k, t := range resources {
if t.Done {
delete(resources, k)
}
}
return resources, nil
}
func (d *clusterDiscoveryALI) ListScalingGroups() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
// ScalingGroup Name defined in /pkg/model/alimodel/context.go
// The should be changed at the same time.
remove := []string{
"masters." + d.clusterName,
"nodes." + d.clusterName,
"bastions." + d.clusterName,
}
pageNumber := 1
pageSize := 50
for {
describeScalingGroupsArgs := &ess.DescribeScalingGroupsArgs{
RegionId: common.Region(d.aliCloud.Region()),
Pagination: common.Pagination{
PageNumber: pageNumber,
PageSize: pageSize,
},
}
groups, _, err := d.aliCloud.EssClient().DescribeScalingGroups(describeScalingGroupsArgs)
if err != nil {
return nil, fmt.Errorf("error listing ScalingGroup: %v", err)
}
scalingGroups := []ess.ScalingGroupItemType{}
for _, group := range groups {
for _, r := range remove {
if strings.Contains(group.ScalingGroupName, r) {
scalingGroups = append(scalingGroups, group)
}
}
}
for _, scalingGroup := range scalingGroups {
resourceTracker := &resources.Resource{
Name: scalingGroup.ScalingGroupName,
ID: scalingGroup.ScalingGroupId,
Type: typeScalingGroup,
Deleter: DeleteScalingGroup,
}
d.scalingGroup[scalingGroup.ScalingGroupName] = scalingGroup.ScalingGroupId
resourceTrackers = append(resourceTrackers, resourceTracker)
}
if len(groups) < pageSize {
break
} else {
pageNumber++
}
}
return resourceTrackers, nil
}
func DeleteScalingGroup(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
klog.V(2).Infof("Removing ScalingGroup with Id %s", r.ID)
// Force to delete the ScalingGroup
// TODO: Should we delete the group softly? Like set ForceDelete to false.
// That will be safer.
// All the resource of the ScalingGroup will be deleted.
deleteScalingGroupArgs := &ess.DeleteScalingGroupArgs{
ScalingGroupId: r.ID,
ForceDelete: true,
}
_, err := c.EssClient().DeleteScalingGroup(deleteScalingGroupArgs)
if err != nil {
return fmt.Errorf("error deleting ScalingGroup: %v", err)
}
return nil
}
func (d *clusterDiscoveryALI) ListLoadBalancer() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
// LoadBalancer has the cluster tag: KubernetesCluster:$ClusterName
// Find the cluster loadBalancer by this key value pair.
// TODO: Should we check the tags of loadbalancer?
// ScalingGroup Name defined in /pkg/model/alimodel/context.go
// The should be changed at the same time.
// ScalingGroup can not be renamed in alicloud, so we recognize by name
loadBalancerName := "api." + d.clusterName
describeLoadBalancersArgs := &slb.DescribeLoadBalancersArgs{
RegionId: common.Region(d.aliCloud.Region()),
LoadBalancerName: loadBalancerName,
}
loadBalancers, err := d.aliCloud.SlbClient().DescribeLoadBalancers(describeLoadBalancersArgs)
if err != nil {
return nil, fmt.Errorf("err listing LoadBalancers")
}
if len(loadBalancers) > 1 {
return nil, fmt.Errorf("found multiple LoadBalancers with the same name: %v", loadBalancerName)
}
if len(loadBalancers) == 1 {
resourceTracker := &resources.Resource{
Name: loadBalancers[0].LoadBalancerName,
ID: loadBalancers[0].LoadBalancerId,
Type: typeLoadBalancer,
Deleter: DeleteLoadBalancer,
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
return resourceTrackers, nil
}
func DeleteLoadBalancer(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
klog.V(2).Infof("Removing LoadBalancer with Id %s", r.ID)
err := c.SlbClient().DeleteLoadBalancer(r.ID)
if err != nil {
return fmt.Errorf("err deleting LoadBalancer:%v", err)
}
return nil
}
func (d *clusterDiscoveryALI) ListSecurityGroup() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
// SecurityGroup's name can be renamed in alicloud.
// We recognize SecurityGroup with its name and clusterTags
tags := make(map[string]string)
d.aliCloud.AddClusterTags(tags)
securityGroupNames := []string{
"masters." + d.clusterName,
"bastions." + d.clusterName,
"nodes." + d.clusterName,
}
describeResourceByTagsArgs := &ecs.DescribeResourceByTagsArgs{
ResourceType: ecs.TagResourceType(alitasks.SecurityResource),
RegionId: common.Region(d.aliCloud.Region()),
Tag: tags,
}
resourceList, _, err := d.aliCloud.EcsClient().DescribeResourceByTags(describeResourceByTagsArgs)
if err != nil {
return nil, fmt.Errorf("err listing securityGroup:%v", err)
}
blocked := []string{}
groupTrackers := []*resources.Resource{}
if len(resourceList) != 0 {
for _, resource := range resourceList {
find := false
describeSecurityGroupAttributeArgs := &ecs.DescribeSecurityGroupAttributeArgs{
SecurityGroupId: resource.ResourceId,
RegionId: resource.RegionId,
}
respGroup, err := d.aliCloud.EcsClient().DescribeSecurityGroupAttribute(describeSecurityGroupAttributeArgs)
if err != nil {
return nil, fmt.Errorf("err listing securityGroup:%v", err)
}
for _, value := range securityGroupNames {
if respGroup.SecurityGroupName == value {
find = true
}
}
if find {
groupTracker := &resources.Resource{
Name: respGroup.SecurityGroupName,
ID: respGroup.SecurityGroupId,
Type: typeSecurityGroup,
Deleter: DeleteSecurityGroup,
}
groupTrackers = append(groupTrackers, groupTracker)
if len(respGroup.Permissions.Permission) != 0 {
for i, securityGroupRole := range respGroup.Permissions.Permission {
roleTracker := &resources.Resource{
Name: respGroup.SecurityGroupId,
ID: respGroup.SecurityGroupName + strconv.Itoa(i),
Type: typeSecurityGroupRole,
Deleter: DeleteSecurityGroupRole,
Obj: securityGroupRole,
}
resourceTrackers = append(resourceTrackers, roleTracker)
// Any SecurityGroupRole which depends on the current SecurityGroup should be deleted before delete the SecurityGroup.
// So before deleting any SecurityGroup, we should delete all SecurityGroupRoles of all SecurityGroups.
blocked = append(blocked, roleTracker.Type+":"+roleTracker.ID)
}
}
}
}
}
if len(groupTrackers) != 0 {
for _, groupTracker := range groupTrackers {
for scalingGroupName, scalingGroupId := range d.scalingGroup {
if strings.Contains(scalingGroupName, groupTracker.Name) {
groupTracker.Blocked = append(groupTracker.Blocked, typeScalingGroup+":"+scalingGroupId)
}
}
groupTracker.Blocked = append(groupTracker.Blocked, blocked...)
resourceTrackers = append(resourceTrackers, groupTracker)
}
}
return resourceTrackers, nil
}
func DeleteSecurityGroup(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
region := common.Region(c.Region())
klog.V(2).Infof("Removing SecurityGroup with Id %s", r.ID)
err := c.EcsClient().DeleteSecurityGroup(region, r.ID)
if err != nil {
return fmt.Errorf("err deleting securityGroup:%v", err)
}
return nil
}
func DeleteSecurityGroupRole(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
permission := r.Obj.(ecs.PermissionType)
klog.V(2).Infof("Removing SecurityGroupRole of SecurityGroup %s", r.Name)
if permission.Direction == "ingress" {
authorizeSecurityGroupArgs := ecs.AuthorizeSecurityGroupArgs{
SecurityGroupId: r.Name,
RegionId: common.Region(c.Region()),
IpProtocol: permission.IpProtocol,
PortRange: permission.PortRange,
SourceGroupId: permission.SourceGroupId,
SourceGroupOwnerAccount: permission.SourceGroupOwnerAccount,
SourceCidrIp: permission.SourceCidrIp,
Policy: permission.Policy,
Priority: permission.Priority,
NicType: permission.NicType,
}
revokeSecurityGroupArgs := &ecs.RevokeSecurityGroupArgs{
AuthorizeSecurityGroupArgs: authorizeSecurityGroupArgs,
}
err := c.EcsClient().RevokeSecurityGroup(revokeSecurityGroupArgs)
if err != nil {
return fmt.Errorf("err deleting securityGroup Role:%v", err)
}
}
if permission.Direction == "egress" {
authorizeSecurityGroupEgressArgs := ecs.AuthorizeSecurityGroupEgressArgs{
SecurityGroupId: r.Name,
RegionId: common.Region(c.Region()),
IpProtocol: permission.IpProtocol,
PortRange: permission.PortRange,
DestGroupId: permission.DestGroupId,
DestGroupOwnerAccount: permission.DestGroupOwnerAccount,
DestCidrIp: permission.DestCidrIp,
Policy: permission.Policy,
Priority: permission.Priority,
NicType: permission.NicType,
}
revokeSecurityGroupEgressArgs := &ecs.RevokeSecurityGroupEgressArgs{
AuthorizeSecurityGroupEgressArgs: authorizeSecurityGroupEgressArgs,
}
err := c.EcsClient().RevokeSecurityGroupEgress(revokeSecurityGroupEgressArgs)
if err != nil {
return fmt.Errorf("err deleting securityGroup Role:%v", err)
}
}
return nil
}
func (d *clusterDiscoveryALI) ListRam() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
clusterName := strings.Replace(d.clusterName, ".", "-", -1)
names := []string{
"masters-" + clusterName,
"nodes-" + clusterName,
"bastions-" + clusterName,
}
response, err := d.aliCloud.RamClient().ListRoles()
if err != nil {
return nil, fmt.Errorf("err listing RamRole:%v", err)
}
if len(response.Roles.Role) != 0 {
for _, role := range response.Roles.Role {
for _, roleName := range names {
if role.RoleName == roleName {
resourceTracker := &resources.Resource{
Name: role.RoleName,
ID: role.RoleId,
Type: typeRamRole,
Deleter: DeleteRoleRam,
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
}
}
}
return resourceTrackers, nil
}
func DeleteRoleRam(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
policies := []string{}
klog.V(2).Infof("Removing RamRole %s", r.Name)
roleQueryRequest := ram.RoleQueryRequest{
RoleName: r.Name,
}
response, err := c.RamClient().ListPoliciesForRole(roleQueryRequest)
if err != nil {
return fmt.Errorf("err listing Policies for role:%v", err)
}
if len(response.Policies.Policy) != 0 {
for _, policy := range response.Policies.Policy {
policies = append(policies, policy.PolicyName)
}
}
for _, policy := range policies {
klog.V(2).Infof("Removing RolePolicy %s of RamRole %s", policy, r.Name)
policyRequest := ram.PolicyRequest{
PolicyName: policy,
PolicyType: ram.Custom,
}
attachPolicyToRoleRequest := ram.AttachPolicyToRoleRequest{
PolicyRequest: policyRequest,
RoleName: r.Name,
}
_, err := c.RamClient().DetachPolicyFromRole(attachPolicyToRoleRequest)
if err != nil {
return fmt.Errorf("err detaching policy from role:%v", err)
}
_, err = c.RamClient().DeletePolicy(policyRequest)
if err != nil {
return fmt.Errorf("err deleting policy:%v", err)
}
}
_, err = c.RamClient().DeleteRole(roleQueryRequest)
if err != nil {
return fmt.Errorf("err deleting ram role:%v", err)
}
return nil
}
func (d *clusterDiscoveryALI) ListSSHKey() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
name := "k8s.sshkey." + d.clusterName
resourceTracker := &resources.Resource{
Name: name,
ID: name,
Type: typeSSHKey,
Deleter: DeleteSSHKey,
}
resourceTrackers = append(resourceTrackers, resourceTracker)
return resourceTrackers, nil
}
func DeleteSSHKey(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
region := common.Region(c.Region())
klog.V(2).Infof("Removing SSHKey %s", r.Name)
deleteKeyPairsArgs := &ecs.DeleteKeyPairsArgs{
RegionId: region,
}
keyPairs := []string{r.Name}
keyPairsJson, _ := json.Marshal(keyPairs)
deleteKeyPairsArgs.KeyPairNames = string(keyPairsJson)
err := c.EcsClient().DeleteKeyPairs(deleteKeyPairsArgs)
if err != nil {
return fmt.Errorf("err deleting sshkey pairs:%v", err)
}
return nil
}
func (d *clusterDiscoveryALI) ListVPC() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
// Delete VPC with specified name. All of the Switches will be deleted.
// We think the VPC which owns the designated name is owned.
name := d.clusterName
vpcsToDelete := []string{}
vswitchsToDelete := []string{}
pageNumber := 1
pageSize := 50
for {
describeVpcsArgs := &ecs.DescribeVpcsArgs{
RegionId: common.Region(d.aliCloud.Region()),
Pagination: common.Pagination{
PageNumber: pageNumber,
PageSize: pageSize,
},
}
vpcs, _, err := d.aliCloud.EcsClient().DescribeVpcs(describeVpcsArgs)
if err != nil {
return nil, fmt.Errorf("err listing VPC:%v", err)
}
if len(vpcs) != 0 {
for _, vpc := range vpcs {
if name == vpc.VpcName {
vpcsToDelete = append(vpcsToDelete, vpc.VpcId)
vswitchsToDelete = append(vswitchsToDelete, vpc.VSwitchIds.VSwitchId...)
}
}
}
if len(vpcs) < pageSize {
break
} else {
pageNumber++
}
}
if len(vpcsToDelete) > 1 {
klog.V(8).Infof("Found multiple vpcs with name %q", name)
} else if len(vpcsToDelete) == 1 {
vpcTracker := &resources.Resource{
Name: name,
ID: vpcsToDelete[0],
Type: typeVPC,
Deleter: DeleteVPC,
}
resourceTrackers = append(resourceTrackers, vpcTracker)
for _, vswitchId := range vswitchsToDelete {
resourceTracker := &resources.Resource{
Name: name,
ID: vswitchId,
Type: typeVswitch,
Deleter: DeleteVswitch,
}
resourceTracker.Blocks = append(resourceTracker.Blocks, typeVPC+":"+vpcTracker.ID)
// Waiting for all autoScalingGroups to be deleted.
for _, scalingGroupId := range d.scalingGroup {
resourceTracker.Blocked = append(resourceTracker.Blocked, typeScalingGroup+":"+scalingGroupId)
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
ListNatGateway(d, vpcsToDelete[0], &resourceTrackers)
}
return resourceTrackers, nil
}
func DeleteVswitch(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
klog.V(2).Infof("Removing Vswitch with Id %s", r.ID)
err := c.EcsClient().DeleteVSwitch(r.ID)
if err != nil {
return fmt.Errorf("err deleting Vswitch:%v", err)
}
return nil
}
func DeleteVPC(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
klog.V(2).Infof("Removing VPC with Id %s", r.ID)
err := c.EcsClient().DeleteVpc(r.ID)
if err != nil {
return fmt.Errorf("err deleting VPC:%v", err)
}
return nil
}
func (d *clusterDiscoveryALI) ListVolume() ([]*resources.Resource, error) {
var resourceTrackers []*resources.Resource
// Volume has cluster tags
tags := make(map[string]string)
d.aliCloud.AddClusterTags(tags)
disksToDelete := []string{}
pageNumber := 1
pageSize := 50
for {
describeDisksArgs := &ecs.DescribeDisksArgs{
RegionId: common.Region(d.aliCloud.Region()),
Tag: tags,
DiskType: ecs.DiskTypeAllData,
Pagination: common.Pagination{
PageNumber: pageNumber,
PageSize: pageSize,
},
}
disks, _, err := d.aliCloud.EcsClient().DescribeDisks(describeDisksArgs)
if err != nil {
return nil, fmt.Errorf("err listing disks:%v", err)
}
if len(disks) != 0 {
for _, disk := range disks {
disksToDelete = append(disksToDelete, disk.DiskId)
}
}
if len(disks) < pageSize {
break
} else {
pageNumber++
}
}
if len(disksToDelete) == 0 {
klog.V(8).Infof("Found no disks to delete")
} else {
for _, disk := range disksToDelete {
resourceTracker := &resources.Resource{
Name: disk,
ID: disk,
Type: typeVolume,
Deleter: DeleteVolume,
}
resourceTrackers = append(resourceTrackers, resourceTracker)
}
}
return resourceTrackers, nil
}
func DeleteVolume(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
klog.V(2).Infof("Removing Disk with Id %s", r.ID)
err := c.EcsClient().DeleteDisk(r.ID)
if err != nil {
return fmt.Errorf("err deleting volume:%v", err)
}
return nil
}
func ListNatGateway(d *clusterDiscoveryALI, vpcID string, resourceTrackers *[]*resources.Resource) error {
// Delete NateGateway with specified name. All of the EIPs will be deleted.
// We think the NateGateway which owns the designated name is owned.
natGatewaysToDelete := []string{}
eipToDelete := []string{}
name := d.clusterName
pageNumber := 1
pageSize := 50
for {
describeNatGatewaysArgs := &ecs.DescribeNatGatewaysArgs{
RegionId: common.Region(d.aliCloud.Region()),
VpcId: vpcID,
Pagination: common.Pagination{
PageNumber: pageNumber,
PageSize: pageSize,
},
}
natGateways, _, err := d.aliCloud.VpcClient().DescribeNatGateways(describeNatGatewaysArgs)
if err != nil {
return fmt.Errorf("err listing NatGateway:%v", err)
}
if len(natGateways) != 0 {
for _, nateGateway := range natGateways {
natGatewaysToDelete = append(natGatewaysToDelete, nateGateway.NatGatewayId)
for _, ip := range nateGateway.IpLists.IpList {
eipToDelete = append(eipToDelete, ip.AllocationId)
}
}
}
if len(natGateways) < pageSize {
break
} else {
pageNumber++
}
}
if len(natGatewaysToDelete) > 1 {
klog.V(8).Infof("Found multiple natGateways with name %q", name)
} else if len(natGatewaysToDelete) == 1 {
natGatwayTracker := &resources.Resource{
Name: name,
ID: natGatewaysToDelete[0],
Type: typeNatGateway,
Deleter: DeleteNatGateway,
}
*resourceTrackers = append(*resourceTrackers, natGatwayTracker)
natGatwayTracker.Blocks = append(natGatwayTracker.Blocks, typeVPC+":"+vpcID)
for _, eip := range eipToDelete {
resourceTracker := &resources.Resource{
Name: name,
ID: eip,
Type: typeEIP,
Deleter: DeleteEIP,
}
resourceTracker.Blocked = append(resourceTracker.Blocked, typeNatGateway+":"+natGatwayTracker.ID)
*resourceTrackers = append(*resourceTrackers, resourceTracker)
}
}
return nil
}
func DeleteNatGateway(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
describeNatGatewaysArgs := &ecs.DescribeNatGatewaysArgs{
RegionId: common.Region(c.Region()),
NatGatewayId: r.ID,
}
natGateways, _, err := c.VpcClient().DescribeNatGateways(describeNatGatewaysArgs)
if err != nil {
return fmt.Errorf("err listing NatGateway:%v", err)
}
if len(natGateways) > 1 {
klog.V(8).Infof("Found multiple natGateways with ID %q", r.ID)
} else if len(natGateways) == 1 {
for _, snatTableId := range natGateways[0].SnatTableIds.SnatTableId {
describeSnatTableEntriesArgs := &ecs.DescribeSnatTableEntriesArgs{
RegionId: common.Region(c.Region()),
SnatTableId: snatTableId,
}
snatTableEntries, _, err := c.VpcClient().DescribeSnatTableEntries(describeSnatTableEntriesArgs)
if err != nil {
return fmt.Errorf("err Listing snatTableEntries:%v", err)
}
for _, snatTableEntry := range snatTableEntries {
deleteSnatEntryArgs := &ecs.DeleteSnatEntryArgs{
RegionId: common.Region(c.Region()),
SnatTableId: snatTableId,
SnatEntryId: snatTableEntry.SnatEntryId,
}
err := c.VpcClient().DeleteSnatEntry(deleteSnatEntryArgs)
if err != nil {
return fmt.Errorf("err deleting SnatEntryArgs:%v", err)
}
}
}
for _, ip := range natGateways[0].IpLists.IpList {
err := c.VpcClient().UnassociateEipAddress(ip.AllocationId, r.ID)
if err != nil {
return fmt.Errorf("err unassociating EIP:%v", err)
}
}
}
klog.V(2).Infof("Removing NatGateway with Id %s", r.ID)
deleteNatGatewayArgs := &ecs.DeleteNatGatewayArgs{
RegionId: common.Region(c.Region()),
NatGatewayId: r.ID,
}
err = c.EcsClient().DeleteNatGateway(deleteNatGatewayArgs)
if err != nil {
return fmt.Errorf("err deleting NatGateway:%v", err)
}
return nil
}
func DeleteEIP(cloud fi.Cloud, r *resources.Resource) error {
c := cloud.(aliup.ALICloud)
klog.V(2).Infof("Removing EIP with Id %s", r.ID)
err := c.VpcClient().ReleaseEipAddress(r.ID)
if err != nil {
return fmt.Errorf("err deleting EIP:%v", err)
}
return nil
}