mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ecs): add opt-in dynamic awsvpc host ports (#6509)
This commit is contained in:
1 parent
f4cd3110e7
commit
c910de57cc
13 files changed
+243
-67
No files matched your search
+31
-3
@@ -1046,9 +1046,31 @@ services:
|
||||
|
||||
### Host access to awsvpc task ports
|
||||
|
||||
By default, Floci preserves the isolation expected from `awsvpc`: native runs use a dynamic Docker host port, while Floci-in-Docker exposes the container port only on the configured Docker network. A process running directly on the Docker host therefore has no stable port for an `awsvpc` task.
|
||||
Native runs publish `awsvpc` task ports on dynamically allocated Docker host ports.
|
||||
When Floci runs in Docker, task ports remain private to the configured Docker network
|
||||
unless `FLOCI_SERVICES_ECS_PUBLISH_AWSVPC_PORTS_TO_HOST=true` is set.
|
||||
|
||||
Set `FLOCI_SERVICES_ECS_PUBLISH_AWSVPC_PORTS_TO_HOST=true` to opt into stable host publishing. Floci binds each `containerPort` to the same host port, or uses an explicit non-zero `hostPort` when one is present. A host-side Terraform provider can then connect to `localhost:<port>`.
|
||||
Enabling host publishing preserves stable ports by default: an explicit non-zero
|
||||
`hostPort` is used, otherwise the host port equals `containerPort`. Existing host-side
|
||||
clients can keep using `localhost:<hostPort>`. Only one task at a time can reserve each
|
||||
stable port on the shared Docker host.
|
||||
|
||||
To run tasks with matching container ports concurrently, including rolling-deployment
|
||||
revisions, also set `FLOCI_SERVICES_ECS_DYNAMIC_AWSVPC_HOST_PORTS=true`
|
||||
(`floci.services.ecs.dynamic-awsvpc-host-ports`, default `false`). Docker then allocates
|
||||
an available host port per task, including tasks declaring an explicit `hostPort`.
|
||||
Discover the actual published address and port in `DescribeTasks`:
|
||||
|
||||
```bash
|
||||
aws ecs describe-tasks --cluster my-cluster --tasks "$TASK_ARN" \
|
||||
--query 'tasks[].containers[].networkBindings[]'
|
||||
```
|
||||
|
||||
Published `awsvpc` ports bind to `127.0.0.1` by default. Set
|
||||
`floci.services.ecs.host-port-bind-address` (`FLOCI_SERVICES_ECS_HOST_PORT_BIND_ADDRESS`)
|
||||
to another Docker-host interface if clients need one. The same stable or dynamic
|
||||
allocation and interface settings apply to security-group network-namespace helpers.
|
||||
These are interfaces on the Docker daemon's host, which may differ from Floci's host.
|
||||
|
||||
```yaml
|
||||
services:
|
||||
@@ -1058,9 +1080,15 @@ services:
|
||||
- /var/run/docker.sock:/var/run/docker.sock
|
||||
environment:
|
||||
FLOCI_SERVICES_ECS_PUBLISH_AWSVPC_PORTS_TO_HOST: "true"
|
||||
FLOCI_SERVICES_ECS_DYNAMIC_AWSVPC_HOST_PORTS: "true"
|
||||
FLOCI_SERVICES_ECS_HOST_PORT_BIND_ADDRESS: "127.0.0.1"
|
||||
```
|
||||
|
||||
This setting is an emulator-specific networking convenience and defaults to `false`. Docker cannot bind the same host port twice, so use it only when at most one running task publishes each port.
|
||||
Host publishing is an emulator convenience and defaults to `false` in Docker mode.
|
||||
With dynamic allocation enabled, host-side clients must use each task's reported host
|
||||
port. Load balancers inside Docker use the container's Docker-network address and
|
||||
container port; native load balancers use the published host port. Bridge/host-mode
|
||||
explicit bindings retain their existing fixed-port behavior.
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
@@ -2344,12 +2344,19 @@ public interface EmulatorConfig {
|
||||
|
||||
/**
|
||||
* Publish {@code awsvpc} task ports on the Docker host so local host processes can
|
||||
* reach them. This is an emulator-only escape hatch and can cause port collisions
|
||||
* when more than one task exposes the same port.
|
||||
* reach them. Uses stable host ports unless {@code dynamic-awsvpc-host-ports} is enabled.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean publishAwsvpcPortsToHost();
|
||||
|
||||
/** Allocate a free host port per awsvpc task when host publishing is enabled. */
|
||||
@WithDefault("false")
|
||||
boolean dynamicAwsvpcHostPorts();
|
||||
|
||||
/** Host interface for published awsvpc task ports, including firewall namespaces. */
|
||||
@WithDefault("127.0.0.1")
|
||||
String hostPortBindAddress();
|
||||
|
||||
Optional<String> dockerNetwork();
|
||||
|
||||
@WithDefault("512")
|
||||
|
||||
+14
-1
@@ -101,6 +101,13 @@ public class SecurityGroupFirewallManager {
|
||||
public Namespace createNamespace(String service, String resourceId, String accountId, String region,
|
||||
Optional<String> dockerNetwork, Map<Integer, Integer> portBindings,
|
||||
Map<String, String> extraLabels) {
|
||||
return createNamespace(service, resourceId, accountId, region, dockerNetwork, portBindings, extraLabels, null);
|
||||
}
|
||||
|
||||
/** Lets the owning service choose the host interface for published namespace ports. */
|
||||
public Namespace createNamespace(String service, String resourceId, String accountId, String region,
|
||||
Optional<String> dockerNetwork, Map<Integer, Integer> portBindings,
|
||||
Map<String, String> extraLabels, String hostIp) {
|
||||
if (!enabled()) {
|
||||
throw new IllegalStateException("Security-group enforcement is disabled");
|
||||
}
|
||||
@@ -128,7 +135,13 @@ public class SecurityGroupFirewallManager {
|
||||
ContainerStorageHelper.OWNER_LABEL, ContainerStorageHelper.ownerIdentity(config)))
|
||||
.withLabels(extraLabels);
|
||||
if (portBindings != null) {
|
||||
portBindings.forEach(builder::withPortBinding);
|
||||
portBindings.forEach((containerPort, hostPort) -> {
|
||||
if (hostIp == null) {
|
||||
builder.withPortBinding(containerPort, hostPort);
|
||||
} else {
|
||||
builder.withPortBinding(containerPort, hostPort, hostIp);
|
||||
}
|
||||
});
|
||||
}
|
||||
ContainerSpec spec = builder.build();
|
||||
String helperId = null;
|
||||
|
||||
@@ -224,7 +224,7 @@ public class EcsLoadBalancerRegistrar {
|
||||
}
|
||||
TargetDescription td = new TargetDescription();
|
||||
td.setId(containerManager.resolveContainerHost(container));
|
||||
td.setPort(binding.hostPort());
|
||||
td.setPort(EcsTaskAddress.isLoopback(td.getId()) ? binding.hostPort() : binding.containerPort());
|
||||
action.accept(lb.getTargetGroupArn(), td);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2135,10 +2135,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
continue;
|
||||
}
|
||||
for (NetworkBinding binding : container.getNetworkBindings()) {
|
||||
addresses.add(new EcsTaskAddress(host, binding.hostPort()));
|
||||
if (!EcsTaskAddress.isLoopback(host)) {
|
||||
addresses.add(new EcsTaskAddress(host, binding.containerPort()));
|
||||
}
|
||||
int port = EcsTaskAddress.isLoopback(host) ? binding.hostPort() : binding.containerPort();
|
||||
addresses.add(new EcsTaskAddress(host, port));
|
||||
}
|
||||
}
|
||||
return addresses;
|
||||
|
||||
+31
-27
@@ -446,30 +446,21 @@ public class EcsContainerManager {
|
||||
EcsLinuxParameters.apply(specBuilder, def,
|
||||
def.getMemory() != null ? def.getMemory() : parseTaskMemoryMb(task.getMemory()));
|
||||
|
||||
// Add port mappings. In bridge/host mode an explicit hostPort is
|
||||
// published to the Docker host literally, matching AWS bridge mode
|
||||
// (mirrors the ECR registry's fixed-port publishing). In awsvpc mode
|
||||
// every AWS task gets its own ENI, so a literal hostPort carries no
|
||||
// host-binding semantics and would collide across tasks on the single
|
||||
// local Docker host (#1778) — awsvpc mappings always get a dynamic
|
||||
// host port in native mode, or expose-only in Docker mode where ECS
|
||||
// consumers reach containers via the docker network IP. The explicit
|
||||
// emulator opt-in below publishes a stable port for host-side clients.
|
||||
// Stable publishing serves existing host clients; dynamic publishing lets
|
||||
// tasks sharing an awsvpc container port coexist on the Docker host.
|
||||
if (protectedNetwork == null && def.getPortMappings() != null) {
|
||||
boolean awsvpc = taskDef.getNetworkMode() == NetworkMode.awsvpc;
|
||||
boolean publishAwsvpcPorts = awsvpc
|
||||
&& config.services().ecs().publishAwsvpcPortsToHost();
|
||||
boolean publishDynamicPortsToHost = !containerDetector.isRunningInContainer();
|
||||
boolean publishAwsvpcPorts = awsvpc && config.services().ecs().publishAwsvpcPortsToHost();
|
||||
boolean publishToHost = !containerDetector.isRunningInContainer()
|
||||
|| publishAwsvpcPorts;
|
||||
for (PortMapping pm : def.getPortMappings()) {
|
||||
if (publishAwsvpcPorts) {
|
||||
int hostPort = pm.hostPort() > 0 ? pm.hostPort() : pm.containerPort();
|
||||
LOG.warnv("Publishing ECS awsvpc container port {0} on host port {1}; "
|
||||
+ "multiple tasks cannot share this host port",
|
||||
pm.containerPort(), hostPort);
|
||||
specBuilder.withPortBinding(pm.containerPort(), hostPort);
|
||||
if (awsvpc && publishToHost) {
|
||||
int hostPort = awsvpcHostPort(pm, publishAwsvpcPorts,
|
||||
config.services().ecs().dynamicAwsvpcHostPorts());
|
||||
specBuilder.withPortBinding(pm.containerPort(), hostPort, hostPortBindAddress());
|
||||
} else if (!awsvpc && pm.hostPort() > 0) {
|
||||
specBuilder.withPortBinding(pm.containerPort(), pm.hostPort());
|
||||
} else if (publishDynamicPortsToHost) {
|
||||
} else if (publishToHost) {
|
||||
specBuilder.withDynamicPort(pm.containerPort());
|
||||
} else {
|
||||
specBuilder.withExposedPort(pm.containerPort());
|
||||
@@ -1342,9 +1333,11 @@ public class EcsContainerManager {
|
||||
Map<Integer, Integer> bindings = namespacePortBindings(
|
||||
definition,
|
||||
containerDetector.isRunningInContainer(),
|
||||
config.services().ecs().publishAwsvpcPortsToHost());
|
||||
config.services().ecs().publishAwsvpcPortsToHost(),
|
||||
config.services().ecs().dynamicAwsvpcHostPorts());
|
||||
namespace = firewallManager.createNamespace("ecs", taskId, regionResolver.getAccountId(),
|
||||
region, config.services().ecs().dockerNetwork(), bindings, Map.of(RUN_LABEL, runId));
|
||||
region, config.services().ecs().dockerNetwork(), bindings, Map.of(RUN_LABEL, runId),
|
||||
hostPortBindAddress());
|
||||
List<String> groupIds = eni.getGroups().stream().map(g -> g.getGroupId()).toList();
|
||||
List<SecurityGroup> groups = ec2Service.describeSecurityGroups(region, groupIds, List.of(), Map.of());
|
||||
if (groups.size() != groupIds.size()) {
|
||||
@@ -1376,8 +1369,14 @@ public class EcsContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
private String hostPortBindAddress() {
|
||||
String address = config.services().ecs().hostPortBindAddress();
|
||||
return address == null || address.isBlank() ? "127.0.0.1" : address;
|
||||
}
|
||||
|
||||
static Map<Integer, Integer> namespacePortBindings(
|
||||
TaskDefinition definition, boolean runningInContainer, boolean publishAwsvpcPortsToHost) {
|
||||
TaskDefinition definition, boolean runningInContainer, boolean publishAwsvpcPortsToHost,
|
||||
boolean dynamicAwsvpcHostPorts) {
|
||||
Map<Integer, Integer> bindings = new LinkedHashMap<>();
|
||||
if (runningInContainer && !publishAwsvpcPortsToHost) {
|
||||
return bindings;
|
||||
@@ -1387,15 +1386,20 @@ public class EcsContainerManager {
|
||||
continue;
|
||||
}
|
||||
for (PortMapping port : container.getPortMappings()) {
|
||||
int hostPort = publishAwsvpcPortsToHost
|
||||
? (port.hostPort() > 0 ? port.hostPort() : port.containerPort())
|
||||
: 0;
|
||||
bindings.put(port.containerPort(), hostPort);
|
||||
bindings.put(port.containerPort(), awsvpcHostPort(port, publishAwsvpcPortsToHost,
|
||||
dynamicAwsvpcHostPorts));
|
||||
}
|
||||
}
|
||||
return bindings;
|
||||
}
|
||||
|
||||
private static int awsvpcHostPort(PortMapping port, boolean publishToHost, boolean dynamicHostPorts) {
|
||||
if (publishToHost && !dynamicHostPorts) {
|
||||
return port.hostPort() > 0 ? port.hostPort() : port.containerPort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private record PreparedNetwork(NetworkInterface eni, SecurityGroupFirewallManager.Namespace namespace) {}
|
||||
|
||||
/**
|
||||
@@ -2204,7 +2208,7 @@ public class EcsContainerManager {
|
||||
int hostPort = pm.containerPort();
|
||||
String bindIp = "0.0.0.0";
|
||||
|
||||
if (!containerDetector.isRunningInContainer() && binding != null && binding.length > 0) {
|
||||
if (binding != null && binding.length > 0) {
|
||||
hostPort = Integer.parseInt(binding[0].getHostPortSpec());
|
||||
if (binding[0].getHostIp() != null && !binding[0].getHostIp().isBlank()) {
|
||||
bindIp = binding[0].getHostIp();
|
||||
|
||||
@@ -733,6 +733,8 @@ floci:
|
||||
enabled: true
|
||||
mock: false
|
||||
publish-awsvpc-ports-to-host: false
|
||||
dynamic-awsvpc-host-ports: false
|
||||
host-port-bind-address: "127.0.0.1"
|
||||
host-volume-roots: []
|
||||
allow-unsafe-host-volumes: false
|
||||
# Remove the ECS containers a previous run of this deployment left on the daemon before
|
||||
|
||||
+3
-1
@@ -55,10 +55,12 @@ class SecurityGroupFirewallManagerTest {
|
||||
embeddedDnsServer);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> manager.createNamespace("ec2", "i-1", "000000000000",
|
||||
"us-east-1", Optional.empty(), Map.of()));
|
||||
"us-east-1", Optional.empty(), Map.of(8080, 0), Map.of(), "127.0.0.1"));
|
||||
|
||||
ArgumentCaptor<ContainerSpec> helper = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
verify(lifecycleManager).createAndStart(helper.capture());
|
||||
assertEquals(Map.of(8080, 0), helper.getValue().portBindings());
|
||||
assertEquals(Map.of(8080, "127.0.0.1"), helper.getValue().portBindingHostIps());
|
||||
assertTrue(helper.getValue().dnsServers().contains("172.18.0.2"), "the helper uses Floci's DNS");
|
||||
assertTrue(helper.getValue().extraHosts().contains("host.docker.internal:host-gateway"),
|
||||
"the helper resolves host.docker.internal");
|
||||
|
||||
+23
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.Container;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel;
|
||||
@@ -25,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* Component test for {@link EcsLoadBalancerRegistrar}: drives the register/deregister
|
||||
@@ -42,6 +44,9 @@ class EcsLoadBalancerRegistrarTest {
|
||||
@InjectSpy
|
||||
ElbV2Service elbV2Service;
|
||||
|
||||
@InjectSpy
|
||||
EcsContainerManager containerManager;
|
||||
|
||||
private String createTargetGroup(String name) {
|
||||
TargetGroup tg = elbV2Service.createTargetGroup(REGION, name, "HTTP", "HTTP1",
|
||||
8080, "vpc-regtest", "ip",
|
||||
@@ -110,6 +115,24 @@ class EcsLoadBalancerRegistrarTest {
|
||||
registrar.deregisterTask(task, svc, REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishedAwsvpcBindingRegistersTheContainerPortAtTheDockerNetworkAddress() {
|
||||
String tgArn = createTargetGroup("reg-tg-published-awsvpc");
|
||||
EcsTask task = taskWithContainer("web", 80, 32781);
|
||||
EcsServiceModel svc = serviceWithLb(tgArn, "web", 80);
|
||||
doReturn("172.18.0.7").when(containerManager).resolveContainerHost(task.getContainers().getFirst());
|
||||
|
||||
registrar.registerTask(task, svc, REGION);
|
||||
|
||||
List<TargetHealth> health = elbV2Service.describeTargetHealth(REGION, tgArn, null);
|
||||
assertEquals(1, health.size());
|
||||
assertEquals("172.18.0.7", health.getFirst().getTarget().getId());
|
||||
assertEquals(80, health.getFirst().getTarget().getPort());
|
||||
|
||||
registrar.deregisterTask(task, svc, REGION);
|
||||
assertTrue(elbV2Service.describeTargetHealth(REGION, tgArn, null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceWithoutLoadBalancersRegistersNothing() {
|
||||
String tgArn = createTargetGroup("reg-tg-2");
|
||||
|
||||
+8
-6
@@ -18,6 +18,8 @@ import io.github.hectorvent.floci.services.ecs.model.LaunchType;
|
||||
import io.github.hectorvent.floci.services.ecs.model.NetworkBinding;
|
||||
import io.github.hectorvent.floci.services.ecs.model.NetworkMode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -237,21 +239,21 @@ class EcsServiceRestartLeftoversTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStartingTaskEvictsTheUnrecordedRegistrationsAtItsAddress() {
|
||||
@ParameterizedTest
|
||||
@CsvSource({"127.0.0.1, 32781", "172.19.0.4, 80"})
|
||||
void aStartingTaskEvictsTheUnrecordedRegistrationsAtItsAddress(String host, int targetPort) {
|
||||
EcsContainerManager containerManager = mock(EcsContainerManager.class);
|
||||
when(containerManager.removeLeftoverContainers()).thenReturn(true);
|
||||
when(containerManager.startTask(any(), any(), any(), anyString())).thenAnswer(invocation -> {
|
||||
EcsTask task = invocation.getArgument(0);
|
||||
Container container = new Container();
|
||||
container.setName("web");
|
||||
container.setNetworkBindings(List.of(new NetworkBinding("0.0.0.0", 80, 80, "tcp")));
|
||||
container.setNetworkBindings(List.of(new NetworkBinding("0.0.0.0", 80, 32781, "tcp")));
|
||||
task.setContainers(List.of(container));
|
||||
task.setPrivateIpAddress("10.0.1.5");
|
||||
return new EcsTaskHandle(task.getTaskArn(), Map.of("web", "docker-id"), Map.of());
|
||||
});
|
||||
// Floci running natively: targets reach the task on loopback, Cloud Map registers its Docker address.
|
||||
when(containerManager.resolveContainerHost(any())).thenReturn("127.0.0.1");
|
||||
when(containerManager.resolveContainerHost(any())).thenReturn(host);
|
||||
when(containerManager.resolvePeerAddress(any())).thenReturn(Optional.of("172.19.0.4"));
|
||||
when(containerManager.taskVpcId(any(), anyString())).thenReturn("vpc-app");
|
||||
EcsLoadBalancerRegistrar lbRegistrar = mock(EcsLoadBalancerRegistrar.class);
|
||||
@@ -279,7 +281,7 @@ class EcsServiceRestartLeftoversTest {
|
||||
assertEquals("RUNNING", task.getLastStatus());
|
||||
// A target only on the ports the container serves, and never at the VPC-scoped ENI address.
|
||||
verify(lbRegistrar).evictUnrecordedTargets(task, Set.of(TARGET_GROUP_ARN),
|
||||
Set.of(new EcsTaskAddress("127.0.0.1", 80)), REGION);
|
||||
Set.of(new EcsTaskAddress(host, targetPort)), REGION);
|
||||
verify(discoveryRegistrar).evictUnrecordedInstances(task, Set.of("srv-alpha"),
|
||||
Set.of(new EcsTaskAddress("10.0.1.5", null, "vpc-app"), new EcsTaskAddress("172.19.0.4", null)),
|
||||
REGION);
|
||||
|
||||
+113
-17
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.ecs.container;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -24,6 +26,8 @@ import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.ssm.SsmService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -105,11 +109,11 @@ class EcsContainerManagerPortMappingsTest {
|
||||
ecrRegistryManager, mock(HostVolumePolicy.class));
|
||||
}
|
||||
|
||||
private void startWith(List<PortMapping> portMappings) {
|
||||
startWith(portMappings, null);
|
||||
private EcsTask startWith(List<PortMapping> portMappings) {
|
||||
return startWith(portMappings, null);
|
||||
}
|
||||
|
||||
private void startWith(List<PortMapping> portMappings, NetworkMode networkMode) {
|
||||
private EcsTask startWith(List<PortMapping> portMappings, NetworkMode networkMode) {
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setName("app");
|
||||
app.setImage("app:latest");
|
||||
@@ -124,6 +128,7 @@ class EcsContainerManagerPortMappingsTest {
|
||||
task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/test-cluster/abc123");
|
||||
|
||||
manager.startTask(task, taskDef, List.of(), "us-east-1");
|
||||
return task;
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -183,7 +188,7 @@ class EcsContainerManagerPortMappingsTest {
|
||||
|
||||
// Regression guard for #1778: awsvpc tasks have per-task ENIs on AWS,
|
||||
// so the literal hostPort must not be claimed on the shared Docker host.
|
||||
verify(builder, times(1)).withDynamicPort(80);
|
||||
verify(builder, times(1)).withPortBinding(80, 0, "127.0.0.1");
|
||||
verify(builder, never()).withPortBinding(eq(80), anyInt());
|
||||
}
|
||||
|
||||
@@ -198,30 +203,65 @@ class EcsContainerManagerPortMappingsTest {
|
||||
verify(builder, never()).withDynamicPort(80);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void awsvpcHostPublishingKeepsTheContainerPortByDefault(boolean runningInContainer) {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(runningInContainer);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
|
||||
startWith(List.of(new PortMapping(80)), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder).withPortBinding(80, 80, "127.0.0.1");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void awsvpcHostPublishingKeepsTheExplicitHostPortByDefault(boolean runningInContainer) {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(runningInContainer);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
|
||||
startWith(List.of(new PortMapping(80, 18080, "tcp")), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder).withPortBinding(80, 18080, "127.0.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsvpcPortCanBePublishedStablyForHostRunners() {
|
||||
void dynamicAllocationAloneDoesNotPublishAwsvpcPortsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().ecs().dynamicAwsvpcHostPorts()).thenReturn(true);
|
||||
|
||||
startWith(List.of(new PortMapping(80)), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder).withExposedPort(80);
|
||||
verify(builder, never()).withPortBinding(eq(80), anyInt(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsvpcPortUsesDynamicLoopbackBindingForHostRunners() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
when(config.services().ecs().dynamicAwsvpcHostPorts()).thenReturn(true);
|
||||
|
||||
startWith(List.of(new PortMapping(15_672)), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder, times(1)).withPortBinding(15_672, 15_672);
|
||||
verify(builder, times(1)).withPortBinding(15_672, 0, "127.0.0.1");
|
||||
verify(builder, never()).withExposedPort(15_672);
|
||||
verify(builder, never()).withDynamicPort(15_672);
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsvpcHostPublishingHonorsAnExplicitHostPort() {
|
||||
void awsvpcHostPublishingDoesNotReserveTheEniPortOnTheSharedHost() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
when(config.services().ecs().dynamicAwsvpcHostPorts()).thenReturn(true);
|
||||
|
||||
startWith(List.of(new PortMapping(8080, 18_080, "tcp")), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder, times(1)).withPortBinding(8080, 18_080);
|
||||
verify(builder, times(1)).withPortBinding(8080, 0, "127.0.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void protectedAwsvpcNamespaceUsesTheStableHostBindings() {
|
||||
void protectedAwsvpcNamespaceUsesDynamicHostBindings() {
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setPortMappings(List.of(
|
||||
new PortMapping(6379),
|
||||
@@ -230,9 +270,9 @@ class EcsContainerManagerPortMappingsTest {
|
||||
taskDefinition.setContainerDefinitions(List.of(app));
|
||||
|
||||
Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
|
||||
taskDefinition, true, true);
|
||||
taskDefinition, true, true, true);
|
||||
|
||||
assertEquals(Map.of(6379, 6379, 8080, 18_080), bindings);
|
||||
assertEquals(Map.of(6379, 0, 8080, 0), bindings);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -243,13 +283,40 @@ class EcsContainerManagerPortMappingsTest {
|
||||
taskDefinition.setContainerDefinitions(List.of(app));
|
||||
|
||||
Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
|
||||
taskDefinition, true, false);
|
||||
taskDefinition, true, false, false);
|
||||
|
||||
assertEquals(Map.of(), bindings);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void protectedAwsvpcNamespaceKeepsStablePublishingByDefault(boolean runningInContainer) {
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setPortMappings(List.of(new PortMapping(6379), new PortMapping(8080, 18080, "tcp")));
|
||||
TaskDefinition taskDefinition = new TaskDefinition();
|
||||
taskDefinition.setContainerDefinitions(List.of(app));
|
||||
|
||||
Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
|
||||
taskDefinition, runningInContainer, true, false);
|
||||
|
||||
assertEquals(Map.of(6379, 6379, 8080, 18080), bindings);
|
||||
}
|
||||
|
||||
@Test
|
||||
void protectedAwsvpcNamespaceUsesStableBindingsInNativeModeWithOptIn() {
|
||||
void protectedAwsvpcNamespaceUsesDynamicBindingsInNativeModeWithoutPublishingOptIn() {
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setPortMappings(List.of(new PortMapping(8080, 18080, "tcp")));
|
||||
TaskDefinition taskDefinition = new TaskDefinition();
|
||||
taskDefinition.setContainerDefinitions(List.of(app));
|
||||
|
||||
Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
|
||||
taskDefinition, false, false, false);
|
||||
|
||||
assertEquals(Map.of(8080, 0), bindings);
|
||||
}
|
||||
|
||||
@Test
|
||||
void protectedAwsvpcNamespaceUsesDynamicBindingsInNativeModeWithOptIn() {
|
||||
ContainerDefinition app = new ContainerDefinition();
|
||||
app.setPortMappings(List.of(
|
||||
new PortMapping(6379),
|
||||
@@ -258,22 +325,51 @@ class EcsContainerManagerPortMappingsTest {
|
||||
taskDefinition.setContainerDefinitions(List.of(app));
|
||||
|
||||
Map<Integer, Integer> bindings = EcsContainerManager.namespacePortBindings(
|
||||
taskDefinition, false, true);
|
||||
taskDefinition, false, true, true);
|
||||
|
||||
assertEquals(Map.of(6379, 6379, 8080, 18_080), bindings);
|
||||
assertEquals(Map.of(6379, 0, 8080, 0), bindings);
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoAwsvpcTasksWithSameHostPortNeverBindLiterally() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
when(config.services().ecs().dynamicAwsvpcHostPorts()).thenReturn(true);
|
||||
|
||||
startWith(List.of(new PortMapping(80, 80, "tcp")), NetworkMode.awsvpc);
|
||||
startWith(List.of(new PortMapping(80, 80, "tcp")), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder, times(2)).withDynamicPort(80);
|
||||
verify(builder, times(2)).withPortBinding(80, 0, "127.0.0.1");
|
||||
verify(builder, never()).withPortBinding(eq(80), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishedPortsReportDockersAllocationEvenWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
when(config.services().ecs().dynamicAwsvpcHostPorts()).thenReturn(true);
|
||||
when(lifecycleManager.getDockerClient().inspectContainerCmd("docker-id").exec()
|
||||
.getNetworkSettings().getPorts().getBindings()).thenReturn(Map.of(
|
||||
ExposedPort.tcp(80), new Ports.Binding[]{new Ports.Binding("127.0.0.1", "32781")}));
|
||||
|
||||
EcsTask task = startWith(List.of(new PortMapping(80, 80, "tcp")), NetworkMode.awsvpc);
|
||||
|
||||
assertEquals(32781, task.getContainers().getFirst().getNetworkBindings().getFirst().hostPort());
|
||||
assertEquals("127.0.0.1", task.getContainers().getFirst().getNetworkBindings().getFirst().bindIP());
|
||||
}
|
||||
|
||||
@Test
|
||||
void awsvpcHostBindingUsesTheConfiguredInterface() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(config.services().ecs().publishAwsvpcPortsToHost()).thenReturn(true);
|
||||
when(config.services().ecs().dynamicAwsvpcHostPorts()).thenReturn(true);
|
||||
when(config.services().ecs().hostPortBindAddress()).thenReturn("192.0.2.10");
|
||||
|
||||
startWith(List.of(new PortMapping(80)), NetworkMode.awsvpc);
|
||||
|
||||
verify(builder).withPortBinding(80, 0, "192.0.2.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
void bridgeModeKeepsLiteralHostPortBinding() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
|
||||
+5
-5
@@ -105,7 +105,7 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
void awsvpcTaskWithFirewallJoinsHelperNamespaceAndSkipsHostPorts() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(firewallManager.enabled()).thenReturn(true);
|
||||
when(firewallManager.createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any()))
|
||||
when(firewallManager.createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new SecurityGroupFirewallManager.Namespace("helper-id", "10.0.0.5"));
|
||||
|
||||
NetworkInterface eni = new NetworkInterface();
|
||||
@@ -149,14 +149,14 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
verify(builder, never()).withNetworkMode(anyString());
|
||||
verify(builder, never()).withLabels(Map.of("io.floci.security-group.workload", "true"));
|
||||
verify(ec2Service, never()).createNetworkInterface(any(), any(), any(), any(), any(), any(), any());
|
||||
verify(builder).withDynamicPort(80);
|
||||
verify(builder).withPortBinding(80, 0, "127.0.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopTaskUnregistersEniAndReleasingTheTaskNetworkDeletesIt() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(firewallManager.enabled()).thenReturn(true);
|
||||
when(firewallManager.createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any()))
|
||||
when(firewallManager.createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new SecurityGroupFirewallManager.Namespace("helper-id", "10.0.0.5"));
|
||||
|
||||
NetworkInterface eni = new NetworkInterface();
|
||||
@@ -192,7 +192,7 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
void firelensRouterAndAppBothJoinTheHelperNamespace() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(firewallManager.enabled()).thenReturn(true);
|
||||
when(firewallManager.createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any()))
|
||||
when(firewallManager.createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new SecurityGroupFirewallManager.Namespace("helper-id", "10.0.0.5"));
|
||||
|
||||
NetworkInterface eni = new NetworkInterface();
|
||||
@@ -240,7 +240,7 @@ class EcsContainerManagerSecurityGroupTest {
|
||||
verify(builder, times(2)).withNetworkMode("container:helper-id");
|
||||
verify(builder, times(2)).withLabels(Map.of("io.floci.security-group.workload", "true"));
|
||||
|
||||
verify(firewallManager).createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any());
|
||||
verify(firewallManager).createNamespace(eq("ecs"), eq("abc123"), any(), any(), any(), any(), any(), any());
|
||||
verify(lifecycleManager).create(any());
|
||||
verify(lifecycleManager).startCreated(eq("router-id"), any());
|
||||
verify(lifecycleManager).createAndStart(any());
|
||||
|
||||
@@ -346,6 +346,7 @@ floci:
|
||||
ecs:
|
||||
enabled: true
|
||||
mock: true
|
||||
dynamic-awsvpc-host-ports: false
|
||||
host-volume-roots: []
|
||||
allow-unsafe-host-volumes: false
|
||||
image-pull-behavior: prefer-cached
|
||||
|
||||
Reference in new issue
Block a user