-
Notifications
You must be signed in to change notification settings - Fork 18
/
vpc_root.go
181 lines (148 loc) · 4 KB
/
vpc_root.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
package aws
import (
"github.com/Qovery/pleco/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
log "github.com/sirupsen/logrus"
"sync"
"time"
)
type VpcInfo struct {
VpcId *string
SecurityGroups []SecurityGroup
NatGateways []NatGateway
InternetGateways []InternetGateway
Subnets []Subnet
RouteTables []RouteTable
Status string
TTL int64
Tag string
CreationDate time.Time
IsProtected bool
}
func GetVpcsIdsByClusterNameTag(ec2Session ec2.EC2, clusterName string) []*string {
result, err := ec2Session.DescribeVpcs(
&ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag:ClusterName"),
Values: []*string{aws.String(clusterName)},
},
},
})
if err != nil {
log.Error(err)
return nil
}
var vpcsIds []*string
for _, vpc := range result.Vpcs {
vpcsIds = append(vpcsIds, vpc.VpcId)
}
return vpcsIds
}
func getVPCs(ec2Session ec2.EC2, tagName string) []*ec2.Vpc {
input := &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag-key"),
Values: []*string{&tagName},
},
},
}
result, err := ec2Session.DescribeVpcs(input)
if err != nil {
log.Error(err)
return nil
}
if len(result.Vpcs) == 0 {
return nil
}
return result.Vpcs
}
func listTaggedVPC(ec2Session ec2.EC2, tagName string) ([]VpcInfo, error) {
var taggedVPCs []VpcInfo
var VPCs = getVPCs(ec2Session, tagName)
for _, vpc := range VPCs {
creationDate, ttl, isprotected, _, _ := utils.GetEssentialTags(vpc.Tags, tagName)
taggedVpc := VpcInfo{
VpcId: vpc.VpcId,
Status: *vpc.State,
CreationDate: creationDate,
TTL: ttl,
IsProtected: isprotected,
}
if *vpc.State != "available" {
continue
}
if len(vpc.Tags) == 0 {
continue
}
for _, tag := range vpc.Tags {
if *tag.Key == tagName {
if *tag.Value == "" {
log.Warnf("Tag %s was empty and it wasn't expected, skipping", *tag.Value)
continue
}
taggedVpc.Tag = *tag.Value
}
getCompleteVpc(ec2Session, &taggedVpc, tagName)
}
if utils.CheckIfExpired(taggedVpc.CreationDate, taggedVpc.TTL, "vpc: "+*taggedVpc.VpcId) && !taggedVpc.IsProtected {
taggedVPCs = append(taggedVPCs, taggedVpc)
}
}
return taggedVPCs, nil
}
func deleteVPC(ec2Session ec2.EC2, VpcList []VpcInfo, dryRun bool) error {
if dryRun {
return nil
}
if len(VpcList) == 0 {
return nil
}
region := *ec2Session.Config.Region
for _, vpc := range VpcList {
DeleteSecurityGroupsByIds(ec2Session, vpc.SecurityGroups)
DeleteNatGatewaysByIds(ec2Session, vpc.NatGateways)
DeleteInternetGatewaysByIds(ec2Session, vpc.InternetGateways, *vpc.VpcId)
DeleteSubnetsByIds(ec2Session, vpc.Subnets)
DeleteRouteTablesByIds(ec2Session, vpc.RouteTables)
_, deleteErr := ec2Session.DeleteVpc(
&ec2.DeleteVpcInput{
VpcId: aws.String(*vpc.VpcId),
},
)
if deleteErr != nil {
log.Errorf("Can't delete VPC %s in %s yet: %s", *vpc.VpcId, region, deleteErr.Error())
}
}
return nil
}
func DeleteExpiredVPC(ec2Session ec2.EC2, tagName string, dryRun bool) {
VPCs, err := listTaggedVPC(ec2Session, tagName)
region := ec2Session.Config.Region
if err != nil {
log.Errorf("can't list VPC: %s\n", err)
}
count, start := utils.ElemToDeleteFormattedInfos("tagged VPC resource", len(VPCs), *region)
log.Debug(count)
if dryRun || len(VPCs) == 0 {
return
}
log.Debug(start)
_ = deleteVPC(ec2Session, VPCs, dryRun)
}
func getCompleteVpc(ec2Session ec2.EC2, vpc *VpcInfo, tagName string) {
var waitGroup sync.WaitGroup
waitGroup.Add(1)
go SetSecurityGroupsIdsByVpcId(ec2Session, vpc, &waitGroup, tagName)
waitGroup.Add(1)
go SetNatGatewaysIdsByVpcId(ec2Session, vpc, &waitGroup, tagName)
waitGroup.Add(1)
go SetInternetGatewaysIdsByVpcId(ec2Session, vpc, &waitGroup, tagName)
waitGroup.Add(1)
go SetSubnetsIdsByVpcId(ec2Session, vpc, &waitGroup, tagName)
waitGroup.Add(1)
go SetRouteTablesIdsByVpcId(ec2Session, vpc, &waitGroup, tagName)
waitGroup.Wait()
}