Skip to content

Commit 2703ad4

Browse files
committed
Delete instances, roles and security groups, witout having created them in the same run
1 parent 92776f5 commit 2703ad4

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

gitpod-network-check/cmd/common.go

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/aws/aws-sdk-go-v2/aws"
88
"github.com/aws/aws-sdk-go-v2/config"
99
"github.com/aws/aws-sdk-go-v2/service/ec2"
10+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
1011
"github.com/aws/aws-sdk-go-v2/service/iam"
1112
iam_types "github.com/aws/aws-sdk-go-v2/service/iam/types"
1213
log "github.com/sirupsen/logrus"
@@ -18,6 +19,7 @@ var (
1819
SecurityGroups []string
1920
Roles []string
2021
InstanceProfile string
22+
Subnets map[string]bool
2123
)
2224

2325
const gitpodRoleName = "GitpodNetworkCheck"
@@ -35,14 +37,58 @@ func initAwsConfig(ctx context.Context, region string) (aws.Config, error) {
3537
}
3638

3739
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+
3858
if len(InstanceIds) > 0 {
3959
_, err := svc.TerminateInstances(ctx, &ec2.TerminateInstancesInput{
4060
InstanceIds: InstanceIds,
4161
})
4262
if err != nil {
4363
log.WithError(err).WithField("instanceIds", InstanceIds).Warnf("Failed to cleanup instances, please cleanup manually")
4464
}
65+
66+
log.Info("✅ Instances terminated")
4567
}
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+
4692
if len(Roles) > 0 {
4793
for _, role := range Roles {
4894
_, 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) {
61107
_, err = iamsvc.DeleteRole(ctx, &iam.DeleteRoleInput{RoleName: aws.String(role)})
62108
if err != nil {
63109
log.WithError(err).WithField("rolename", role).Warnf("Failed to cleanup role, please cleanup manaullay")
110+
continue
64111
}
112+
113+
log.Infof("✅ Role '%v' deleted", role)
65114
}
66115

67116
_, err := iamsvc.DeleteInstanceProfile(ctx, &iam.DeleteInstanceProfileInput{
@@ -71,11 +120,32 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
71120
if err != nil {
72121
log.WithError(err).WithField("instanceProfile", InstanceProfile).Warnf("Failed to clean up instance profile, please cleanup manually")
73122
}
123+
124+
log.Info("✅ Instance profile deleted")
74125
}
75126

76127
log.Info("Cleaning up: Waiting for 1 minute so network interfaces are deleted")
77128
time.Sleep(time.Minute)
78129

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+
79149
if len(SecurityGroups) > 0 {
80150
for _, sg := range SecurityGroups {
81151
deleteSGInput := &ec2.DeleteSecurityGroupInput{
@@ -85,9 +155,9 @@ func cleanup(ctx context.Context, svc *ec2.Client, iamsvc *iam.Client) {
85155
_, err := svc.DeleteSecurityGroup(ctx, deleteSGInput)
86156
if err != nil {
87157
log.WithError(err).WithField("securityGroup", sg).Warnf("Failed to clean up security group, please cleanup manually")
158+
continue
88159
}
89-
160+
log.Infof("✅ Security group '%v' deleted", sg)
90161
}
91-
92162
}
93163
}

0 commit comments

Comments
 (0)