fix(ecr): follow the registry when deleting the last repository replaces it (#7224)

In memory mode, or with prune-volumes-on-delete, deleting the last repository
removes the backing registry container and the next CreateRepository starts a
new one. Floci running in Docker reached the registry by container name, so
when the new container got a different IP the JVM's cached lookup kept
answering with the old address. Until it expired, DeleteRepository returned
500 and CreateRepository could wait long enough for the SDK to time out and
retry, which then failed with RepositoryAlreadyExistsException.

Use the address Docker reports for the container just started or adopted,
and only fall back to the name when none is known. EcrTest no longer needs to
keep a repository to avoid this.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-01 12:45:46 -04:00
1 parent b38fe7d96d
commit 38eb938f22
3 files changed
+63 -9

No files matched your search

@@ -35,12 +35,6 @@ class EcrTest {
@BeforeAll
static void setup() {
ecr = TestFixtures.ecrClient();
// Floci removes the backing registry when the last repository goes and starts a new one
// on the next create, which can stall for as long as Floci caches the old container's
// address. These tests are not about that, so one repository stays for the whole class.
String keepRegistry = "floci-it/keep-registry-" + System.nanoTime();
ecr.createRepository(b -> b.repositoryName(keepRegistry));
reposToCleanup.add(keepRegistry);
}
@AfterAll
@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerPresence;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
@@ -98,6 +99,8 @@ public class EcrRegistryManager {
private volatile boolean started;
private volatile boolean reconciled;
/** host:port Docker reports for the running registry; null until one is started or adopted. */
private volatile String registryAddress;
private volatile boolean unavailableLogged;
private volatile int hostPort;
private volatile String containerId;
@@ -277,7 +280,10 @@ public class EcrRegistryManager {
/** Returns a {@link RegistryHttpClient} bound to the current registry endpoint. */
public RegistryHttpClient httpClient() {
if (containerDetector.isRunningInContainer()) {
return new RegistryHttpClient(internalEndpoint());
// Not the container name: deleting the last repository replaces the container, and a
// name lookup Floci cached for the old one keeps answering with its address.
String address = registryAddress;
return new RegistryHttpClient(address != null ? "http://" + address : internalEndpoint());
}
return new RegistryHttpClient("http://localhost:" + effectivePort());
}
@@ -337,6 +343,7 @@ public class EcrRegistryManager {
String previousContainerId = containerId;
closeLogStream();
containerId = null;
registryAddress = null;
started = false;
if (presence == ContainerPresence.ABSENT) {
portAllocator.release(hostPort);
@@ -374,7 +381,9 @@ public class EcrRegistryManager {
config.services().ecr().registryBasePort(),
config.services().ecr().registryMaxPort(),
port -> {
this.containerId = lifecycleManager.createAndStart(registryContainerSpec(name, port)).containerId();
ContainerInfo info = lifecycleManager.createAndStart(registryContainerSpec(name, port));
this.containerId = info.containerId();
rememberRegistryAddress(info);
return port;
});
this.started = true;
@@ -651,6 +660,7 @@ public class EcrRegistryManager {
lifecycleManager.stopAndRemove(containerId, logStream);
portAllocator.release(hostPort);
containerId = null;
registryAddress = null;
logStream = null;
started = false;
reconciled = false;
@@ -682,8 +692,9 @@ public class EcrRegistryManager {
try {
ContainerInfo info = lifecycleManager.adopt(containerId, List.of(CONTAINER_INTERNAL_PORT),
containerBuilder.resolveDockerNetwork(resolveRegistryDockerNetwork()));
rememberRegistryAddress(info);
// The control plane reaches the backing registry through this published loopback
// binding when Floci runs on the host. In Docker, httpClient() uses container DNS.
// binding when Floci runs on the host. In Docker, httpClient() uses the in-network address.
OptionalInt published = info.publishedHostPort(CONTAINER_INTERNAL_PORT);
if (published.isPresent()) {
this.hostPort = published.getAsInt();
@@ -700,9 +711,15 @@ public class EcrRegistryManager {
} catch (Exception e) {
LOG.warnv("Failed to adopt existing ECR registry container: {0}", e.getMessage());
this.containerId = null;
this.registryAddress = null;
}
}
private void rememberRegistryAddress(ContainerInfo info) {
EndpointInfo endpoint = info.getEndpoint(CONTAINER_INTERNAL_PORT);
this.registryAddress = endpoint != null ? endpoint.toString() : null;
}
private static boolean hasLoopbackBinding(Container container) {
ContainerPort[] ports = container.getPorts();
if (ports == null) {
@@ -324,6 +324,49 @@ class EcrRegistryManagerTest {
assertEquals("http://" + REGISTRY_NAME + ":5000", manager.httpClient().baseUrl());
}
@Test
void httpClient_usesTheStartedRegistrysAddressInsideDocker() {
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(containerAt("container-id", "172.18.0.3"));
manager.ensureStarted();
assertEquals("http://172.18.0.3:5000", manager.httpClient().baseUrl());
}
@Test
void httpClient_followsTheRegistryWhenDeletingTheLastRepositoryReplacesIt() {
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(storage.pruneVolumesOnDelete()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
containerAt("old-container", "172.18.0.3"), containerAt("new-container", "172.18.0.9"));
manager.ensureStarted();
manager.pruneStorage();
assertEquals("http://" + REGISTRY_NAME + ":5000", manager.httpClient().baseUrl());
manager.ensureStarted();
assertEquals("http://172.18.0.9:5000", manager.httpClient().baseUrl());
}
@Test
void httpClient_forgetsTheAddressOfARegistryThatDisappeared() {
when(containerDetector.isRunningInContainer()).thenReturn(true);
when(lifecycleManager.createAndStart(any())).thenReturn(
containerAt("old-container", "172.18.0.3"), containerAt("new-container", "172.18.0.9"));
manager.ensureStarted();
when(lifecycleManager.presenceOf("old-container")).thenReturn(ContainerPresence.ABSENT);
manager.ensureStarted();
assertEquals("http://172.18.0.9:5000", manager.httpClient().baseUrl());
}
private static ContainerLifecycleManager.ContainerInfo containerAt(String containerId, String ip) {
return new ContainerLifecycleManager.ContainerInfo(containerId,
Map.of(5000, new ContainerLifecycleManager.EndpointInfo(ip, 5000)));
}
@Test
void freshlyCreatedRegistryStreamsItsCompleteLog() {
when(lifecycleManager.createAndStart(any())).thenReturn(