mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
00183c0bf1
commit
ce25aa0247
5 files changed
+135
-7
No files matched your search
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user