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>
This commit is contained in:
ShryukGrandhi authored and GitHub committed 2026-11-04 21:37:39 -04:00
1 parent 4abccec0c8
commit 4e7d4a1104
4 files changed
+115 -17

No files matched your search

@@ -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<String, ContainerNetwork> 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;
}
}
@@ -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) {
@@ -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<String> 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. */