diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java index 0edce34caf..25a7d82335 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/EcrTest.java @@ -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 diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java index c3452872fb..b461004d9b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java @@ -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) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java index 49cb3b05cc..561f518587 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java @@ -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(