mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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