Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(eks): release the API server port when a cluster is deleted or fails to start (#4937)

Niels Kootstra72788dfdb84f
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");