mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(ec2): config toggle to report the AWS-faithful private IP (#2024)
* feat(ec2): config toggle to report the AWS-faithful private IP (#1983) DescribeInstances reported the Docker container's bridge IP as PrivateIpAddress, disagreeing with CloudFormation's subnet-allocated IP. Rather than force a fidelity-vs-reachability tradeoff, this adds an opt-in: ec2.aws-faithful-private-ip (default false) Default (false) is unchanged: the reported private IP is the reachable container bridge IP, so instances can reach each other at that address on the shared Docker network. When true, the CFN/subnet-allocated private IP set at launch is reported instead (AWS-faithful); routing and IMDS always use the container bridge IP (tracked separately as containerBridgeIp) regardless of the flag. Wired via a new awsFaithful overload of exposeReachablePrivateAddress; the three call sites pass the config flag. Test covers both modes. Fixes #1983. * docs(ec2): list aws-faithful-private-ip in application.yml and the config table Every other floci.services.ec2 knob appears in both application.yml files and the docs configuration table; this one was declared only on EmulatorConfig, so a user-facing flag was undiscoverable.
This commit is contained in:
1 parent
5e01c07b33
commit
33d35b2079
6 files changed
+55
-4
No files matched your search
@@ -420,6 +420,7 @@ Launch templates store versioned launch data. New template versions can be creat
|
||||
| `FLOCI_SERVICES_EC2_MAX_PUBLISHED_PORTS_PER_INSTANCE` | `20` | Max published ports per instance; also the widest single-rule span published |
|
||||
| `FLOCI_SERVICES_EC2_SOCAT_IMAGE` | `alpine/socat` | Image used for the port-forwarding sidecar |
|
||||
| `FLOCI_SERVICES_EC2_MOCK` | `false` | Skip Docker; instances jump directly to final state (useful for tests) |
|
||||
| `FLOCI_SERVICES_EC2_AWS_FAITHFUL_PRIVATE_IP` | `false` | Report the CFN/subnet-allocated private IP instead of the container bridge IP; routing and IMDS are unaffected |
|
||||
|
||||
## Requirements
|
||||
|
||||
|
||||
@@ -1545,6 +1545,18 @@ public interface EmulatorConfig {
|
||||
@WithDefault("true")
|
||||
boolean enabled();
|
||||
|
||||
/**
|
||||
* When true, DescribeInstances and IMDS report each instance's CFN- and
|
||||
* subnet-allocated private IP (AWS-faithful) instead of the Docker
|
||||
* container's bridge IP (#1983). Default false keeps the bridge IP as the
|
||||
* reported private address, which lets instances reach each other at that
|
||||
* address on the shared Docker network. Routing/IMDS always use the
|
||||
* container bridge IP regardless of this flag; only the reported
|
||||
* PrivateIpAddress changes.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean awsFaithfulPrivateIp();
|
||||
|
||||
/** Port on the Floci host for the IMDS HTTP server (169.254.169.254 equivalent). */
|
||||
@WithDefault("9169")
|
||||
int imdsPort();
|
||||
|
||||
@@ -192,7 +192,7 @@ public class Ec2ContainerManager {
|
||||
String containerIp = waitForContainerBridgeIp(containerId, instanceId);
|
||||
if (containerIp != null && !containerIp.isBlank()) {
|
||||
instance.setContainerBridgeIp(containerIp);
|
||||
exposeReachablePrivateAddress(instance, containerIp);
|
||||
exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp());
|
||||
metadataServer.registerContainer(containerIp, instanceId, instance);
|
||||
}
|
||||
else {
|
||||
@@ -321,7 +321,7 @@ public class Ec2ContainerManager {
|
||||
String containerIp = waitForContainerBridgeIp(containerId, instanceId);
|
||||
if (containerIp != null && !containerIp.isBlank()) {
|
||||
instance.setContainerBridgeIp(containerIp);
|
||||
exposeReachablePrivateAddress(instance, containerIp);
|
||||
exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp());
|
||||
metadataServer.registerContainer(containerIp, instanceId, instance);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
@@ -361,15 +361,29 @@ public class Ec2ContainerManager {
|
||||
metadataServer.unregisterContainer(previousContainerIp, instance);
|
||||
}
|
||||
instance.setContainerBridgeIp(containerIp);
|
||||
exposeReachablePrivateAddress(instance, containerIp);
|
||||
exposeReachablePrivateAddress(instance, containerIp, config.services().ec2().awsFaithfulPrivateIp());
|
||||
metadataServer.registerContainer(containerIp, instance.getInstanceId(), instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void exposeReachablePrivateAddress(Instance instance, String privateIp) {
|
||||
exposeReachablePrivateAddress(instance, privateIp, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite the instance's reported private address with the container's
|
||||
* reachable bridge IP — unless {@code awsFaithful} is true (#1983), in which
|
||||
* case the CFN/subnet-allocated private IP set at launch is left untouched.
|
||||
* The container bridge IP is tracked separately (setContainerBridgeIp) and
|
||||
* used for routing/IMDS regardless of this flag.
|
||||
*/
|
||||
static void exposeReachablePrivateAddress(Instance instance, String privateIp, boolean awsFaithful) {
|
||||
if (instance == null || privateIp == null || privateIp.isBlank()) {
|
||||
return;
|
||||
}
|
||||
if (awsFaithful) {
|
||||
return;
|
||||
}
|
||||
|
||||
String privateDnsName = "ip-" + privateIp.replace('.', '-') + ".ec2.internal";
|
||||
instance.setPrivateIpAddress(privateIp);
|
||||
|
||||
@@ -363,6 +363,7 @@ floci:
|
||||
max-published-ports-per-instance: 20
|
||||
socat-image: alpine/socat
|
||||
mock: false
|
||||
aws-faithful-private-ip: false
|
||||
ecs:
|
||||
enabled: true
|
||||
mock: false
|
||||
|
||||
@@ -47,6 +47,7 @@ import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Answers.RETURNS_SELF;
|
||||
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
@@ -87,6 +88,27 @@ class Ec2ContainerManagerTest {
|
||||
assertEquals("ip-192-168-215-21.ec2.internal", networkInterface.getPrivateDnsName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exposeReachablePrivateAddressPreservesAllocatedIpWhenAwsFaithful() {
|
||||
// #1983: with awsFaithfulPrivateIp=true, the CFN/subnet-allocated private
|
||||
// IP set at launch is left untouched — the container bridge IP is not
|
||||
// reported (routing still uses it via containerBridgeIp, tracked elsewhere).
|
||||
Instance instance = new Instance();
|
||||
instance.setPrivateIpAddress("10.82.32.10");
|
||||
instance.setPrivateDnsName("ip-10-82-32-10.ec2.internal");
|
||||
|
||||
InstanceNetworkInterface networkInterface = new InstanceNetworkInterface();
|
||||
networkInterface.setPrivateIpAddress("10.82.32.10");
|
||||
networkInterface.setPrivateDnsName("ip-10-82-32-10.ec2.internal");
|
||||
instance.setNetworkInterfaces(List.of(networkInterface));
|
||||
|
||||
Ec2ContainerManager.exposeReachablePrivateAddress(instance, "192.168.215.21", true);
|
||||
|
||||
assertEquals("10.82.32.10", instance.getPrivateIpAddress());
|
||||
assertEquals("ip-10-82-32-10.ec2.internal", instance.getPrivateDnsName());
|
||||
assertEquals("10.82.32.10", networkInterface.getPrivateIpAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreMetadataRegistrationRegistersRunningPersistedContainer() {
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
@@ -107,7 +129,7 @@ class Ec2ContainerManagerTest {
|
||||
mock(DockerHostResolver.class),
|
||||
dockerClient,
|
||||
mock(PortAllocator.class),
|
||||
mock(EmulatorConfig.class),
|
||||
mock(EmulatorConfig.class, RETURNS_DEEP_STUBS),
|
||||
metadataServer,
|
||||
mock(Ec2PortForwardManager.class));
|
||||
|
||||
|
||||
@@ -216,6 +216,7 @@ floci:
|
||||
max-published-ports-per-instance: 20
|
||||
socat-image: alpine/socat
|
||||
mock: true
|
||||
aws-faithful-private-ip: false
|
||||
ecs:
|
||||
enabled: true
|
||||
mock: true
|
||||
|
||||
Reference in new issue
Block a user