Skip to content

Commit

Permalink
Cleanup EKS VPC peering connections (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndhanushkodi authored Apr 15, 2022
1 parent 3adee48 commit a9357c3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions hack/aws-acceptance-test-cleanup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ func realMain(ctx context.Context) error {

vpcID := cluster.ResourcesVpcConfig.VpcId

// Once we have the VPC ID, collect VPC peering connections to delete.
filternameAccepter := "accepter-vpc-info.vpc-id"
filternameRequester := "requester-vpc-info.vpc-id"
vpcPeeringConnectionsWithAccepter, err := ec2Client.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{
Filters: []*ec2.Filter{
{
Name: &filternameAccepter,
Values: []*string{vpcID},
},
},
})

if err != nil {
return err
}
vpcPeeringConnectionsWithRequester, err := ec2Client.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{
Filters: []*ec2.Filter{
{
Name: &filternameRequester,
Values: []*string{vpcID},
},
},
})
vpcPeeringConnectionsToDelete := append(vpcPeeringConnectionsWithAccepter.VpcPeeringConnections, vpcPeeringConnectionsWithRequester.VpcPeeringConnections...)

// Delete NAT gateways.
natGateways, err := ec2Client.DescribeNatGatewaysWithContext(ctx, &ec2.DescribeNatGatewaysInput{
Filter: []*ec2.Filter{
Expand Down Expand Up @@ -419,6 +444,15 @@ func realMain(ctx context.Context) error {
fmt.Printf("Security group: Destroyed [id=%s]\n", *sg.GroupId)
}

// Delete VPC Peering Connections.
for _, vpcpc := range vpcPeeringConnectionsToDelete {
_, err = ec2Client.DeleteVpcPeeringConnection(&ec2.DeleteVpcPeeringConnectionInput{VpcPeeringConnectionId: vpcpc.VpcPeeringConnectionId})
if err != nil {
return err
}
fmt.Printf("VPC PeeringConnection: Destroyed [id=%s]\n", *vpcpc.VpcPeeringConnectionId)
}

// Delete VPC. Sometimes there's a race condition where AWS thinks
// the VPC still has dependencies but they've already been deleted so
// we may need to retry a couple times.
Expand Down

0 comments on commit a9357c3

Please sign in to comment.