fix(eks): record a node's container address so ELB instance targets reach it (#8334)

This commit is contained in:
mrfelek authored and GitHub committed 2026-11-14 13:13:13 -04:00
1 parent e182cdad82
commit 0b19c15e71
4 files changed
+136 -4

No files matched your search

@@ -9930,6 +9930,41 @@ class Ec2ServiceTest {
}
}
/**
* A cluster node found by its id keeps the address its provider recorded for the container behind it, which an
* ELB instance target naming the node connects to.
*/
@Test
void aClusterNodeFoundByIdKeepsTheContainerAddressItsProviderRecorded() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
Instance nodeInstance = new Instance();
nodeInstance.setInstanceId("i-0de1234567890abd");
nodeInstance.setRegion("us-east-1");
nodeInstance.setPlacement(new Placement("us-east-1a"));
nodeInstance.setState(InstanceState.running());
nodeInstance.setPrivateIpAddress("10.10.80.10");
nodeInstance.setDockerContainerId("node-container");
nodeInstance.setContainerBridgeIp("10.10.80.10");
service.setClusterNodeInstanceProvider(new ClusterNodeInstanceProvider() {
@Override
public Optional<Instance> findInstance(String accountId, String region, String instanceId) {
return "i-0de1234567890abd".equals(instanceId) ? Optional.of(nodeInstance) : Optional.empty();
}
@Override
public List<Instance> listInstances(String accountId, String region) {
return List.of(nodeInstance);
}
});
Instance found = service.findInstanceById("123456789012", "i-0de1234567890abd");
assertEquals("node-container", found.getDockerContainerId());
assertEquals("10.10.80.10", found.getContainerBridgeIp());
}
@Test
void attachVolumeResolvesClusterNodeInstance() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
@@ -14,6 +14,7 @@ import com.github.dockerjava.api.command.InspectExecCmd;
import com.github.dockerjava.api.command.InspectExecResponse;
import com.github.dockerjava.api.command.InspectVolumeCmd;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.api.model.NetworkSettings;
@@ -2088,6 +2089,83 @@ class EksClusterManagerTest {
+ logs.stream().map(LogRecord::getMessage).toList());
}
@Test
void aRegisteredNodeIsReachedAtItsPrivateAddress() {
Cluster cluster = new Cluster();
cluster.setName("reach-cluster");
manager.registerClusterNodeInstance(cluster, "container-reach");
Instance node = manager.getRegisteredClusterNodeInstance(cluster);
assertEquals("container-reach", node.getDockerContainerId());
assertEquals("172.17.0.2", node.getPrivateIpAddress());
assertEquals("172.17.0.2", node.getContainerBridgeIp());
}
@Test
void aNodeOnSeveralNetworksIsReachedAtThePrivateAddressItsContainerHolds() {
// The first network that isn't the default bridge, the one EC2 would pick, is Floci's own here, but the
// node reports its VPC address, and that is where an instance target's traffic goes.
InspectContainerCmd inspect = Mockito.mock(InspectContainerCmd.class);
InspectContainerResponse response = Mockito.mock(InspectContainerResponse.class);
NetworkSettings settings = Mockito.mock(NetworkSettings.class);
Map<String, ContainerNetwork> networks = new LinkedHashMap<>();
networks.put("floci_default", new ContainerNetwork().withIpv4Address("172.18.0.4"));
networks.put("floci-vpc", new ContainerNetwork().withIpv4Address("10.10.0.3"));
when(dockerClient.inspectContainerCmd(anyString())).thenReturn(inspect);
when(inspect.exec()).thenReturn(response);
when(response.getNetworkSettings()).thenReturn(settings);
when(settings.getNetworks()).thenReturn(networks);
Cluster cluster = new Cluster();
cluster.setName("vpc-cluster");
cluster.setNodePrivateIp("10.10.0.3");
manager.registerClusterNodeInstance(cluster, "container-vpc");
Instance node = manager.getRegisteredClusterNodeInstance(cluster);
assertEquals("10.10.0.3", node.getPrivateIpAddress());
assertEquals("10.10.0.3", node.getContainerBridgeIp());
}
@Test
void aNodeWhosePrivateAddressItsContainerDoesNotHoldIsReachedAtTheContainersAddress() {
Cluster cluster = new Cluster();
cluster.setName("moved-cluster");
cluster.setNodePrivateIp("10.10.0.9");
manager.registerClusterNodeInstance(cluster, "container-moved");
Instance node = manager.getRegisteredClusterNodeInstance(cluster);
assertEquals("10.10.0.9", node.getPrivateIpAddress());
assertEquals("172.17.0.2", node.getContainerBridgeIp());
}
@Test
void aNodeWhoseAddressesCannotBeDeterminedRecordsNoneToReachItAt() {
when(dockerClient.inspectContainerCmd(anyString()))
.thenThrow(new RuntimeException("docker is unreachable"));
Cluster cluster = new Cluster();
cluster.setName("unknown-cluster");
manager.registerClusterNodeInstance(cluster, "container-unknown");
Instance node = manager.getRegisteredClusterNodeInstance(cluster);
assertEquals("10.0.0.1", node.getPrivateIpAddress());
assertNull(node.getContainerBridgeIp());
}
@Test
void theMetadataEndpointRecordsANodeItRegistersAsRegistrationDoes() {
Cluster cluster = new Cluster();
cluster.setName("imds-first-cluster");
manager.configureLinkLocalMetadataEndpoint(cluster, "container-42");
Instance node = manager.getRegisteredClusterNodeInstance(cluster);
assertEquals("container-42", node.getDockerContainerId());
assertEquals("172.17.0.2", node.getContainerBridgeIp());
}
@Test
void clusterNodeInstanceProviderFindsAndListsRegisteredInstances() {
Cluster cluster = new Cluster();