mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 22:37:55 -05:00
feat(ecs): publish awsvpc ports on demand (#3766)
This commit is contained in:
1 parent
5d23a70cc0
commit
869589e932
6 files changed
+132
-13
No files matched your search
@@ -1853,6 +1853,14 @@ public interface EmulatorConfig {
|
||||
@WithDefault("false")
|
||||
boolean mock();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean publishAwsvpcPortsToHost();
|
||||
|
||||
Optional<String> dockerNetwork();
|
||||
|
||||
@WithDefault("512")
|
||||
|
||||
+37
-12
@@ -264,14 +264,23 @@ public class EcsContainerManager {
|
||||
// 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.
|
||||
// consumers reach containers via the docker network IP. The explicit
|
||||
// emulator opt-in below publishes a stable port for host-side clients.
|
||||
if (protectedNetwork == null && def.getPortMappings() != null) {
|
||||
boolean awsvpc = taskDef.getNetworkMode() == NetworkMode.awsvpc;
|
||||
boolean publishToHost = !containerDetector.isRunningInContainer();
|
||||
boolean publishAwsvpcPorts = awsvpc
|
||||
&& config.services().ecs().publishAwsvpcPortsToHost();
|
||||
boolean publishDynamicPortsToHost = !containerDetector.isRunningInContainer();
|
||||
for (PortMapping pm : def.getPortMappings()) {
|
||||
if (!awsvpc && pm.hostPort() > 0) {
|
||||
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);
|
||||
} else if (!awsvpc && pm.hostPort() > 0) {
|
||||
specBuilder.withPortBinding(pm.containerPort(), pm.hostPort());
|
||||
} else if (publishToHost) {
|
||||
} else if (publishDynamicPortsToHost) {
|
||||
specBuilder.withDynamicPort(pm.containerPort());
|
||||
} else {
|
||||
specBuilder.withExposedPort(pm.containerPort());
|
||||
@@ -474,14 +483,10 @@ public class EcsContainerManager {
|
||||
String eniId = eni.getNetworkInterfaceId();
|
||||
SecurityGroupFirewallManager.Namespace namespace = null;
|
||||
try {
|
||||
Map<Integer, Integer> bindings = new LinkedHashMap<>();
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
for (ContainerDefinition container : definition.getContainerDefinitions()) {
|
||||
if (container.getPortMappings() != null) {
|
||||
container.getPortMappings().forEach(port -> bindings.put(port.containerPort(), 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<Integer, Integer> bindings = namespacePortBindings(
|
||||
definition,
|
||||
containerDetector.isRunningInContainer(),
|
||||
config.services().ecs().publishAwsvpcPortsToHost());
|
||||
namespace = firewallManager.createNamespace("ecs", taskId, regionResolver.getAccountId(),
|
||||
region, config.services().ecs().dockerNetwork(), bindings);
|
||||
List<String> groupIds = eni.getGroups().stream().map(g -> g.getGroupId()).toList();
|
||||
@@ -514,6 +519,26 @@ public class EcsContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
static Map<Integer, Integer> namespacePortBindings(
|
||||
TaskDefinition definition, boolean runningInContainer, boolean publishAwsvpcPortsToHost) {
|
||||
Map<Integer, Integer> bindings = new LinkedHashMap<>();
|
||||
if (runningInContainer && !publishAwsvpcPortsToHost) {
|
||||
return bindings;
|
||||
}
|
||||
for (ContainerDefinition container : definition.getContainerDefinitions()) {
|
||||
if (container.getPortMappings() == null) {
|
||||
continue;
|
||||
}
|
||||
for (PortMapping port : container.getPortMappings()) {
|
||||
int hostPort = publishAwsvpcPortsToHost
|
||||
? (port.hostPort() > 0 ? port.hostPort() : port.containerPort())
|
||||
: 0;
|
||||
bindings.put(port.containerPort(), hostPort);
|
||||
}
|
||||
}
|
||||
return bindings;
|
||||
}
|
||||
|
||||
private record PreparedNetwork(NetworkInterface eni, SecurityGroupFirewallManager.Namespace namespace) {}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user