mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eks): restore persisted clusters at startup (#6214)
EksService restores persisted clusters in @PostConstruct, but CDI creates the bean on first use. After a restart no cluster's k3s container came back until something called the EKS API, so workloads on it stayed down. EmulatorLifecycle now calls EksService.restorePersistedRuntime from onHttpStart, after the start and ready hooks. It waits for the IMDS server to start, because a recreated cluster's Pod Identity and IMDS relays probe Floci's HTTP port and IMDS before they route pod traffic there. It runs after the hooks so they and the ready line don't wait minutes for k3s. If the restore throws, it is logged and the emulator stays up; EKS requests retry creating the service, as before. EksBootRestoreIntegrationTest boots a fresh application with EKS out of mock mode and checks EksService was created without an EKS request, as EcsBootReconcilerIntegrationTest does for ECS. Its profile pins the HTTP port, since onHttpStart acts only on quarkus.http.port. Co-authored-by: Robin Baeckman <27041454+RobinBaeckman@users.noreply.github.com>
This commit is contained in:
1 parent
bc760f743d
commit
204231d4b3
4 files changed
+198
-3
No files matched your search
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.ecs.EcsService;
|
||||
import io.github.hectorvent.floci.services.ecs.container.EcsTaskRoleCredentialsServer;
|
||||
import io.github.hectorvent.floci.services.eks.EksService;
|
||||
import io.github.hectorvent.floci.services.elasticache.ElastiCacheMemcachedService;
|
||||
import io.github.hectorvent.floci.services.elasticache.ElastiCacheService;
|
||||
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
|
||||
@@ -104,6 +105,7 @@ class EmulatorLifecycleTest {
|
||||
@Mock private io.github.hectorvent.floci.services.elbv2.ElbV2Service elbV2Service;
|
||||
@Mock private io.github.hectorvent.floci.services.elb.ElbClassicService elbClassicService;
|
||||
@Mock private EcsService ecsService;
|
||||
@Mock private EksService eksService;
|
||||
@Mock private InitializationHooksRunner initializationHooksRunner;
|
||||
@Mock private SqsEventSourcePoller sqsPoller;
|
||||
@Mock private KinesisEventSourcePoller kinesisPoller;
|
||||
@@ -166,7 +168,7 @@ class EmulatorLifecycleTest {
|
||||
pipesService, ec2MetadataServer, ecsTaskRoleCredentialsServer, ecrRegistryManager, flociUiManager, initLifecycleState,
|
||||
schemaCreationWorker, stepFunctionsService, durableExecutionService, containerTeardowns,
|
||||
persistentPathValidator,
|
||||
dynamoDbRuntime, redshiftZeroEtlConsumer);
|
||||
dynamoDbRuntime, redshiftZeroEtlConsumer, eksService);
|
||||
Mockito.lenient().when(containerTeardowns.iterator())
|
||||
.thenReturn(java.util.Collections.emptyIterator());
|
||||
}
|
||||
@@ -285,6 +287,100 @@ class EmulatorLifecycleTest {
|
||||
inOrder.verify(ecsService).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoresPersistedEksClustersOnceTheHttpPortListens() throws Exception {
|
||||
stubStorageConfig();
|
||||
when(eksServiceConfig.enabled()).thenReturn(true);
|
||||
when(eksServiceConfig.mock()).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
|
||||
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
verify(eksService, never()).restorePersistedRuntime();
|
||||
|
||||
emulatorLifecycle.onHttpStart(new HttpServerStart(new HttpServerOptions().setPort(4566)));
|
||||
verify(eksService).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoresPersistedEksClustersOnlyOnceTheImdsServerHasStarted() throws Exception {
|
||||
stubStorageConfig();
|
||||
when(eksServiceConfig.enabled()).thenReturn(true);
|
||||
when(eksServiceConfig.mock()).thenReturn(false);
|
||||
when(eksServiceConfig.imds()).thenReturn(true);
|
||||
CompletableFuture<Void> imdsStarted = new CompletableFuture<>();
|
||||
when(ec2MetadataServer.start()).thenReturn(imdsStarted);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
|
||||
Thread httpStart = Thread.ofVirtual().start(() ->
|
||||
emulatorLifecycle.onHttpStart(new HttpServerStart(new HttpServerOptions().setPort(4566))));
|
||||
verify(eksService, Mockito.after(300).never()).restorePersistedRuntime();
|
||||
|
||||
imdsStarted.complete(null);
|
||||
httpStart.join(5000);
|
||||
verify(eksService).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoresPersistedEksClustersAfterTheStartAndReadyHooks() throws Exception {
|
||||
when(eksServiceConfig.enabled()).thenReturn(true);
|
||||
when(eksServiceConfig.mock()).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(true);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(true);
|
||||
|
||||
emulatorLifecycle.onHttpStart(new HttpServerStart(new HttpServerOptions().setPort(4566)));
|
||||
|
||||
InOrder inOrder = Mockito.inOrder(initializationHooksRunner, initLifecycleState, eksService);
|
||||
inOrder.verify(initializationHooksRunner).run(InitializationHook.START);
|
||||
inOrder.verify(initializationHooksRunner).run(InitializationHook.READY);
|
||||
inOrder.verify(initLifecycleState).markReadyCompleted();
|
||||
inOrder.verify(eksService).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedEksRestoreLeavesTheEmulatorReady() throws Exception {
|
||||
when(eksServiceConfig.enabled()).thenReturn(true);
|
||||
when(eksServiceConfig.mock()).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(true);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
|
||||
doThrow(new IllegalStateException("corrupt cluster record")).when(eksService).restorePersistedRuntime();
|
||||
|
||||
try (MockedStatic<Quarkus> quarkus = Mockito.mockStatic(Quarkus.class,
|
||||
Mockito.withSettings().mockMaker(MockMakers.INLINE))) {
|
||||
emulatorLifecycle.onHttpStart(new HttpServerStart(new HttpServerOptions().setPort(4566)));
|
||||
quarkus.verify(Quarkus::asyncExit, never());
|
||||
}
|
||||
verify(initLifecycleState).markReadyCompleted();
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesEksUntouchedAtBootWhenEksIsDisabled() throws Exception {
|
||||
stubStorageConfig();
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
|
||||
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
emulatorLifecycle.onHttpStart(new HttpServerStart(new HttpServerOptions().setPort(4566)));
|
||||
|
||||
verify(eksService, Mockito.never()).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesEksUntouchedAtBootInMockMode() throws Exception {
|
||||
stubStorageConfig();
|
||||
when(eksServiceConfig.enabled()).thenReturn(true);
|
||||
when(eksServiceConfig.mock()).thenReturn(true);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
|
||||
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
emulatorLifecycle.onHttpStart(new HttpServerStart(new HttpServerOptions().setPort(4566)));
|
||||
|
||||
verify(eksService, Mockito.never()).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesEcsUntouchedAtBootWhenEcsIsDisabled() {
|
||||
stubStorageConfig();
|
||||
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.github.hectorvent.floci.testutil.FreePorts;
|
||||
import io.quarkus.arc.Arc;
|
||||
import io.quarkus.arc.InjectableBean;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* EksService is created lazily, and creating it is what restarts each persisted cluster's k3s
|
||||
* container. Boot has to create it, or persisted clusters stay down after a restart until the
|
||||
* first EKS request. The profile of its own gives this test a fresh application that no other
|
||||
* test has sent an EKS request to, with EKS out of mock mode as boot restores only real clusters.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(EksBootRestoreIntegrationTest.FreshBootProfile.class)
|
||||
class EksBootRestoreIntegrationTest {
|
||||
|
||||
public static final class FreshBootProfile implements QuarkusTestProfile {
|
||||
// Boot restores once quarkus.http.port listens; the suite's random port 0 never matches the bound one.
|
||||
private static final int TEST_PORT = freePort();
|
||||
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of(
|
||||
"floci.services.eks.mock", "false",
|
||||
"quarkus.http.test-port", String.valueOf(TEST_PORT),
|
||||
"quarkus.http.port", String.valueOf(TEST_PORT),
|
||||
"floci.port", String.valueOf(TEST_PORT),
|
||||
"floci.base-url", "http://localhost:" + TEST_PORT);
|
||||
}
|
||||
|
||||
private static int freePort() {
|
||||
try {
|
||||
return FreePorts.anyInterfacePort();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("No free port for the EKS boot test application", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void bootCreatesEksServiceWithoutAnEksRequest() {
|
||||
// Boot restores once the HTTP port listens, from an async observer, so this can run first.
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
Map<InjectableBean<?>, Object> created = Arc.container().getActiveContext(ApplicationScoped.class)
|
||||
.getState().getContextualInstances();
|
||||
assertTrue(created.keySet().stream().anyMatch(bean -> bean.getBeanClass() == EksService.class),
|
||||
"EksService must be created at boot, not on the first EKS request");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user