From 4e7d4a11043fde8c0c2923c2ae7f2287192b84b6 Mon Sep 17 00:00:00 2001 From: ShryukGrandhi Date: Wed, 4 Nov 2026 17:37:39 -0800 Subject: [PATCH] fix(ecr): replace a backing registry left on another Docker network (#7544) * fix(ecr): replace a backing registry left on another Docker network Signed-off-by: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com> * refactor(docker): share the surviving-container network check between ECR and EKS Signed-off-by: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com> --------- Signed-off-by: ShryukGrandhi <85529990+ShryukGrandhi@users.noreply.github.com> --- .../core/common/docker/ContainerNetworks.java | 39 ++++++++++++ .../ecr/registry/EcrRegistryManager.java | 12 +++- .../floci/services/eks/EksClusterManager.java | 18 +----- .../ecr/registry/EcrRegistryManagerTest.java | 63 +++++++++++++++++++ 4 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerNetworks.java diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerNetworks.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerNetworks.java new file mode 100644 index 0000000000..36a4c3a0c4 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerNetworks.java @@ -0,0 +1,39 @@ +package io.github.hectorvent.floci.core.common.docker; + +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.ContainerNetwork; +import org.jboss.logging.Logger; + +import java.util.Map; + +/** + * Checks whether a container that survived a restart still sits on the Docker network Floci uses. + */ +public final class ContainerNetworks { + + private static final Logger LOG = Logger.getLogger(ContainerNetworks.class); + + private ContainerNetworks() { + } + + /** + * True when {@code survivor} is attached to Docker networks but not to {@code network}, so Floci in a + * container cannot resolve it by name. A {@code null} network, or a survivor reporting no networks, + * is never treated as moved. + */ + public static boolean onAnotherNetwork(Container survivor, String network) { + if (network == null || survivor.getNetworkSettings() == null) { + return false; + } + Map networks = survivor.getNetworkSettings().getNetworks(); + if (networks == null || networks.isEmpty()) { + return false; + } + boolean moved = !networks.containsKey(network); + if (moved) { + LOG.infov("Surviving container {0} is not on Docker network {1}; replacing it (its data is kept)", + survivor.getId(), network); + } + return moved; + } +} 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 b461004d9b..a2446fa541 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 @@ -18,6 +18,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.C 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.ContainerNetworks; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver; @@ -364,14 +365,15 @@ public class EcrRegistryManager { existing = lifecycleManager.findByName(adoptedName); } if (existing.isPresent()) { - if (hasLoopbackBinding(existing.get())) { + if (!hasLoopbackBinding(existing.get())) { + LOG.infov("Recreating ECR backing registry {0} with a loopback-only port binding", adoptedName); + } else if (!onAnotherNetwork(existing.get())) { this.activeContainerName = adoptedName; adoptExisting(existing.get()); runReconcileOnce(); return; } // Recreated under the current name; the data volume is resolved by its own probe. - LOG.infov("Recreating ECR backing registry {0} with a loopback-only port binding", adoptedName); lifecycleManager.stopAndRemove(existing.get().getId(), null); } this.activeContainerName = name; @@ -720,6 +722,12 @@ public class EcrRegistryManager { this.registryAddress = endpoint != null ? endpoint.toString() : null; } + // Floci in a container reaches the registry over the Docker network; on the host it uses the loopback port. + private boolean onAnotherNetwork(Container survivor) { + return containerDetector.isRunningInContainer() && ContainerNetworks.onAnotherNetwork( + survivor, containerBuilder.resolveDockerNetwork(resolveRegistryDockerNetwork()).orElse(null)); + } + private static boolean hasLoopbackBinding(Container container) { ContainerPort[] ports = container.getPorts(); if (ports == null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index da5b978638..4561bd8bad 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -25,6 +25,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerExec; 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.ContainerLogStreamer; +import io.github.hectorvent.floci.core.common.docker.ContainerNetworks; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.docker.DockerHostResolver; @@ -871,21 +872,8 @@ public class EksClusterManager // Floci in a container reaches k3s over the Docker network; on the host it uses the published port. private boolean onAnotherNetwork(Container survivor) { - if (!containerDetector.isRunningInContainer()) { - return false; - } - Optional network = containerBuilder.resolveDockerNetwork(config.services().eks().dockerNetwork()); - if (network.isEmpty() || survivor.getNetworkSettings() == null - || survivor.getNetworkSettings().getNetworks() == null - || survivor.getNetworkSettings().getNetworks().isEmpty()) { - return false; - } - boolean moved = !survivor.getNetworkSettings().getNetworks().containsKey(network.get()); - if (moved) { - LOG.infov("Surviving k3s container {0} is not on Docker network {1}; replacing it (its data volume is kept)", - survivor.getId(), network.get()); - } - return moved; + return containerDetector.isRunningInContainer() && ContainerNetworks.onAnotherNetwork( + survivor, containerBuilder.resolveDockerNetwork(config.services().eks().dockerNetwork()).orElse(null)); } /** An adopted container keeps advertising the host its certificate was created for. */ 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 561f518587..655671957a 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 @@ -12,6 +12,8 @@ import com.github.dockerjava.api.command.InspectImageResponse; import com.github.dockerjava.api.exception.DockerClientException; import com.github.dockerjava.api.exception.NotFoundException; import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.ContainerNetwork; +import com.github.dockerjava.api.model.ContainerNetworkSettings; import com.github.dockerjava.api.model.ContainerPort; import com.github.dockerjava.api.model.Frame; import io.github.hectorvent.floci.config.EmulatorConfig; @@ -452,6 +454,67 @@ class EcrRegistryManagerTest { verify(builder).withLoopbackPortBinding(eq(5000), anyInt()); } + @Test + void survivorOnAnotherDockerNetworkIsRecreatedKeepingItsVolume() { + when(containerDetector.isRunningInContainer()).thenReturn(true); + when(currentContainerNetworkResolver.resolveNetworkName()).thenReturn(Optional.of("net-b")); + Container survivor = loopbackSurvivorOn("net-a"); + when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(survivor)); + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerLifecycleManager.ContainerInfo("container-id", Map.of())); + + manager.ensureStarted(); + + verify(lifecycleManager, never()).adopt(anyString(), any(), any()); + verify(lifecycleManager).stopAndRemove("0123456789abcdef", null); + verify(builder).withDockerNetwork(Optional.of("net-b")); + verify(builder).withNamedVolume(anyString(), eq("/var/lib/registry")); + verify(lifecycleManager, never()).removeVolume(anyString()); + assertTrue(manager.isStarted()); + } + + @Test + void survivorOnFlocisDockerNetworkIsAdopted() { + when(containerDetector.isRunningInContainer()).thenReturn(true); + when(currentContainerNetworkResolver.resolveNetworkName()).thenReturn(Optional.of("net-b")); + Container survivor = loopbackSurvivorOn("resolved:net-b"); + when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(survivor)); + when(lifecycleManager.adopt("0123456789abcdef", List.of(5000), Optional.of("resolved:net-b"))) + .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of())); + + manager.ensureStarted(); + + verify(lifecycleManager, never()).stopAndRemove(anyString(), any()); + verify(lifecycleManager, never()).createAndStart(any()); + assertTrue(manager.isStarted()); + } + + @Test + void survivorOnAnotherDockerNetworkIsAdoptedWhenFlociRunsOnTheHost() { + Container survivor = loopbackSurvivorOn("net-a"); + when(lifecycleManager.findByName(REGISTRY_NAME)).thenReturn(Optional.of(survivor)); + when(ecr.dockerNetwork()).thenReturn(Optional.of("net-b")); + when(lifecycleManager.adopt("0123456789abcdef", List.of(5000), Optional.of("resolved:net-b"))) + .thenReturn(new ContainerLifecycleManager.ContainerInfo("0123456789abcdef", Map.of())); + + manager.ensureStarted(); + + verify(lifecycleManager, never()).stopAndRemove(anyString(), any()); + verify(lifecycleManager, never()).createAndStart(any()); + assertTrue(manager.isStarted()); + } + + private static Container loopbackSurvivorOn(String network) { + Container existing = Mockito.mock(Container.class); + when(existing.getId()).thenReturn("0123456789abcdef"); + when(existing.getPorts()).thenReturn(new ContainerPort[] { + new ContainerPort().withIp("127.0.0.1").withPrivatePort(5000).withPublicPort(BASE_PORT) + }); + when(existing.getNetworkSettings()).thenReturn( + new ContainerNetworkSettings().withNetworks(Map.of(network, new ContainerNetwork()))); + return existing; + } + @Test void httpClient_usesNamespacedRegistryContainerDnsWhenConfigured() { when(containerDetector.isRunningInContainer()).thenReturn(true);