mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ec2): remove a VPC's network after a restart too (#6874)
* fix(ec2): remove a VPC's network after a restart too deleteVpcNetwork removed a VPC's Docker network only when this process had materialised it (vpc.created), which happens when an instance in the VPC starts. After a restart, a VPC that kept its network from the previous run but had no instance started since was deleted without its network: the bridge outlived the VPC and held its CIDR, so the next VPC declaring that CIDR collided with it. Reproduced by: run an instance in a VPC, restart with persisted state, terminate it, delete the VPC, create one with the same CIDR. Startup reconciliation already lists this deployment's VPC networks and keeps those whose VPC is still declared; it now remembers them, and deleting such a VPC removes its network. The listing runs whether or not reconcile-on-startup is on (only reaping orphans is behind that flag), so the fix does not depend on it. Upstream #5869 already detaches Floci before removal; this keeps that path. * fix(ec2): remove a restored VPC's network by the name it has Startup reconciliation remembered only which VPCs kept a network from a previous run, and deleting such a VPC then removed the name this version computes, floci-aws-vpc-.... A network created before the floci-aws- prefix is named floci-vpc-...; removing the computed name found nothing, the NotFound was ignored, and the old bridge kept holding the CIDR. Reconciliation now records each surviving network's own name and the deletion removes that one, along with the computed one if this run materialised it. * test(ec2): cover deleting a VPC this run materialised after a restart A VPC an instance started in after the restart has both the network this run created and the one its predecessor left. Deleting it removes both when the predecessor's carries a legacy name, and removes one network once when this run reused the predecessor's. * fix(ec2): remember every surviving network of a VPC after a restart Startup reconciliation kept one network name per VPC, so when both the legacy-named and the current-named bridge survived, deleting the VPC before this run materialised it left one behind with its CIDR reserved.
This commit is contained in:
1 parent
cc51f40cf8
commit
ddae4fdbe9
3 files changed
+123
-4
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<String> previousRunNetworks = networksFromPreviousRun.remove(key(region, vpcId));
|
||||
Set<String> 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<String, Set<String>> 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.
|
||||
*
|
||||
* <p>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<String, String> stillDeclared) {
|
||||
if (!enabled() || !config.services().ec2().vpcNetworks().reconcileOnStartup()) {
|
||||
if (!enabled()) {
|
||||
return;
|
||||
}
|
||||
boolean reap = config.services().ec2().vpcNetworks().reconcileOnStartup();
|
||||
int removed = 0;
|
||||
try {
|
||||
List<Network> 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);
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in new issue
Block a user