mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user