mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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
@@ -16,6 +16,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;
|
||||
@@ -56,6 +57,7 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EmulatorLifecycle {
|
||||
@@ -115,8 +117,11 @@ public class EmulatorLifecycle {
|
||||
private final DurableExecutionService durableExecutionService;
|
||||
private final Instance<ContainerTeardown> containerTeardowns;
|
||||
private final PersistentPathValidator persistentPathValidator;
|
||||
private final EksService eksService;
|
||||
private final DynamoDbRuntime dynamoDbRuntime;
|
||||
private final RedshiftDynamoDbZeroEtlConsumer redshiftZeroEtlConsumer;
|
||||
// Set on the boot thread, read by the async HttpServerStart observer.
|
||||
private volatile CompletableFuture<Void> metadataServerStarted = CompletableFuture.completedFuture(null);
|
||||
|
||||
@Inject
|
||||
public EmulatorLifecycle(StorageFactory storageFactory, ServiceRegistry serviceRegistry,
|
||||
@@ -157,7 +162,8 @@ public class EmulatorLifecycle {
|
||||
Instance<ContainerTeardown> containerTeardowns,
|
||||
PersistentPathValidator persistentPathValidator,
|
||||
DynamoDbRuntime dynamoDbRuntime,
|
||||
RedshiftDynamoDbZeroEtlConsumer redshiftZeroEtlConsumer) {
|
||||
RedshiftDynamoDbZeroEtlConsumer redshiftZeroEtlConsumer,
|
||||
EksService eksService) {
|
||||
this.storageFactory = storageFactory;
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.config = config;
|
||||
@@ -196,6 +202,7 @@ public class EmulatorLifecycle {
|
||||
this.durableExecutionService = durableExecutionService;
|
||||
this.containerTeardowns = containerTeardowns;
|
||||
this.persistentPathValidator = persistentPathValidator;
|
||||
this.eksService = eksService;
|
||||
this.dynamoDbRuntime = dynamoDbRuntime;
|
||||
this.redshiftZeroEtlConsumer = redshiftZeroEtlConsumer;
|
||||
}
|
||||
@@ -279,7 +286,7 @@ public class EmulatorLifecycle {
|
||||
}
|
||||
|
||||
if (isMetadataServerNeeded()) {
|
||||
ec2MetadataServer.start().exceptionally(ex -> {
|
||||
metadataServerStarted = ec2MetadataServer.start().exceptionally(ex -> {
|
||||
LOG.warnv("EC2 IMDS server failed to start: {0}", ex.getMessage());
|
||||
return null;
|
||||
});
|
||||
@@ -310,6 +317,7 @@ public class EmulatorLifecycle {
|
||||
boolean hasStart = initializationHooksRunner.hasHooks(InitializationHook.START);
|
||||
boolean hasReady = initializationHooksRunner.hasHooks(InitializationHook.READY);
|
||||
if (!hasStart && !hasReady) {
|
||||
restoreEksClusters();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -322,6 +330,7 @@ public class EmulatorLifecycle {
|
||||
}
|
||||
initLifecycleState.markReadyCompleted();
|
||||
logReady();
|
||||
restoreEksClusters();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
LOG.error("Startup hook execution interrupted — shutting down", e);
|
||||
@@ -332,6 +341,25 @@ public class EmulatorLifecycle {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creating EksService restarts persisted clusters; without this call that waited for the first EKS request.
|
||||
* It runs once Floci's HTTP port and IMDS server listen, because a recreated cluster's Pod Identity and IMDS
|
||||
* relays probe them before they route pod traffic there, and after the start and ready hooks, which it
|
||||
* could otherwise hold up for as long as the clusters take to start.
|
||||
*/
|
||||
private void restoreEksClusters() {
|
||||
if (!config.services().eks().enabled() || config.services().eks().mock()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
metadataServerStarted.join();
|
||||
eksService.restorePersistedRuntime();
|
||||
} catch (RuntimeException e) {
|
||||
// EKS requests retry the bean's creation, as before; the rest of the emulator still starts.
|
||||
LOG.error("Failed to restore persisted EKS clusters at startup", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalStack prints a line ending in "Ready." when its gateway is up, and
|
||||
* ecosystem tooling keys on it — Testcontainers' LocalStackContainer default
|
||||
|
||||
@@ -156,6 +156,14 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
|
||||
oidcService, accessEntries, null, null);
|
||||
}
|
||||
|
||||
/** Called at boot so this lazily created bean runs {@link #init()}, which restarts persisted clusters. */
|
||||
public void restorePersistedRuntime() {
|
||||
int persistedClusters = allClusterEntries().size();
|
||||
if (persistedClusters > 0) {
|
||||
LOG.infov("Found {0} persisted EKS cluster record(s)", persistedClusters);
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
backfillOidcIdentities();
|
||||
|
||||
Reference in new issue
Block a user