fix(eks): implement cluster restoration after restart to maintain state (#2780)

Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
Matheus André authored and GitHub committed 2026-08-30 14:44:05 -05:00
1 parent cfe8566511
commit eaecb36425
6 files changed
+763 -23

No files matched your search

@@ -12,8 +12,13 @@ import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
import io.github.hectorvent.floci.core.common.docker.PortAllocator;
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
import io.github.hectorvent.floci.services.eks.model.Cluster;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
import com.github.dockerjava.api.command.InspectVolumeCmd;
import com.github.dockerjava.api.model.Container;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@@ -207,6 +212,293 @@ class EksClusterManagerTest {
"io.floci.region", "us-east-1"));
}
/** Re-latching persisted clusters after a Floci/Docker restart (#2609), without a Docker daemon. */
@Nested
class RestoreCluster {
private EmulatorConfig config;
private EmulatorConfig.StorageConfig storage;
private ContainerLifecycleManager lifecycleManager;
private PortAllocator portAllocator;
private EksClusterManager manager;
@BeforeEach
void setUp() {
config = Mockito.mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = Mockito.mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.EksServiceConfig eks = Mockito.mock(EmulatorConfig.EksServiceConfig.class);
storage = Mockito.mock(EmulatorConfig.StorageConfig.class);
when(config.services()).thenReturn(services);
when(config.storage()).thenReturn(storage);
when(services.eks()).thenReturn(eks);
when(eks.defaultImage()).thenReturn("rancher/k3s:v1.30.0-k3s1");
when(eks.apiServerBasePort()).thenReturn(6440);
when(eks.apiServerMaxPort()).thenReturn(6499);
when(eks.dockerNetwork()).thenReturn(Optional.empty());
when(eks.endpointMode()).thenReturn("host");
when(eks.disableCni()).thenReturn(false);
when(eks.iamAuthWebhook()).thenReturn(false);
when(eks.ecrRegistryMirror()).thenReturn(false);
lifecycleManager = Mockito.mock(ContainerLifecycleManager.class);
ContainerBuilder containerBuilder = Mockito.mock(ContainerBuilder.class);
ContainerBuilder.Builder builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF);
when(containerBuilder.newContainer(anyString())).thenReturn(builder);
when(builder.build()).thenReturn(Mockito.mock(ContainerSpec.class));
portAllocator = Mockito.mock(PortAllocator.class);
manager = new EksClusterManager(containerBuilder, lifecycleManager,
Mockito.mock(ContainerDetector.class), portAllocator,
Mockito.mock(DockerHostResolver.class), Mockito.mock(EcrRegistryManager.class),
config, Mockito.mock(RegionResolver.class));
}
// Container's getters are final, so survivors are built from JSON instead of mocked.
private Container survivingContainer(String id) {
return containerFromJson("{\"Id\":\"" + id + "\"}");
}
private Container survivingContainerOwnedBy(String id, String accountId) {
return containerFromJson("{\"Id\":\"" + id + "\","
+ "\"Labels\":{\"io.floci.account\":\"" + accountId + "\"}}");
}
private Container containerFromJson(String json) {
try {
return new ObjectMapper().readValue(json, Container.class);
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
}
private Cluster cluster() {
Cluster cluster = new Cluster();
cluster.setName("demo");
return cluster;
}
private void stubFreshStart(String containerId, int allocatedPort) {
when(lifecycleManager.create(any())).thenReturn(containerId);
when(lifecycleManager.startCreated(any(), any()))
.thenReturn(new ContainerInfo(containerId, Map.of()));
when(portAllocator.allocate(6440, 6499)).thenReturn(allocatedPort);
}
@Test
void adoptsASurvivingContainerAndKeepsItsPublishedPort() {
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(survivingContainer("cid-1")));
// adopt() starts a stopped container — the Docker-reboot case from #2609.
when(lifecycleManager.adopt("cid-1", List.of(6443)))
.thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6512)));
Cluster cluster = cluster();
manager.restoreCluster(cluster);
assertEquals("cid-1", cluster.getContainerId());
assertEquals(6512, cluster.getHostPort());
assertEquals("https://localhost:6512", cluster.getEndpoint());
assertEquals("https://localhost:6512", cluster.getInternalEndpoint());
// The port Docker already holds must not be handed out to another cluster.
verify(portAllocator).markReserved(6512);
verify(lifecycleManager, never()).create(any());
}
@Test
void recreatesTheContainerWhenNoneSurvives() {
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty());
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
manager.restoreCluster(cluster);
assertEquals("cid-new", cluster.getContainerId());
assertEquals(6440, cluster.getHostPort());
verify(lifecycleManager).create(any());
verify(lifecycleManager, never()).adopt(anyString(), any());
}
@Test
void recreatesWhenTheSurvivingContainerPublishesNoPort() {
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(survivingContainer("cid-1")));
when(lifecycleManager.adopt("cid-1", List.of(6443)))
.thenReturn(new ContainerInfo("cid-1", Map.of()));
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
manager.restoreCluster(cluster);
assertEquals("cid-new", cluster.getContainerId());
verify(portAllocator, never()).markReserved(6440);
}
@Test
void recreatesWhenAdoptionFails() {
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(survivingContainer("cid-1")));
when(lifecycleManager.adopt(anyString(), any())).thenThrow(new RuntimeException("broken"));
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
manager.restoreCluster(cluster);
assertEquals("cid-new", cluster.getContainerId());
// startCluster removes the broken survivor before reusing its name.
verify(lifecycleManager).removeIfExists("floci-eks-demo");
}
@Test
void stopClusterRetainsTheDataVolumeInPersistentStorageMode() {
when(storage.mode()).thenReturn("hybrid");
when(storage.pruneVolumesOnDelete()).thenReturn(false);
Cluster cluster = cluster();
cluster.setContainerId("cid-1");
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemove("cid-1", null);
// The volume must survive so restoreCluster can bring the workloads back.
verify(lifecycleManager, never()).removeVolume(anyString());
}
@Test
void stopClusterRemovesTheDataVolumeInMemoryStorageMode() {
when(storage.mode()).thenReturn("memory");
Cluster cluster = cluster();
cluster.setContainerId("cid-1");
manager.stopCluster(cluster);
verify(lifecycleManager).stopAndRemove("cid-1", null);
verify(lifecycleManager).removeVolume("floci-eks-demo");
}
@Test
void nonDefaultAccountClustersGetAccountQualifiedDockerNames() {
// Cluster names are unique only per account: an unqualified name would cross-bind two
// accounts' same-named clusters to one container and data volume.
when(config.defaultAccountId()).thenReturn("000000000000");
when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.empty());
when(lifecycleManager.findByName("floci-eks-999999999999.demo"))
.thenReturn(Optional.of(survivingContainer("cid-9")));
when(lifecycleManager.adopt("cid-9", List.of(6443)))
.thenReturn(new ContainerInfo("cid-9", Map.of(), Map.of(6443, 6520)));
Cluster cluster = cluster();
cluster.setAccountId("999999999999");
manager.restoreCluster(cluster);
assertEquals("cid-9", cluster.getContainerId());
assertEquals("floci-eks-999999999999.demo", cluster.getDockerName());
}
@Test
void defaultAccountClustersKeepTheHistoricalUnqualifiedName() {
when(config.defaultAccountId()).thenReturn("000000000000");
Cluster cluster = cluster();
cluster.setAccountId("000000000000");
assertEquals("floci-eks-demo", manager.clusterResourceName(cluster));
}
@Test
void legacyNamedContainerIsKeptWhenItsAccountLabelMatchesTheOwner() {
// A non-default-account cluster created before account-qualified naming left its
// container (and mounted data volume) under floci-eks-<name>. Restoration must keep
// that name — recreating under the qualified name would orphan the workloads.
when(config.defaultAccountId()).thenReturn("000000000000");
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(survivingContainerOwnedBy("cid-legacy", "999999999999")));
when(lifecycleManager.adopt("cid-legacy", List.of(6443)))
.thenReturn(new ContainerInfo("cid-legacy", Map.of(), Map.of(6443, 6512)));
Cluster cluster = cluster();
cluster.setAccountId("999999999999");
manager.restoreCluster(cluster);
assertEquals("cid-legacy", cluster.getContainerId());
assertEquals("floci-eks-demo", cluster.getDockerName());
verify(lifecycleManager, never()).create(any());
}
@Test
void legacyNamedContainerOfAnotherAccountIsNeverClaimed() {
// The legacy container belongs to whoever's label it carries. A different account's
// restored cluster must start fresh under its qualified name, leaving the survivor
// (and its data) untouched.
when(config.defaultAccountId()).thenReturn("000000000000");
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(survivingContainerOwnedBy("cid-other", "111111111111")));
when(lifecycleManager.findByName("floci-eks-999999999999.demo")).thenReturn(Optional.empty());
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
cluster.setAccountId("999999999999");
manager.restoreCluster(cluster);
assertEquals("floci-eks-999999999999.demo", cluster.getDockerName());
assertEquals("cid-new", cluster.getContainerId());
verify(lifecycleManager, never()).adopt(anyString(), any());
verify(lifecycleManager, never()).removeIfExists("floci-eks-demo");
}
@Test
void legacyNamedContainerWithoutAnOwnerLabelIsNeverClaimed() {
// No label means no verifiable owner — adopting on a guess could hand another
// account's workloads over. The cluster starts fresh under its qualified name and
// the unclaimed survivor is only reported.
when(config.defaultAccountId()).thenReturn("000000000000");
when(lifecycleManager.findByName("floci-eks-demo"))
.thenReturn(Optional.of(survivingContainer("cid-unlabeled")));
when(lifecycleManager.findByName("floci-eks-999999999999.demo")).thenReturn(Optional.empty());
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
cluster.setAccountId("999999999999");
manager.restoreCluster(cluster);
assertEquals("floci-eks-999999999999.demo", cluster.getDockerName());
verify(lifecycleManager, never()).adopt(anyString(), any());
verify(lifecycleManager, never()).removeIfExists("floci-eks-demo");
}
@Test
void legacyVolumeWithoutItsContainerIsNeverClaimed() {
// A surviving volume carries no ownership label at all, so it cannot be verified for
// any account — the cluster starts fresh under its qualified name and the volume is
// reported for manual migration instead of being silently mounted or orphaned.
when(config.defaultAccountId()).thenReturn("000000000000");
DockerClient dockerClient = Mockito.mock(DockerClient.class);
InspectVolumeCmd inspectVolumeCmd = Mockito.mock(InspectVolumeCmd.class);
when(lifecycleManager.getDockerClient()).thenReturn(dockerClient);
when(dockerClient.inspectVolumeCmd("floci-eks-demo")).thenReturn(inspectVolumeCmd);
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
cluster.setAccountId("999999999999");
manager.restoreCluster(cluster);
assertEquals("floci-eks-999999999999.demo", cluster.getDockerName());
assertEquals("cid-new", cluster.getContainerId());
verify(inspectVolumeCmd).exec();
}
@Test
void startClusterAssignsTheAccountQualifiedDockerName() {
when(config.defaultAccountId()).thenReturn("000000000000");
stubFreshStart("cid-new", 6440);
Cluster cluster = cluster();
cluster.setAccountId("999999999999");
manager.startCluster(cluster);
assertEquals("floci-eks-999999999999.demo", cluster.getDockerName());
verify(lifecycleManager).removeIfExists("floci-eks-999999999999.demo");
}
}
/** Mirror-injection guard behavior, without a Docker daemon. */
@Nested
class InjectEcrRegistryMirror {
@@ -41,7 +41,11 @@ import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class EksServiceTest {
@@ -67,9 +71,14 @@ class EksServiceTest {
}
private EmulatorConfig testConfig() {
return testConfig(true);
}
private EmulatorConfig testConfig(boolean mock) {
EmulatorConfig.EksServiceConfig eksConfig = proxy(EmulatorConfig.EksServiceConfig.class,
(proxy, method, args) -> switch (method.getName()) {
case "enabled", "mock" -> true;
case "enabled" -> true;
case "mock" -> mock;
case "apiServerBasePort" -> 6500;
default -> defaultValue(method);
});
@@ -256,7 +265,9 @@ class EksServiceTest {
void initBackfillsUnderTheOwningAccountNotTheDefault() {
// Startup has no request context, so the account-scoped put() resolves to the default
// account. A cluster owned by another account must still be migrated in place, or the owner
// keeps an issuer-less record while a duplicate appears under the default account.
// keeps an issuer-less record while a duplicate appears under the default account. The
// record itself carries no accountId (it is @JsonIgnore, dropped on reload) — the owner
// can only come from the storage key.
String otherAccount = "999999999999";
StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();
StorageBackend<String, ClusterOidcKey> rawKeys = new InMemoryStorage<>();
@@ -265,7 +276,6 @@ class EksServiceTest {
Cluster legacy = new Cluster();
legacy.setName("legacy-cluster");
legacy.setAccountId(otherAccount);
legacy.setStatus(ClusterStatus.ACTIVE);
clusterStore.putForAccount(otherAccount, "legacy-cluster", legacy);
@@ -277,6 +287,8 @@ class EksServiceTest {
Cluster migrated = clusterStore.getForAccount(otherAccount, "legacy-cluster").orElseThrow();
String issuer = migrated.getIdentity().getOidc().getIssuer();
assertNotNull(issuer);
// The owner rehydrated from the storage key sticks to the record for later puts.
assertEquals(otherAccount, migrated.getAccountId());
// No duplicate stranded under the default account.
assertTrue(clusterStore.getForAccount("000000000000", "legacy-cluster").isEmpty());
// The signing key is stored under the owner too, and is still resolvable by issuer.
@@ -284,6 +296,138 @@ class EksServiceTest {
assertTrue(oidcService.findVerificationKey(issuer).isPresent());
}
@Test
void initRestoresPersistedClustersAfterARestart() {
// A cluster restored from eks-clusters.json after a Floci/Docker restart (#2609) has no
// container attached — init must re-latch it and hand it back to the readiness poller.
StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();
var clusterStore = new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");
Cluster persisted = new Cluster();
persisted.setName("persisted-cluster");
persisted.setStatus(ClusterStatus.ACTIVE);
clusterStore.putForAccount("000000000000", "persisted-cluster", persisted);
Cluster failed = new Cluster();
failed.setName("failed-cluster");
failed.setStatus(ClusterStatus.FAILED);
clusterStore.putForAccount("000000000000", "failed-cluster", failed);
EksClusterManager clusterManager = mock(EksClusterManager.class);
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
try {
restarted.init();
verify(clusterManager).restoreCluster(persisted);
// CREATING hands the cluster to the readiness poller, which re-extracts the CA and
// flips it back to ACTIVE once the API server answers.
assertEquals(ClusterStatus.CREATING,
restarted.describeCluster("persisted-cluster").getStatus());
// A FAILED record has nothing to re-latch.
verify(clusterManager, never()).restoreCluster(failed);
assertEquals(ClusterStatus.FAILED,
restarted.describeCluster("failed-cluster").getStatus());
} finally {
restarted.shutdown();
}
}
@Test
void initRestoresUnderTheOwningAccountNotTheDefault() {
// Cluster.accountId is @JsonIgnore: a record reloaded from eks-clusters.json carries no
// account — only its storage key does. Restoration must derive the owner from the key,
// or the restored state lands under the default account while the owner keeps a stale
// record with no restored runtime fields.
String otherAccount = "999999999999";
StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();
var clusterStore = new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");
Cluster persisted = new Cluster();
persisted.setName("persisted-cluster");
persisted.setStatus(ClusterStatus.ACTIVE);
// An existing identity keeps backfillOidcIdentities from writing the record itself.
persisted.setIdentity(new ClusterIdentity(new OidcIdentity(
"https://oidc.eks.us-east-1.amazonaws.com/id/ABCDEF0123456789ABCDEF0123456789")));
clusterStore.putForAccount(otherAccount, "persisted-cluster", persisted);
EksClusterManager clusterManager = mock(EksClusterManager.class);
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
try {
restarted.init();
verify(clusterManager).restoreCluster(persisted);
Cluster restored = clusterStore.getForAccount(otherAccount, "persisted-cluster").orElseThrow();
assertEquals(ClusterStatus.CREATING, restored.getStatus());
// Rehydrated from the storage key, so the readiness poller's later put also lands
// under the owner.
assertEquals(otherAccount, restored.getAccountId());
// No duplicate stranded under the default account.
assertTrue(clusterStore.getForAccount("000000000000", "persisted-cluster").isEmpty());
} finally {
restarted.shutdown();
}
}
@Test
void initDoesNotRestoreClustersWithNamesOutsideTheAwsCharset() {
// A record persisted before create-time name validation can carry a name with a dot —
// which would map to another account's qualified Docker name (999999999999.demo in the
// default account aliases account 999999999999's "demo"). Restoration must refuse it
// rather than adopt or remove that account's container.
StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();
var clusterStore = new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");
Cluster invalid = new Cluster();
invalid.setName("999999999999.demo");
invalid.setStatus(ClusterStatus.ACTIVE);
clusterStore.putForAccount("000000000000", "999999999999.demo", invalid);
EksClusterManager clusterManager = mock(EksClusterManager.class);
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
try {
restarted.init();
verify(clusterManager, never()).restoreCluster(any(Cluster.class));
assertEquals(ClusterStatus.FAILED,
restarted.describeCluster("999999999999.demo").getStatus());
} finally {
restarted.shutdown();
}
}
@Test
void initMarksAClusterFailedWhenItsContainerCannotBeRestored() {
StorageBackend<String, Cluster> rawClusters = new InMemoryStorage<>();
var clusterStore = new AccountAwareStorageBackend<>(rawClusters, null, "000000000000");
Cluster persisted = new Cluster();
persisted.setName("persisted-cluster");
persisted.setStatus(ClusterStatus.ACTIVE);
clusterStore.putForAccount("000000000000", "persisted-cluster", persisted);
EksClusterManager clusterManager = mock(EksClusterManager.class);
doThrow(new RuntimeException("no docker")).when(clusterManager).restoreCluster(persisted);
EksService restarted = new EksService(fixedStorageFactory(clusterStore), testConfig(false),
new RegionResolver("us-east-1", "000000000000"), clusterManager, null,
new EksOidcService(fixedStorageFactory(new InMemoryStorage<String, ClusterOidcKey>()),
new ObjectMapper()));
try {
restarted.init();
// Better an honest FAILED than an ACTIVE cluster no kubectl can reach.
assertEquals(ClusterStatus.FAILED,
restarted.describeCluster("persisted-cluster").getStatus());
} finally {
restarted.shutdown();
}
}
@SuppressWarnings("unchecked")
private StorageFactory fixedStorageFactory(StorageBackend<String, ?> backend) {
return new StorageFactory(null, null) {
@@ -310,6 +454,32 @@ class EksServiceTest {
assertThrows(AwsException.class, () -> eksService.createCluster(req));
}
@Test
void createClusterRejectsNamesOutsideTheAwsCharset() {
// Matches real EKS validation. The dot matters most: EksClusterManager account-qualifies
// Docker names as <account>.<name>, so a name containing a dot could spell out another
// account's qualified name and collide with its container and data volume.
for (String invalid : List.of("999999999999.demo", "has space", "-starts-with-dash",
"_starts-with-underscore", "a".repeat(101))) {
CreateClusterRequest req = new CreateClusterRequest();
req.setName(invalid);
req.setRoleArn("arn:aws:iam::000000000000:role/eks-role");
AwsException ex = assertThrows(AwsException.class, () -> eksService.createCluster(req),
"should reject: " + invalid);
assertEquals("InvalidParameterException", ex.getErrorCode());
assertEquals(400, ex.getHttpStatus());
}
assertTrue(eksService.listClusters().isEmpty());
}
@Test
void createClusterAcceptsTheFullAwsNameCharset() {
createTestCluster("Valid-Name_123");
assertTrue(eksService.listClusters().contains("Valid-Name_123"));
}
@Test
void createClusterWithNonExistentSubnetFails() {
StorageFactory storageFactory = new StorageFactory(null, null) {