mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ec2): plan a VPC's CIDR against another Region's network for the same VPC id (#5134)
When planning a VPC's CIDR, Floci skips its own surviving Docker network for that VPC so a restart does not read it as a collision. The match was on the VPC id and owner only, but two Regions can hold a VPC with the same id (the legacy vpc-default). Planning that id in one Region then skipped the other Region's network, accepted an overlapping CIDR, and the Docker network create failed, leaving instances without VPC connectivity. The skip now also requires the network's Region label to match; every VPC network has carried one since VPC networks were introduced.
This commit is contained in:
1 parent
a3118eb42b
commit
22ff93365e
2 files changed
+41
-10
No files matched your search
@@ -208,13 +208,13 @@ public class VpcNetworkManager {
|
||||
|
||||
private VpcBinding planVpc(String region, String vpcId, String declaredCidr) {
|
||||
Optional<Cidr4> declared = Cidr4.parse(declaredCidr);
|
||||
String rejection = rejectionReason(vpcId, declared, declaredCidr, List.of());
|
||||
String rejection = rejectionReason(region, vpcId, declared, declaredCidr, List.of());
|
||||
Cidr4 effective;
|
||||
if (rejection == null) {
|
||||
effective = declared.orElseThrow();
|
||||
}
|
||||
else {
|
||||
effective = allocateFromFallbackPool(vpcId, declared.map(Cidr4::prefix).orElse(null));
|
||||
effective = allocateFromFallbackPool(region, vpcId, declared.map(Cidr4::prefix).orElse(null));
|
||||
if (effective == null) {
|
||||
LOG.warnv("VPC {0} in {1}: declared CIDR {2} is unusable ({3}) and the fallback pool {4} "
|
||||
+ "is exhausted. This VPC gets no Docker network; its instances keep "
|
||||
@@ -237,7 +237,7 @@ public class VpcNetworkManager {
|
||||
* @return null when the block can be used as declared, otherwise a human-readable reason
|
||||
* it cannot, which goes verbatim into the substitution WARN.
|
||||
*/
|
||||
private String rejectionReason(String vpcId, Optional<Cidr4> declared, String declaredText,
|
||||
private String rejectionReason(String region, String vpcId, Optional<Cidr4> declared, String declaredText,
|
||||
Collection<Cidr4> extraTaken) {
|
||||
if (declared.isEmpty()) {
|
||||
return declaredText == null || declaredText.isBlank()
|
||||
@@ -247,11 +247,11 @@ public class VpcNetworkManager {
|
||||
if (!cidr.isRfc1918()) {
|
||||
return "outside RFC 1918 private space (10/8, 172.16/12, 192.168/16)";
|
||||
}
|
||||
Optional<String> clash = firstClash(vpcId, cidr, extraTaken);
|
||||
Optional<String> clash = firstClash(region, vpcId, cidr, extraTaken);
|
||||
return clash.orElse(null);
|
||||
}
|
||||
|
||||
private Optional<String> firstClash(String vpcId, Cidr4 candidate, Collection<Cidr4> extraTaken) {
|
||||
private Optional<String> firstClash(String region, String vpcId, Cidr4 candidate, Collection<Cidr4> extraTaken) {
|
||||
for (Cidr4 taken : extraTaken) {
|
||||
if (candidate.overlaps(taken)) {
|
||||
return Optional.of("overlaps an address range already in use (" + taken + ")");
|
||||
@@ -264,7 +264,7 @@ public class VpcNetworkManager {
|
||||
+ "cannot route two identical ranges");
|
||||
}
|
||||
}
|
||||
for (Cidr4 taken : dockerNetworkSubnets(vpcId)) {
|
||||
for (Cidr4 taken : dockerNetworkSubnets(region, vpcId)) {
|
||||
if (candidate.overlaps(taken)) {
|
||||
return Optional.of("overlaps an existing Docker network (" + taken + ")");
|
||||
}
|
||||
@@ -352,7 +352,7 @@ public class VpcNetworkManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Cidr4 allocateFromFallbackPool(String vpcId, Integer declaredPrefix) {
|
||||
private Cidr4 allocateFromFallbackPool(String region, String vpcId, Integer declaredPrefix) {
|
||||
EmulatorConfig.VpcNetworksConfig cfg = config.services().ec2().vpcNetworks();
|
||||
Optional<Cidr4> pool = Cidr4.parse(cfg.fallbackPool());
|
||||
if (pool.isEmpty() || !pool.get().isRfc1918()) {
|
||||
@@ -366,7 +366,7 @@ public class VpcNetworkManager {
|
||||
// asked for, not a /16 that wastes fifteen sixteenths of the pool per VPC.
|
||||
prefix = Math.min(declaredPrefix, MAX_ALLOCATABLE_PREFIX);
|
||||
}
|
||||
List<Cidr4> taken = new ArrayList<>(dockerNetworkSubnets(vpcId));
|
||||
List<Cidr4> taken = new ArrayList<>(dockerNetworkSubnets(region, vpcId));
|
||||
bindings.values().stream().map(b -> b.effective).filter(c -> c != null).forEach(taken::add);
|
||||
return allocateSubBlock(pool.get(), prefix, taken);
|
||||
}
|
||||
@@ -938,16 +938,20 @@ public class VpcNetworkManager {
|
||||
/**
|
||||
* Every IPv4 range the daemon has already reserved, so a declared CIDR can be checked before use.
|
||||
*
|
||||
* @param ownRegion the Region of the VPC being planned
|
||||
* @param ownVpcId the VPC being planned; this instance's surviving network for that same VPC is
|
||||
* excluded, so a Floci restart re-planning a VPC whose bridge is still up does
|
||||
* not read its own network as a collision and substitute a CIDR it already has
|
||||
* not read its own network as a collision and substitute a CIDR it already has.
|
||||
* Two Regions can hold a VPC with the same id (the legacy {@code vpc-default}),
|
||||
* so the network must be for that VPC in the same Region
|
||||
*/
|
||||
private Set<Cidr4> dockerNetworkSubnets(String ownVpcId) {
|
||||
private Set<Cidr4> dockerNetworkSubnets(String ownRegion, String ownVpcId) {
|
||||
Set<Cidr4> taken = new java.util.LinkedHashSet<>();
|
||||
try {
|
||||
for (Network network : dockerClient.listNetworksCmd().exec()) {
|
||||
Map<String, String> labels = network.getLabels() == null ? Map.of() : network.getLabels();
|
||||
if (ownVpcId != null && ALIASES.matches(labels, ContainerStorageHelper.RESOURCE_ID_LABEL, ownVpcId)
|
||||
&& ALIASES.matches(labels, ContainerStorageHelper.REGION_LABEL, ownRegion)
|
||||
&& ownedByThisDeployment(labels)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -521,6 +521,33 @@ class VpcNetworkManagerTest {
|
||||
assertEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-1").orElseThrow());
|
||||
}
|
||||
|
||||
// Catches: planning a VPC skipping another Region's surviving network for a VPC with the same id,
|
||||
// which then accepts an overlapping CIDR and fails when its own network is created.
|
||||
@Test
|
||||
void anotherRegionsNetworkForTheSameVpcIdIsStillACollision() {
|
||||
Map<String, String> otherRegion = new LinkedHashMap<>(newLabels("vpc-default", "4650"));
|
||||
otherRegion.put("io.floci.region", "eu-west-1");
|
||||
existingNetwork(manager.networkName("eu-west-1", "vpc-default"), "10.0.0.0/16", otherRegion);
|
||||
|
||||
manager.declareVpc(REGION, "vpc-default", "10.0.0.0/16");
|
||||
|
||||
assertTrue(manager.isSubstituted(REGION, "vpc-default"),
|
||||
"eu-west-1's network for vpc-default holds 10.0.0.0/16; us-east-1's vpc-default cannot reuse it");
|
||||
assertNotEquals("10.0.0.0/16", manager.effectiveVpcCidr(REGION, "vpc-default").orElseThrow());
|
||||
}
|
||||
|
||||
// The same rule through the labels a network carried before the io.floci.* keys.
|
||||
@Test
|
||||
void anotherRegionsLegacyNetworkForTheSameVpcIdIsStillACollision() {
|
||||
Map<String, String> otherRegion = new LinkedHashMap<>(ourLabels("vpc-default", "4650"));
|
||||
otherRegion.put("floci_vpc_region", "eu-west-1");
|
||||
existingNetwork("floci-aws-vpc-4650-eu-west-1-vpc-default", "10.0.0.0/16", otherRegion);
|
||||
|
||||
manager.declareVpc(REGION, "vpc-default", "10.0.0.0/16");
|
||||
|
||||
assertTrue(manager.isSubstituted(REGION, "vpc-default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createdNetworkCarriesTheIdentityLabelsAndTheirLegacyAliases() {
|
||||
manager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
|
||||
|
||||
Reference in new issue
Block a user