fix(mwaa): let the web proxy use a port range Floci's own container publishes (#8420)

With Floci in a container and the MWAA proxy range published on that
container (`8760-8769` in compose), every CreateEnvironment ended
CREATE_FAILED with "No free port available in range". The allocator
counts every host port any container publishes as taken, Floci's own
included, but the proxy listens inside Floci's container, so a port
Floci's container publishes is its way in, not a conflict.

PortAllocator gains allocateForInProcessListener: inside a container it
decides on the bind probe in Floci's own namespace alone, and outside
one it is allocate. The MWAA proxy allocates its port with it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-15 01:09:59 -04:00
1 parent 00183c0bf1
commit ce25aa0247
5 files changed
+135 -7

No files matched your search

+3
View File
@@ -73,10 +73,13 @@ services:
- /var/run/docker.sock:/var/run/docker.sock
ports:
- "4566:4566"
- "8700-8799:8700-8799"
environment:
FLOCI_SERVICES_MWAA_DOCKER_NETWORK: my_project_default
```
Publishing the proxy range, as above, makes each environment's `WebserverUrl` reachable from the host. Floci still hands out ports from a range its own container publishes, since each proxy listens inside Floci's container, and skips a port in it another container publishes. Floci finds its own container by its hostname, so leave `hostname:` unset or set it to the container's name.
## Configuration
| Variable | Default | Description |
@@ -10,6 +10,7 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
@@ -28,6 +29,7 @@ import java.util.function.IntFunction;
public class PortAllocator {
private static final Logger LOG = Logger.getLogger(PortAllocator.class);
private static final int SHORT_ID_LENGTH = 12;
// Ports reserved by this process but not yet bound by Docker.
// Prevents TOCTOU races when multiple containers are launched concurrently.
@@ -39,14 +41,21 @@ public class PortAllocator {
// toward every existing caller byte-identical. Only the CDI-managed
// instance - built via the @Inject constructor - ever has one.
private final DockerClient dockerClient;
// Nullable for the same reason: without it, allocateForInProcessListener is allocate.
private final CurrentContainerNetworkResolver currentContainer;
public PortAllocator() {
this(null);
this(null, null);
}
public PortAllocator(DockerClient dockerClient) {
this(dockerClient, null);
}
@Inject
public PortAllocator(DockerClient dockerClient) {
public PortAllocator(DockerClient dockerClient, CurrentContainerNetworkResolver currentContainer) {
this.dockerClient = dockerClient;
this.currentContainer = currentContainer;
}
/**
@@ -64,7 +73,25 @@ public class PortAllocator {
// snapshot is only as fresh as the moment allocate() started either way,
// and this loop runs inside a lock every consumer of the shared
// allocator blocks on.
Set<Integer> published = publishedHostPorts();
return allocateFrom(basePort, maxPort, publishedHostPorts(null));
}
/**
* Allocates a port for a listener Floci binds in its own process, such as an MWAA environment's
* web proxy. Inside a container, a host port Docker publishes for Floci's own container is that
* listener's way in, not a port someone else holds, so publishing the range on Floci's container
* must not use it up; a port another container publishes still is, since the host would route it
* there. Floci's container is the one its hostname names, which Docker sets to the container's
* short id unless told otherwise; one it cannot find there has its ports counted as taken.
* Outside a container this is {@link #allocate}, since a port Docker publishes there is held in
* the same namespace the listener binds in.
*/
public synchronized int allocateForInProcessListener(int basePort, int maxPort) {
String ownContainer = currentContainer != null ? currentContainer.resolveContainerId().orElse(null) : null;
return allocateFrom(basePort, maxPort, publishedHostPorts(ownContainer));
}
private int allocateFrom(int basePort, int maxPort, Set<Integer> published) {
for (int port = basePort; port <= maxPort; port++) {
if (available(port, published)) {
reserved.add(port);
@@ -81,7 +108,7 @@ public class PortAllocator {
* as a taken port.
*/
public synchronized OptionalInt allocateIfFree(int port) {
if (!available(port, publishedHostPorts())) {
if (!available(port, publishedHostPorts(null))) {
return OptionalInt.empty();
}
reserved.add(port);
@@ -208,8 +235,11 @@ public class PortAllocator {
* contract, unchanged for every caller that predates this method - or when
* the Docker API call itself fails, matching {@link #isPortFree}'s own
* fail-permissive posture for a probe that could not be completed.
*
* <p>{@code ownContainer}, when not null, is Floci's own container's id or name, whose ports are
* left out.
*/
Set<Integer> publishedHostPorts() {
Set<Integer> publishedHostPorts(String ownContainer) {
if (dockerClient == null) {
return Set.of();
}
@@ -217,6 +247,9 @@ public class PortAllocator {
List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
Set<Integer> published = new HashSet<>();
for (Container c : containers) {
if (ownContainer != null && isContainer(c, ownContainer)) {
continue;
}
ContainerPort[] ports = c.getPorts();
if (ports == null) {
continue;
@@ -234,4 +267,15 @@ public class PortAllocator {
return Set.of();
}
}
// A container's id starts with the short id Docker gives it as its hostname, twelve characters; a name is listed
// with a leading slash.
private static boolean isContainer(Container container, String idOrName) {
String id = container.getId();
if (id != null && idOrName.length() >= SHORT_ID_LENGTH && id.startsWith(idOrName)) {
return true;
}
String[] names = container.getNames();
return names != null && Arrays.asList(names).contains("/" + idOrName);
}
}
@@ -217,7 +217,7 @@ public class MwaaService implements TagHandler {
}
environmentManager.startEnvironment(environment, version, startupScriptContent,
requirements != null ? requirements.getData() : null);
proxyPort = portAllocator.allocate(
proxyPort = portAllocator.allocateForInProcessListener(
config.services().mwaa().proxyBasePort(), config.services().mwaa().proxyMaxPort());
environment.setProxyPort(proxyPort);
environment.setWebserverUrl(buildWebserverUrl(proxyPort));
@@ -7,9 +7,11 @@ import com.github.dockerjava.api.model.ContainerPort;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
@@ -65,6 +67,85 @@ class PortAllocatorDockerAwareTest {
allocator.release(allocated);
}
private static final String FLOCI_ID = "0123456789ab" + "c".repeat(52);
private static Container container(String id, String name, int... publicPorts) {
ContainerPort[] ports = new ContainerPort[publicPorts.length];
for (int i = 0; i < publicPorts.length; i++) {
ports[i] = new ContainerPort().withPublicPort(publicPorts[i]);
}
Container container = mock(Container.class);
when(container.getId()).thenReturn(id);
when(container.getNames()).thenReturn(new String[] {"/" + name});
when(container.getPorts()).thenReturn(ports);
return container;
}
private static DockerClient dockerListing(Container... containers) {
DockerClient dockerClient = mock(DockerClient.class);
ListContainersCmd listCmd = mock(ListContainersCmd.class);
when(dockerClient.listContainersCmd()).thenReturn(listCmd);
when(listCmd.withShowAll(anyBoolean())).thenReturn(listCmd);
when(listCmd.exec()).thenReturn(List.of(containers));
return dockerClient;
}
private static CurrentContainerNetworkResolver flociIn(String hostname) {
CurrentContainerNetworkResolver resolver = mock(CurrentContainerNetworkResolver.class);
when(resolver.resolveContainerId()).thenReturn(Optional.ofNullable(hostname));
return resolver;
}
@Test
void anInProcessListenerInAContainerCanUseARangeFlocisOwnContainerPublishes() {
// Floci's compose file publishes 19900-19902 for its own MWAA web proxies. allocate() reads
// those as taken and runs out; the listener binds inside Floci's container, where they are free.
DockerClient dockerClient = dockerListing(container(FLOCI_ID, "floci", 19900, 19901, 19902));
PortAllocator allocator = new PortAllocator(dockerClient, flociIn("0123456789ab"));
assertThrows(RuntimeException.class, () -> allocator.allocate(19900, 19902));
int port = allocator.allocateForInProcessListener(19900, 19902);
assertEquals(19900, port);
allocator.release(port);
}
@Test
void anInProcessListenerInAContainerSkipsAPortAnotherContainerPublishes() {
// Floci publishes only part of the range and another container a port in it: the host routes
// that port to the other container, so a proxy listening on it in Floci's container is unreachable.
DockerClient dockerClient = dockerListing(container(FLOCI_ID, "floci", 19901),
container("fedcba987654" + "d".repeat(52), "other", 19900));
PortAllocator allocator = new PortAllocator(dockerClient, flociIn("0123456789ab"));
int port = allocator.allocateForInProcessListener(19900, 19901);
assertEquals(19901, port);
allocator.release(port);
}
@Test
void flocisOwnContainerIsAlsoFoundByTheNameItsHostnameGives() {
DockerClient dockerClient = dockerListing(container(FLOCI_ID, "floci", 19900));
PortAllocator allocator = new PortAllocator(dockerClient, flociIn("floci"));
int port = allocator.allocateForInProcessListener(19900, 19900);
assertEquals(19900, port);
allocator.release(port);
}
@Test
void anInProcessListenerOutsideAContainerStillSkipsPublishedPorts() {
DockerClient dockerClient = dockerListing(container(FLOCI_ID, "some-container", 19900));
PortAllocator allocator = new PortAllocator(dockerClient, flociIn(null));
int port = allocator.allocateForInProcessListener(19900, 19999);
assertNotEquals(19900, port);
allocator.release(port);
}
@Test
void allocateStillWorksWithNoDockerClient() {
// The no-arg constructor's own contract, unchanged: every pre-existing
@@ -648,7 +648,7 @@ class MwaaServiceTest {
environmentManager = Mockito.mock(MwaaEnvironmentManager.class);
proxyManager = Mockito.mock(MwaaProxyManager.class);
portAllocator = Mockito.mock(PortAllocator.class);
when(portAllocator.allocate(8700, 8799)).thenReturn(8701);
when(portAllocator.allocateForInProcessListener(8700, 8799)).thenReturn(8701);
realModeService = new MwaaService(storageFactory, config, regionResolver,
environmentManager, proxyManager, portAllocator, s3Service, queuedInstalls::add, Clock.systemUTC());