mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(core): avoid Podman host-gateway aliases in container builder (#7572)
* fix(core): avoid Podman host-gateway aliases in container builder * fix(core): share host alias parsing and log probe failures * refactor(lambda): reuse builder host alias guard and test fixture * fix(docker): register version response for native image
This commit is contained in:
1 parent
a613d7b0c3
commit
e9378bb71a
8 files changed
+376
-126
No files matched your search
@@ -39,19 +39,30 @@ import java.util.Optional;
|
||||
@ApplicationScoped
|
||||
public class ContainerBuilder {
|
||||
|
||||
private static final String HOST_DOCKER_INTERNAL = "host.docker.internal";
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final EmbeddedDnsServer embeddedDnsServer;
|
||||
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
|
||||
private final ContainerEngineDetector containerEngineDetector;
|
||||
|
||||
@Inject
|
||||
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
EmbeddedDnsServer embeddedDnsServer,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver,
|
||||
ContainerEngineDetector containerEngineDetector) {
|
||||
this.config = config;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.embeddedDnsServer = embeddedDnsServer;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
this.containerEngineDetector = containerEngineDetector;
|
||||
}
|
||||
|
||||
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
EmbeddedDnsServer embeddedDnsServer,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
this(config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver, null);
|
||||
}
|
||||
|
||||
public ContainerBuilder(EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
@@ -66,7 +77,8 @@ public class ContainerBuilder {
|
||||
* @return a new Builder instance
|
||||
*/
|
||||
public Builder newContainer(String image) {
|
||||
return new Builder(resolveImage(image), config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);
|
||||
return new Builder(resolveImage(image), config, dockerHostResolver, embeddedDnsServer,
|
||||
currentContainerNetworkResolver, containerEngineDetector);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,6 +134,13 @@ public class ContainerBuilder {
|
||||
});
|
||||
}
|
||||
|
||||
/** Returns whether an extra-host entry explicitly maps host.docker.internal to an address. */
|
||||
public static boolean isHostDockerInternalEntry(String entry) {
|
||||
int separator = entry.indexOf(':');
|
||||
return separator > 0 && separator < entry.length() - 1
|
||||
&& HOST_DOCKER_INTERNAL.equalsIgnoreCase(entry.substring(0, separator));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fluent builder for constructing ContainerSpec instances.
|
||||
*/
|
||||
@@ -136,12 +155,14 @@ public class ContainerBuilder {
|
||||
* Docker's own NVIDIA path uses the count/id form instead.
|
||||
*/
|
||||
private static final String CDI_DRIVER = "cdi";
|
||||
private static final String HOST_DOCKER_INTERNAL_GATEWAY = HOST_DOCKER_INTERNAL + ":host-gateway";
|
||||
|
||||
private final String image;
|
||||
private final EmulatorConfig config;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final EmbeddedDnsServer embeddedDnsServer;
|
||||
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
|
||||
private final ContainerEngineDetector containerEngineDetector;
|
||||
|
||||
private String name;
|
||||
private final List<String> env = new ArrayList<>();
|
||||
@@ -162,6 +183,7 @@ public class ContainerBuilder {
|
||||
private final List<Bind> binds = new ArrayList<>();
|
||||
private final List<VolumesFrom> volumesFrom = new ArrayList<>();
|
||||
private final List<String> extraHosts = new ArrayList<>();
|
||||
private boolean automaticHostGatewayAdded;
|
||||
private final Map<String, String> labels = new HashMap<>();
|
||||
private LogConfig logConfig;
|
||||
private boolean privileged;
|
||||
@@ -182,12 +204,14 @@ public class ContainerBuilder {
|
||||
|
||||
Builder(String image, EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
EmbeddedDnsServer embeddedDnsServer,
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
|
||||
CurrentContainerNetworkResolver currentContainerNetworkResolver,
|
||||
ContainerEngineDetector containerEngineDetector) {
|
||||
this.image = image;
|
||||
this.config = config;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.embeddedDnsServer = embeddedDnsServer;
|
||||
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
|
||||
this.containerEngineDetector = containerEngineDetector;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,13 +466,15 @@ public class ContainerBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the host.docker.internal extra host entry on Linux.
|
||||
* This allows containers to reach the host via a consistent hostname
|
||||
* across all platforms (already exists on Docker Desktop).
|
||||
* Adds the host.docker.internal extra host entry on Linux Docker.
|
||||
* Docker Desktop provides this hostname; Podman may provide it when the host address is known.
|
||||
*/
|
||||
public Builder withHostDockerInternalOnLinux() {
|
||||
if (dockerHostResolver.isLinuxHost()) {
|
||||
this.extraHosts.add("host.docker.internal:host-gateway");
|
||||
if (dockerHostResolver.isLinuxHost()
|
||||
&& extraHosts.stream().noneMatch(ContainerBuilder::isHostDockerInternalEntry)
|
||||
&& (containerEngineDetector == null || !containerEngineDetector.isPodmanDaemon())) {
|
||||
this.extraHosts.add(HOST_DOCKER_INTERNAL_GATEWAY);
|
||||
this.automaticHostGatewayAdded = true;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -457,7 +483,12 @@ public class ContainerBuilder {
|
||||
* Adds a custom extra host entry.
|
||||
*/
|
||||
public Builder withExtraHost(String hostname, String ip) {
|
||||
this.extraHosts.add(hostname + ":" + ip);
|
||||
String entry = hostname + ":" + ip;
|
||||
if (automaticHostGatewayAdded && isHostDockerInternalEntry(entry)) {
|
||||
this.extraHosts.remove(HOST_DOCKER_INTERNAL_GATEWAY);
|
||||
this.automaticHostGatewayAdded = false;
|
||||
}
|
||||
this.extraHosts.add(entry);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/** Identifies the daemon behind the configured Docker-compatible socket. */
|
||||
@ApplicationScoped
|
||||
public class ContainerEngineDetector {
|
||||
|
||||
private enum ContainerEngine { PODMAN, DOCKER, UNKNOWN }
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ContainerEngineDetector.class);
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final AtomicBoolean warningLogged = new AtomicBoolean();
|
||||
private volatile ContainerEngine containerEngine;
|
||||
|
||||
@Inject
|
||||
public ContainerEngineDetector(DockerClient dockerClient) {
|
||||
this.dockerClient = dockerClient;
|
||||
}
|
||||
|
||||
boolean isPodmanDaemon() {
|
||||
ContainerEngine cached = containerEngine;
|
||||
if (cached != null) {
|
||||
return cached == ContainerEngine.PODMAN;
|
||||
}
|
||||
return detectContainerEngine() == ContainerEngine.PODMAN;
|
||||
}
|
||||
|
||||
private synchronized ContainerEngine detectContainerEngine() {
|
||||
if (containerEngine != null) {
|
||||
return containerEngine;
|
||||
}
|
||||
Version version;
|
||||
try {
|
||||
// The socket's daemon can differ from the JVM's OS or container runtime.
|
||||
version = dockerClient.versionCmd().exec();
|
||||
} catch (RuntimeException e) {
|
||||
if (warningLogged.compareAndSet(false, true)) {
|
||||
LOG.warnv(e, "Could not identify the container engine; retaining the Docker host-gateway mapping");
|
||||
}
|
||||
// A transient probe failure must not permanently classify a Podman daemon as unknown.
|
||||
return ContainerEngine.UNKNOWN;
|
||||
}
|
||||
if (version != null) {
|
||||
List<VersionComponent> components = version.getComponents();
|
||||
List<String> engineNames = Stream.concat(
|
||||
Stream.of(version.getPlatform() == null ? null : version.getPlatform().getName()),
|
||||
components == null ? Stream.empty()
|
||||
: components.stream().filter(Objects::nonNull).map(VersionComponent::getName))
|
||||
.filter(Objects::nonNull)
|
||||
.map(name -> name.toLowerCase(Locale.ROOT))
|
||||
.toList();
|
||||
if (engineNames.stream().anyMatch(name -> name.contains("podman"))) {
|
||||
containerEngine = ContainerEngine.PODMAN;
|
||||
return containerEngine;
|
||||
}
|
||||
if (engineNames.stream().anyMatch(name -> name.contains("docker") || name.contains("moby"))) {
|
||||
containerEngine = ContainerEngine.DOCKER;
|
||||
return containerEngine;
|
||||
}
|
||||
}
|
||||
warnUnknownEngine("no Docker or Podman identity in /version");
|
||||
containerEngine = ContainerEngine.UNKNOWN;
|
||||
return containerEngine;
|
||||
}
|
||||
|
||||
private void warnUnknownEngine(String reason) {
|
||||
if (warningLogged.compareAndSet(false, true)) {
|
||||
LOG.warnv("Could not identify the container engine ({0}); retaining the Docker host-gateway mapping",
|
||||
reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -113,6 +113,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
"com.github.dockerjava.api.model.ThrottlingDataConfig",
|
||||
"com.github.dockerjava.api.model.TmpfsOptions",
|
||||
"com.github.dockerjava.api.model.Ulimit",
|
||||
"com.github.dockerjava.api.model.Version",
|
||||
"com.github.dockerjava.api.model.VersionComponent",
|
||||
"com.github.dockerjava.api.model.VersionPlatform",
|
||||
"com.github.dockerjava.api.model.Volume",
|
||||
"com.github.dockerjava.api.model.VolumeBind",
|
||||
"com.github.dockerjava.api.model.VolumeBinds",
|
||||
|
||||
+1
-69
@@ -3,8 +3,6 @@ package io.github.hectorvent.floci.services.lambda.launcher;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmd;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
@@ -49,7 +47,6 @@ import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -63,7 +60,6 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -81,8 +77,6 @@ import java.util.stream.Stream;
|
||||
@Typed(ContainerLauncher.class)
|
||||
public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
|
||||
private enum ContainerEngine { PODMAN, DOCKER, UNKNOWN }
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ContainerLauncher.class);
|
||||
private static final String TASK_DIR = "/var/task";
|
||||
private static final String RUNTIME_DIR = "/var/runtime";
|
||||
@@ -130,8 +124,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
private final LambdaLayerService layerService;
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
private final LambdaExecutionRoleCredentials executionRoleCredentials;
|
||||
private final AtomicBoolean engineWarningLogged = new AtomicBoolean();
|
||||
private volatile ContainerEngine containerEngine;
|
||||
|
||||
@Inject
|
||||
public ContainerLauncher(ContainerBuilder containerBuilder,
|
||||
@@ -302,12 +294,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
|
||||
"lambda", fn.getFunctionName(), lambdaAccountId, lambdaRegion));
|
||||
List<String> configuredExtraHosts = config.services().lambda().extraHosts().orElse(List.of());
|
||||
boolean hasExplicitHostAlias = Stream.concat(dockerFlags.extraHosts().stream(), configuredExtraHosts.stream())
|
||||
.anyMatch(ContainerLauncher::isHostDockerInternalEntry);
|
||||
if (dockerHostResolver.isLinuxHost() && !hasExplicitHostAlias && !isPodmanDaemon()) {
|
||||
specBuilder.withHostDockerInternalOnLinux();
|
||||
}
|
||||
|
||||
applyDockerFlags(specBuilder, dockerFlags);
|
||||
|
||||
specBuilder.withEmbeddedDns();
|
||||
@@ -327,6 +313,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
}
|
||||
specBuilder.withExtraHost(entry.substring(0, sep), entry.substring(sep + 1));
|
||||
});
|
||||
specBuilder.withHostDockerInternalOnLinux();
|
||||
|
||||
// Whether /var/task is served from a shared read-only volume (large code) or copied
|
||||
// directly into this container (small code). Decided once here so both the spec (below)
|
||||
@@ -575,61 +562,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
.orElseGet(() -> lifecycleManager.create(spec));
|
||||
}
|
||||
|
||||
private boolean isPodmanDaemon() {
|
||||
ContainerEngine cached = containerEngine;
|
||||
if (cached != null) {
|
||||
return cached == ContainerEngine.PODMAN;
|
||||
}
|
||||
return detectContainerEngine() == ContainerEngine.PODMAN;
|
||||
}
|
||||
|
||||
private synchronized ContainerEngine detectContainerEngine() {
|
||||
if (containerEngine != null) {
|
||||
return containerEngine;
|
||||
}
|
||||
try {
|
||||
// The socket's daemon can differ from the JVM's OS or container runtime.
|
||||
Version version = lifecycleManager.getDockerClient().versionCmd().exec();
|
||||
if (version != null) {
|
||||
List<VersionComponent> components = version.getComponents();
|
||||
List<String> engineNames = Stream.concat(
|
||||
Stream.of(version.getPlatform() == null ? null : version.getPlatform().getName()),
|
||||
components == null ? Stream.empty()
|
||||
: components.stream().filter(Objects::nonNull).map(VersionComponent::getName))
|
||||
.filter(Objects::nonNull)
|
||||
.map(name -> name.toLowerCase(Locale.ROOT))
|
||||
.toList();
|
||||
if (engineNames.stream().anyMatch(name -> name.contains("podman"))) {
|
||||
containerEngine = ContainerEngine.PODMAN;
|
||||
return containerEngine;
|
||||
}
|
||||
if (engineNames.stream().anyMatch(name -> name.contains("docker") || name.contains("moby"))) {
|
||||
containerEngine = ContainerEngine.DOCKER;
|
||||
return containerEngine;
|
||||
}
|
||||
}
|
||||
warnUnknownEngine("no Docker or Podman identity in /version");
|
||||
containerEngine = ContainerEngine.UNKNOWN;
|
||||
} catch (RuntimeException e) {
|
||||
warnUnknownEngine(e.toString());
|
||||
return ContainerEngine.UNKNOWN;
|
||||
}
|
||||
return containerEngine;
|
||||
}
|
||||
|
||||
private void warnUnknownEngine(String reason) {
|
||||
if (engineWarningLogged.compareAndSet(false, true)) {
|
||||
LOG.warnv("Could not identify the container engine ({0}); retaining the Docker host-gateway mapping",
|
||||
reason);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isHostDockerInternalEntry(String entry) {
|
||||
int separator = entry.indexOf(':');
|
||||
return separator > 0 && separator < entry.length() - 1
|
||||
&& "host.docker.internal".equalsIgnoreCase(entry.substring(0, separator));
|
||||
}
|
||||
|
||||
private LambdaDockerFlags configuredDockerFlags() {
|
||||
Optional<String> configured = Optional.ofNullable(config.services().lambda().dockerFlags())
|
||||
.orElse(Optional.empty());
|
||||
|
||||
+184
-1
@@ -1,23 +1,204 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.VersionCmd;
|
||||
import com.github.dockerjava.api.model.DeviceRequest;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.DockerDaemonVersionFixtures.stubDaemonVersion;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
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.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ContainerBuilderTest {
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_omitsAutomaticAliasForPodmanDaemon() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "linux/amd64/fedora-42",
|
||||
List.of(new VersionComponent().withName("Podman Engine")));
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec explicitSpec = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withExtraHost("host.docker.internal", "192.0.2.10")
|
||||
.build();
|
||||
|
||||
assertEquals(List.of(), spec.extraHosts());
|
||||
assertEquals(List.of("host.docker.internal:192.0.2.10"), explicitSpec.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_explicitAliasSkipsPodmanProbe() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "Podman Engine", List.of());
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withExtraHost("host.docker.internal", "192.0.2.10")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:192.0.2.10"), spec.extraHosts());
|
||||
verify(fixture.dockerClient, never()).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_emptyExplicitAliasDoesNotSuppressGateway() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
ContainerSpec before = fixture.builder.newContainer("alpine")
|
||||
.withExtraHost("host.docker.internal", "")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec after = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withExtraHost("host.docker.internal", "")
|
||||
.build();
|
||||
|
||||
assertFalse(ContainerBuilder.isHostDockerInternalEntry("host.docker.internal:"));
|
||||
assertTrue(ContainerBuilder.isHostDockerInternalEntry("HOST.DOCKER.INTERNAL:192.0.2.10"));
|
||||
assertTrue(before.extraHosts().contains("host.docker.internal:host-gateway"));
|
||||
assertTrue(after.extraHosts().contains("host.docker.internal:host-gateway"));
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"Docker Engine - Community", "Moby Engine"})
|
||||
void withHostDockerInternalOnLinux_addsAliasOnceForDockerDaemon(String engineName) {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, engineName, List.of());
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(first.extraHosts(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_skipsNonLinuxHostWithoutProbingDaemon() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
|
||||
ContainerSpec spec = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of(), spec.extraHosts());
|
||||
verify(fixture.dockerClient, never()).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_prefersExplicitAliasInEitherOrder() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
ContainerSpec before = fixture.builder.newContainer("alpine")
|
||||
.withExtraHost("HOST.DOCKER.INTERNAL", "192.0.2.10")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec after = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.withExtraHost("host.docker.internal", "192.0.2.11")
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("HOST.DOCKER.INTERNAL:192.0.2.10"), before.extraHosts());
|
||||
assertEquals(List.of("host.docker.internal:192.0.2.11"), after.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_keepsAliasForUnknownDaemonAndCachesResult() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(fixture.dockerClient, "unidentified engine", List.of());
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(first.extraHosts(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_retriesFailedProbeOnNextSpec() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
Version podmanVersion = mock(Version.class);
|
||||
when(fixture.dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenThrow(new IllegalStateException("/version unavailable"))
|
||||
.thenReturn(podmanVersion);
|
||||
when(podmanVersion.getPlatform()).thenReturn(new VersionPlatform().withName("Podman Engine"));
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(List.of(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(2)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withHostDockerInternalOnLinux_keepsAliasForMissingDaemonMetadata() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
when(fixture.dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
when(fixture.dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(new Version());
|
||||
|
||||
ContainerSpec first = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
ContainerSpec second = fixture.builder.newContainer("alpine")
|
||||
.withHostDockerInternalOnLinux()
|
||||
.build();
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), first.extraHosts());
|
||||
assertEquals(first.extraHosts(), second.extraHosts());
|
||||
verify(fixture.dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void withDockerNetwork_usesExplicitServiceNetworkFirst() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
@@ -352,12 +533,14 @@ class ContainerBuilderTest {
|
||||
final EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
final EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);
|
||||
final EmulatorConfig.DnsConfig dns = mock(EmulatorConfig.DnsConfig.class);
|
||||
final DockerClient dockerClient = mock(DockerClient.class);
|
||||
final DockerHostResolver dockerHostResolver = mock(DockerHostResolver.class);
|
||||
final EmbeddedDnsServer embeddedDnsServer = mock(EmbeddedDnsServer.class);
|
||||
final CurrentContainerNetworkResolver currentContainerNetworkResolver =
|
||||
mock(CurrentContainerNetworkResolver.class);
|
||||
final ContainerBuilder builder =
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer, currentContainerNetworkResolver);
|
||||
new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer,
|
||||
currentContainerNetworkResolver, new ContainerEngineDetector(dockerClient));
|
||||
|
||||
TestFixture() {
|
||||
when(config.services()).thenReturn(services);
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class DockerJavaNativeSupportTest {
|
||||
|
||||
@Test
|
||||
void versionCommandResponseTypesAreRegisteredForNativeReflection() {
|
||||
RegisterForReflection registration = DockerJavaNativeSupport.class.getAnnotation(RegisterForReflection.class);
|
||||
assertNotNull(registration);
|
||||
List<String> registered = Arrays.asList(registration.classNames());
|
||||
for (Class<?> type : List.of(Version.class, VersionComponent.class, VersionPlatform.class)) {
|
||||
assertTrue(registered.contains(type.getName()), type.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-47
@@ -7,19 +7,17 @@ import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmd;
|
||||
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
|
||||
import com.github.dockerjava.api.command.ExecStartCmd;
|
||||
import com.github.dockerjava.api.command.VersionCmd;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.AccessMode;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.Mount;
|
||||
import com.github.dockerjava.api.model.MountType;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerEngineDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerReachableEndpoint;
|
||||
@@ -74,6 +72,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.DockerDaemonVersionFixtures.stubDaemonVersion;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -458,7 +457,7 @@ class ContainerLauncherTest {
|
||||
private ContainerLauncher newLauncher(ImageResolver selectedResolver,
|
||||
CurrentContainerNetworkResolver networkResolver) {
|
||||
ContainerBuilder builder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer,
|
||||
networkResolver);
|
||||
networkResolver, new ContainerEngineDetector(dockerClient));
|
||||
ContainerReachableEndpoint endpoint =
|
||||
new ContainerReachableEndpoint(config, dockerHostResolver, embeddedDnsServer, 4566);
|
||||
return new ContainerLauncher(builder, lifecycleManager, logStreamer, selectedResolver,
|
||||
@@ -467,15 +466,6 @@ class ContainerLauncherTest {
|
||||
executionRoleCredentials);
|
||||
}
|
||||
|
||||
private void stubDaemonVersion(String platformName, List<VersionComponent> components) {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
Version version = mock(Version.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(version);
|
||||
when(version.getPlatform()).thenReturn(new VersionPlatform().withName(platformName));
|
||||
when(version.getComponents()).thenReturn(components);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"arm64, true, , linux/arm64, arm64",
|
||||
@@ -654,7 +644,8 @@ class ContainerLauncherTest {
|
||||
when(dockerHostResolver.resolve()).thenReturn("10.89.0.5");
|
||||
lenient().when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
when(config.services().lambda().extraHosts()).thenReturn(Optional.of(List.of("db.internal:10.0.0.5")));
|
||||
stubDaemonVersion("linux/amd64/fedora-42", List.of(new VersionComponent().withName("Podman Engine")));
|
||||
stubDaemonVersion(dockerClient, "linux/amd64/fedora-42",
|
||||
List.of(new VersionComponent().withName("Podman Engine")));
|
||||
|
||||
newLauncher(imageResolver, networkResolver).launch(imageFunction());
|
||||
|
||||
@@ -671,7 +662,7 @@ class ContainerLauncherTest {
|
||||
when(networkResolver.resolveNetworkName()).thenReturn(Optional.of("docker_compose_default"));
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
lenient().when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion("Docker Engine - Community", List.of());
|
||||
stubDaemonVersion(dockerClient, "Docker Engine - Community", List.of());
|
||||
LambdaFunction fn = imageFunction();
|
||||
fn.setEnvironment(Map.of("HOST_SERVICE_URL", "http://host.docker.internal:8080"));
|
||||
|
||||
@@ -688,7 +679,7 @@ class ContainerLauncherTest {
|
||||
void launchFunction_onNativeLinuxDockerAddsHostGatewayForCallback() {
|
||||
when(dockerHostResolver.resolve()).thenReturn("host.docker.internal");
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion("Docker Engine - Community", List.of());
|
||||
stubDaemonVersion(dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
@@ -724,42 +715,14 @@ class ContainerLauncherTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_keepsHostAliasWhenDaemonMetadataIsUnknown() {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(new Version());
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), captureRealContainerSpec().extraHosts());
|
||||
verify(dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_keepsHostAliasWhenVersionProbeFails() {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenThrow(new IllegalStateException("/version unavailable"));
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
void launchFunction_ignoresEmptyHostAliasFromExtraHosts() {
|
||||
when(config.services().lambda().extraHosts()).thenReturn(Optional.of(List.of("host.docker.internal:")));
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion(dockerClient, "Docker Engine - Community", List.of());
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
assertEquals(List.of("host.docker.internal:host-gateway"), captureRealContainerSpec().extraHosts());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_reusesDetectedEngineAcrossInvocations() {
|
||||
when(dockerHostResolver.resolve()).thenReturn("172.18.0.5");
|
||||
when(dockerHostResolver.isLinuxHost()).thenReturn(true);
|
||||
stubDaemonVersion("Docker Engine - Community", List.of());
|
||||
|
||||
launcher.launch(imageFunction());
|
||||
launcher.launch(imageFunction());
|
||||
|
||||
verify(dockerClient, times(1)).versionCmd();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.VersionCmd;
|
||||
import com.github.dockerjava.api.model.Version;
|
||||
import com.github.dockerjava.api.model.VersionComponent;
|
||||
import com.github.dockerjava.api.model.VersionPlatform;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public final class DockerDaemonVersionFixtures {
|
||||
private DockerDaemonVersionFixtures() {
|
||||
}
|
||||
|
||||
public static void stubDaemonVersion(DockerClient dockerClient, String platformName,
|
||||
List<VersionComponent> components) {
|
||||
VersionCmd versionCmd = mock(VersionCmd.class);
|
||||
Version version = mock(Version.class);
|
||||
when(dockerClient.versionCmd()).thenReturn(versionCmd);
|
||||
when(versionCmd.exec()).thenReturn(version);
|
||||
when(version.getPlatform()).thenReturn(new VersionPlatform().withName(platformName));
|
||||
when(version.getComponents()).thenReturn(components);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user