feat(eks): lease VPC /28 prefixes onto the cluster node's interface (#9279)

The Amazon VPC CNI plugin's prefix delegation hands a node IPv4 /28
prefixes of its subnet, listed on the node's network interface, and the
AWS Load Balancer Controller finds the interface behind a pod address
by looking for the prefix that holds it.

With floci.services.eks.vpc-pod-network, a node holding an address in a
Docker-backed subnet now also leases vpc-pod-prefixes /28 prefixes of
that subnet, two by default, as a node running pods holds under the VPC
CNI's default WARM_PREFIX_TARGET of one. DescribeInstances and
DescribeNetworkInterfaces list them on the node's interface in
ipv4PrefixSet.

EC2 leases a prefix from the top of the subnet down, clear of the
subnet's last address and of every address something holds, Docker's
own container addresses on the VPC network included, and counts its
sixteen addresses as held, so no instance or interface gets one and
AvailableIpAddressCount leaves them out. The node keeps its prefixes
across a restart as it keeps its address, re-reserving them; an adopted
node leases no new ones, and a stopped or failed start gives them back.

Part of #5929
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 12:24:45 -04:00
1 parent 5909ff5b80
commit 20e0ca6946
18 files changed
+811 -13

No files matched your search

@@ -7,8 +7,15 @@ import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.ec2.model.BlockDeviceMapping;
import io.floci.aws.services.ec2.model.EbsBlockDevice;
import io.floci.aws.services.ec2.model.Image;
import io.floci.aws.services.ec2.model.Instance;
import io.floci.aws.services.ec2.model.InstanceNetworkInterface;
import io.floci.aws.services.ec2.model.InstanceState;
import io.floci.aws.services.ec2.model.KeyPair;
import io.floci.aws.services.ec2.model.NetworkAcl;
import io.floci.aws.services.ec2.model.NetworkInterface;
import io.floci.aws.services.ec2.model.NetworkInterfaceListResult;
import io.floci.aws.services.ec2.model.Placement;
import io.floci.aws.services.ec2.model.Reservation;
import io.floci.aws.services.ec2.model.Subnet;
import io.floci.aws.services.ec2.model.Tag;
import io.floci.aws.services.ec2.model.Vpc;
@@ -41,6 +48,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -673,6 +681,39 @@ class Ec2QueryHandlerTest {
verify(service, never()).createSecurityGroup(any(), any(), any(), any());
}
@Test
void anInterfacesIpv4PrefixesAreDescribedWithItAndWithItsInstance() {
Ec2Service service = mock(Ec2Service.class);
List<String> prefixes = List.of("10.60.0.224/28", "10.60.0.208/28");
NetworkInterface ni = new NetworkInterface();
ni.setNetworkInterfaceId("eni-0de0000000000005a");
ni.setPrivateIpAddress("10.60.0.25");
ni.setIpv4Prefixes(prefixes);
when(service.describeNetworkInterfaces(eq("us-east-1"), anyList(), anyList(), anyInt(), any()))
.thenReturn(new NetworkInterfaceListResult(List.of(ni), null));
Instance node = new Instance();
node.setInstanceId("i-0de0000000000005a");
node.setRegion("us-east-1");
node.setState(InstanceState.running());
node.setPlacement(new Placement("us-east-1a"));
InstanceNetworkInterface eni = new InstanceNetworkInterface();
eni.setNetworkInterfaceId("eni-0de0000000000005a");
eni.setPrivateIpAddress("10.60.0.25");
eni.setIpv4Prefixes(prefixes);
node.getNetworkInterfaces().add(eni);
Reservation reservation = new Reservation();
reservation.setInstances(List.of(node));
when(service.describeInstances(eq("us-east-1"), anyList(), anyList())).thenReturn(List.of(reservation));
String prefixSet = "<ipv4PrefixSet><item><ipv4Prefix>10.60.0.224/28</ipv4Prefix></item>"
+ "<item><ipv4Prefix>10.60.0.208/28</ipv4Prefix></item></ipv4PrefixSet>";
for (String action : List.of("DescribeNetworkInterfaces", "DescribeInstances")) {
String body = ((String) handler(service).handle(action, new MultivaluedHashMap<>(), "us-east-1")
.getEntity()).replaceAll(">\\s+<", "><");
assertTrue(body.contains(prefixSet), action + ": " + body);
}
}
private Ec2QueryHandler handler(Ec2Service service) {
return handler(service, new RegionResolver("us-east-1", "000000000000"));
}
@@ -64,6 +64,7 @@ import io.floci.aws.services.ec2.model.Vpc;
import io.floci.aws.services.ec2.model.VpcCidrBlockAssociation;
import io.floci.aws.services.ec2.model.VpcEndpoint;
import io.floci.aws.services.ec2.model.VpcEndpointSubnetConfiguration;
import io.floci.aws.services.ec2.net.Cidr4;
import io.floci.aws.services.ec2.net.VpcNetworkManager;
import io.floci.aws.services.ec2.portforward.Ec2PortForwardManager;
import io.floci.aws.testing.ParkingStore;
@@ -144,6 +145,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
@@ -2990,6 +2992,57 @@ class Ec2ServiceTest {
assertTrue(fixture.service().describeNatGateways("us-east-1", List.of(), Map.of()).isEmpty());
}
/** Prefixes leased for a container's own addresses hold all their addresses until they are released. */
@Test
void leasedPrefixesHoldTheirAddressesUntilReleased() {
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/24");
Ec2Service service = fixture.service();
when(fixture.vpcNetworks().networkEndpointAddresses("000000000000", "us-east-1", fixture.subnetId()))
.thenReturn(Optional.of(Set.of("10.60.0.250")));
when(fixture.vpcNetworks().allocatePrefixes(eq("000000000000"), eq("us-east-1"), eq(fixture.subnetId()),
eq(2), any())).thenReturn(List.of(Cidr4.parse("10.60.0.224/28").orElseThrow(),
Cidr4.parse("10.60.0.208/28").orElseThrow()));
when(fixture.vpcNetworks().reservePrefix(eq("000000000000"), eq("us-east-1"), eq(fixture.subnetId()),
eq(Cidr4.parse("10.60.0.192/28").orElseThrow()), any())).thenReturn(true);
int before = service.describeSubnets("us-east-1", List.of(fixture.subnetId()), Map.of()).getFirst()
.getAvailableIpAddressCount();
assertEquals(List.of("10.60.0.224/28", "10.60.0.208/28"),
service.allocateVpcPrefixes("000000000000", "us-east-1", fixture.subnetId(), 2));
assertEquals(List.of("10.60.0.192/28"), service.reserveVpcPrefixes("000000000000", "us-east-1",
fixture.subnetId(), List.of("10.60.0.192/28")));
assertEquals(before - 48, service.describeSubnets("us-east-1", List.of(fixture.subnetId()), Map.of())
.getFirst().getAvailableIpAddressCount());
@SuppressWarnings("unchecked")
ArgumentCaptor<Set<String>> held = ArgumentCaptor.forClass(Set.class);
verify(fixture.vpcNetworks()).reservePrefix(eq("000000000000"), eq("us-east-1"), eq(fixture.subnetId()),
any(), held.capture());
assertTrue(held.getValue().containsAll(List.of("10.60.0.250", "10.60.0.224", "10.60.0.239")),
"the reservation sees the container's address and the prefixes leased before it as held");
for (String prefix : List.of("10.60.0.224/28", "10.60.0.208/28", "10.60.0.192/28")) {
service.releaseVpcPrefix("000000000000", "us-east-1", fixture.subnetId(), prefix);
}
assertEquals(before, service.describeSubnets("us-east-1", List.of(fixture.subnetId()), Map.of()).getFirst()
.getAvailableIpAddressCount());
verify(fixture.vpcNetworks()).releasePrivateIp("000000000000", "us-east-1", fixture.subnetId(), "10.60.0.230");
}
/** Without knowing which addresses Docker's containers hold, no prefix is leased or reserved. */
@Test
void noPrefixIsLeasedOrReservedWhenDockerCannotSayWhichAddressesItsContainersHold() {
MockedNetworkFixture fixture = mockedNetworkFixture("10.60.0.0/24");
when(fixture.vpcNetworks().networkEndpointAddresses("000000000000", "us-east-1", fixture.subnetId()))
.thenReturn(Optional.empty());
assertTrue(fixture.service().allocateVpcPrefixes("000000000000", "us-east-1", fixture.subnetId(), 1).isEmpty());
assertTrue(fixture.service().reserveVpcPrefixes("000000000000", "us-east-1", fixture.subnetId(),
List.of("10.60.0.224/28")).isEmpty());
verify(fixture.vpcNetworks(), never()).allocatePrefixes(any(), any(), any(), anyInt(), any());
verify(fixture.vpcNetworks(), never()).reservePrefix(any(), any(), any(), any(), any());
}
/** The same on a secondary-address update, which also leaves the gateway as it was. */
@Test
void aNatGatewayUpdateThatRunsOutOfAddressesGivesBackTheLeaseItTook() {
@@ -5,11 +5,14 @@ import com.github.dockerjava.api.command.ConnectToNetworkCmd;
import com.github.dockerjava.api.command.CreateNetworkCmd;
import com.github.dockerjava.api.command.CreateNetworkResponse;
import com.github.dockerjava.api.command.DisconnectFromNetworkCmd;
import com.github.dockerjava.api.command.InspectContainerCmd;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.InspectNetworkCmd;
import com.github.dockerjava.api.command.ListNetworksCmd;
import com.github.dockerjava.api.command.RemoveNetworkCmd;
import com.github.dockerjava.api.exception.ConflictException;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Network;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.docker.CurrentContainerNetworkResolver;
@@ -44,6 +47,8 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -428,6 +433,114 @@ class VpcNetworkManagerTest {
"a terminated instance's address must come back into circulation");
}
// ─── Prefixes: /28 blocks a container hands its own addresses out of ────
@Test
void prefixesAreTheHighestFreeSlashTwentyEightsOfTheSubnet() {
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
// .240/28 would hold .255, which AWS reserves; a block one of whose addresses something else holds is skipped.
assertEquals(List.of("10.0.1.224/28", "10.0.1.192/28"), manager.allocatePrefixes(ACCOUNT, REGION, "subnet-a",
2, Set.of("10.0.1.215")).stream().map(Cidr4::toString).toList());
// The prefixes' addresses are leased until they are released.
assertFalse(manager.reservePrivateIp(ACCOUNT, REGION, "subnet-a", "10.0.1.230"));
manager.releasePrefix(ACCOUNT, REGION, "subnet-a", Cidr4.parse("10.0.1.224/28").orElseThrow());
assertTrue(manager.reservePrivateIp(ACCOUNT, REGION, "subnet-a", "10.0.1.230"));
}
@Test
void aSubnetWithNoSlashTwentyEightClearOfReservedAddressesLeasesNoPrefix() {
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-small", "10.0.9.0/27");
assertTrue(manager.allocatePrefixes(ACCOUNT, REGION, "subnet-small", 1, Set.of()).isEmpty());
}
@Test
void theAddressesContainersHoldOnTheVpcNetworkAreReadFromDocker() {
Network.ContainerNetworkConfig endpoint = mock(Network.ContainerNetworkConfig.class);
when(endpoint.getIpv4Address()).thenReturn("10.0.1.231/16");
existingNetwork(manager.networkName(ACCOUNT, REGION, "vpc-1"), "10.0.0.0/16", ourLabels("vpc-1", "4650"),
Map.of("container-1", endpoint));
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
assertEquals(Optional.of(Set.of("10.0.1.231")),
manager.networkEndpointAddresses(ACCOUNT, REGION, "subnet-a"));
}
@Test
void aNetworkNotCreatedYetHoldsNoAddressAndOneDockerCannotDescribeHoldsUnknownOnes() {
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
assertEquals(Optional.of(Set.of()), manager.networkEndpointAddresses(ACCOUNT, REGION, "subnet-a"));
InspectNetworkCmd failing = mock(InspectNetworkCmd.class, RETURNS_SELF);
when(failing.exec()).thenThrow(new RuntimeException("daemon timed out"));
when(docker.inspectNetworkCmd()).thenReturn(failing);
assertEquals(Optional.empty(), manager.networkEndpointAddresses(ACCOUNT, REGION, "subnet-a"));
}
@Test
void aRestartReservesAPrefixOnlyWhenNoneOfItsAddressesIsLeasedOrHeld() {
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
Cidr4 held = Cidr4.parse("10.0.1.224/28").orElseThrow();
assertTrue(manager.reservePrivateIp(ACCOUNT, REGION, "subnet-a", "10.0.1.20"));
assertFalse(manager.reservePrefix(ACCOUNT, REGION, "subnet-a", held, Set.of("10.0.1.233")),
"a container holds 10.0.1.233");
assertTrue(manager.reservePrefix(ACCOUNT, REGION, "subnet-a", held, Set.of()));
assertFalse(manager.reservePrefix(ACCOUNT, REGION, "subnet-a", held, Set.of()), "its addresses are leased now");
assertFalse(manager.reservePrefix(ACCOUNT, REGION, "subnet-a", Cidr4.parse("10.0.1.16/28").orElseThrow(),
Set.of()), "10.0.1.20 is leased");
assertFalse(manager.reservePrefix(ACCOUNT, REGION, "subnet-a", Cidr4.parse("10.0.2.0/28").orElseThrow(),
Set.of()), "outside the subnet");
assertFalse(manager.reservePrefix(ACCOUNT, REGION, "subnet-a", Cidr4.parse("10.0.1.64/27").orElseThrow(),
Set.of()), "not a /28");
}
@Test
void aContainerAttachedWithoutAnAddressTakesTheLowestOneNothingLeasesOrHolds() {
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-low", "10.0.0.0/24");
assertTrue(manager.reservePrivateIp(ACCOUNT, REGION, "subnet-low", "10.0.0.2"));
assertTrue(manager.reservePrivateIp(ACCOUNT, REGION, "subnet-low", "10.0.0.3"));
Network.ContainerNetworkConfig endpoint = mock(Network.ContainerNetworkConfig.class);
when(endpoint.getIpv4Address()).thenReturn("10.0.0.4/16");
String networkName = manager.networkName(ACCOUNT, REGION, "vpc-1");
existingNetwork(networkName, "10.0.0.0/16", ourLabels("vpc-1", "4650"), Map.of("container-1", endpoint));
InspectContainerCmd inspect = mock(InspectContainerCmd.class);
when(inspect.exec()).thenReturn(mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS));
when(docker.inspectContainerCmd("node-1")).thenReturn(inspect);
ConnectToNetworkCmd connect = docker.connectToNetworkCmd();
assertTrue(manager.attachContainer(ACCOUNT, REGION, "vpc-1", "node-1"));
ArgumentCaptor<ContainerNetwork> placed = ArgumentCaptor.forClass(ContainerNetwork.class);
verify(connect).withContainerNetwork(placed.capture());
assertEquals("10.0.0.5", placed.getValue().getIpamConfig().getIpv4Address());
}
@Test
void aContainerDockerRefusesAtThePickedAddressIsAttachedWhereDockerPutsIt() {
manager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
manager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-low", "10.0.0.0/24");
existingNetwork(manager.networkName(ACCOUNT, REGION, "vpc-1"), "10.0.0.0/16", ourLabels("vpc-1", "4650"),
Map.of());
InspectContainerCmd inspect = mock(InspectContainerCmd.class);
when(inspect.exec()).thenReturn(mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS));
when(docker.inspectContainerCmd("node-1")).thenReturn(inspect);
ConnectToNetworkCmd connect = docker.connectToNetworkCmd();
doThrow(new ConflictException("Address already in use")).doNothing().when(connect).exec();
assertTrue(manager.attachContainer(ACCOUNT, REGION, "vpc-1", "node-1"));
verify(connect, times(2)).exec();
verify(connect, times(1)).withContainerNetwork(any());
}
// ─── Restart: addresses persisted instances still hold ───────────────────
@Test
@@ -650,6 +763,27 @@ class VpcNetworkManagerTest {
verify(connect, times(2)).withNetworkId(networkName);
}
@Test
void theCurrentFlociContainerTakesTheLowestAddressNoOtherContainerOnTheNetworkHolds() {
CurrentContainerNetworkResolver currentContainerNetworkResolver = mock(CurrentContainerNetworkResolver.class);
when(currentContainerNetworkResolver.resolveContainerId()).thenReturn(Optional.of("floci-container"));
VpcNetworkManager dockerHostedManager = new VpcNetworkManager(config, docker, currentContainerNetworkResolver);
Network.ContainerNetworkConfig endpoint = mock(Network.ContainerNetworkConfig.class);
when(endpoint.getIpv4Address()).thenReturn("10.0.0.2/16");
existingNetwork(dockerHostedManager.networkName(ACCOUNT, REGION, "vpc-1"), "10.0.0.0/16",
ourLabels("vpc-1", "4650"), Map.of("instance-0", endpoint));
dockerHostedManager.declareVpc(ACCOUNT, REGION, "vpc-1", "10.0.0.0/16");
dockerHostedManager.declareSubnet(ACCOUNT, REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
String address = dockerHostedManager.allocatePrivateIp(ACCOUNT, REGION, "subnet-a").orElseThrow();
assertTrue(dockerHostedManager.attach(ACCOUNT, REGION, "vpc-1", "subnet-a", "instance-1", address).isPresent());
ArgumentCaptor<ContainerNetwork> placed = ArgumentCaptor.forClass(ContainerNetwork.class);
verify(docker.connectToNetworkCmd(), times(2)).withContainerNetwork(placed.capture());
assertEquals(List.of("10.0.0.3", address), placed.getAllValues().stream()
.map(network -> network.getIpamConfig().getIpv4Address()).toList());
}
@Test
void doesNotReconnectCurrentFlociContainerAlreadyAttachedToVpcNetwork() {
CurrentContainerNetworkResolver currentContainerNetworkResolver =
@@ -121,6 +121,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
@@ -5448,6 +5449,29 @@ class EksClusterManagerTest {
assertEquals(firstLaunch.stream().filter(a -> !a.startsWith("--node-ip")).toList(), cmdCaptor.getValue());
}
@Test
void aFirstLaunchThatFallsBackToTheBridgeGivesItsPrefixesBackWithItsAddress() {
when(config.services().eks().vpcPodNetwork()).thenReturn(true);
when(config.services().eks().vpcPodPrefixes()).thenReturn(1);
when(portAllocator.allocate(6440, 6499)).thenReturn(6440);
when(lifecycleManager.create(any())).thenReturn("cid-vpc", "cid-bridge");
when(lifecycleManager.startCreated(eq("cid-bridge"), any()))
.thenReturn(new ContainerInfo("cid-bridge", Map.of()));
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"))
.thenReturn(Optional.of("10.0.1.25"));
when(ec2Service.allocateVpcPrefixes("000000000000", "us-east-1", "subnet-1", 1))
.thenReturn(List.of("10.0.1.224/28"));
when(ec2Service.attachContainerToVpc("000000000000", "us-east-1", "vpc-1", "subnet-1", "cid-vpc", "10.0.1.25"))
.thenReturn(Optional.empty());
Cluster cluster = vpcCluster("prefix-fallback", "vpc-1", List.of("subnet-1"));
manager.startCluster(cluster);
assertNull(cluster.getNodePrivateIp());
assertNull(cluster.getNodePodPrefixes());
verify(ec2Service).releaseVpcPrefix("000000000000", "us-east-1", "subnet-1", "10.0.1.224/28");
}
@Test
void restoreClusterFailsStartAndRetainsNodeIpWhenVpcAttachFails() {
when(lifecycleManager.findByName("floci-eks-restore-attach-fail")).thenReturn(Optional.empty());
@@ -5471,6 +5495,123 @@ class EksClusterManagerTest {
verify(lifecycleManager, never()).startCreated(anyString(), any());
}
@Test
void withVpcPodNetworkANodeLeasesItsPrefixesListsThemOnItsInterfaceAndReleasesThemOnStop() {
when(config.services().eks().vpcPodNetwork()).thenReturn(true);
when(config.services().eks().vpcPodPrefixes()).thenReturn(2);
when(portAllocator.allocate(6440, 6499)).thenReturn(6440);
when(lifecycleManager.create(any())).thenReturn("cid-1");
when(lifecycleManager.startCreated(eq("cid-1"), any())).thenReturn(new ContainerInfo("cid-1", Map.of()));
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"))
.thenReturn(Optional.of("10.0.1.25"));
when(ec2Service.allocateVpcPrefixes("000000000000", "us-east-1", "subnet-1", 2))
.thenReturn(List.of("10.0.1.224/28", "10.0.1.208/28"));
Cluster cluster = vpcCluster("prefix-cluster", "vpc-1", List.of("subnet-1"));
manager.startCluster(cluster);
assertEquals(List.of("10.0.1.224/28", "10.0.1.208/28"), cluster.getNodePodPrefixes());
Instance node = manager.synthesizeClusterNodeInstance(cluster, "172.17.0.2", "us-east-1", "000000000000");
assertEquals(List.of("10.0.1.224/28", "10.0.1.208/28"),
node.getNetworkInterfaces().getFirst().getIpv4Prefixes());
manager.stopCluster(cluster);
verify(ec2Service).releaseVpcPrefix("000000000000", "us-east-1", "subnet-1", "10.0.1.224/28");
verify(ec2Service).releaseVpcPrefix("000000000000", "us-east-1", "subnet-1", "10.0.1.208/28");
// Kept with the cluster, as its address is, for the next start to reserve.
assertEquals(List.of("10.0.1.224/28", "10.0.1.208/28"), cluster.getNodePodPrefixes());
}
@Test
void aRestartReservesTheNodesPrefixesAndLeasesOnlyTheOnesItLost() {
when(config.services().eks().vpcPodNetwork()).thenReturn(true);
when(config.services().eks().vpcPodPrefixes()).thenReturn(2);
when(lifecycleManager.findByName("floci-eks-recreated")).thenReturn(Optional.empty());
when(portAllocator.allocate(6440, 6499)).thenReturn(6440);
when(lifecycleManager.create(any())).thenReturn("cid-recreated");
when(lifecycleManager.startCreated(eq("cid-recreated"), any()))
.thenReturn(new ContainerInfo("cid-recreated", Map.of()));
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25")).thenReturn(true);
when(ec2Service.reserveVpcPrefixes("000000000000", "us-east-1", "subnet-1",
List.of("10.0.1.224/28", "10.0.1.208/28"))).thenReturn(List.of("10.0.1.224/28"));
when(ec2Service.allocateVpcPrefixes("000000000000", "us-east-1", "subnet-1", 1))
.thenReturn(List.of("10.0.1.192/28"));
Cluster cluster = vpcCluster("recreated", "vpc-1", List.of("subnet-1"));
cluster.setNodePrivateIp("10.0.1.25");
cluster.setNodePodPrefixes(List.of("10.0.1.224/28", "10.0.1.208/28"));
manager.restoreCluster(cluster);
assertEquals(List.of("10.0.1.224/28", "10.0.1.192/28"), cluster.getNodePodPrefixes());
}
@Test
void anAdoptedNodeKeepsThePrefixesItCanReserveAndLeasesNoOthers() {
when(config.services().eks().vpcPodNetwork()).thenReturn(true);
when(config.services().eks().vpcPodPrefixes()).thenReturn(2);
when(lifecycleManager.findByName("floci-eks-survivor"))
.thenReturn(Optional.of(survivingContainer("cid-survivor")));
when(lifecycleManager.adopt(eq("cid-survivor"), eq(List.of(6443)), any()))
.thenReturn(new ContainerInfo("cid-survivor", Map.of(), Map.of(6443, 6440)));
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25")).thenReturn(true);
when(ec2Service.reserveVpcPrefixes("000000000000", "us-east-1", "subnet-1",
List.of("10.0.1.224/28", "10.0.1.208/28"))).thenReturn(List.of("10.0.1.224/28"));
Cluster cluster = vpcCluster("survivor", "vpc-1", List.of("subnet-1"));
cluster.setNodePrivateIp("10.0.1.25");
cluster.setNodePodPrefixes(List.of("10.0.1.224/28", "10.0.1.208/28"));
manager.restoreCluster(cluster);
// Its pods keep the addresses it handed out; a prefix it did not start with would not be in its config.
assertEquals(List.of("10.0.1.224/28"), cluster.getNodePodPrefixes());
verify(ec2Service, never()).allocateVpcPrefixes(anyString(), anyString(), anyString(), anyInt());
}
@Test
void aContainerReplacingAnAdoptedNodeLeasesThePrefixesTheNodeLacks() {
when(config.services().eks().vpcPodNetwork()).thenReturn(true);
when(config.services().eks().vpcPodPrefixes()).thenReturn(2);
when(lifecycleManager.findByName("floci-eks-survivor"))
.thenReturn(Optional.of(survivingContainer("cid-survivor")));
when(lifecycleManager.adopt(eq("cid-survivor"), eq(List.of(6443)), any()))
.thenReturn(new ContainerInfo("cid-survivor", Map.of(), Map.of(6443, 6440)));
when(ec2Service.reserveVpcPrivateIp("000000000000", "us-east-1", "subnet-1", "10.0.1.25")).thenReturn(true);
when(ec2Service.reserveVpcPrefixes("000000000000", "us-east-1", "subnet-1", List.of("10.0.1.224/28")))
.thenReturn(List.of("10.0.1.224/28"));
when(ec2Service.allocateVpcPrefixes("000000000000", "us-east-1", "subnet-1", 1))
.thenReturn(List.of("10.0.1.192/28"));
when(lifecycleManager.create(any())).thenReturn("cid-new");
when(lifecycleManager.startCreated(eq("cid-new"), any())).thenReturn(new ContainerInfo("cid-new", Map.of()));
Cluster cluster = vpcCluster("survivor", "vpc-1", List.of("subnet-1"));
cluster.setDockerName("floci-eks-survivor");
cluster.setNodePrivateIp("10.0.1.25");
cluster.setNodePodPrefixes(List.of("10.0.1.224/28"));
manager.restoreCluster(cluster);
assertEquals(List.of("10.0.1.224/28"), cluster.getNodePodPrefixes());
assertTrue(manager.restartForNodeCapacity(cluster, "m5.large"));
assertEquals(List.of("10.0.1.224/28", "10.0.1.192/28"), cluster.getNodePodPrefixes());
verify(ec2Service, Mockito.times(1)).reserveVpcPrefixes(anyString(), anyString(), anyString(), anyList());
}
@Test
void withoutVpcPodNetworkANodeHoldsNoPrefixes() {
when(portAllocator.allocate(6440, 6499)).thenReturn(6440);
when(lifecycleManager.create(any())).thenReturn("cid-1");
when(lifecycleManager.startCreated(eq("cid-1"), any())).thenReturn(new ContainerInfo("cid-1", Map.of()));
when(ec2Service.allocateVpcPrivateIp("000000000000", "us-east-1", "subnet-1"))
.thenReturn(Optional.of("10.0.1.25"));
Cluster cluster = vpcCluster("plain-cluster", "vpc-1", List.of("subnet-1"));
cluster.setNodePodPrefixes(List.of("10.0.1.224/28"));
manager.startCluster(cluster);
assertNull(cluster.getNodePodPrefixes());
verify(ec2Service, never()).allocateVpcPrefixes(anyString(), anyString(), anyString(), anyInt());
verify(ec2Service, never()).reserveVpcPrefixes(anyString(), anyString(), anyString(), anyList());
}
@Test
void restoreClusterAdoptingSurvivingContainerReservesPersistedNodeIp() {
when(lifecycleManager.findByName("floci-eks-survivor"))
@@ -10,6 +10,7 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -38,6 +39,31 @@ class EksStartupValidatorTest {
assertDoesNotThrow(() -> mock.onStart(new StartupEvent()));
}
@ParameterizedTest
@ValueSource(ints = {0, -1, 17})
void aPodPrefixCountOutOfRangeIsRejectedWithTheVpcPodNetworkOn(int prefixes) {
EksStartupValidator validator = vpcPodNetworkValidator(true, prefixes);
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
() -> validator.onStart(new StartupEvent()));
assertTrue(refused.getMessage().contains("floci.services.eks.vpc-pod-prefixes"), refused.getMessage());
}
@Test
void aPodPrefixCountIsCheckedOnlyWithTheVpcPodNetworkOn() {
assertDoesNotThrow(() -> vpcPodNetworkValidator(true, 1).onStart(new StartupEvent()));
assertDoesNotThrow(() -> vpcPodNetworkValidator(true, 16).onStart(new StartupEvent()));
assertDoesNotThrow(() -> vpcPodNetworkValidator(false, 0).onStart(new StartupEvent()));
}
private static EksStartupValidator vpcPodNetworkValidator(boolean vpcPodNetwork, int prefixes) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().eks().endpointHost()).thenReturn(Optional.empty());
when(config.services().eks().enabled()).thenReturn(true);
when(config.services().eks().vpcPodNetwork()).thenReturn(vpcPodNetwork);
when(config.services().eks().vpcPodPrefixes()).thenReturn(prefixes);
return new EksStartupValidator(config);
}
private static EksStartupValidator validator(Optional<String> endpointHost) {
return validator(endpointHost, true, false);
}