feat(ecs): let the startup container sweep be turned off, and derive the credentials proxy owner from the shared helper (#4696)

* feat(ecs): let the startup container sweep be turned off, and derive the credentials proxy owner from the shared helper

The ECS startup sweep removes every task container carrying this deployment's
floci_owner_port label, so two Flocis sharing a Docker daemon with the same API
port and no resource namespace remove each other's task containers. EC2's sweep
already has an opt-out; ECS now has the same one,
FLOCI_SERVICES_ECS_RECONCILE_CONTAINERS_ON_STARTUP (default true). With it off,
containers are left alone and tasks start at once. The recorded load balancer
targets and Cloud Map instances are still released, since each Floci releases
only what its own persisted state recorded.

EcsCredentialsProxy.owner() now uses ContainerStorageHelper.ownerIdentity and
OWNER_LABEL instead of its own copy of the rule.

* fix(ecs): leave task-role credentials proxies alone when the startup sweep is off

The credentials proxy removed every proxy carrying this deployment's owner label
at startup regardless of reconcile-containers-on-startup. With the sweep off for
two instances sharing an owner identity, the second instance's startup still
tore down the first's proxy, and its running tasks lost their credentials
endpoint. The proxy's startup reap now follows the same setting.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-30 17:22:46 -05:00
1 parent 18128b432a
commit 55d37cfc41
9 files changed
+86 -8

No files matched your search

@@ -2098,6 +2098,18 @@ public interface EmulatorConfig {
@WithDefault("false")
boolean allowUnsafeHostVolumes();
/**
* When true, Floci removes on startup, in Docker mode, every ECS container a previous run
* of <em>this same</em> Floci left on the daemon (matched by the {@code floci_owner_port}
* label), task-role credentials proxies included, before the service scheduler starts
* replacement tasks. Turn it off when two Floci instances share a daemon with the same
* port and no resource namespace, so one does not remove the other's containers.
*
* Env var: FLOCI_SERVICES_ECS_RECONCILE_CONTAINERS_ON_STARTUP
*/
@WithDefault("true")
boolean reconcileContainersOnStartup();
EcsTaskRoleCredentialsConfig taskRoleCredentials();
}
@@ -114,6 +114,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
// what every such caller already expects.
private final EcsServiceDiscoveryRegistrar discoveryRegistrar;
private final boolean dockerMode;
private final boolean sweepLeftoverContainers;
private final String baseUrl;
// Replaced by afterReset() after a state reset, whose container teardown shuts this scheduler down.
private volatile ScheduledExecutorService reconciler = newReconciler();
@@ -210,6 +211,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
this.regionResolver = regionResolver;
this.containerManager = containerManager;
this.dockerMode = !config.services().ecs().mock();
this.sweepLeftoverContainers = dockerMode && config.services().ecs().reconcileContainersOnStartup();
this.baseUrl = config.effectiveBaseUrl();
this.lbRegistrar = lbRegistrar;
this.storageFactory = storageFactory;
@@ -242,10 +244,11 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
* target would follow its address to whichever container Docker hands it to next. Only what
* the registrars recorded registering is released, never a target or instance registered by
* hand. A container Docker would not remove is retried by {@link #leftoverContainersCleared()}
* before each task launch, and no task starts until it is gone.
* before each task launch, and no task starts until it is gone. With
* {@code reconcile-containers-on-startup} off, containers are left alone and tasks start at once.
*/
void releasePreviousRunLeftovers() {
if (dockerMode) {
if (sweepLeftoverContainers) {
leftoverContainersRemoved = containerManager.removeLeftoverContainers();
}
if (lbRegistrar != null) {
@@ -74,14 +74,21 @@ public class EcsCredentialsProxy implements ContainerTeardown {
* SecurityGroupFirewallManager} uses: several Floci processes can share one Docker daemon, and
* a label match alone would let one instance's restart tear down another's still-running,
* still-in-use proxy.
*
* <p>Skipped with {@code reconcile-containers-on-startup} off, like the task container sweep:
* that is the setting for instances sharing one owner identity, where a proxy carrying this
* owner label may still serve another instance's running tasks.
*/
@PostConstruct
void reapSurvivingProxies() {
if (!config.services().ecs().reconcileContainersOnStartup()) {
return;
}
try {
dockerClient.listContainersCmd().withShowAll(true)
.withLabelFilter(Map.of(OWNS_NETWORK_LABEL, "true"))
.exec().stream()
.filter(container -> owner().equals(container.getLabels().get("floci_owner_port")))
.filter(container -> owner().equals(container.getLabels().get(ContainerStorageHelper.OWNER_LABEL)))
.forEach(container -> lifecycleManager.removeIfExists(container.getId()));
} catch (Exception e) {
LOG.warnv("Could not reap surviving ECS credentials proxy containers: {0}", e.getMessage());
@@ -90,8 +97,7 @@ public class EcsCredentialsProxy implements ContainerTeardown {
/** Identifies this Floci process among others that may share the same Docker daemon. */
String owner() {
String namespace = config.docker().resourceNamespace().orElse("");
return namespace.isBlank() ? String.valueOf(config.port()) : namespace + "/" + config.port();
return ContainerStorageHelper.ownerIdentity(config);
}
/**
@@ -148,7 +154,7 @@ public class EcsCredentialsProxy implements ContainerTeardown {
.withCmd(List.of(
"TCP-LISTEN:80,bind=" + CREDENTIALS_ADDRESS + ",fork,reuseaddr",
"TCP:" + flociHost + ":" + port))
.withLabels(Map.of(OWNS_NETWORK_LABEL, "true", "floci_owner_port", owner()))
.withLabels(Map.of(OWNS_NETWORK_LABEL, "true", ContainerStorageHelper.OWNER_LABEL, owner()))
.build();
String proxyId = null;
@@ -198,7 +204,7 @@ public class EcsCredentialsProxy implements ContainerTeardown {
}
Map<String, String> labels = candidate.getLabels();
boolean isOurProxy = labels != null && "true".equals(labels.get(OWNS_NETWORK_LABEL));
String candidateOwner = labels == null ? null : labels.get("floci_owner_port");
String candidateOwner = labels == null ? null : labels.get(ContainerStorageHelper.OWNER_LABEL);
if (isOurProxy && (candidateOwner == null || owner().equals(candidateOwner))) {
lifecycleManager.removeIfExists(candidate.getId());
}