feat(eks): give the cluster node instance its primary network interface (#9273)

An EKS node's primary network interface carries the cluster security
group, and the AWS Load Balancer Controller reads both from the node's
instance: it finds the interface behind a pod address there and opens
the target's ports on that group. Floci's node instance had neither.

A node holding an address in its subnet now has a primary interface
there, at that address, holding the cluster security group, and the
instance holds the group too. A node without one, whose address is then
its container's on another network, has no interface. DescribeInstances
reports them, and DescribeNetworkInterfaces lists the interface as it
lists an instance's launch-created one, with the same filters. Its ids
derive from the cluster, so they survive a restart.
ModifyNetworkInterfaceAttribute and the IPv6 address calls on it are
refused with OperationNotPermitted, as the node's lifecycle actions are,
and the cluster security group cannot be deleted while the node holds
it.

EC2 keeps a copy of the node once an action such as
ModifyInstanceAttribute is stored. The copy now keeps only what such
actions change: its interfaces are its own, so a change no longer
reaches the record the cluster serves, and the node's address, subnet
and interfaces follow the cluster, which may replace the node. The copy
is stored only while the cluster serves the node. Deleting the cluster
ends the node in EC2 as terminating it would: the copy is dropped, and
volumes attached to the node are detached, or deleted when their
DeleteOnTermination is set. Otherwise the copy would stay a running
instance holding the cluster security group, and the volumes in use.

Part of #5929
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 11:04:27 -04:00
1 parent 4897db0bad
commit 39fbc98efe
10 files changed
+565 -25

No files matched your search

@@ -21,6 +21,7 @@ import io.floci.aws.services.ec2.model.EgressOnlyInternetGateway;
import io.floci.aws.services.ec2.model.GroupIdentifier;
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.InternetGateway;
import io.floci.aws.services.ec2.model.IpPermission;
@@ -73,6 +74,7 @@ import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@@ -113,6 +115,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
@@ -10659,6 +10662,217 @@ class Ec2ServiceTest {
service.stopInstances("us-east-1", List.of("i-node1234567890abc"))).getErrorCode());
}
/** A cluster's node in a new subnet of {@code vpcId}, at 10.10.1.25, its interface holding {@code groupId}. */
private static Instance clusterNodeInSubnet(Ec2Service service, String vpcId, String groupId) {
return clusterNode(vpcId, service.createSubnet("us-east-1", vpcId, "10.10.1.0/24", "us-east-1a").getSubnetId(),
groupId);
}
private static Instance clusterNode(String vpcId, String subnetId, String groupId) {
GroupIdentifier group = new GroupIdentifier(groupId, "eks-cluster-sg-demo-0123abcd");
Instance node = new Instance();
node.setInstanceId("i-0de0000000000005a");
node.setRegion("us-east-1");
node.setPlacement(new Placement("us-east-1a"));
node.setState(InstanceState.running());
node.setInstanceType("m5.large");
node.setVpcId(vpcId);
node.setSubnetId(subnetId);
node.setPrivateIpAddress("10.10.1.25");
node.setLaunchTime(Instant.parse("2026-11-23T10:00:00Z"));
node.setSecurityGroups(new ArrayList<>(List.of(group)));
InstanceNetworkInterface eni = new InstanceNetworkInterface();
eni.setNetworkInterfaceId("eni-0de0000000000005a");
eni.setSubnetId(subnetId);
eni.setVpcId(vpcId);
eni.setOwnerId("000000000000");
eni.setPrivateIpAddress("10.10.1.25");
eni.setGroups(new ArrayList<>(List.of(group)));
eni.setAttachmentId("eni-attach-0de0000000000005a");
node.getNetworkInterfaces().add(eni);
return node;
}
private static void provideClusterNode(Ec2Service service, AtomicReference<Instance> node) {
service.setClusterNodeInstanceProvider(new ClusterNodeInstanceProvider() {
@Override
public Optional<Instance> findInstance(String accountId, String region, String instanceId) {
Instance current = node.get();
return current != null && current.getInstanceId().equals(instanceId)
&& (region == null || "us-east-1".equals(region)) ? Optional.of(current) : Optional.empty();
}
@Override
public List<Instance> listInstances(String accountId, String region) {
Instance current = node.get();
return current != null && (region == null || "us-east-1".equals(region)) ? List.of(current) : List.of();
}
});
}
@Test
void aClusterNodesInterfaceIsListedAsAnInstancesInterfaceAndRefusesChanges() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.10.0.0/16", false).getVpcId();
String groupId = service.createSecurityGroup("us-east-1", "eks-cluster-sg-demo-0123abcd", "x", vpcId)
.getGroupId();
Instance node = clusterNodeInSubnet(service, vpcId, groupId);
provideClusterNode(service, new AtomicReference<>(node));
for (Map<String, List<String>> filter : List.of(Map.<String, List<String>>of(),
Map.of("attachment.instance-id", List.of(node.getInstanceId())),
Map.of("addresses.private-ip-address", List.of("10.10.1.25")),
Map.of("group-id", List.of(groupId)))) {
List<NetworkInterface> listed = service.describeNetworkInterfaces("us-east-1", List.of(), filter)
.networkInterfaces();
assertEquals(List.of("eni-0de0000000000005a"),
listed.stream().map(NetworkInterface::getNetworkInterfaceId).toList(), filter.toString());
}
NetworkInterface eni = service.describeNetworkInterfaces("us-east-1", List.of("eni-0de0000000000005a"),
Map.of()).networkInterfaces().getFirst();
assertEquals(node.getInstanceId(), eni.getAttachment().getInstanceId());
assertEquals("eni-attach-0de0000000000005a", eni.getAttachment().getAttachmentId());
assertEquals("10.10.1.25", eni.getPrivateIpAddresses().getFirst().getPrivateIpAddress());
assertEquals(List.of(groupId), eni.getGroups().stream().map(GroupIdentifier::getGroupId).toList());
assertEquals("us-east-1a", eni.getAvailabilityZone());
// DescribeInstances' copy carries the attach time an instance's interface reports.
assertEquals("2026-11-23T10:00:00.000Z", service.describeInstances("us-east-1", List.of(node.getInstanceId()),
Map.of()).getFirst().getInstances().getFirst().getNetworkInterfaces().getFirst().getAttachTime());
for (Executable change : List.<Executable>of(
() -> service.modifyNetworkInterfaceAttribute("us-east-1", "eni-0de0000000000005a",
new Ec2Service.NetworkInterfaceAttributeChange(null, "x", null, null, null), false),
() -> service.assignIpv6Addresses("us-east-1", "eni-0de0000000000005a", null, 1),
() -> service.unassignIpv6Addresses("us-east-1", "eni-0de0000000000005a", List.of("2001:db8::1")))) {
AwsException refused = assertThrows(AwsException.class, change);
assertEquals("OperationNotPermitted", refused.getErrorCode());
assertEquals("The network interface 'eni-0de0000000000005a' belongs to a cluster node and cannot be "
+ "modified through EC2", refused.getMessage());
}
}
@Test
void aGroupChangeOnAClusterNodeReachesOnlyEc2sCopyOfIt() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.10.0.0/16", false).getVpcId();
String groupId = service.createSecurityGroup("us-east-1", "eks-cluster-sg-demo-0123abcd", "x", vpcId)
.getGroupId();
String otherId = service.createSecurityGroup("us-east-1", "other", "x", vpcId).getGroupId();
Instance node = clusterNodeInSubnet(service, vpcId, groupId);
provideClusterNode(service, new AtomicReference<>(node));
service.modifyInstanceGroups("us-east-1", node.getInstanceId(), List.of(otherId));
assertEquals(List.of(groupId), node.getNetworkInterfaces().getFirst().getGroups().stream()
.map(GroupIdentifier::getGroupId).toList());
assertEquals(List.of(otherId), service.describeNetworkInterfaces("us-east-1",
List.of("eni-0de0000000000005a"), Map.of()).networkInterfaces().getFirst().getGroups().stream()
.map(GroupIdentifier::getGroupId).toList());
}
@Test
void ec2sCopyOfAClusterNodeFollowsTheNetworkOfTheNodeItsClusterServesNow() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.10.0.0/16", false).getVpcId();
String groupId = service.createSecurityGroup("us-east-1", "eks-cluster-sg-demo-0123abcd", "x", vpcId)
.getGroupId();
String otherId = service.createSecurityGroup("us-east-1", "other", "x", vpcId).getGroupId();
Instance node = clusterNodeInSubnet(service, vpcId, groupId);
AtomicReference<Instance> provided = new AtomicReference<>(node);
provideClusterNode(service, provided);
// A group change stores EC2's copy of the node.
service.modifyInstanceGroups("us-east-1", node.getInstanceId(), List.of(otherId));
// The cluster replaces its node, which keeps its ids and gets another address.
Instance replacement = clusterNode(vpcId, node.getSubnetId(), groupId);
replacement.setPrivateIpAddress("10.10.1.40");
replacement.getNetworkInterfaces().getFirst().setPrivateIpAddress("10.10.1.40");
provided.set(replacement);
NetworkInterface eni = service.describeNetworkInterfaces("us-east-1", List.of("eni-0de0000000000005a"),
Map.of()).networkInterfaces().getFirst();
assertEquals("10.10.1.40", eni.getPrivateIpAddress());
assertEquals(List.of(otherId), eni.getGroups().stream().map(GroupIdentifier::getGroupId).toList());
Instance described = service.describeInstances("us-east-1", List.of(node.getInstanceId()), Map.of())
.getFirst().getInstances().getFirst();
assertEquals("10.10.1.40", described.getPrivateIpAddress());
assertEquals("10.10.1.40", described.getNetworkInterfaces().getFirst().getPrivateIpAddress());
assertEquals(List.of(otherId), described.getSecurityGroups().stream().map(GroupIdentifier::getGroupId).toList());
}
@Test
void aChangeThatReadANodeBeforeItsClusterEndedItStoresNoCopyOfIt() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.10.0.0/16", false).getVpcId();
String groupId = service.createSecurityGroup("us-east-1", "eks-cluster-sg-demo-0123abcd", "x", vpcId)
.getGroupId();
Instance node = clusterNodeInSubnet(service, vpcId, groupId);
// The change reads the node, then its cluster is deleted and stops serving it before the change stores it.
AtomicInteger lookups = new AtomicInteger();
service.setClusterNodeInstanceProvider(new ClusterNodeInstanceProvider() {
@Override
public Optional<Instance> findInstance(String accountId, String region, String instanceId) {
return lookups.getAndIncrement() == 0 && node.getInstanceId().equals(instanceId)
? Optional.of(node) : Optional.empty();
}
@Override
public List<Instance> listInstances(String accountId, String region) {
return List.of();
}
});
AwsException gone = assertThrows(AwsException.class, () ->
service.modifyInstanceAttribute("us-east-1", node.getInstanceId(), "sourceDestCheck", "false"));
assertEquals("InvalidInstanceID.NotFound", gone.getErrorCode());
// No copy holds the cluster security group, which goes with the cluster.
service.deleteSecurityGroup("us-east-1", groupId);
}
@Test
void aClusterNodesGroupIsInUseUntilTheNodeAndEc2sCopyOfItAreGone() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
String vpcId = service.createVpc("us-east-1", "10.10.0.0/16", false).getVpcId();
String groupId = service.createSecurityGroup("us-east-1", "eks-cluster-sg-demo-0123abcd", "x", vpcId)
.getGroupId();
Instance node = clusterNodeInSubnet(service, vpcId, groupId);
AtomicReference<Instance> provided = new AtomicReference<>(node);
provideClusterNode(service, provided);
assertEquals("DependencyViolation", assertThrows(AwsException.class,
() -> service.deleteSecurityGroup("us-east-1", groupId)).getErrorCode());
// An attribute change keeps EC2's copy of the node, which stays behind when the cluster stops serving it, and
// a volume attached to the node stays in use.
service.modifyInstanceAttribute("us-east-1", node.getInstanceId(), "sourceDestCheck", "false");
Volume volume = service.createVolume("us-east-1", "us-east-1a", "gp3", 10, false, 3000, null, null, List.of());
service.attachVolume("us-east-1", volume.getVolumeId(), node.getInstanceId(), "/dev/xvdf");
provided.set(null);
assertEquals("DependencyViolation", assertThrows(AwsException.class,
() -> service.deleteSecurityGroup("us-east-1", groupId)).getErrorCode());
service.endClusterNodeInstance("000000000000", "us-east-1", node.getInstanceId());
service.deleteSecurityGroup("us-east-1", groupId);
assertTrue(service.describeInstances("us-east-1", List.of(), Map.of()).stream()
.flatMap(reservation -> reservation.getInstances().stream())
.noneMatch(instance -> node.getInstanceId().equals(instance.getInstanceId())));
Volume released = service.describeVolumes("us-east-1", List.of(volume.getVolumeId()), Map.of())
.getFirst();
assertEquals("available", released.getState());
assertTrue(released.getAttachments().isEmpty());
}
@Test
void ordinaryEc2InstancesUnaffectedByClusterNodeInstanceProvider() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
@@ -47,7 +47,9 @@ import io.floci.aws.services.ec2.Ec2ImageCatalog;
import io.floci.aws.services.ec2.Ec2InstanceTypeCatalog;
import io.floci.aws.services.ec2.Ec2MetadataServer;
import io.floci.aws.services.ec2.Ec2Service;
import io.floci.aws.services.ec2.model.GroupIdentifier;
import io.floci.aws.services.ec2.model.Instance;
import io.floci.aws.services.ec2.model.InstanceNetworkInterface;
import io.floci.aws.services.ec2.model.LaunchTemplateData;
import io.floci.aws.services.ec2.model.Placement;
import io.floci.aws.services.ec2.net.VpcNetworkManager;
@@ -5243,6 +5245,67 @@ class EksClusterManagerTest {
return cluster;
}
@Test
void aNodeInASubnetHasAPrimaryInterfaceHoldingTheClusterSecurityGroup() {
Cluster cluster = vpcCluster("eni-cluster", "vpc-1", List.of("subnet-1", "subnet-2"));
cluster.getResourcesVpcConfig().setClusterSecurityGroupId("sg-cluster");
cluster.setNodePrivateIp("10.0.1.25");
when(ec2Service.securityGroupName("000000000000", "us-east-1", "sg-cluster"))
.thenReturn(Optional.of("eks-cluster-sg-eni-cluster-0123abcd"));
Instance node = manager.synthesizeClusterNodeInstance(cluster, "172.17.0.2", "us-east-1", "000000000000");
assertEquals(List.of("sg-cluster:eks-cluster-sg-eni-cluster-0123abcd"), groupsOf(node.getSecurityGroups()));
assertEquals(1, node.getNetworkInterfaces().size());
InstanceNetworkInterface eni = node.getNetworkInterfaces().getFirst();
assertEquals(manager.deriveClusterNodeNetworkInterfaceId(cluster, "us-east-1", "000000000000"),
eni.getNetworkInterfaceId());
assertTrue(eni.getNetworkInterfaceId().matches("eni-[0-9a-f]{17}"), eni.getNetworkInterfaceId());
assertTrue(eni.getAttachmentId().matches("eni-attach-[0-9a-f]{17}"), eni.getAttachmentId());
assertEquals(0, eni.getDeviceIndex());
assertEquals("subnet-1", eni.getSubnetId());
assertEquals("vpc-1", eni.getVpcId());
assertEquals("000000000000", eni.getOwnerId());
assertEquals("10.0.1.25", eni.getPrivateIpAddress());
assertEquals(node.getPrivateDnsName(), eni.getPrivateDnsName());
assertEquals(List.of("sg-cluster:eks-cluster-sg-eni-cluster-0123abcd"), groupsOf(eni.getGroups()));
// The ids are the cluster's, so the node keeps them across a restart.
InstanceNetworkInterface again = manager.synthesizeClusterNodeInstance(
cluster, "172.17.0.3", "us-east-1", "000000000000").getNetworkInterfaces().getFirst();
assertEquals(eni.getNetworkInterfaceId(), again.getNetworkInterfaceId());
assertEquals(eni.getAttachmentId(), again.getAttachmentId());
}
@Test
void aNodeWithoutASubnetAddressHasNoInterfaceAndANodeWhoseGroupIsGoneHasNoGroup() {
Cluster noSubnet = new Cluster();
noSubnet.setName("no-subnet");
Instance bare = manager.synthesizeClusterNodeInstance(noSubnet, "172.17.0.2", "us-east-1", "000000000000");
assertTrue(bare.getNetworkInterfaces().isEmpty());
assertTrue(bare.getSecurityGroups().isEmpty());
// No address leased in the subnet: the node's own address is its container's, on another network.
Cluster unleased = vpcCluster("unleased", "vpc-1", List.of("subnet-1"));
unleased.getResourcesVpcConfig().setClusterSecurityGroupId("sg-cluster");
Instance fallback = manager.synthesizeClusterNodeInstance(unleased, "172.17.0.2", "us-east-1", "000000000000");
assertEquals("172.17.0.2", fallback.getPrivateIpAddress());
assertTrue(fallback.getNetworkInterfaces().isEmpty());
assertTrue(fallback.getSecurityGroups().isEmpty());
Cluster groupGone = vpcCluster("group-gone", "vpc-1", List.of("subnet-1"));
groupGone.setNodePrivateIp("10.0.1.26");
groupGone.getResourcesVpcConfig().setClusterSecurityGroupId("sg-gone");
when(ec2Service.securityGroupName("000000000000", "us-east-1", "sg-gone")).thenReturn(Optional.empty());
Instance node = manager.synthesizeClusterNodeInstance(groupGone, "172.17.0.2", "us-east-1", "000000000000");
assertEquals(1, node.getNetworkInterfaces().size());
assertTrue(node.getNetworkInterfaces().getFirst().getGroups().isEmpty());
assertTrue(node.getSecurityGroups().isEmpty());
}
private List<String> groupsOf(List<GroupIdentifier> groups) {
return groups.stream().map(group -> group.getGroupId() + ":" + group.getGroupName()).toList();
}
@Test
void buildServerArgsIncludesNodeIpWhenProvided() {
List<String> args = EksClusterManager.buildServerArgs(
@@ -637,6 +637,33 @@ class EksServiceTest {
assertEquals(ClusterStatus.ACTIVE, service.describeCluster("running-cluster").getStatus());
}
@Test
void deletingAClusterEndsItsNodeInEc2BeforeDeletingItsSecurityGroup() {
Cluster cluster = activeCluster();
cluster.setArn("arn:aws:eks:us-east-1:000000000000:cluster/running-cluster");
ResourcesVpcConfig vpc = new ResourcesVpcConfig();
vpc.setVpcId("vpc-1");
vpc.setClusterSecurityGroupId("sg-cluster");
cluster.setResourcesVpcConfig(vpc);
EksClusterManager clusterManager = mock(EksClusterManager.class);
when(clusterManager.deriveClusterNodeInstanceId(cluster, "us-east-1", "000000000000")).thenReturn("i-0de00node");
Ec2Service ec2 = mock(Ec2Service.class);
AccountAwareStorageBackend<Cluster> clusterStore =
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), null, "000000000000");
clusterStore.putForAccount("000000000000", EksService.clusterKey("us-east-1", cluster.getName()), cluster);
EksService service = new EksService(fixedStorageFactory(clusterStore), testConfig(false, false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, ec2,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()), mock(EksAccessEntryService.class));
service.deleteCluster(cluster.getName());
InOrder order = inOrder(clusterManager, ec2);
order.verify(clusterManager).stopCluster(cluster);
order.verify(ec2).endClusterNodeInstance("000000000000", "us-east-1", "i-0de00node");
order.verify(ec2).deleteSecurityGroup("us-east-1", "sg-cluster");
}
private static Cluster activeCluster() {
Cluster cluster = new Cluster();
cluster.setName("running-cluster");