fix(eks): keep a cluster's API server port when its container is recreated (#8343)

* fix(eks): keep a cluster's API server port when its container is recreated
* fix(eks): leave a network-mode endpoint's port out of the restored port
* fix(eks): free an unadoptable survivor's port first, and tell a taken port from an error
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-14 14:56:51 -04:00
1 parent c0747f9c07
commit acd884a739
5 files changed
+220 -9

No files matched your search

@@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.OptionalInt;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.function.IntFunction;
@@ -65,7 +66,7 @@ public class PortAllocator {
// allocator blocks on.
Set<Integer> published = publishedHostPorts();
for (int port = basePort; port <= maxPort; port++) {
if (!reserved.contains(port) && !published.contains(port) && isPortFree(port)) {
if (available(port, published)) {
reserved.add(port);
LOG.debugv("Allocated port {0} from range {1}-{2}", String.valueOf(port), String.valueOf(basePort), String.valueOf(maxPort));
return port;
@@ -74,6 +75,24 @@ public class PortAllocator {
throw new RuntimeException("No free port available in range " + basePort + "-" + maxPort);
}
/**
* Allocates {@code port} itself when {@link #allocate} would hand it out, or answers empty when it is reserved,
* published by a container or bound. A failure reading Docker's published ports propagates rather than reading
* as a taken port.
*/
public synchronized OptionalInt allocateIfFree(int port) {
if (!available(port, publishedHostPorts())) {
return OptionalInt.empty();
}
reserved.add(port);
LOG.debugv("Allocated port {0}", String.valueOf(port));
return OptionalInt.of(port);
}
private boolean available(int port, Set<Integer> published) {
return !reserved.contains(port) && !published.contains(port) && isPortFree(port);
}
/**
* Marks a port as reserved without probing whether it is free. Used on restart to
* re-reserve host ports already held by surviving containers (e.g. persisted EC2
@@ -93,6 +93,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -673,9 +674,7 @@ public class EksClusterManager
cluster.getName(), image);
// Allocate host port for the k3s API server
int hostPort = retainedPort != null ? retainedPort : portAllocator.allocate(
config.services().eks().apiServerBasePort(),
config.services().eks().apiServerMaxPort());
int hostPort = retainedPort != null ? retainedPort : allocateApiServerPort(cluster);
cluster.setHostPort(hostPort);
try {
@@ -690,6 +689,41 @@ public class EksClusterManager
}
}
/**
* Allocates the k3s API server's host port. A cluster recreated on restore asks first for the
* port its persisted endpoint names, since an EKS endpoint never changes and every kubeconfig
* written for the cluster names that port. Only when that port is taken does the cluster get the
* first free one, which changes its endpoint, and a warning says so. A port outside the
* configured range is not asked for, and neither is the port of a network-mode endpoint: that
* endpoint names the container's own API server port, whatever host port the container gets.
*/
private int allocateApiServerPort(Cluster cluster) {
int basePort = config.services().eks().apiServerBasePort();
int maxPort = config.services().eks().apiServerMaxPort();
int previousPort = networkEndpointMode() ? -1 : endpointPort(cluster.getEndpoint());
if (previousPort >= basePort && previousPort <= maxPort) {
OptionalInt kept = portAllocator.allocateIfFree(previousPort);
if (kept.isPresent()) {
return kept.getAsInt();
}
LOG.warnv("API server port {0} of EKS cluster {1} is taken; moving the cluster to another port "
+ "changes its endpoint", String.valueOf(previousPort), cluster.getName());
}
return portAllocator.allocate(basePort, maxPort);
}
/** The port an endpoint URL names, or -1 when it names none. */
private static int endpointPort(String endpoint) {
if (endpoint == null) {
return -1;
}
try {
return URI.create(endpoint).getPort();
} catch (IllegalArgumentException e) {
return -1;
}
}
private void launchCluster(Cluster cluster, String image, String containerName, int hostPort) {
// Remove any stale container
ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName);
@@ -986,7 +1020,8 @@ public class EksClusterManager
* keeping its published API server port and data volume, so the cluster's workloads come back
* as they were. A survivor whose node capacity changed, or that is not on the Docker network
* Floci now uses, is replaced on the same volume. When the container is gone, the cluster is
* recreated via {@link #startCluster}; the named k3s data volume is reused if it survived.
* recreated via {@link #startCluster}, on the API server port its endpoint names if that port is
* still free; the named k3s data volume is reused if it survived.
* Callers should put the cluster back into CREATING so the readiness poller re-verifies the API
* server and re-extracts the certificate authority before marking it ACTIVE again.
*/
@@ -1024,6 +1059,9 @@ public class EksClusterManager
LOG.infov("Adopting pre-upgrade EKS cluster {0} with local-storage enabled", cluster.getName());
}
if (!adoptSurvivingCluster(cluster, existing.get().getId(), certificateHost(existingLabels))) {
// The survivor is replaced, and it may still publish the port the endpoint names: it goes before a
// port is chosen, so the new container can take that port and the endpoint stays.
lifecycleManager.removeIfExistsStrict(containerName);
startCluster(cluster);
return;
}
@@ -1856,13 +1894,18 @@ public class EksClusterManager
* {@link EksStartupValidator} has already refused an invalid value at boot.
*/
private String endpointHost() {
if (containerDetector.isRunningInContainer()
&& ENDPOINT_MODE_NETWORK.equalsIgnoreCase(config.services().eks().endpointMode())) {
if (networkEndpointMode()) {
return "localhost";
}
return endpointHostFrom(config.services().eks().endpointHost());
}
/** Whether endpoints name the k3s container rather than a host port: network mode, with Floci in a container. */
private boolean networkEndpointMode() {
return containerDetector.isRunningInContainer()
&& ENDPOINT_MODE_NETWORK.equalsIgnoreCase(config.services().eks().endpointMode());
}
/**
* The configured endpoint host as a bare hostname or IP address, the form a {@code --tls-san} takes.
* A bracketed IPv6 literal is unbracketed; a scheme, port or path is refused, since the endpoint