7
7
"github.com/aws/aws-sdk-go-v2/aws"
8
8
"github.com/aws/aws-sdk-go-v2/config"
9
9
"github.com/aws/aws-sdk-go-v2/service/ec2"
10
+ "github.com/aws/aws-sdk-go-v2/service/ec2/types"
10
11
"github.com/aws/aws-sdk-go-v2/service/iam"
11
12
iam_types "github.com/aws/aws-sdk-go-v2/service/iam/types"
12
13
log "github.com/sirupsen/logrus"
18
19
SecurityGroups []string
19
20
Roles []string
20
21
InstanceProfile string
22
+ Subnets map [string ]bool
21
23
)
22
24
23
25
const gitpodRoleName = "GitpodNetworkCheck"
@@ -35,14 +37,58 @@ func initAwsConfig(ctx context.Context, region string) (aws.Config, error) {
35
37
}
36
38
37
39
func cleanup (ctx context.Context , svc * ec2.Client , iamsvc * iam.Client ) {
40
+ if len (InstanceIds ) == 0 {
41
+ instances , err := svc .DescribeInstances (ctx , & ec2.DescribeInstancesInput {
42
+ Filters : []types.Filter {
43
+ {
44
+ Name : aws .String ("tag:gitpod.io/network-check" ),
45
+ Values : []string {"true" },
46
+ },
47
+ },
48
+ })
49
+ if err != nil {
50
+ log .WithError (err ).Warn ("Failed to list instances, please cleanup manually" )
51
+ }
52
+
53
+ for _ , i := range instances .Reservations [0 ].Instances {
54
+ InstanceIds = append (InstanceIds , * i .InstanceId )
55
+ }
56
+ }
57
+
38
58
if len (InstanceIds ) > 0 {
39
59
_ , err := svc .TerminateInstances (ctx , & ec2.TerminateInstancesInput {
40
60
InstanceIds : InstanceIds ,
41
61
})
42
62
if err != nil {
43
63
log .WithError (err ).WithField ("instanceIds" , InstanceIds ).Warnf ("Failed to cleanup instances, please cleanup manually" )
44
64
}
65
+
66
+ log .Info ("✅ Instances terminated" )
45
67
}
68
+
69
+ if len (Roles ) == 0 {
70
+ roles , err := iamsvc .ListRoles (ctx , & iam.ListRolesInput {
71
+ PathPrefix : aws .String ("/GitpodNetworkCheck" ),
72
+ })
73
+ if err != nil {
74
+ log .WithError (err ).Warn ("Failed to list roles, please cleanup manually" )
75
+ }
76
+
77
+ for _ , role := range roles .Roles {
78
+ if role .RoleName == nil {
79
+ continue
80
+ }
81
+
82
+ if * role .RoleName == gitpodRoleName {
83
+ Roles = append (Roles , * role .RoleName )
84
+ }
85
+ }
86
+ }
87
+
88
+ if InstanceProfile == "" {
89
+ InstanceProfile = gitpodInstanceProfile
90
+ }
91
+
46
92
if len (Roles ) > 0 {
47
93
for _ , role := range Roles {
48
94
_ , err := iamsvc .DetachRolePolicy (ctx , & iam.DetachRolePolicyInput {PolicyArn : aws .String ("arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore" ), RoleName : aws .String (role )})
@@ -61,7 +107,10 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
61
107
_ , err = iamsvc .DeleteRole (ctx , & iam.DeleteRoleInput {RoleName : aws .String (role )})
62
108
if err != nil {
63
109
log .WithError (err ).WithField ("rolename" , role ).Warnf ("Failed to cleanup role, please cleanup manaullay" )
110
+ continue
64
111
}
112
+
113
+ log .Infof ("✅ Role '%v' deleted" , role )
65
114
}
66
115
67
116
_ , err := iamsvc .DeleteInstanceProfile (ctx , & iam.DeleteInstanceProfileInput {
@@ -71,11 +120,32 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
71
120
if err != nil {
72
121
log .WithError (err ).WithField ("instanceProfile" , InstanceProfile ).Warnf ("Failed to clean up instance profile, please cleanup manually" )
73
122
}
123
+
124
+ log .Info ("✅ Instance profile deleted" )
74
125
}
75
126
76
127
log .Info ("Cleaning up: Waiting for 1 minute so network interfaces are deleted" )
77
128
time .Sleep (time .Minute )
78
129
130
+ if len (SecurityGroups ) == 0 {
131
+ securityGroups , err := svc .DescribeSecurityGroups (ctx , & ec2.DescribeSecurityGroupsInput {
132
+ Filters : []types.Filter {
133
+ {
134
+ Name : aws .String ("tag:gitpod.io/network-check" ),
135
+ Values : []string {"true" },
136
+ },
137
+ },
138
+ })
139
+
140
+ if err != nil {
141
+ log .WithError (err ).Error ("Failed to list security groups, please cleanup manually" )
142
+ }
143
+
144
+ for _ , sg := range securityGroups .SecurityGroups {
145
+ SecurityGroups = append (SecurityGroups , * sg .GroupId )
146
+ }
147
+ }
148
+
79
149
if len (SecurityGroups ) > 0 {
80
150
for _ , sg := range SecurityGroups {
81
151
deleteSGInput := & ec2.DeleteSecurityGroupInput {
@@ -85,9 +155,9 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
85
155
_ , err := svc .DeleteSecurityGroup (ctx , deleteSGInput )
86
156
if err != nil {
87
157
log .WithError (err ).WithField ("securityGroup" , sg ).Warnf ("Failed to clean up security group, please cleanup manually" )
158
+ continue
88
159
}
89
-
160
+ log . Infof ( "✅ Security group '%v' deleted" , sg )
90
161
}
91
-
92
162
}
93
163
}
0 commit comments