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.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-10-02 15:23:35 -04:00
1 parent 3597afda07
commit 72788dfdb8
2 files changed
+55

No files matched your search

@@ -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.
@@ -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");