mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
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;
|
||||
}
|
||||
}
|
||||
+10
-2
@@ -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. */
|
||||
|
||||
+63
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user