mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(docker): attach link-local addresses to a container's network endpoint (#4063)
Add ContainerSpec.linkLocalIps and ContainerBuilder.withLinkLocalIp so a container can be given link-local IPv4 addresses on its Docker network endpoint. docker-java does not expose NetworkingConfig on create, so the endpoint is reconnected with LinkLocalIPs while the container is still in the created state, before it starts. A failed attach removes the container. Taken from #2987, adapted to the current ContainerSpec and create flow. Co-authored-by: jkvw-del <j.kvw@mejortechnologies.com>
This commit is contained in:
1 parent
d5b7b7f676
commit
30c89986bd
5 files changed
+313
-7
No files matched your search
@@ -145,6 +145,7 @@ public class ContainerBuilder {
|
||||
private String user;
|
||||
private final List<String> groupAdd = new ArrayList<>();
|
||||
private final List<String> dnsServers = new ArrayList<>();
|
||||
private final List<String> linkLocalIps = new ArrayList<>();
|
||||
private final List<DeviceRequest> deviceRequests = new ArrayList<>();
|
||||
|
||||
Builder(String image, EmulatorConfig config, DockerHostResolver dockerHostResolver,
|
||||
@@ -300,6 +301,19 @@ public class ContainerBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a link-local IPv4 address to the container's endpoint on the configured Docker
|
||||
* network, which must be user-defined. Only that one network is supported. The spec is
|
||||
* rejected at {@link #build()} when the address or the network is not valid for this.
|
||||
* Docker does not check that an address is unique on the network, so callers allocate them.
|
||||
*/
|
||||
public Builder withLinkLocalIp(String ip) {
|
||||
if (ip != null && !ip.isBlank() && !linkLocalIps.contains(ip.trim())) {
|
||||
linkLocalIps.add(ip.trim());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Docker network from a service-specific Optional, falling back to
|
||||
* the global services.dockerNetwork() if not present.
|
||||
@@ -642,7 +656,8 @@ public class ContainerBuilder {
|
||||
List.copyOf(deviceRequests),
|
||||
nanoCpus,
|
||||
cpuShares,
|
||||
readonlyRootfs
|
||||
readonlyRootfs,
|
||||
List.copyOf(linkLocalIps)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+56
-1
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common.docker;
|
||||
import io.github.hectorvent.floci.config.ContainerCaBundle;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.CreateContainerCmd;
|
||||
import com.github.dockerjava.api.command.CreateContainerResponse;
|
||||
@@ -21,6 +22,7 @@ import com.github.dockerjava.api.model.Mount;
|
||||
import com.github.dockerjava.api.model.MountType;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import com.github.dockerjava.core.command.WaitContainerResultCallback;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
@@ -134,6 +136,20 @@ public class ContainerLifecycleManager {
|
||||
}
|
||||
|
||||
private String create(ContainerSpec spec, String resolvedImage, String platform) {
|
||||
String containerId = createWithCaBundle(spec, resolvedImage, platform);
|
||||
if (spec.hasNetworkConfiguration()) {
|
||||
try {
|
||||
attachNetworkBeforeStart(containerId, spec);
|
||||
} catch (RuntimeException e) {
|
||||
removeIfExists(containerId);
|
||||
throw new IllegalStateException(
|
||||
"Failed to attach pre-start network configuration for container " + containerId, e);
|
||||
}
|
||||
}
|
||||
return containerId;
|
||||
}
|
||||
|
||||
private String createWithCaBundle(ContainerSpec spec, String resolvedImage, String platform) {
|
||||
LOG.debugv("Creating container from spec: image={0}, name={1}", spec.image(), spec.name());
|
||||
|
||||
// Built once: a dynamic port binding allocates its host port here.
|
||||
@@ -194,6 +210,44 @@ public class ContainerLifecycleManager {
|
||||
return containerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies endpoint IPAM while the container is still in CREATED state. docker-java does not
|
||||
* expose Docker's {@code NetworkingConfig} on create, but it does expose the equivalent
|
||||
* network-connect endpoint, so the container is reconnected before it starts.
|
||||
*/
|
||||
private void attachNetworkBeforeStart(String containerId, ContainerSpec spec) {
|
||||
if (!spec.hasPortBindings()) {
|
||||
dockerClient.disconnectFromNetworkCmd()
|
||||
.withContainerId(containerId)
|
||||
.withNetworkId(spec.networkMode())
|
||||
.exec();
|
||||
}
|
||||
ContainerNetwork endpoint = new ContainerNetwork().withIpamConfig(new LinkLocalIpam(spec.linkLocalIps()));
|
||||
dockerClient.connectToNetworkCmd()
|
||||
.withContainerId(containerId)
|
||||
.withNetworkId(spec.networkMode())
|
||||
.withContainerNetwork(endpoint)
|
||||
.exec();
|
||||
}
|
||||
|
||||
/**
|
||||
* Docker Engine accepts LinkLocalIPs, but docker-java omits the model property. Registered for
|
||||
* reflection because Jackson finds the added getter reflectively in a native image.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
private static final class LinkLocalIpam extends ContainerNetwork.Ipam {
|
||||
private final List<String> linkLocalIps;
|
||||
|
||||
private LinkLocalIpam(List<String> linkLocalIps) {
|
||||
this.linkLocalIps = List.copyOf(linkLocalIps);
|
||||
}
|
||||
|
||||
@JsonProperty("LinkLocalIPs")
|
||||
public List<String> getLinkLocalIps() {
|
||||
return linkLocalIps;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the CA bundle into the created, not yet started, container so runtimes that read
|
||||
* {@code SSL_CERT_FILE} and friends at init find it. A copy rather than a bind mount because
|
||||
@@ -237,7 +291,8 @@ public class ContainerLifecycleManager {
|
||||
startContainer(containerId);
|
||||
LOG.infov("Started container {0}", containerId);
|
||||
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank() && spec.hasPortBindings()) {
|
||||
if (spec.networkMode() != null && !spec.networkMode().isBlank()
|
||||
&& spec.hasPortBindings() && !spec.hasNetworkConfiguration()) {
|
||||
try {
|
||||
dockerClient.connectToNetworkCmd()
|
||||
.withContainerId(containerId)
|
||||
|
||||
@@ -8,6 +8,9 @@ import com.github.dockerjava.api.model.VolumesFrom;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Immutable specification for a Docker container to be created.
|
||||
@@ -39,6 +42,7 @@ import java.util.Map;
|
||||
* @param nanoCpus Hard CPU quota in billionths of a CPU (null = no quota)
|
||||
* @param cpuShares Relative CPU weight against other containers (null = daemon default)
|
||||
* @param readonlyRootfs Whether the container's own filesystem is mounted read only
|
||||
* @param linkLocalIps Link-local IPv4 addresses assigned to the container's endpoint on the configured network
|
||||
*/
|
||||
public record ContainerSpec(
|
||||
String image,
|
||||
@@ -66,8 +70,19 @@ public record ContainerSpec(
|
||||
List<DeviceRequest> deviceRequests,
|
||||
Long nanoCpus,
|
||||
Integer cpuShares,
|
||||
boolean readonlyRootfs
|
||||
boolean readonlyRootfs,
|
||||
List<String> linkLocalIps
|
||||
) {
|
||||
private static final Pattern LINK_LOCAL_IPV4 = Pattern.compile("^169\\.254\\.(\\d{1,3})\\.(\\d{1,3})$");
|
||||
private static final Set<String> NETWORKS_WITHOUT_ENDPOINT_IPAM = Set.of("bridge", "default", "host", "none");
|
||||
|
||||
public ContainerSpec {
|
||||
if (linkLocalIps != null && !linkLocalIps.isEmpty()) {
|
||||
requireUserDefinedNetwork(networkMode);
|
||||
linkLocalIps.forEach(ContainerSpec::requireLinkLocalIpv4);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a minimal spec with just the image name.
|
||||
* All other fields will be null or empty lists.
|
||||
@@ -75,7 +90,7 @@ public record ContainerSpec(
|
||||
public ContainerSpec(String image) {
|
||||
this(image, null, List.of(), null, null, null, Map.of(), List.of(), List.of(), null,
|
||||
List.of(), List.of(), List.of(), List.of(), Map.of(), null, false, null, List.of(),
|
||||
null, null, List.of(), List.of(), null, null, false);
|
||||
null, null, List.of(), List.of(), null, null, false, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,7 +121,7 @@ public record ContainerSpec(
|
||||
) {
|
||||
this(image, name, env, cmd, entrypoint, memoryBytes, portBindings, List.of(), exposedPorts,
|
||||
networkMode, mounts, binds, List.of(), extraHosts, labels, logConfig, privileged,
|
||||
cgroupnsMode, dnsServers, workingDir, user, groupAdd, List.of(), null, null, false);
|
||||
cgroupnsMode, dnsServers, workingDir, user, groupAdd, List.of(), null, null, false, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,7 +155,7 @@ public record ContainerSpec(
|
||||
this(image, name, env, cmd, entrypoint, memoryBytes, portBindings, loopbackPortBindings,
|
||||
exposedPorts, networkMode, mounts, binds, List.of(), extraHosts, labels, logConfig,
|
||||
privileged, cgroupnsMode, dnsServers, workingDir, user, groupAdd, List.of(),
|
||||
null, null, false);
|
||||
null, null, false, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,7 +189,7 @@ public record ContainerSpec(
|
||||
this(image, name, env, cmd, entrypoint, memoryBytes, portBindings, loopbackPortBindings,
|
||||
exposedPorts, networkMode, mounts, binds, List.of(), extraHosts, labels, logConfig,
|
||||
privileged, cgroupnsMode, dnsServers, workingDir, user, groupAdd, deviceRequests,
|
||||
null, null, false);
|
||||
null, null, false, List.of());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,4 +219,35 @@ public record ContainerSpec(
|
||||
public boolean hasDeviceRequests() {
|
||||
return deviceRequests != null && !deviceRequests.isEmpty();
|
||||
}
|
||||
|
||||
private static void requireUserDefinedNetwork(String networkMode) {
|
||||
if (networkMode == null || networkMode.isBlank()
|
||||
|| NETWORKS_WITHOUT_ENDPOINT_IPAM.contains(networkMode) || networkMode.startsWith("container:")) {
|
||||
throw new IllegalArgumentException(
|
||||
"Link-local addresses need a user-defined Docker network, but the network is '"
|
||||
+ networkMode + "'");
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireLinkLocalIpv4(String ip) {
|
||||
Matcher matcher = ip == null ? null : LINK_LOCAL_IPV4.matcher(ip);
|
||||
if (matcher == null || !matcher.matches()
|
||||
|| Integer.parseInt(matcher.group(1)) > 255 || Integer.parseInt(matcher.group(2)) > 255) {
|
||||
throw new IllegalArgumentException("'" + ip + "' is not an IPv4 address in 169.254.0.0/16");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when the container asks for link-local addresses on its network endpoint.
|
||||
*/
|
||||
public boolean hasLinkLocalIps() {
|
||||
return linkLocalIps != null && !linkLocalIps.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true when the configured network must be attached with endpoint settings before start.
|
||||
*/
|
||||
public boolean hasNetworkConfiguration() {
|
||||
return networkMode != null && !networkMode.isBlank() && hasLinkLocalIps();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user