Light, fluffy, and always free - The AWS Local Emulator alternative
fix(eks): isolate capacity backups from live clusters (#4663)
Aniket Shuklaef0887783743
diff --git a/docs/services/eks.md b/docs/services/eks.mdindex e1a7332748..d8599f924e 100644--- a/docs/services/eks.md+++ b/docs/services/eks.md@@ -417,8 +417,15 @@ container until the replacement starts and restarts it if replacement fails. Thicontainers started before resource limits were available. If Docker cannot remove the stoppedbackup after a successful replacement, the new node keeps running and cluster deletion retriesthe backup cleanup after stopping the live node. If cleanup still fails, deletion reports the-error and can be retried before the data volume is removed. Backup container names end in-`.capacity-backup`, which cannot match an EKS cluster name.+error and can be retried before the data volume is removed. Backup container names put the+`capacity-backup.` marker before the account-qualified cluster name, for example+`floci-aws-eks-capacity-backup.demo` for the default account and+`floci-aws-eks-capacity-backup.999999999999.demo` for another account. The configured Docker resource+namespace applies to backups too. This keeps backup names outside both accounts' live-container+namespaces without changing existing cluster container names or data volumes.+Cleanup only targets this backup namespace; it does not look up old suffix-form names, which can+identify another account's live cluster. Verify ownership before manually removing leftover stopped+backups after upgrading.#### Cluster node provider ID and topology labelsdiff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.javaindex 7fe1d86473..23a543cc0d 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@@ -711,8 +711,11 @@ public class EksClusterManager}}- private static String capacityBackupName(Cluster cluster) {- return cluster.getDockerName() + ".capacity-backup";+ private String capacityBackupName(Cluster cluster) {+ // The marker must precede the account qualifier: no account ID can be capacity-backup,+ // and no cluster name can contain a dot. A suffix can collide with another account's node.+ return ContainerStorageHelper.resourceName(config, "eks", null,+ "capacity-backup." + accountQualifiedClusterName(cluster));}/**@@ -1072,10 +1075,13 @@ public class EksClusterManager* default-account name.*/private String accountQualifiedName(Cluster cluster) {+ return ContainerStorageHelper.resourceName(config, "eks", null, accountQualifiedClusterName(cluster));+ }++ private String accountQualifiedClusterName(Cluster cluster) {String accountId = cluster.getAccountId();boolean defaultAccount = accountId == null || accountId.equals(config.defaultAccountId());- return ContainerStorageHelper.resourceName(config, "eks", null,- defaultAccount ? cluster.getName() : accountId + "." + cluster.getName());+ return defaultAccount ? cluster.getName() : accountId + "." + cluster.getName();}/**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.javaindex 5ad7b7cf3f..af8387b3db 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@@ -511,7 +511,7 @@ class EksClusterManagerTest {verify(portAllocator, never()).release(6441);verify(dockerClient).stopContainerCmd("cid-old");verify(lifecycleManager, Mockito.times(2))- .removeIfExistsStrict("floci-eks-demo.capacity-backup");+ .removeIfExistsStrict("floci-aws-eks-capacity-backup.demo");}@Test@@ -537,11 +537,81 @@ class EksClusterManagerTest {assertFalse("foo.capacity-backup".matches(EksService.CLUSTER_NAME_REGEX));assertEquals("floci-eks-foo-capacity-backup", manager.clusterResourceName(other));verify(lifecycleManager, Mockito.times(3))- .removeIfExistsStrict("floci-eks-foo.capacity-backup");+ .removeIfExistsStrict("floci-aws-eks-capacity-backup.foo");verify(lifecycleManager, never()).removeIfExistsStrict(other.getDockerName());verify(lifecycleManager, never()).stopAndRemove("cid-other", null);}+ @ParameterizedTest+ @ValueSource(strings = {"floci-eks-", "floci-aws-eks-"})+ void deletingNumericClusterDoesNotRemoveAnotherAccountsCluster(String prefix) {+ when(config.defaultAccountId()).thenReturn("000000000000");+ Cluster cluster = cluster();+ cluster.setAccountId("000000000000");+ cluster.setName("999999999999");+ cluster.setDockerName(prefix + cluster.getName());+ cluster.setContainerId("cid-numeric");+ String otherClusterName = "capacity-backup";+ String otherDockerName = prefix + "999999999999." + otherClusterName;++ assertTrue(cluster.getName().matches(EksService.CLUSTER_NAME_REGEX));+ assertTrue(otherClusterName.matches(EksService.CLUSTER_NAME_REGEX));++ manager.stopCluster(cluster);++ verify(lifecycleManager).stopAndRemove("cid-numeric", null);+ verify(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.999999999999");+ verify(lifecycleManager, never()).removeIfExistsStrict(otherDockerName);+ }++ @ParameterizedTest+ @ValueSource(strings = {"floci-eks-", "floci-aws-eks-"})+ void replacingNumericClusterDoesNotRemoveAnotherAccountsCluster(String prefix) {+ when(config.defaultAccountId()).thenReturn("000000000000");+ stubFreshStart("cid-new", 6441);+ Cluster cluster = cluster();+ cluster.setAccountId("000000000000");+ cluster.setName("999999999999");+ cluster.setDockerName(prefix + cluster.getName());+ cluster.setContainerId("cid-numeric");+ cluster.setHostPort(6441);+ cluster.setNodeInstanceType("t3.medium");+ String otherDockerName = prefix + "999999999999.capacity-backup";++ assertTrue(manager.restartForNodeCapacity(cluster, "m5.large"));++ assertEquals("cid-new", cluster.getContainerId());+ verify(lifecycleManager, Mockito.times(2))+ .removeIfExistsStrict("floci-aws-eks-capacity-backup.999999999999");+ verify(dockerClient.renameContainerCmd("cid-numeric"))+ .withName("floci-aws-eks-capacity-backup.999999999999");+ verify(lifecycleManager, never()).removeIfExistsStrict(otherDockerName);+ verify(dockerClient.renameContainerCmd("cid-numeric"), never()).withName(otherDockerName);+ }++ @ParameterizedTest+ @ValueSource(strings = {"000000000000", "111111111111", "999999999999"})+ void capacityBackupsRespectAccountAndResourceNamespace(String accountId) {+ when(config.defaultAccountId()).thenReturn("000000000000");+ EmulatorConfig.DockerConfig docker = Mockito.mock(EmulatorConfig.DockerConfig.class);+ when(config.docker()).thenReturn(docker);+ when(docker.resourceNamespace()).thenReturn(Optional.of("isolated"));+ Cluster cluster = cluster();+ cluster.setAccountId(accountId);+ cluster.setName("demo");+ cluster.setDockerName(manager.clusterResourceName(cluster));+ cluster.setContainerId("cid-node");+ String qualifiedName = accountId.equals("000000000000")+ ? cluster.getName() : accountId + "." + cluster.getName();++ manager.stopCluster(cluster);++ verify(lifecycleManager).removeIfExistsStrict(+ "floci-aws-isolated-eks-capacity-backup." + qualifiedName);+ verify(lifecycleManager, never()).removeIfExistsStrict(cluster.getDockerName() + ".capacity-backup");+ assertEquals("floci-aws-isolated-eks-" + qualifiedName, cluster.getDockerName());+ }+@Testvoid failedFirstNodeGroupReplacementRestoresPreviousContainerAndType() {when(lifecycleManager.create(any())).thenReturn("cid-new");@@ -574,7 +644,7 @@ class EksClusterManagerTest {.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));stubFreshStart("cid-new", 6440);Mockito.doNothing().doThrow(new IllegalStateException("Docker cleanup failed"))- .when(lifecycleManager).removeIfExistsStrict("floci-eks-demo.capacity-backup");+ .when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo");Cluster cluster = cluster();manager.restoreCluster(cluster);@@ -648,7 +718,7 @@ class EksClusterManagerTest {cluster.setDockerName("floci-eks-demo");cluster.setContainerId("cid-new");Mockito.doThrow(new IllegalStateException("Docker cleanup failed"))- .when(lifecycleManager).removeIfExistsStrict("floci-eks-demo.capacity-backup");+ .when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo");assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));