fix(ec2): attach Docker-hosted Floci to VPC networks (#3862)

This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-09-18 10:17:20 -04:00
1 parent fdec637c27
commit c3ec3819ae
4 files changed
+117 -1

No files matched your search

@@ -46,6 +46,14 @@ public class CurrentContainerNetworkResolver {
return resolve().map(CurrentContainerNetwork::name);
}
public Optional<String> resolveContainerId() {
if (!containerDetector.isRunningInContainer()) {
return Optional.empty();
}
String containerId = currentContainerId();
return containerId.isBlank() ? Optional.empty() : Optional.of(containerId);
}
public Optional<String> resolveContainerIp() {
return resolve().map(CurrentContainerNetwork::ipAddress);
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ec2.net;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
@@ -124,6 +125,7 @@ public class VpcNetworkManager {
private final EmulatorConfig config;
private final DockerClient dockerClient;
private final CurrentContainerNetworkResolver currentContainerNetworkResolver;
/** region::vpcId -> binding. Rebuilt from persisted VPCs at startup. */
private final Map<String, VpcBinding> bindings = new ConcurrentHashMap<>();
@@ -143,9 +145,15 @@ public class VpcNetworkManager {
});
@Inject
public VpcNetworkManager(EmulatorConfig config, DockerClient dockerClient) {
public VpcNetworkManager(EmulatorConfig config, DockerClient dockerClient,
CurrentContainerNetworkResolver currentContainerNetworkResolver) {
this.config = config;
this.dockerClient = dockerClient;
this.currentContainerNetworkResolver = currentContainerNetworkResolver;
}
VpcNetworkManager(EmulatorConfig config, DockerClient dockerClient) {
this(config, dockerClient, null);
}
@PreDestroy
@@ -562,6 +570,7 @@ public class VpcNetworkManager {
private String materialise(VpcBinding vpc) {
synchronized (vpc) {
if (vpc.created) {
attachCurrentContainer(vpc, null);
return vpc.networkName;
}
Network existing = null;
@@ -576,6 +585,7 @@ public class VpcNetworkManager {
List<Cidr4> actual = ipamSubnets(existing);
if (actual.contains(vpc.effective)) {
vpc.created = true;
attachCurrentContainer(vpc, existing);
return vpc.networkName;
}
// A leftover under this VPC's own name, routing a range this VPC does not plan
@@ -609,6 +619,7 @@ public class VpcNetworkManager {
.withLabels(networkLabels(vpc))
.exec();
vpc.created = true;
attachCurrentContainer(vpc, null);
LOG.infov("Created Docker network {0} for VPC {1} ({2}){3}",
vpc.networkName, vpc.vpcId, vpc.effective,
vpc.substituted ? " [substituted; declared " + vpc.declared + "]" : "");
@@ -623,6 +634,34 @@ public class VpcNetworkManager {
}
}
private void attachCurrentContainer(VpcBinding vpc, Network network) {
if (currentContainerNetworkResolver == null) {
return;
}
Optional<String> currentContainerId = currentContainerNetworkResolver.resolveContainerId();
if (currentContainerId.isEmpty() || currentContainerId.get().equals(vpc.flociContainerId)) {
return;
}
String containerId = currentContainerId.get();
Map<String, Network.ContainerNetworkConfig> containers = network == null ? null : network.getContainers();
if (containers != null && containers.containsKey(containerId)) {
vpc.flociContainerId = containerId;
return;
}
try {
dockerClient.connectToNetworkCmd()
.withContainerId(containerId)
.withNetworkId(vpc.networkName)
.exec();
vpc.flociContainerId = containerId;
LOG.infov("Attached Floci container {0} to VPC network {1}", containerId, vpc.networkName);
} catch (Exception e) {
LOG.warnv("Could not attach Floci container {0} to VPC network {1}: {2}. ELBv2 and other "
+ "in-process services cannot reach private targets on this VPC.",
containerId, vpc.networkName, e.getMessage());
}
}
/**
* @return false when the stale network is still there afterwards, in which case the caller
* must not adopt it: an instance on the shared bridge with an unreachable private IP
@@ -876,6 +915,7 @@ public class VpcNetworkManager {
final String networkName;
final Map<String, SubnetBinding> subnets = new ConcurrentHashMap<>();
volatile boolean created;
volatile String flociContainerId;
VpcBinding(String region, String vpcId, Cidr4 declared, Cidr4 effective,
boolean substituted, String networkName) {
@@ -55,6 +55,30 @@ class CurrentContainerNetworkResolverTest {
assertTrue(resolver.resolveNetworkName().isEmpty());
}
@Test
void resolveContainerIdReturnsCurrentContainerWhenRunningInDocker() {
DockerClient dockerClient = mock(DockerClient.class);
ContainerDetector containerDetector = mock(ContainerDetector.class);
when(containerDetector.isRunningInContainer()).thenReturn(true);
CurrentContainerNetworkResolver resolver =
new TestResolver(dockerClient, containerDetector, "floci-container");
assertEquals(Optional.of("floci-container"), resolver.resolveContainerId());
}
@Test
void resolveContainerIdReturnsEmptyOutsideDocker() {
DockerClient dockerClient = mock(DockerClient.class);
ContainerDetector containerDetector = mock(ContainerDetector.class);
when(containerDetector.isRunningInContainer()).thenReturn(false);
CurrentContainerNetworkResolver resolver =
new TestResolver(dockerClient, containerDetector, "floci-container");
assertTrue(resolver.resolveContainerId().isEmpty());
}
@Test
void resolvePublishedPort_retriesUntilSuccessfulAndCachesResult() {
DockerClient dockerClient = mock(DockerClient.class);
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ec2.net;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.ConnectToNetworkCmd;
import com.github.dockerjava.api.command.CreateNetworkCmd;
@@ -31,6 +32,7 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.RETURNS_SELF;
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;
@@ -332,6 +334,48 @@ class VpcNetworkManagerTest {
assertTrue(removedNetworks.isEmpty(), "a matching network is reused, not churned");
}
@Test
void connectsCurrentFlociContainerBeforeAttachingInstanceToVpcNetwork() {
CurrentContainerNetworkResolver currentContainerNetworkResolver =
mock(CurrentContainerNetworkResolver.class);
when(currentContainerNetworkResolver.resolveContainerId()).thenReturn(Optional.of("floci-container"));
VpcNetworkManager dockerHostedManager =
new VpcNetworkManager(config, docker, currentContainerNetworkResolver);
String networkName = dockerHostedManager.networkName(REGION, "vpc-1");
existingNetwork(networkName, "10.0.0.0/16", ourLabels("vpc-1", "4650"));
dockerHostedManager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
dockerHostedManager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
String address = dockerHostedManager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();
assertTrue(dockerHostedManager.attach(REGION, "vpc-1", "subnet-a", "instance-1", address).isPresent());
ConnectToNetworkCmd connect = docker.connectToNetworkCmd();
verify(connect).withContainerId("floci-container");
verify(connect).withContainerId("instance-1");
verify(connect, times(2)).withNetworkId(networkName);
}
@Test
void doesNotReconnectCurrentFlociContainerAlreadyAttachedToVpcNetwork() {
CurrentContainerNetworkResolver currentContainerNetworkResolver =
mock(CurrentContainerNetworkResolver.class);
when(currentContainerNetworkResolver.resolveContainerId()).thenReturn(Optional.of("floci-container"));
VpcNetworkManager dockerHostedManager =
new VpcNetworkManager(config, docker, currentContainerNetworkResolver);
String networkName = dockerHostedManager.networkName(REGION, "vpc-1");
existingNetwork(networkName, "10.0.0.0/16", ourLabels("vpc-1", "4650"), Map.of(
"floci-container", mock(Network.ContainerNetworkConfig.class)));
dockerHostedManager.declareVpc(REGION, "vpc-1", "10.0.0.0/16");
dockerHostedManager.declareSubnet(REGION, "vpc-1", "subnet-a", "10.0.1.0/24");
String address = dockerHostedManager.allocatePrivateIp(REGION, "subnet-a").orElseThrow();
assertTrue(dockerHostedManager.attach(REGION, "vpc-1", "subnet-a", "instance-1", address).isPresent());
ConnectToNetworkCmd connect = docker.connectToNetworkCmd();
verify(connect, never()).withContainerId("floci-container");
verify(connect).withContainerId("instance-1");
}
@Test
void recreatesALeftoverNetworkWhoseSubnetIsNotTheRangeThisVpcAllocatesFrom() {
// What a previous run with a different declaration leaves behind: this VPC's own network