mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(eks): record a node's container address so ELB instance targets reach it (#8334)
This commit is contained in:
1 parent
e182cdad82
commit
0b19c15e71
4 files changed
+136
-4
No files matched your search
@@ -6165,6 +6165,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
copy.setStateReasonMessage(source.getStateReasonMessage());
|
||||
copy.setRegion(source.getRegion());
|
||||
copy.setDockerContainerId(source.getDockerContainerId());
|
||||
copy.setContainerBridgeIp(source.getContainerBridgeIp());
|
||||
copy.setRootVolumeId(source.getRootVolumeId());
|
||||
copy.setDisableApiStop(source.isDisableApiStop());
|
||||
copy.setDisableApiTermination(source.isDisableApiTermination());
|
||||
|
||||
@@ -2665,7 +2665,7 @@ public class EksClusterManager
|
||||
String region = clusterRegion(cluster);
|
||||
ContainerIps containerIps = resolveContainerIps(containerId);
|
||||
Instance nodeInstance = synthesizeClusterNodeInstance(cluster, containerIps.primaryIp(), region, accountId);
|
||||
nodeInstance.setDockerContainerId(containerId);
|
||||
recordNodeContainer(nodeInstance, containerId, containerIps);
|
||||
clusterNodeInstances.put(clusterResourceName(cluster), new ClusterNodeRecord(accountId, region, nodeInstance));
|
||||
registerClusterNodeVpc(cluster, accountId, region, containerIps.allIps());
|
||||
for (Consumer<Instance> listener : nodeRegistrationListeners) {
|
||||
@@ -2694,10 +2694,10 @@ public class EksClusterManager
|
||||
String accountId = resolveClusterAccountId(cluster);
|
||||
String region = clusterRegion(cluster);
|
||||
nodeInstance = synthesizeClusterNodeInstance(cluster, containerIps.primaryIp(), region, accountId);
|
||||
nodeInstance.setDockerContainerId(containerId);
|
||||
recordNodeContainer(nodeInstance, containerId, containerIps);
|
||||
clusterNodeInstances.put(clusterResourceName(cluster), new ClusterNodeRecord(accountId, region, nodeInstance));
|
||||
} else if (nodeInstance.getDockerContainerId() == null) {
|
||||
nodeInstance.setDockerContainerId(containerId);
|
||||
recordNodeContainer(nodeInstance, containerId, containerIps);
|
||||
}
|
||||
|
||||
if (metadataServer != null) {
|
||||
@@ -3461,8 +3461,26 @@ public class EksClusterManager
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A node container's addresses: {@code primaryIp} is the one EC2 would pick for a container it runs
|
||||
* ({@link Ec2MetadataProxy#preferredMetadataSourceIp}), or null when inspecting the container found none.
|
||||
*/
|
||||
record ContainerIps(String primaryIp, Set<String> allIps) {}
|
||||
|
||||
/**
|
||||
* Records the container behind a cluster's node on the node's instance, as EC2 does for the containers it runs:
|
||||
* the container, and the address Floci reaches it at, which an ELB instance target or health check naming the
|
||||
* node connects to. That address is the node's private IP when its container holds it, since AWS sends an
|
||||
* instance target's traffic to the instance's primary private IP, and otherwise the container's primary
|
||||
* address. No address is recorded when inspecting the container found none.
|
||||
*/
|
||||
static void recordNodeContainer(Instance nodeInstance, String containerId, ContainerIps containerIps) {
|
||||
nodeInstance.setDockerContainerId(containerId);
|
||||
String privateIp = nodeInstance.getPrivateIpAddress();
|
||||
nodeInstance.setContainerBridgeIp(containerIps.allIps().contains(privateIp)
|
||||
? privateIp : containerIps.primaryIp());
|
||||
}
|
||||
|
||||
ContainerIps resolveContainerIps(String containerId) {
|
||||
Set<String> ips = new LinkedHashSet<>();
|
||||
String preferred = null;
|
||||
@@ -3489,7 +3507,7 @@ public class EksClusterManager
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Could not inspect container {0} for IPs: {1}", containerId, e.getMessage());
|
||||
}
|
||||
return new ContainerIps(preferred != null ? preferred : "10.0.0.1", ips);
|
||||
return new ContainerIps(preferred, ips);
|
||||
}
|
||||
|
||||
Instance getRegisteredClusterNodeInstance(Cluster cluster) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user