diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 714a402cfc..04695e541a 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -1264,7 +1264,7 @@ State is reported settled rather than transitional, as elsewhere in this service | `FLOCI_SERVICES_EC2_VPC_NETWORKS_ENABLED` | `true` | Back each VPC with a real Docker network (see [VPC Docker networks](#vpc-docker-networks)) | | `FLOCI_SERVICES_EC2_VPC_NETWORKS_FALLBACK_POOL` | `10.240.0.0/12` | RFC 1918 pool that substituted CIDRs are drawn from | | `FLOCI_SERVICES_EC2_VPC_NETWORKS_FALLBACK_PREFIX_LENGTH` | `16` | Prefix length of each block handed out of that pool | -| `FLOCI_SERVICES_EC2_VPC_NETWORKS_RECONCILE_ON_STARTUP` | `true` | Remove VPC networks left behind by a previous run of this same emulator | +| `FLOCI_SERVICES_EC2_VPC_NETWORKS_RECONCILE_ON_STARTUP` | `true` | Remove VPC networks left behind by a previous run of this same emulator. Whatever its value, the network a previous run created for a VPC that still exists is kept, and goes when that VPC is deleted | | `FLOCI_SERVICES_EC2_VPC_NETWORKS_DRIVER` | `bridge` | Docker network driver used for VPC networks | ## VPC Docker networks 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 a9e813e87f..4f0fe0c60c 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 @@ -842,14 +842,28 @@ public class VpcNetworkManager { synchronized (vpc.declarationLock) { vpc.subnetDeclarations.keySet().forEach(subnetId -> subnetOwner.remove(key(region, subnetId))); } - if (!vpc.created) { + // A network this process never materialised can still exist: one a previous run created + // for a VPC none of whose instances has started since. Startup reconciliation noted it, + // under its own name, which an older version spelled differently from vpc.networkName. + // 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); + } + if (previousRunNetworks != null) { + networkNames.addAll(previousRunNetworks); + } + if (networkNames.isEmpty()) { return; } String flociContainerId = vpc.flociContainerId; if (flociContainerId == null && currentContainerNetworkResolver != null) { flociContainerId = currentContainerNetworkResolver.resolveContainerId().orElse(null); } - removeNetworkWithRetry(vpc.networkName, flociContainerId, 1); + for (String networkName : networkNames) { + removeNetworkWithRetry(networkName, flociContainerId, 1); + } } private void removeNetworkWithRetry(String networkName, String flociContainerId, int attempt) { @@ -875,6 +889,9 @@ public class VpcNetworkManager { // ─── Startup reconciliation ────────────────────────────────────────────── + /** Network names by VPC, for VPCs still declared at startup whose networks a previous run created. */ + private final Map> networksFromPreviousRun = new ConcurrentHashMap<>(); + /** * Removes VPC networks this Floci left behind on a previous run. * @@ -887,12 +904,17 @@ public class VpcNetworkManager { * API port) are considered. Several Floci instances share a daemon here; scoping by owner is * what makes the deletion safe. * + *

The networks of VPCs that are still declared are kept and remembered, so deleting such a + * VPC later removes its network even though this run never materialised it. That listing runs + * with {@code reconcile-on-startup} off too; only the removal of orphans is behind the flag. + * * @param stillDeclared answers whether (region, vpcId) exists in the restored state */ public void reconcileOrphans(BiPredicate stillDeclared) { - if (!enabled() || !config.services().ec2().vpcNetworks().reconcileOnStartup()) { + if (!enabled()) { return; } + boolean reap = config.services().ec2().vpcNetworks().reconcileOnStartup(); int removed = 0; try { List networks = ALIASES.listByLabels( @@ -912,6 +934,11 @@ public class VpcNetworkManager { String vpcId = ALIASES.labelValue(labels, ContainerStorageHelper.RESOURCE_ID_LABEL); String region = ALIASES.labelValue(labels, ContainerStorageHelper.REGION_LABEL); if (vpcId != null && region != null && stillDeclared.test(region, vpcId)) { + networksFromPreviousRun.computeIfAbsent(key(region, vpcId), k -> ConcurrentHashMap.newKeySet()) + .add(network.getName()); + continue; + } + if (!reap) { continue; } disconnectAll(network); 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 864db6a27a..9125e48e0f 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 @@ -118,6 +118,98 @@ class VpcNetworkManagerTest { existingNetwork(name, subnet, labels, Map.of()); } + @Test + void deletingAVpcAfterARestartRemovesTheNetworkItsPredecessorCreated() { + // A restarted process declares the VPC again but has not materialised it: no instance in + // it has started since. The network from before the restart must still go with the VPC. + String networkName = manager.networkName(REGION, "vpc-1"); + existingNetwork(networkName, "10.0.0.0/16", ourLabels("vpc-1", "4650")); + manager.reconcileOrphans((region, vpcId) -> true); // startup: the VPC is still declared + manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16"); + + manager.deleteVpcNetwork(REGION, "vpc-1"); + + assertEquals(List.of(networkName), removedNetworks); + } + + @Test + void thePredecessorsNetworkGoesWithItsVpcWithStartupReapingOffToo() { + when(config.services().ec2().vpcNetworks().reconcileOnStartup()).thenReturn(false); + String networkName = manager.networkName(REGION, "vpc-1"); + existingNetwork(networkName, "10.0.0.0/16", ourLabels("vpc-1", "4650")); + existingNetwork("floci-vpc-4650-us-east-1-vpc-dead", "10.7.0.0/16", ourLabels("vpc-dead", "4650")); + manager.reconcileOrphans((region, vpcId) -> "vpc-1".equals(vpcId)); + assertTrue(removedNetworks.isEmpty(), "with reaping off an orphan stays"); + manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16"); + + manager.deleteVpcNetwork(REGION, "vpc-1"); + + assertEquals(List.of(networkName), removedNetworks); + } + + @Test + void aPredecessorsNetworkUnderItsLegacyNameGoesWithItsVpc() { + // A version before the floci-aws- prefix named the network floci-vpc-...; it is deleted + // by that name, not by the one this version would give it. + String legacyName = "floci-vpc-4650-us-east-1-vpc-1"; + existingNetwork(legacyName, "10.0.0.0/16", ourLabels("vpc-1", "4650")); + manager.reconcileOrphans((region, vpcId) -> true); + manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16"); + + manager.deleteVpcNetwork(REGION, "vpc-1"); + + assertEquals(List.of(legacyName), removedNetworks); + } + + @Test + void bothSpellingsOfAPredecessorsNetworkGoWithAVpcThisRunNeverMaterialised() { + // An upgrade plus another restart can leave both: the legacy bridge on a fallback CIDR and + // the current-named one on the declared CIDR. Neither may be forgotten. + String legacyName = "floci-vpc-4650-us-east-1-vpc-1"; + String currentName = manager.networkName(REGION, "vpc-1"); + existingNetwork(legacyName, "10.200.0.0/16", ourLabels("vpc-1", "4650")); + existingNetwork(currentName, "10.0.0.0/16", ourLabels("vpc-1", "4650")); + manager.reconcileOrphans((region, vpcId) -> true); + manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16"); + + manager.deleteVpcNetwork(REGION, "vpc-1"); + + assertEquals(2, removedNetworks.size(), "each removed once: " + removedNetworks); + assertEquals(Set.of(legacyName, currentName), Set.copyOf(removedNetworks)); + } + + @Test + void aVpcThisRunMaterialisedNextToItsPredecessorsLegacyNetworkTakesBothWithIt() { + // An instance started after the restart: this run created the computed-name network while + // the legacy-named one from before still exists. Deleting the VPC removes both. + String legacyName = "floci-vpc-4650-us-east-1-vpc-1"; + existingNetwork(legacyName, "10.0.0.0/16", ourLabels("vpc-1", "4650")); + manager.reconcileOrphans((region, vpcId) -> true); + 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(); + assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address).isPresent()); + + manager.deleteVpcNetwork(REGION, "vpc-1"); + + assertEquals(List.of(manager.networkName(REGION, "vpc-1"), legacyName), removedNetworks); + } + + @Test + void aVpcThisRunReusedItsPredecessorsNetworkForRemovesItOnce() { + String networkName = manager.networkName(REGION, "vpc-1"); + existingNetwork(networkName, "10.0.0.0/16", ourLabels("vpc-1", "4650")); + manager.reconcileOrphans((region, vpcId) -> true); + 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(); + assertTrue(manager.attach(REGION, "vpc-1", "subnet-a", "container-1", address).isPresent()); + + manager.deleteVpcNetwork(REGION, "vpc-1"); + + assertEquals(List.of(networkName), removedNetworks); + } + @Test void declaringAndDeletingUnusedNetworksDoesNotContactDocker() { manager.declareVpc(REGION, "vpc-private", "10.0.0.0/16");