mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(docker): keep host network mode for containers that publish ports (#4192)
* fix(docker): keep host network mode for containers that publish ports A spec with port bindings skipped HostConfig.NetworkMode on create and connected to the network after start, which Docker refuses for host, none and container:<id>. Such containers silently stayed on the default bridge with a published port, so an OpenSearch domain under FLOCI_SERVICES_DOCKER_NETWORK=host stopped listening on the host's :9200 once domains always published a host port (#2798). Namespace network modes now always go on the HostConfig, their port bindings are dropped since Docker publishes nothing there, and the post-start connect is skipped. A host-network container's endpoint resolves to localhost when Floci itself runs in Docker instead of a null host. * fix(docker): resolve container:<id> endpoints through the owning container resolveEndpoint only special-cased host, so a container sharing another container's network namespace fell through to resolveContainerIp, which has no network to read, and Floci stored a null host for it. The endpoint now follows HostConfig.NetworkMode to the owning container: localhost when that container is on the host network, otherwise its IP on the Docker network. Reading the mode from the inspect rather than the spec also covers adopted containers. * test(docker): pass portBindingHostIps to the network test spec helper ContainerSpec gained a 28th component in #4175 after this branch was cut, so the spec(...) helper added here no longer compiled once rebased onto main.
This commit is contained in:
1 parent
a624608b5d
commit
a574a6d01f
5 files changed
+240
-14
No files matched your search
+76
@@ -0,0 +1,76 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class ContainerHostNetworkDockerIntegrationTest {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ContainerHostNetworkDockerIntegrationTest.class);
|
||||
private static final String IMAGE = "public.ecr.aws/docker/library/busybox:1.36";
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
@Inject
|
||||
ContainerBuilder containerBuilder;
|
||||
|
||||
@Inject
|
||||
ContainerLifecycleManager lifecycleManager;
|
||||
|
||||
@BeforeEach
|
||||
void requireDocker() {
|
||||
Assumptions.assumeTrue(isDockerAvailable(),
|
||||
"Docker daemon must be available for host network integration tests");
|
||||
}
|
||||
|
||||
@Test
|
||||
void portBoundContainerIsCreatedInHostNetworkMode() {
|
||||
ContainerSpec spec = containerBuilder.newContainer(IMAGE)
|
||||
.withName("floci-host-network-test-" + UUID.randomUUID())
|
||||
.withCmd(List.of("sleep", "30"))
|
||||
.withNetworkMode("host")
|
||||
.withPortBinding(8080, 18080)
|
||||
.build();
|
||||
|
||||
String containerId = null;
|
||||
try {
|
||||
containerId = lifecycleManager.createAndStart(spec).containerId();
|
||||
|
||||
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
assertEquals("host", inspect.getHostConfig().getNetworkMode());
|
||||
Ports bindings = inspect.getHostConfig().getPortBindings();
|
||||
assertTrue(bindings == null || bindings.getBindings().isEmpty(),
|
||||
"host network mode publishes no ports, got " + bindings);
|
||||
assertTrue(inspect.getNetworkSettings().getNetworks().containsKey("host"),
|
||||
"container should sit on the host network, got " + inspect.getNetworkSettings().getNetworks().keySet());
|
||||
} finally {
|
||||
if (containerId != null) {
|
||||
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDockerAvailable() {
|
||||
try {
|
||||
dockerClient.pingCmd().exec();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Docker daemon is not available for the host network integration test", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
@@ -11,12 +11,17 @@ import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.command.RemoveContainerCmd;
|
||||
import com.github.dockerjava.api.command.StartContainerCmd;
|
||||
import com.github.dockerjava.api.model.ContainerNetwork;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.NetworkSettings;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.Mock;
|
||||
@@ -26,6 +31,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -152,11 +159,110 @@ class ContainerLifecycleManagerNetworkTest {
|
||||
verify(disconnectCmd, never()).exec();
|
||||
}
|
||||
|
||||
/**
|
||||
* Docker only honours host, none and container:<id> on the HostConfig at creation, refuses
|
||||
* to connect a container to them afterwards, and publishes no host ports through them. A
|
||||
* port-bound spec must therefore still put the mode on the HostConfig instead of taking the
|
||||
* publish-then-connect path, which left such containers on the default bridge.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"host", "none", "container:sibling"})
|
||||
void createAppliesNamespaceNetworkModeInsteadOfPublishingPorts(String networkMode) {
|
||||
manager().create(spec(networkMode, Map.of(9200, 9400), List.of()));
|
||||
|
||||
HostConfig hostConfig = createdHostConfig();
|
||||
assertEquals(networkMode, hostConfig.getNetworkMode());
|
||||
assertNull(hostConfig.getPortBindings());
|
||||
verify(connectCmd, never()).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDoesNotAllocateDynamicPortsOnHostNetwork() {
|
||||
manager().create(spec("host", Map.of(9200, 0), List.of()));
|
||||
|
||||
verify(portAllocator, never()).allocateAny();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndStartDoesNotConnectHostNetworkAfterStart() {
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("host", Map.of(9200, 9400), List.of()));
|
||||
|
||||
verify(connectCmd, never()).exec();
|
||||
assertTrue(info.publishedHostPort(9200).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointsOfHostNetworkContainersPointAtLocalhostWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("host"));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("host", Map.of(9200, 9400), List.of(9200)));
|
||||
|
||||
assertEquals(new EndpointInfo("localhost", 9200), info.getEndpoint(9200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointsOfNamespaceSharingContainersPointAtTheOwningContainerWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getHostConfig())
|
||||
.thenReturn(HostConfig.newHostConfig().withNetworkMode("container:sibling-id"));
|
||||
InspectContainerResponse owner = inspectOf("sibling-id");
|
||||
NetworkSettings ownerNetworks = mock(NetworkSettings.class);
|
||||
when(owner.getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("bridge"));
|
||||
when(owner.getNetworkSettings()).thenReturn(ownerNetworks);
|
||||
when(ownerNetworks.getNetworks())
|
||||
.thenReturn(Map.of("bridge", new ContainerNetwork().withIpv4Address("172.17.0.7")));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("container:sibling", Map.of(9200, 9400), List.of(9200)));
|
||||
|
||||
assertEquals(new EndpointInfo("172.17.0.7", 9200), info.getEndpoint(9200));
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointsOfContainersJoiningAHostNetworkContainerPointAtLocalhostWhenFlociRunsInDocker() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
when(dockerClient.startContainerCmd("container-id")).thenReturn(mock(StartContainerCmd.class));
|
||||
when(inspectOf("container-id").getHostConfig())
|
||||
.thenReturn(HostConfig.newHostConfig().withNetworkMode("container:sibling-id"));
|
||||
when(inspectOf("sibling-id").getHostConfig()).thenReturn(HostConfig.newHostConfig().withNetworkMode("host"));
|
||||
|
||||
ContainerInfo info = manager().createAndStart(spec("container:sibling", Map.of(9200, 9400), List.of(9200)));
|
||||
|
||||
assertEquals(new EndpointInfo("localhost", 9200), info.getEndpoint(9200));
|
||||
}
|
||||
|
||||
private InspectContainerResponse inspectOf(String containerId) {
|
||||
InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class, RETURNS_SELF);
|
||||
InspectContainerResponse inspect = mock(InspectContainerResponse.class);
|
||||
when(dockerClient.inspectContainerCmd(containerId)).thenReturn(inspectCmd);
|
||||
when(inspectCmd.exec()).thenReturn(inspect);
|
||||
return inspect;
|
||||
}
|
||||
|
||||
private HostConfig createdHostConfig() {
|
||||
ArgumentCaptor<HostConfig> hostConfig = ArgumentCaptor.forClass(HostConfig.class);
|
||||
verify(createCmd).withHostConfig(hostConfig.capture());
|
||||
return hostConfig.getValue();
|
||||
}
|
||||
|
||||
private ContainerLifecycleManager manager() {
|
||||
return new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector,
|
||||
portAllocator, config);
|
||||
}
|
||||
|
||||
private static ContainerSpec spec(String networkMode, Map<Integer, Integer> portBindings,
|
||||
List<Integer> exposedPorts) {
|
||||
return new ContainerSpec(
|
||||
"busybox:stable", "probe", List.of(), null, null, null, portBindings, List.of(),
|
||||
exposedPorts, networkMode, List.of(), List.of(), List.of(), List.of(), Map.of(), null,
|
||||
false, null, List.of(), null, null, List.of(), List.of(), null, null, false,
|
||||
List.of(), Map.of());
|
||||
}
|
||||
|
||||
private static ContainerSpec specWithLinkLocalIp(Map<Integer, Integer> portBindings) {
|
||||
return new ContainerSpec(
|
||||
"busybox:stable", null, List.of(), null, null, null, portBindings, List.of(),
|
||||
|
||||
Reference in new issue
Block a user