From 72788dfdb84f63c8a6a5e98635cc4c1e42ea0a77 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:23:35 +0200 Subject: [PATCH] fix(eks): release the API server port when a cluster is deleted or fails to start (#4937) stopCluster never returned the k3s API server port to the PortAllocator, so after about 100 create/delete cycles (the default 6500-6599 range) CreateCluster failed with "No free port" until restart. A start that failed after allocating its port leaked it the same way. Release the port on delete and clear it from the cluster so a delete retried after a failed capacity-backup cleanup cannot free a port another cluster now holds. Release a newly allocated port when the launch fails; a port retained across a capacity restart still belongs to the surviving container and is kept. --- .../floci/services/eks/EksClusterManager.java | 17 +++++++++ .../services/eks/EksClusterManagerTest.java | 38 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index 76a4dca4e3..1cdefaec09 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -402,7 +402,19 @@ public class EksClusterManager config.services().eks().apiServerMaxPort()); cluster.setHostPort(hostPort); + try { + launchCluster(cluster, image, containerName, hostPort); + } catch (RuntimeException e) { + // A retained port still belongs to the surviving container the caller restores. + if (retainedPort == null) { + portAllocator.release(hostPort); + cluster.setHostPort(0); + } + throw e; + } + } + private void launchCluster(Cluster cluster, String image, String containerName, int hostPort) { // Remove any stale container ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName); @@ -822,6 +834,11 @@ public class EksClusterManager return; } lifecycleManager.stopAndRemove(cluster.getContainerId(), logStream); + if (cluster.getHostPort() > 0) { + portAllocator.release(cluster.getHostPort()); + // Cleared so a delete retried after a failed backup cleanup cannot free a reused port. + cluster.setHostPort(0); + } if (cluster.getDockerName() != null) { // A failed backup cleanup must not leave the live node running. Keep the cluster // record so explicit deletion can be retried once Docker accepts the removal. diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java index b74feaaf1a..c5e2183815 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java @@ -517,6 +517,44 @@ class EksClusterManagerTest { .removeIfExistsStrict("floci-aws-eks-capacity-backup.demo"); } + @Test + void deletingAClusterHandsItsApiServerPortBackForTheNextCluster() { + stubFreshStart("cid-1", 6441); + Cluster cluster = cluster(); + manager.startCluster(cluster); + + manager.stopCluster(cluster); + + verify(portAllocator).release(6441); + } + + @Test + void retryingADeleteAfterBackupCleanupFailedHandsThePortBackOnlyOnce() { + Cluster cluster = cluster(); + cluster.setDockerName("floci-eks-demo"); + cluster.setContainerId("cid-1"); + cluster.setHostPort(6441); + Mockito.doThrow(new IllegalStateException("Docker cleanup failed")).doNothing() + .when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo"); + + assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster)); + manager.stopCluster(cluster); + + verify(portAllocator, Mockito.times(1)).release(6441); + } + + @Test + void aClusterWhoseContainerCannotBeCreatedHandsItsPortBackExactlyOnce() { + when(portAllocator.allocate(6440, 6499)).thenReturn(6441); + when(lifecycleManager.create(any())).thenThrow(new IllegalStateException("Docker unreachable")); + Cluster cluster = cluster(); + + assertThrows(IllegalStateException.class, () -> manager.startCluster(cluster)); + manager.stopCluster(cluster); + + verify(portAllocator, Mockito.times(1)).release(6441); + } + @Test void capacityBackupDoesNotCollideWithAnotherClusterName() { when(lifecycleManager.create(any())).thenReturn("cid-new");