fix(ec2): serialize VPC deletion with Docker network creation (#7470)

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

(cherry picked from commit 7797b47de690c157d056866de6e51eaa7ae86301)

Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Jackson PriceandAndre Kurait authored and GitHub committed 2026-11-03 22:49:24 -04:00
1 parent 4a2c0a8a5b
commit d481f8396c
3 files changed
+72 -3

No files matched your search

+1 -1
View File
@@ -301,7 +301,7 @@ naming different VPCs find nothing.
|--------|-------------|
| CreateVpc | Creates a VPC with the requested CIDR block. `InstanceTenancy` accepts `default` or `dedicated`; `host` is rejected with `InvalidParameterValue`, as on AWS. `DryRun=true` is answered with `DryRunOperation` and creates nothing. A tag specification error keeps its own error; the CIDR block and the tenancy are checked only without `DryRun`. |
| DescribeVpcs | Lists or returns stored VPCs. |
| DeleteVpc | Deletes a VPC from the local EC2 store, together with its default security group and rules, main route table and default network ACL. Fails with `DependencyViolation` while the VPC still has a subnet, a security group, route table or network ACL other than those defaults, a VPC endpoint, an attached internet gateway or egress-only internet gateway, a transit gateway attachment, an Elastic Load Balancing v2 load balancer, or anything still holding a network interface in one of its subnets: an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone network interface, or an interface endpoint. |
| DeleteVpc | Deletes a VPC from the local EC2 store, together with its default security group and rules, main route table and default network ACL. Docker network cleanup waits for an in-flight creation and prevents queued attaches from recreating the deleted network. Fails with `DependencyViolation` while the VPC still has a subnet, a security group, route table or network ACL other than those defaults, a VPC endpoint, an attached internet gateway or egress-only internet gateway, a transit gateway attachment, an Elastic Load Balancing v2 load balancer, or anything still holding a network interface in one of its subnets: an instance that is not terminated, a NAT gateway that is not deleted or failed, a standalone network interface, or an interface endpoint. |
| ModifyVpcAttribute | Updates supported VPC attributes. |
| DescribeVpcAttribute | Returns a supported VPC attribute. |
| CreateDhcpOptions | Creates a DHCP options set with the supplied configurations and optional tags. |
@@ -662,6 +662,9 @@ public class VpcNetworkManager {
/** @return the network name, or null when creation failed */
private String materialise(VpcBinding vpc) {
synchronized (vpc) {
if (bindings.get(key(vpc.region, vpc.vpcId)) != vpc) {
return null;
}
if (vpc.created) {
attachCurrentContainer(vpc, null);
return vpc.networkName;
@@ -848,8 +851,12 @@ public class VpcNetworkManager {
// An upgrade can leave both spellings behind, so every one noted is removed.
Set<String> previousRunNetworks = networksFromPreviousRun.remove(key(region, vpcId));
Set<String> networkNames = new LinkedHashSet<>();
if (vpc.created) {
networkNames.add(vpc.networkName);
synchronized (vpc) {
// An in-flight Docker create must finish before its network is collected for removal.
// The retired binding cannot create another network.
if (vpc.created) {
networkNames.add(vpc.networkName);
}
}
if (previousRunNetworks != null) {
networkNames.addAll(previousRunNetworks);
@@ -33,6 +33,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -114,6 +115,67 @@ class VpcNetworkManagerTest {
manager = new VpcNetworkManager(config, docker);
}
@Test
void deletingVpcRemovesANetworkWhoseCreationIsStillInFlight() throws Exception {
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
String address = manager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();
CountDownLatch entered = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
when(docker.createNetworkCmd().exec()).thenAnswer(invocation -> {
entered.countDown();
assertTrue(release.await(5, TimeUnit.SECONDS));
return new CreateNetworkResponse();
});
try (ExecutorService executor = Executors.newFixedThreadPool(2)) {
Future<?> attaching = executor.submit(() ->
manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address));
try {
assertTrue(entered.await(2, TimeUnit.SECONDS));
Future<?> deleting = executor.submit(() -> manager.deleteVpcNetwork(REGION, "vpc-1"));
await().atMost(2, TimeUnit.SECONDS)
.until(() -> manager.effectiveVpcCidr(REGION, "vpc-1").isEmpty());
release.countDown();
attaching.get(2, TimeUnit.SECONDS);
deleting.get(2, TimeUnit.SECONDS);
} finally {
release.countDown();
}
}
assertEquals(List.of(manager.networkName(REGION, "vpc-1")), removedNetworks);
assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-2", address).isEmpty());
verify(docker.createNetworkCmd(), times(1)).exec();
}
@Test
void anAttachThatWasPlanningWhenVpcWasDeletedCannotCreateItsNetwork() throws Exception {
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
CountDownLatch entered = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
when(docker.listNetworksCmd().exec()).thenAnswer(invocation -> {
entered.countDown();
assertTrue(release.await(5, TimeUnit.SECONDS));
return List.of();
});
try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
Future<Boolean> attaching = executor.submit(() ->
manager.attachContainer(REGION, "vpc-1", "container-1"));
try {
assertTrue(entered.await(2, TimeUnit.SECONDS));
manager.deleteVpcNetwork(REGION, "vpc-1");
} finally {
release.countDown();
}
assertFalse(attaching.get(2, TimeUnit.SECONDS));
}
verify(docker, never()).createNetworkCmd();
assertTrue(removedNetworks.isEmpty());
}
private void existingNetwork(String name, String subnet, Map<String, String> labels) {
existingNetwork(name, subnet, labels, Map.of());
}