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

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