fix(eks): reclaim anonymous volumes during node teardown (#7458)

Use existing strict volume-removing cleanup for EKS node deletion, capacity backups and failed replacements, and request anonymous-volume removal at the first failed-start cleanup, before replacement rollback. Preserve named-volume policy, the original startup failure and failed-removal retry ownership.

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-03 21:33:34 -04:00
1 parent a796e2e58f
commit bc31464ab9
2 files changed
+64 -41

No files matched your search

@@ -794,7 +794,13 @@ public class EksClusterManager
try {
info = lifecycleManager.startCreated(containerId, spec);
} catch (Exception e) {
lifecycleManager.removeIfExists(containerName);
try {
lifecycleManager.removeIfExistsStrict(containerName, true);
} catch (RuntimeException cleanup) {
e.addSuppressed(cleanup);
LOG.warnv("Could not remove failed EKS container {0}; retaining it for cleanup retry: {1}",
containerName, cleanup.getMessage());
}
throw e;
}
@@ -933,7 +939,7 @@ public class EksClusterManager
.map(Container::getLabels).orElse(null));
DockerClient docker = lifecycleManager.getDockerClient();
try {
lifecycleManager.removeIfExistsStrict(backupName);
lifecycleManager.removeIfExistsStrict(backupName, true);
} catch (RuntimeException cleanup) {
LOG.warnv("Could not clear prior EKS capacity backup for cluster {0}: {1}; keeping current node",
cluster.getName(), cleanup.getMessage());
@@ -958,7 +964,7 @@ public class EksClusterManager
return false;
}
try {
lifecycleManager.removeIfExistsStrict(containerName);
lifecycleManager.removeIfExistsStrict(containerName, true);
docker.renameContainerCmd(oldId).withName(containerName).exec();
if (!adoptSurvivingCluster(cluster, oldId, oldCertificateHost)) {
throw new IllegalStateException("Could not adopt previous EKS container " + oldId);
@@ -970,7 +976,7 @@ public class EksClusterManager
return false;
}
try {
lifecycleManager.removeIfExistsStrict(backupName);
lifecycleManager.removeIfExistsStrict(backupName, true);
} catch (RuntimeException cleanup) {
LOG.warnv("EKS cluster {0} replacement is running, but its stopped capacity backup"
+ " could not be removed: {1}; cluster deletion will retry",
@@ -1121,7 +1127,7 @@ public class EksClusterManager
// Strict: a container Docker could not remove may still be running and publishing the
// port, so the delete fails and keeps the cluster record, its metadata endpoint, log
// handle and port reservation; a retried delete releases them once the container is gone.
lifecycleManager.stopAndRemoveStrict(cluster.getContainerId(), logStream);
lifecycleManager.stopAndRemoveStrict(cluster.getContainerId(), logStream, 5, true);
unregisterMetadataEndpoint(cluster);
// Removed by value so an overlapping delete cannot drop a handle it does not own.
if (logStream != null) {
@@ -1140,7 +1146,7 @@ public class EksClusterManager
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.
lifecycleManager.removeIfExistsStrict(capacityBackupName(cluster));
lifecycleManager.removeIfExistsStrict(capacityBackupName(cluster), true);
}
ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, clusterResourceName(cluster));
LOG.infov("Stopped k3s container for cluster {0}", cluster.getName());
@@ -61,6 +61,7 @@ import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mockito;
import java.io.ByteArrayInputStream;
@@ -866,22 +867,37 @@ class EksClusterManagerTest {
verify(dockerClient).stopContainerCmd("cid-old");
}
@Test
void restoresOldSurvivorWhenCapacityReplacementFails() {
@ParameterizedTest
@ValueSource(booleans = {false, true})
void restoresOldSurvivorWhenCapacityReplacementFails(boolean firstCleanupFails) {
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(containerFromJson("{\"Id\":\"cid-old\"}")));
when(lifecycleManager.adopt("cid-old", List.of(6443), Optional.empty()))
.thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440)));
when(lifecycleManager.create(any())).thenReturn("cid-new");
when(lifecycleManager.startCreated(any(), any()))
.thenThrow(new RuntimeException("replacement failed"));
RuntimeException startup = new RuntimeException("replacement failed");
RuntimeException cleanup = new IllegalStateException("temporary removal failure");
when(lifecycleManager.startCreated(any(), any())).thenThrow(startup);
if (firstCleanupFails) {
Mockito.doThrow(cleanup).doNothing().when(lifecycleManager)
.removeIfExistsStrict("floci-eks-demo", true);
}
Cluster cluster = cluster();
manager.restoreCluster(cluster);
assertEquals("cid-old", cluster.getContainerId());
assertEquals(6440, cluster.getHostPort());
verify(lifecycleManager).removeIfExistsStrict("floci-eks-demo");
InOrder order = Mockito.inOrder(lifecycleManager, dockerClient);
order.verify(lifecycleManager).removeIfExists("floci-eks-demo");
order.verify(lifecycleManager).startCreated(any(), any());
order.verify(lifecycleManager, Mockito.times(2)).removeIfExistsStrict("floci-eks-demo", true);
order.verify(dockerClient).renameContainerCmd("cid-old");
verify(lifecycleManager).removeIfExists("floci-eks-demo");
assertEquals(firstCleanupFails ? 1 : 0, startup.getSuppressed().length);
if (firstCleanupFails) {
assertEquals(cleanup, startup.getSuppressed()[0]);
}
verify(dockerClient).stopContainerCmd("cid-old");
verify(portAllocator, never()).allocate(6440, 6499);
}
@@ -906,7 +922,7 @@ class EksClusterManagerTest {
verify(portAllocator, never()).release(6441);
verify(dockerClient).stopContainerCmd("cid-old");
verify(lifecycleManager, Mockito.times(2))
.removeIfExistsStrict("floci-aws-eks-capacity-backup.demo");
.removeIfExistsStrict("floci-aws-eks-capacity-backup.demo", true);
}
@Test
@@ -924,7 +940,7 @@ class EksClusterManagerTest {
void deletingAClusterWhoseContainerDockerCouldNotRemoveKeepsItsPortReserved() {
stubFreshStart("cid-1", 6441);
Mockito.doThrow(new IllegalStateException("Failed to remove container cid-1"))
.when(lifecycleManager).stopAndRemoveStrict("cid-1", null);
.when(lifecycleManager).stopAndRemoveStrict("cid-1", null, 5, true);
Cluster cluster = cluster();
manager.startCluster(cluster);
@@ -937,7 +953,7 @@ class EksClusterManagerTest {
void retryingADeleteAfterContainerRemovalFailedHandsThePortBack() {
stubFreshStart("cid-1", 6441);
Mockito.doThrow(new IllegalStateException("Failed to remove container cid-1")).doNothing()
.when(lifecycleManager).stopAndRemoveStrict("cid-1", null);
.when(lifecycleManager).stopAndRemoveStrict("cid-1", null, 5, true);
Cluster cluster = cluster();
manager.startCluster(cluster);
@@ -954,7 +970,7 @@ class EksClusterManagerTest {
cluster.setContainerId("cid-1");
cluster.setHostPort(6441);
Mockito.doThrow(new IllegalStateException("Docker cleanup failed")).doNothing()
.when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo");
.when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo", true);
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
manager.stopCluster(cluster);
@@ -998,9 +1014,9 @@ 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-aws-eks-capacity-backup.foo");
verify(lifecycleManager, never()).removeIfExistsStrict(other.getDockerName());
verify(lifecycleManager, never()).stopAndRemoveStrict("cid-other", null);
.removeIfExistsStrict("floci-aws-eks-capacity-backup.foo", true);
verify(lifecycleManager, never()).removeIfExistsStrict(other.getDockerName(), true);
verify(lifecycleManager, never()).stopAndRemoveStrict("cid-other", null, 5, true);
}
@ParameterizedTest
@@ -1020,9 +1036,9 @@ class EksClusterManagerTest {
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemoveStrict("cid-numeric", null);
verify(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.999999999999");
verify(lifecycleManager, never()).removeIfExistsStrict(otherDockerName);
verify(lifecycleManager).stopAndRemoveStrict("cid-numeric", null, 5, true);
verify(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.999999999999", true);
verify(lifecycleManager, never()).removeIfExistsStrict(otherDockerName, true);
}
@ParameterizedTest
@@ -1043,10 +1059,10 @@ class EksClusterManagerTest {
assertEquals("cid-new", cluster.getContainerId());
verify(lifecycleManager, Mockito.times(2))
.removeIfExistsStrict("floci-aws-eks-capacity-backup.999999999999");
.removeIfExistsStrict("floci-aws-eks-capacity-backup.999999999999", true);
verify(dockerClient.renameContainerCmd("cid-numeric"))
.withName("floci-aws-eks-capacity-backup.999999999999");
verify(lifecycleManager, never()).removeIfExistsStrict(otherDockerName);
verify(lifecycleManager, never()).removeIfExistsStrict(otherDockerName, true);
verify(dockerClient.renameContainerCmd("cid-numeric"), never()).withName(otherDockerName);
}
@@ -1068,8 +1084,8 @@ class EksClusterManagerTest {
manager.stopCluster(cluster);
verify(lifecycleManager).removeIfExistsStrict(
"floci-aws-isolated-eks-capacity-backup." + qualifiedName);
verify(lifecycleManager, never()).removeIfExistsStrict(cluster.getDockerName() + ".capacity-backup");
"floci-aws-isolated-eks-capacity-backup." + qualifiedName, true);
verify(lifecycleManager, never()).removeIfExistsStrict(cluster.getDockerName() + ".capacity-backup", true);
assertEquals("floci-aws-isolated-eks-" + qualifiedName, cluster.getDockerName());
}
@@ -1105,14 +1121,14 @@ 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-aws-eks-capacity-backup.demo");
.when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo", true);
Cluster cluster = cluster();
manager.restoreCluster(cluster);
assertEquals("cid-new", cluster.getContainerId());
verify(lifecycleManager, Mockito.times(1)).adopt("cid-old", List.of(6443), Optional.empty());
verify(lifecycleManager, never()).removeIfExistsStrict("floci-eks-demo");
verify(lifecycleManager, never()).removeIfExistsStrict("floci-eks-demo", true);
}
@Test
@@ -1168,7 +1184,7 @@ class EksClusterManagerTest {
cluster.setContainerId("cid-1");
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemoveStrict("cid-1", null);
verify(lifecycleManager).stopAndRemoveStrict("cid-1", null, 5, true);
// The volume must survive so restoreCluster can bring the workloads back.
verify(lifecycleManager, never()).removeVolume(anyString());
}
@@ -1179,11 +1195,11 @@ class EksClusterManagerTest {
cluster.setDockerName("floci-eks-demo");
cluster.setContainerId("cid-new");
Mockito.doThrow(new IllegalStateException("Docker cleanup failed"))
.when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo");
.when(lifecycleManager).removeIfExistsStrict("floci-aws-eks-capacity-backup.demo", true);
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
verify(lifecycleManager).stopAndRemoveStrict("cid-new", null);
verify(lifecycleManager).stopAndRemoveStrict("cid-new", null, 5, true);
verify(lifecycleManager, never()).removeVolume(anyString());
}
@@ -1195,7 +1211,7 @@ class EksClusterManagerTest {
cluster.setContainerId("cid-1");
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemoveStrict("cid-1", null);
verify(lifecycleManager).stopAndRemoveStrict("cid-1", null, 5, true);
verify(lifecycleManager).removeVolume("floci-aws-eks-demo");
}
@@ -2105,7 +2121,7 @@ class EksClusterManagerTest {
manager.configureLinkLocalMetadataEndpoint(cluster, "container-42");
cluster.setContainerId("container-42");
Mockito.doThrow(new IllegalStateException("Failed to remove container container-42"))
.when(lifecycleManager).stopAndRemoveStrict("container-42", null);
.when(lifecycleManager).stopAndRemoveStrict("container-42", null, 5, true);
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
@@ -2124,7 +2140,7 @@ class EksClusterManagerTest {
cluster.setContainerId("container-42");
Mockito.doThrow(new IllegalStateException("Failed to remove container container-42"))
.doNothing()
.when(lifecycleManager).stopAndRemoveStrict("container-42", null);
.when(lifecycleManager).stopAndRemoveStrict("container-42", null, 5, true);
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
manager.stopCluster(cluster);
@@ -3434,7 +3450,8 @@ class EksClusterManagerTest {
cluster.setVersion("1.30");
assertThrows(RuntimeException.class, () -> manager.startCluster(cluster));
verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-aws-eks-fail-cluster");
verify(lifecycleManager).removeIfExists("floci-aws-eks-fail-cluster");
verify(lifecycleManager).removeIfExistsStrict("floci-aws-eks-fail-cluster", true);
}
}
@@ -3717,7 +3734,7 @@ class EksClusterManagerTest {
cluster.setLogging(new Logging(List.of(new LogSetup(List.of("api"), true))));
manager.startCluster(cluster);
Mockito.doThrow(new IllegalStateException("Failed to remove container"))
.when(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle);
.when(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle, 5, true);
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
@@ -3751,7 +3768,7 @@ class EksClusterManagerTest {
manager.startCluster(cluster);
Mockito.doThrow(new IllegalStateException("Failed to remove container"))
.doNothing()
.when(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle);
.when(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle, 5, true);
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
manager.stopCluster(cluster);
@@ -3769,7 +3786,7 @@ class EksClusterManagerTest {
Mockito.doAnswer(failing -> {
manager.stopCluster(cluster);
throw new IllegalStateException("Failed to remove container");
}).doNothing().when(lifecycleManager).stopAndRemoveStrict(eq(containerId), any());
}).doNothing().when(lifecycleManager).stopAndRemoveStrict(eq(containerId), any(), eq(5), eq(true));
assertThrows(IllegalStateException.class, () -> manager.stopCluster(cluster));
@@ -3787,7 +3804,7 @@ class EksClusterManagerTest {
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle);
verify(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle, 5, true);
assertNull(manager.getLogHandle(cluster));
}
@@ -3807,7 +3824,7 @@ class EksClusterManagerTest {
manager.startCluster(cluster);
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle);
verify(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockHandle, 5, true);
assertNull(manager.getLogHandle(cluster));
}
@@ -3821,7 +3838,7 @@ class EksClusterManagerTest {
manager.detachCluster(cluster);
verify(mockHandle).close();
verify(lifecycleManager, never()).stopAndRemoveStrict(anyString(), any());
verify(lifecycleManager, never()).stopAndRemoveStrict(anyString(), any(), eq(5), eq(true));
assertNull(manager.getLogHandle(cluster));
}
@@ -3992,7 +4009,7 @@ class EksClusterManagerTest {
"us-east-1", "eks-audit:audit-cluster", false);
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockAuditHandle);
verify(lifecycleManager).stopAndRemoveStrict("container-id-123456789012345678901234567890", mockAuditHandle, 5, true);
assertNull(manager.getLogHandle(cluster));
}