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

@@ -140,6 +140,29 @@ class EcsServiceRestartLeftoversTest {
verify(containerManager, never()).removeLeftoverContainers();
}
@Test
void optingOutLeavesThePreviousRunsContainersButReleasesItsRegistrations() {
EcsContainerManager containerManager = mock(EcsContainerManager.class);
EcsLoadBalancerRegistrar lbRegistrar = mock(EcsLoadBalancerRegistrar.class);
EcsServiceDiscoveryRegistrar discoveryRegistrar = mock(EcsServiceDiscoveryRegistrar.class);
EcsService restarted = service(new SharedStorageFactory(), false, false, containerManager,
lbRegistrar, discoveryRegistrar);
restarted.createCluster("app-cluster", Map.of(), REGION);
ContainerDefinition container = new ContainerDefinition();
container.setName("web");
container.setImage("nginx:alpine");
restarted.registerTaskDefinition("web", List.of(container), NetworkMode.bridge, null, null,
null, null, List.of(), REGION);
restarted.releasePreviousRunLeftovers();
restarted.runTask("app-cluster", "web", 1, LaunchType.EC2, null, null, null, null, REGION);
verify(containerManager, never()).removeLeftoverContainers();
verify(containerManager).startTask(any(), any(), any(), anyString());
verify(lbRegistrar).releaseRecordedTargets();
verify(discoveryRegistrar).releaseRecordedInstances();
}
@Test
void startupReleasesLeftoversBeforeTheSchedulerRuns() {
EcsContainerManager containerManager = mock(EcsContainerManager.class);
@@ -223,8 +246,17 @@ class EcsServiceRestartLeftoversTest {
EcsContainerManager containerManager,
EcsLoadBalancerRegistrar lbRegistrar,
EcsServiceDiscoveryRegistrar discoveryRegistrar) {
return service(storage, mockMode, true, containerManager, lbRegistrar, discoveryRegistrar);
}
private static EcsService service(StorageFactory storage, boolean mockMode,
boolean reconcileContainersOnStartup,
EcsContainerManager containerManager,
EcsLoadBalancerRegistrar lbRegistrar,
EcsServiceDiscoveryRegistrar discoveryRegistrar) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().ecs().mock()).thenReturn(mockMode);
when(config.services().ecs().reconcileContainersOnStartup()).thenReturn(reconcileContainersOnStartup);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
EcsService service = new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
config, lbRegistrar, storage, null, new EcsExecSessionRegistry(), discoveryRegistrar);
@@ -224,6 +224,7 @@ class EcsCredentialsProxyTest {
@Test
void reapSurvivingProxiesRemovesOnlyThisInstancesOwnLeftovers() {
when(config.services().ecs().reconcileContainersOnStartup()).thenReturn(true);
when(config.docker().resourceNamespace()).thenReturn(Optional.empty());
when(config.port()).thenReturn(4566);
ListContainersCmd listCmd = mock(ListContainersCmd.class, RETURNS_SELF);
@@ -244,8 +245,19 @@ class EcsCredentialsProxyTest {
verify(lifecycleManager, never()).removeIfExists("other-instance-proxy");
}
@Test
void reapSurvivingProxiesLeavesProxiesAloneWhenStartupReconciliationIsOff() {
when(config.services().ecs().reconcileContainersOnStartup()).thenReturn(false);
proxy.reapSurvivingProxies();
verify(dockerClient, never()).listContainersCmd();
verify(lifecycleManager, never()).removeIfExists(any());
}
@Test
void reapSurvivingProxiesToleratesADockerFailure() {
when(config.services().ecs().reconcileContainersOnStartup()).thenReturn(true);
when(dockerClient.listContainersCmd()).thenThrow(new RuntimeException("daemon unreachable"));
proxy.reapSurvivingProxies();