diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 4b8c344b6f..3c244adda4 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -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. | diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java index 4f0fe0c60c..740a7ec7c2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java @@ -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 previousRunNetworks = networksFromPreviousRun.remove(key(region, vpcId)); Set 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); diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java index 9125e48e0f..5cbd19e791 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManagerTest.java @@ -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 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 labels) { existingNetwork(name, subnet, labels, Map.of()); }