fix(ec2): serialize unique IPv6 allocation through VPC publication (#8536)

* fix(ec2): serialize unique IPv6 allocation through VPC publication
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-15 23:54:54 -04:00
1 parent 53ef87af1c
commit 0e86263ac0
3 files changed
+126 -10

No files matched your search

@@ -354,6 +354,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
// a concurrent allocation, which reads persisted resources to see what is taken, cannot pick
// the same address in between. Taken after imageRegistryLock where a caller holds both.
private final Object privateIpAllocationLock = new Object();
private final Object amazonProvidedIpv6AllocationLock = new Object();
/** Host part (1-254) of the last 203.0.113.0/24 address handed out; guarded by the allocation lock. */
private int documentationHost;
@@ -6507,9 +6508,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
vpc.getCidrBlockAssociationSet().add(
new VpcCidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), cidrBlock));
if (amazonProvidedIpv6CidrBlock) {
vpc.getIpv6CidrBlockAssociationSet().add(amazonProvidedIpv6Association(region));
synchronized (amazonProvidedIpv6AllocationLock) {
vpc.getIpv6CidrBlockAssociationSet().add(amazonProvidedIpv6Association(region));
vpcs.put(key(region, vpcId), vpc);
}
} else {
vpcs.put(key(region, vpcId), vpc);
}
vpcs.put(key(region, vpcId), vpc);
declareVpcNetwork(region, vpcId, cidrBlock);
createDefaultSecurityGroup(region, vpcId, "sg-" + randomHex(17));
@@ -6949,10 +6954,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
synchronized (attachmentTopologyLock(region)) {
synchronized (lockFor(key(region, vpcId))) {
Vpc vpc = getRequiredVpc(region, vpcId);
VpcIpv6CidrBlockAssociation assoc = amazonProvidedIpv6Association(region);
vpc.getIpv6CidrBlockAssociationSet().add(assoc);
vpcs.put(key(region, vpcId), vpc);
return assoc;
synchronized (amazonProvidedIpv6AllocationLock) {
VpcIpv6CidrBlockAssociation assoc = amazonProvidedIpv6Association(region);
vpc.getIpv6CidrBlockAssociationSet().add(assoc);
vpcs.put(key(region, vpcId), vpc);
return assoc;
}
}
}
}
@@ -6966,11 +6973,37 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* fourth group folds into the "::".
*/
private VpcIpv6CidrBlockAssociation amazonProvidedIpv6Association(String region) {
String block = CidrCanonicalizer.canonicalize(
"2600:1f18:" + randomHex(4) + ":" + randomHex(2) + "00::/56").orElseThrow();
// AWS gives each VPC its own Amazon-provided block, so a block another VPC holds, in any account of this
// deployment, is drawn again.
Set<String> held = new HashSet<>();
for (Vpc vpc : vpcs instanceof AccountAwareStorageBackend<Vpc> aware
? aware.scanAllAccounts() : vpcs.scan(k -> true)) {
if (vpc.getIpv6CidrBlockAssociationSet() == null) {
continue;
}
for (VpcIpv6CidrBlockAssociation association : vpc.getIpv6CidrBlockAssociationSet()) {
held.add(association.getIpv6CidrBlock());
}
}
String block = unusedAmazonProvidedIpv6Block(held, () -> CidrCanonicalizer.canonicalize(
"2600:1f18:" + randomHex(4) + ":" + randomHex(2) + "00::/56").orElseThrow());
return new VpcIpv6CidrBlockAssociation("vpc-cidr-assoc-" + randomHex(8), block, region);
}
/**
* The first drawn block no VPC holds. After 64 draws that are all held, the request is refused with
* InsufficientCidrBlocks rather than handing out a block twice.
*/
static String unusedAmazonProvidedIpv6Block(Set<String> held, Supplier<String> draw) {
for (int attempt = 0; attempt < 64; attempt++) {
String block = draw.get();
if (!held.contains(block)) {
return block;
}
}
throw new AwsException("InsufficientCidrBlocks", "No Amazon-provided IPv6 CIDR block is available.", 400);
}
private boolean vpcHasAssociatedIpv6CidrBlock(Vpc vpc) {
return vpc.getIpv6CidrBlockAssociationSet().stream()
.anyMatch(assoc -> "associated".equalsIgnoreCase(assoc.getIpv6CidrBlockState()));
@@ -6985,8 +7018,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
vpc.getCidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
// The same operation disassociates either family; AWS takes one association id and
// does not ask which set it belongs to.
vpc.getIpv6CidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
vpcs.put(key(region, vpc.getVpcId()), vpc);
synchronized (amazonProvidedIpv6AllocationLock) {
vpc.getIpv6CidrBlockAssociationSet().removeIf(a -> a.getAssociationId().equals(associationId));
vpcs.put(key(region, vpc.getVpcId()), vpc);
}
}
}
}