feat(eks): program cluster routes from emulated VPC route tables (#4496)

Program static kernel routes in EKS cluster containers from emulated
VPC route tables associated with the cluster subnets or VPC.

- Implement route table resolution for cluster VPC and subnets
- Resolve EC2 instance and network interface targets to reachable IPs
- Generate idempotent routing shell commands using ip route
- Attach cluster containers to the VPC bridge network when present
- Notify active clusters on route table modifications via VpcRouteTableListener
- Add configuration flag floci.services.eks.vpc-route-programming
- Add unit and Docker integration tests for route programming
This commit is contained in:
Simone Primarosa authored and GitHub committed 2026-09-27 20:20:12 -04:00
1 parent 5fc38e33cb
commit 50dc610d35
11 files changed
+1255 -34

No files matched your search

@@ -3180,6 +3180,13 @@ public interface EmulatorConfig {
*/
@WithDefault("true")
boolean embeddedDns();
/**
* When true, programs static routes inside EKS cluster containers from emulated VPC route
* tables associated with the cluster's subnets or VPC.
*/
@WithDefault("true")
boolean vpcRouteProgramming();
}
/**
@@ -115,6 +115,7 @@ import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiPredicate;
import java.util.function.Function;
@@ -252,6 +253,51 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
private jakarta.enterprise.inject.Instance<ClusterNodeInstanceProvider> clusterNodeInstanceProviders;
private ClusterNodeInstanceProvider testClusterNodeInstanceProvider;
private final Ec2VolumeBlockDeviceManager volumeBlockDeviceManager;
private jakarta.enterprise.inject.Instance<VpcRouteTableListener> routeTableListenersInstance;
private final List<VpcRouteTableListener> routeTableListeners = new CopyOnWriteArrayList<>();
public void addRouteTableListener(VpcRouteTableListener listener) {
if (listener != null && !this.routeTableListeners.contains(listener)) {
this.routeTableListeners.add(listener);
}
}
public void setRouteTableListener(VpcRouteTableListener listener) {
this.routeTableListeners.clear();
if (listener != null) {
this.routeTableListeners.add(listener);
}
}
public boolean attachContainerToVpc(String region, String vpcId, String containerId) {
if (vpcNetworkManager != null) {
return vpcNetworkManager.attachContainer(region, vpcId, containerId);
}
return false;
}
public boolean isVpcNetworkEnabled() {
return vpcNetworkManager != null && vpcNetworkManager.enabled();
}
private void notifyRouteTableUpdated(String region, RouteTable routeTable) {
for (VpcRouteTableListener listener : routeTableListeners) {
try {
listener.onRouteTableUpdated(region, routeTable);
} catch (Exception e) {
LOG.warnv("Route table listener failed for {0}: {1}", routeTable.getRouteTableId(), e.getMessage());
}
}
if (routeTableListenersInstance != null && !routeTableListenersInstance.isUnsatisfied()) {
for (VpcRouteTableListener listener : routeTableListenersInstance) {
try {
listener.onRouteTableUpdated(region, routeTable);
} catch (Exception e) {
LOG.warnv("CDI route table listener failed for {0}: {1}", routeTable.getRouteTableId(), e.getMessage());
}
}
}
}
void setClusterNodeInstanceProvider(ClusterNodeInstanceProvider provider) {
this.testClusterNodeInstanceProvider = provider;
@@ -282,7 +328,6 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
this.vpcNetworkManager = vpcNetworkManager;
}
@Inject
public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
Ec2PortForwardManager portForwardManager,
AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,
@@ -297,6 +342,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
this.clusterNodeInstanceProviders = clusterNodeInstanceProviders;
}
@Inject
public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
Ec2PortForwardManager portForwardManager,
AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,
Ec2InstanceTypeCatalog instanceTypeCatalog, StorageFactory storageFactory,
jakarta.enterprise.inject.Instance<RequestContext> requestContextInstance,
VpcNetworkManager vpcNetworkManager, IamService iamService,
jakarta.enterprise.inject.Instance<ClusterNodeInstanceProvider> clusterNodeInstanceProviders,
Ec2VolumeBlockDeviceManager volumeBlockDeviceManager,
jakarta.enterprise.inject.Instance<VpcRouteTableListener> routeTableListenersInstance) {
this(config, containerManager, portForwardManager, amiImageResolver, imageCatalog,
instanceTypeCatalog, storageFactory, requestContextInstance, iamService, volumeBlockDeviceManager);
this.vpcNetworkManager = vpcNetworkManager;
this.clusterNodeInstanceProviders = clusterNodeInstanceProviders;
this.routeTableListenersInstance = routeTableListenersInstance;
}
public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
Ec2PortForwardManager portForwardManager,
AmiImageResolver amiImageResolver, Ec2ImageCatalog imageCatalog,
@@ -3112,11 +3174,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
return resolved;
}
private boolean securityGroupEnforcementEnabled() {
public boolean isSecurityGroupEnforcementEnabled() {
return config.network() != null && config.network().securityGroupEnforcement() != null
&& config.network().securityGroupEnforcement().enabled();
}
private boolean securityGroupEnforcementEnabled() {
return isSecurityGroupEnforcementEnabled();
}
private void reconcileFirewallPolicies(String region) {
if (!securityGroupEnforcementEnabled() || config.services().ec2().mock()) {
return;
@@ -7154,8 +7220,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
public List<RouteTable> describeRouteTables(String region, List<String> routeTableIds, Map<String, List<String>> filters) {
return describeRouteTables(callerAccountId(), region, routeTableIds, filters);
}
public List<RouteTable> describeRouteTables(String accountId, String region, List<String> routeTableIds, Map<String, List<String>> filters) {
ensureDefaultResources(region);
return routeTables.scan(k -> true).stream()
String safeAccount = accountId != null ? accountId : callerAccountId();
List<RouteTable> accountRouteTables = routeTables instanceof AccountAwareStorageBackend<RouteTable> aware
? aware.scanForAccount(safeAccount, k -> true)
: routeTables.scan(k -> true);
return accountRouteTables.stream()
.filter(rt -> rt.getRegion().equals(region))
.filter(rt -> routeTableIds.isEmpty() || routeTableIds.contains(rt.getRouteTableId()))
.filter(rt -> matchesFilters(rt, filters, region))
@@ -7164,10 +7238,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
public void deleteRouteTable(String region, String routeTableId) {
ensureDefaultResources(region);
if (routeTables.get(key(region, routeTableId)).isEmpty()) {
RouteTable rt = routeTables.get(key(region, routeTableId)).orElse(null);
if (rt == null) {
throw new AwsException("InvalidRouteTableID.NotFound", "The route table '" + routeTableId + "' does not exist", 400);
}
routeTables.delete(key(region, routeTableId));
notifyRouteTableUpdated(region, rt);
}
public RouteTableAssociation associateRouteTable(String region, String routeTableId, String subnetId) {
@@ -7187,6 +7263,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
next.add(assoc);
current.setAssociations(next);
routeTables.put(key(region, routeTableId), current);
notifyRouteTableUpdated(region, current);
}
return assoc;
}
@@ -7203,6 +7280,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
next.removeIf(a -> a.getRouteTableAssociationId().equals(associationId));
current.setAssociations(next);
routeTables.put(key(region, current.getRouteTableId()), current);
notifyRouteTableUpdated(region, current);
}
}
}
@@ -7436,6 +7514,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
next.add(route);
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);
notifyRouteTableUpdated(region, current);
}
}
@@ -7515,6 +7594,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
next.set(next.indexOf(existing), replacement);
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);
notifyRouteTableUpdated(region, current);
}
}
@@ -7531,6 +7611,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
destinationPrefixListId));
current.setRoutes(next);
routeTables.put(key(region, routeTableId), current);
notifyRouteTableUpdated(region, current);
}
}
@@ -8891,9 +8972,27 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
);
}
// ─── Network Interfaces ─────────────────────────────────────────────────────
// ─── Network Interfaces ──────────────────────────────────────────────────────────
public NetworkInterfaceListResult describeNetworkInterfaces(String region, List<String> networkInterfaceIds,
Map<String, List<String>> filters) {
return describeNetworkInterfaces(callerAccountId(), region, networkInterfaceIds, filters, 0, null);
}
public NetworkInterfaceListResult describeNetworkInterfaces(String region, List<String> networkInterfaceIds,
Map<String, List<String>> filters,
int maxResults, String nextToken) {
return describeNetworkInterfaces(callerAccountId(), region, networkInterfaceIds, filters, maxResults, nextToken);
}
public NetworkInterfaceListResult describeNetworkInterfaces(String accountId, String region,
List<String> networkInterfaceIds,
Map<String, List<String>> filters) {
return describeNetworkInterfaces(accountId, region, networkInterfaceIds, filters, 0, null);
}
public NetworkInterfaceListResult describeNetworkInterfaces(String accountId, String region,
List<String> networkInterfaceIds,
Map<String, List<String>> filters,
int maxResults, String nextToken) {
// Validate pagination parameters
@@ -8917,9 +9016,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
ensureDefaultResources(region);
String safeAccount = accountId != null ? accountId : callerAccountId();
List<NetworkInterface> result = new ArrayList<>();
Set<String> foundIds = new HashSet<>();
for (Instance inst : instances.scan(k -> true)) {
List<Instance> scopedInstances = instances instanceof AccountAwareStorageBackend<Instance> aware
? aware.scanForAccount(safeAccount, k -> true)
: instances.scan(k -> true);
for (Instance inst : scopedInstances) {
if (!inst.getRegion().equals(region)) continue;
if (inst.getState() != null
&& inst.getState().getName() != null
@@ -8934,7 +9037,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// A standalone ENI attached to this instance is reported from its own record
// below, which is the side that knows its real attach time and its
// deleteOnTermination, both of which the instance-side copy would guess wrong.
if (networkInterfaces.get(key(region, eni.getNetworkInterfaceId())).isPresent()) {
Optional<NetworkInterface> standaloneNi = networkInterfaces instanceof AccountAwareStorageBackend<NetworkInterface> aware
? aware.getForAccount(safeAccount, key(region, eni.getNetworkInterfaceId()))
: networkInterfaces.get(key(region, eni.getNetworkInterfaceId()));
if (standaloneNi.isPresent()) {
continue;
}
foundIds.add(eni.getNetworkInterfaceId());
@@ -8963,7 +9069,10 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// resource. Read the interface's own entry in the tag store instead:
// CreateTags on the eni- id writes it, and so does a RunInstances
// TagSpecification with ResourceType=network-interface.
ni.getTagSet().addAll(tags.get(eni.getNetworkInterfaceId()).orElse(List.of()));
Optional<List<Tag>> tagList = tags instanceof AccountAwareStorageBackend<List<Tag>> aware
? aware.getForAccount(safeAccount, eni.getNetworkInterfaceId())
: tags.get(eni.getNetworkInterfaceId());
ni.getTagSet().addAll(tagList.orElse(List.of()));
NetworkInterfaceAttachment att = new NetworkInterfaceAttachment();
att.setAttachmentId(eni.getAttachmentId());
@@ -8978,7 +9087,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
att.setDeleteOnTermination(true);
ni.setAttachment(att);
// Phase 3: privateIpAddressesSet — primary IP
// Phase 3: privateIpAddressesSet - primary IP
NetworkInterfacePrivateIpAddress primaryIp = new NetworkInterfacePrivateIpAddress();
primaryIp.setPrivateIpAddress(eni.getPrivateIpAddress());
primaryIp.setPrivateDnsName(eni.getPrivateDnsName());
@@ -9007,8 +9116,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
// already surfaced above, an ENI used as an instance's launch-time interface (e.g. via
// RunInstances' NetworkInterface.1.NetworkInterfaceId, the override-default-eni pattern)
// is represented on the instance and would otherwise be double-counted here.
for (NetworkInterface standalone : networkInterfaces.scan(k -> k.startsWith(region + "::"))) {
NetworkInterface ni = releaseIfHostIsGone(region, standalone);
List<NetworkInterface> standaloneInterfaces = networkInterfaces instanceof AccountAwareStorageBackend<NetworkInterface> aware
? aware.scanForAccount(safeAccount, k -> k.startsWith(region + "::"))
: networkInterfaces.scan(k -> k.startsWith(region + "::"));
for (NetworkInterface standalone : standaloneInterfaces) {
NetworkInterface ni = releaseIfHostIsGone(safeAccount, region, standalone);
if (foundIds.contains(ni.getNetworkInterfaceId())) {
continue;
}
@@ -9257,16 +9369,27 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
/** Removes an interface from an instance's own list, the mirror of attaching it. */
private void detachFromInstance(String region, String instanceId, String networkInterfaceId) {
detachFromInstance(callerAccountId(), region, instanceId, networkInterfaceId);
}
private void detachFromInstance(String accountId, String region, String instanceId, String networkInterfaceId) {
if (instanceId == null) {
return;
}
Instance inst = instances.get(key(region, instanceId)).orElse(null);
String safeAccount = accountId != null ? accountId : callerAccountId();
Instance inst = instances instanceof AccountAwareStorageBackend<Instance> aware
? aware.getForAccount(safeAccount, key(region, instanceId)).orElse(null)
: instances.get(key(region, instanceId)).orElse(null);
if (inst == null) {
return;
}
if (inst.getNetworkInterfaces().removeIf(
e -> networkInterfaceId.equals(e.getNetworkInterfaceId()))) {
instances.put(key(region, instanceId), inst);
if (instances instanceof AccountAwareStorageBackend<Instance> aware) {
aware.putForAccount(safeAccount, key(region, instanceId), inst);
} else {
instances.put(key(region, instanceId), inst);
}
}
}
@@ -9347,11 +9470,18 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
* read covers every such path at once, rather than chasing each terminal transition.
*/
private NetworkInterface releaseIfHostIsGone(String region, NetworkInterface ni) {
return releaseIfHostIsGone(callerAccountId(), region, ni);
}
private NetworkInterface releaseIfHostIsGone(String accountId, String region, NetworkInterface ni) {
NetworkInterfaceAttachment attachment = ni.getAttachment();
if (attachment == null || attachment.getInstanceId() == null) {
return ni;
}
Instance host = instances.get(key(region, attachment.getInstanceId())).orElse(null);
String safeAccount = accountId != null ? accountId : (ni.getOwnerId() != null ? ni.getOwnerId() : callerAccountId());
Instance host = instances instanceof AccountAwareStorageBackend<Instance> aware
? aware.getForAccount(safeAccount, key(region, attachment.getInstanceId())).orElse(null)
: instances.get(key(region, attachment.getInstanceId())).orElse(null);
boolean hostIsGone = host == null || host.getState() == null
|| "terminated".equals(host.getState().getName());
if (!hostIsGone) {
@@ -9359,12 +9489,16 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
ni.setAttachment(null);
ni.setStatus("available");
networkInterfaces.put(key(region, ni.getNetworkInterfaceId()), ni);
if (networkInterfaces instanceof AccountAwareStorageBackend<NetworkInterface> aware) {
aware.putForAccount(safeAccount, key(region, ni.getNetworkInterfaceId()), ni);
} else {
networkInterfaces.put(key(region, ni.getNetworkInterfaceId()), ni);
}
// The dead instance has to let go of its copy as well. Releasing only the standalone
// record would leave the terminated instance still reporting the interface, so once it is
// reused two instance records would claim it, and a real one is not the winner by any
// rule DescribeInstances applies.
detachFromInstance(region, attachment.getInstanceId(), ni.getNetworkInterfaceId());
detachFromInstance(safeAccount, region, attachment.getInstanceId(), ni.getNetworkInterfaceId());
return ni;
}
@@ -0,0 +1,11 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
/**
* Listener notified when route tables are updated in EC2 (e.g. routes created, replaced,
* deleted, or associated).
*/
public interface VpcRouteTableListener {
void onRouteTableUpdated(String region, RouteTable routeTable);
}
@@ -1,12 +1,13 @@
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.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Network;
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 jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -537,6 +538,44 @@ public class VpcNetworkManager {
}
}
/**
* Attaches an existing container to the VPC's Docker network without allocating a static
* private IP address (for instance, an EKS cluster container reaching VPC targets).
*
* @return true if the container is attached to the VPC network, false otherwise
*/
public boolean attachContainer(String region, String vpcId, String containerId) {
if (!enabled() || containerId == null || vpcId == null) {
return false;
}
VpcBinding vpc = bindings.get(key(region, vpcId));
if (vpc == null || vpc.effective == null) {
return false;
}
String networkName = materialise(vpc);
if (networkName == null) {
return false;
}
try {
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
if (inspect.getNetworkSettings() != null && inspect.getNetworkSettings().getNetworks() != null) {
if (inspect.getNetworkSettings().getNetworks().containsKey(networkName)) {
return true;
}
}
dockerClient.connectToNetworkCmd()
.withContainerId(containerId)
.withNetworkId(networkName)
.exec();
LOG.infov("Attached container {0} to VPC network {1}", containerId, networkName);
return true;
} catch (Exception e) {
LOG.warnv("Could not attach container {0} to VPC network {1}: {2}",
containerId, networkName, e.getMessage());
return false;
}
}
/**
* Disconnects a container from its VPC network, for a launch that failed after {@link #attach}
* succeeded. Docker holds the endpoint, and so the address, until the container is removed
@@ -1,5 +1,11 @@
package io.github.hectorvent.floci.services.eks;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Frame;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
import io.github.hectorvent.floci.core.common.AwsRegions;
@@ -14,6 +20,16 @@ import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
import io.github.hectorvent.floci.core.common.docker.PortAllocator;
import io.github.hectorvent.floci.core.common.docker.UserDataPipeline;
import io.github.hectorvent.floci.services.ec2.ClusterNodeInstanceProvider;
import io.github.hectorvent.floci.services.ec2.Ec2MetadataProxy;
import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.VpcRouteTableListener;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
import io.github.hectorvent.floci.services.ec2.model.Placement;
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
import io.github.hectorvent.floci.services.eks.model.CertificateAuthority;
import io.github.hectorvent.floci.services.eks.model.Cluster;
@@ -21,19 +37,7 @@ import io.github.hectorvent.floci.services.eks.model.ClusterIdentity;
import io.github.hectorvent.floci.services.eks.model.ClusterOidcKey;
import io.github.hectorvent.floci.services.eks.model.LogSetup;
import io.github.hectorvent.floci.services.eks.model.OidcIdentity;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.ExecCreateCmdResponse;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Frame;
import io.github.hectorvent.floci.services.ec2.ClusterNodeInstanceProvider;
import io.github.hectorvent.floci.services.ec2.Ec2MetadataProxy;
import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
import io.github.hectorvent.floci.services.ec2.model.Placement;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.eks.model.ResourcesVpcConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
@@ -45,6 +49,7 @@ import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
@@ -68,13 +73,14 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Manages the Docker lifecycle of k3s containers for real-mode EKS clusters.
* Not used when {@code floci.services.eks.mock=true}.
*/
@ApplicationScoped
public class EksClusterManager implements ClusterNodeInstanceProvider {
public class EksClusterManager implements ClusterNodeInstanceProvider, VpcRouteTableListener {
private static final Logger LOG = Logger.getLogger(EksClusterManager.class);
private static final int K3S_API_SERVER_PORT = 6443;
@@ -136,6 +142,31 @@ public class EksClusterManager implements ClusterNodeInstanceProvider {
private final Map<String, ClusterNodeRecord> clusterNodeInstances = new ConcurrentHashMap<>();
private final Map<String, Closeable> clusterLogHandles = new ConcurrentHashMap<>();
private final List<Consumer<Instance>> nodeRegistrationListeners = new CopyOnWriteArrayList<>();
private final Map<String, Cluster> activeClusters = new ConcurrentHashMap<>();
@Inject
jakarta.enterprise.inject.Instance<Ec2Service> ec2ServiceInstance;
private Ec2Service ec2Service;
private final Map<String, Set<String>> programmedClusterRoutes = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Object> clusterRouteLocks = new ConcurrentHashMap<>();
private Object routeLockFor(String clusterKey) {
return clusterRouteLocks.computeIfAbsent(clusterKey, k -> new Object());
}
public void setEc2Service(Ec2Service ec2Service) {
this.ec2Service = ec2Service;
}
private Ec2Service ec2Service() {
if (ec2Service != null) {
return ec2Service;
}
if (ec2ServiceInstance != null && !ec2ServiceInstance.isUnsatisfied()) {
return ec2ServiceInstance.get();
}
return null;
}
public void addNodeRegistrationListener(Consumer<Instance> listener) {
this.nodeRegistrationListeners.add(listener);
@@ -455,6 +486,8 @@ public class EksClusterManager implements ClusterNodeInstanceProvider {
registerClusterNodeInstance(cluster, containerId);
configureLinkLocalMetadataEndpoint(cluster, containerId);
configurePodIdentityRelay(cluster, containerId);
activeClusters.put(clusterResourceName(cluster), cluster);
configureVpcRoutes(cluster, containerId);
attachClusterLogs(cluster);
LOG.infov("k3s container {0} started for cluster {1} on port {2} (internal: {3})",
@@ -513,6 +546,8 @@ public class EksClusterManager implements ClusterNodeInstanceProvider {
registerClusterNodeInstance(cluster, info.containerId());
configureLinkLocalMetadataEndpoint(cluster, info.containerId());
configurePodIdentityRelay(cluster, info.containerId());
activeClusters.put(clusterResourceName(cluster), cluster);
configureVpcRoutes(cluster, info.containerId());
attachClusterLogsFromNow(cluster);
LOG.infov("Adopted surviving k3s container {0} for EKS cluster {1} on port {2} (internal: {3})",
@@ -1776,8 +1811,132 @@ public class EksClusterManager implements ClusterNodeInstanceProvider {
}
}
void configureVpcRoutes(Cluster cluster, String containerId) {
if (!config.services().eks().vpcRouteProgramming() || config.services().eks().mock()) {
return;
}
if (cluster == null || containerId == null || cluster.getResourcesVpcConfig() == null) {
return;
}
Ec2Service ec2 = ec2Service();
if (ec2 == null) {
return;
}
String clusterKey = clusterResourceName(cluster);
synchronized (routeLockFor(clusterKey)) {
if (!activeClusters.containsKey(clusterKey)) {
return;
}
try {
ResourcesVpcConfig vpcConfig = cluster.getResourcesVpcConfig();
String vpcId = vpcConfig.getVpcId();
if (vpcId == null || vpcId.isBlank()) {
return;
}
String region = clusterRegion(cluster);
ec2.attachContainerToVpc(region, vpcId, containerId);
String accountId = resolveClusterAccountId(cluster);
List<RouteTable> vpcRouteTables = ec2.describeRouteTables(accountId, region, List.of(), Map.of("vpc-id", List.of(vpcId)));
List<RouteTable> applicable = EksVpcRouteProgramming.findApplicableRouteTables(
vpcId, vpcConfig.getSubnetIds(), vpcRouteTables);
List<EksVpcRouteProgramming.VpcRouteEntry> entries = applicable.isEmpty()
? List.of()
: EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, applicable, ec2);
Set<String> desiredDests = entries.stream()
.map(EksVpcRouteProgramming.VpcRouteEntry::destinationCidrBlock)
.collect(Collectors.toSet());
Set<String> previousDests = programmedClusterRoutes.get(clusterKey);
if (previousDests == null) {
previousDests = inspectProgrammedRoutes(containerId);
}
Set<String> toDelete = new LinkedHashSet<>(previousDests);
toDelete.removeAll(desiredDests);
Optional<String[]> cmd = EksVpcRouteProgramming.buildRoutingCommand(entries, toDelete);
if (cmd.isEmpty()) {
programmedClusterRoutes.put(clusterKey, desiredDests);
return;
}
ContainerExecResult result = execInContainerForResult(containerId, cmd.get(), 30);
if (result.exitCode() != 0) {
LOG.warnv("Could not program VPC routes for EKS cluster {0}: {1}",
cluster.getName(), result.summary());
} else {
programmedClusterRoutes.put(clusterKey, desiredDests);
LOG.infov("Configured VPC routes for EKS cluster {0}: {1} programmed, {2} deleted",
cluster.getName(), entries.size(), toDelete.size());
}
} catch (Exception e) {
LOG.warnv("Could not program VPC routes for EKS cluster {0}: {1}",
cluster.getName(), e.getMessage());
}
}
}
private Set<String> inspectProgrammedRoutes(String containerId) {
try {
String output = execInContainer(containerId,
new String[]{"sh", "-c", "cat /run/floci-vpc-routes.txt 2>/dev/null || true"});
if (output == null || output.isBlank()) {
return Set.of();
}
Set<String> routes = new LinkedHashSet<>();
for (String line : output.split("\\r?\\n")) {
String trimmed = line.trim();
if (EksVpcRouteProgramming.isValidIpv4Cidr(trimmed)) {
routes.add(trimmed);
}
}
return routes;
} catch (Exception e) {
LOG.debugv("Could not inspect programmed routes for container {0}: {1}", containerId, e.getMessage());
return Set.of();
}
}
@Override
public void onRouteTableUpdated(String region, RouteTable routeTable) {
if (!config.services().eks().vpcRouteProgramming() || config.services().eks().mock()) {
return;
}
if (routeTable == null || routeTable.getVpcId() == null) {
return;
}
for (Cluster cluster : activeClusters.values()) {
if (cluster.getResourcesVpcConfig() != null
&& routeTable.getVpcId().equals(cluster.getResourcesVpcConfig().getVpcId())
&& cluster.getContainerId() != null) {
String clusterAccount = resolveClusterAccountId(cluster);
if (routeTable.getOwnerId() != null && clusterAccount != null
&& !routeTable.getOwnerId().equals(clusterAccount)) {
continue;
}
try {
configureVpcRoutes(cluster, cluster.getContainerId());
} catch (Exception e) {
LOG.warnv("Could not update VPC routes for EKS cluster {0}: {1}",
cluster.getName(), e.getMessage());
}
}
}
}
void unregisterMetadataEndpoint(Cluster cluster) {
ClusterNodeRecord record = clusterNodeInstances.remove(clusterResourceName(cluster));
String clusterKey = clusterResourceName(cluster);
Object lock = clusterRouteLocks.get(clusterKey);
if (lock != null) {
synchronized (lock) {
activeClusters.remove(clusterKey);
programmedClusterRoutes.remove(clusterKey);
clusterRouteLocks.remove(clusterKey, lock);
}
} else {
activeClusters.remove(clusterKey);
programmedClusterRoutes.remove(clusterKey);
}
ClusterNodeRecord record = clusterNodeInstances.remove(clusterKey);
Instance nodeInstance = record != null ? record.instance() : null;
if (metadataServer != null && nodeInstance != null) {
metadataServer.unregisterInstance(nodeInstance);
@@ -126,6 +126,9 @@ public class EksService implements TagHandler, ResourceProvider {
this.accessEntries = accessEntries;
this.podIdentityAssociations = podIdentityAssociations;
this.addons = addons;
if (this.clusterManager != null) {
this.clusterManager.setEc2Service(this.ec2Service);
}
}
public EksService(StorageFactory storageFactory, EmulatorConfig config,
@@ -0,0 +1,293 @@
package io.github.hectorvent.floci.services.eks;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.NetworkInterface;
import io.github.hectorvent.floci.services.ec2.model.Route;
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
import io.github.hectorvent.floci.services.ec2.model.RouteTableAssociation;
import io.github.hectorvent.floci.services.ec2.net.Cidr4;
import io.github.hectorvent.floci.services.eks.model.Cluster;
import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
public final class EksVpcRouteProgramming {
private static final Logger LOG = Logger.getLogger(EksVpcRouteProgramming.class);
private EksVpcRouteProgramming() {}
public record VpcRouteEntry(String destinationCidrBlock, String gatewayIp) {}
public static boolean isValidIpv4Cidr(String cidr) {
return cidr != null && !cidr.contains(" ") && Cidr4.parse(cidr.trim()).isPresent();
}
public static boolean isValidIpv4Address(String ip) {
return ip != null && !ip.contains(" ") && Cidr4.parse(ip.trim() + "/32").isPresent();
}
public static Optional<String[]> buildRoutingCommand(List<VpcRouteEntry> routes) {
return buildRoutingCommand(routes, Collections.emptyList());
}
public static Optional<String[]> buildRoutingCommand(List<VpcRouteEntry> routesToApply,
Collection<String> routesToDelete) {
boolean hasApply = routesToApply != null && !routesToApply.isEmpty();
boolean hasDelete = routesToDelete != null && !routesToDelete.isEmpty();
if (!hasApply && !hasDelete) {
return Optional.empty();
}
StringBuilder sb = new StringBuilder();
sb.append("set -eu\n");
sb.append("IP=$(command -v ip 2>/dev/null || command -v /sbin/ip 2>/dev/null || command -v /bin/ip 2>/dev/null || true)\n");
sb.append("if [ -z \"$IP\" ]; then\n");
sb.append(" echo 'ip command not found in container' >&2\n");
sb.append(" exit 1\n");
sb.append("fi\n");
boolean actionAdded = false;
if (hasDelete) {
for (String dest : routesToDelete) {
if (isValidIpv4Cidr(dest)) {
sb.append("if [ -n \"$($IP route show \"").append(dest).append("\" 2>/dev/null)\" ]; then\n");
sb.append(" $IP route del \"").append(dest).append("\"\n");
sb.append("fi\n");
actionAdded = true;
}
}
}
if (hasApply) {
for (VpcRouteEntry entry : routesToApply) {
if (entry != null && isValidIpv4Cidr(entry.destinationCidrBlock())
&& isValidIpv4Address(entry.gatewayIp())) {
sb.append("$IP route replace \"").append(entry.destinationCidrBlock())
.append("\" via \"").append(entry.gatewayIp()).append("\"\n");
actionAdded = true;
}
}
}
if (!actionAdded) {
return Optional.empty();
}
sb.append("mkdir -p /run\n");
sb.append("cat << 'EOF' > /run/floci-vpc-routes.txt\n");
if (hasApply) {
for (VpcRouteEntry entry : routesToApply) {
if (entry != null && isValidIpv4Cidr(entry.destinationCidrBlock())
&& isValidIpv4Address(entry.gatewayIp())) {
sb.append(entry.destinationCidrBlock()).append("\n");
}
}
}
sb.append("EOF\n");
return Optional.of(new String[]{"sh", "-c", sb.toString().trim()});
}
public static List<RouteTable> findApplicableRouteTables(String vpcId, List<String> subnetIds, List<RouteTable> vpcRouteTables) {
if (vpcId == null || vpcId.isBlank() || vpcRouteTables == null || vpcRouteTables.isEmpty()) {
return List.of();
}
RouteTable mainTable = vpcRouteTables.stream()
.filter(rt -> rt.getAssociations() != null && rt.getAssociations().stream().anyMatch(RouteTableAssociation::isMain))
.findFirst()
.orElse(null);
if (subnetIds == null || subnetIds.isEmpty()) {
return mainTable != null ? List.of(mainTable) : List.of();
}
Set<RouteTable> result = new LinkedHashSet<>();
for (String subnetId : subnetIds) {
RouteTable explicit = vpcRouteTables.stream()
.filter(rt -> rt.getAssociations() != null && rt.getAssociations().stream()
.anyMatch(a -> !a.isMain() && subnetId.equals(a.getSubnetId())))
.findFirst()
.orElse(null);
if (explicit != null) {
result.add(explicit);
} else if (mainTable != null) {
result.add(mainTable);
}
}
return new ArrayList<>(result);
}
public static List<VpcRouteEntry> resolveProgrammableRoutes(Cluster cluster, List<RouteTable> applicableTables, Ec2Service ec2Service) {
if (cluster == null || applicableTables == null || applicableTables.isEmpty() || ec2Service == null) {
return List.of();
}
String accountId = clusterAccountId(cluster);
String region = clusterRegion(cluster);
Map<String, String> resolved = new LinkedHashMap<>();
for (RouteTable table : applicableTables) {
if (table.getRoutes() == null) {
continue;
}
for (Route route : table.getRoutes()) {
String dest = route.getDestinationCidrBlock();
if (dest == null || dest.isBlank() || "0.0.0.0/0".equals(dest) || "default".equalsIgnoreCase(dest)) {
continue;
}
if (!isValidIpv4Cidr(dest)) {
LOG.warnv("Cannot program route for EKS cluster {0}: invalid IPv4 CIDR destination {1}",
cluster.getName(), dest);
continue;
}
String gatewayIp = null;
if (isSet(route.getInstanceId())) {
Instance targetInst = accountId != null
? ec2Service.findInstanceById(accountId, route.getInstanceId())
: ec2Service.findInstanceById(route.getInstanceId());
if (targetInst == null) {
LOG.warnv("Cannot program route {0} for EKS cluster {1}: target instance {2} not found",
dest, cluster.getName(), route.getInstanceId());
continue;
}
gatewayIp = resolveInstanceGatewayIp(targetInst);
if (!isSet(gatewayIp)) {
LOG.warnv("Cannot program route {0} for EKS cluster {1}: target instance {2} has no IP address",
dest, cluster.getName(), route.getInstanceId());
continue;
}
} else if (isSet(route.getNetworkInterfaceId())) {
try {
NetworkInterface ni = ec2Service.describeNetworkInterfaces(accountId, region,
List.of(route.getNetworkInterfaceId()), Map.<String, List<String>>of())
.networkInterfaces().stream().findFirst().orElse(null);
if (ni == null) {
LOG.warnv("Cannot program route {0} for EKS cluster {1}: target ENI {2} not found",
dest, cluster.getName(), route.getNetworkInterfaceId());
continue;
}
gatewayIp = resolveEniGatewayIp(ni, ec2Service, accountId);
if (!isSet(gatewayIp)) {
LOG.warnv("Cannot program route {0} for EKS cluster {1}: target ENI {2} has no reachable IP address",
dest, cluster.getName(), route.getNetworkInterfaceId());
continue;
}
} catch (AwsException e) {
LOG.warnv("Cannot program route {0} for EKS cluster {1}: target ENI {2} not found: {3}",
dest, cluster.getName(), route.getNetworkInterfaceId(), e.getMessage());
continue;
}
}
if (gatewayIp != null && isValidIpv4Address(gatewayIp)) {
String canonicalDest = Cidr4.parse(dest).map(Cidr4::toString).orElse(dest.trim());
resolved.put(canonicalDest, gatewayIp);
} else if (gatewayIp != null) {
LOG.warnv("Cannot program route {0} for EKS cluster {1}: invalid gateway IP {2}",
dest, cluster.getName(), gatewayIp);
}
}
}
List<VpcRouteEntry> entries = new ArrayList<>();
resolved.forEach((dest, gw) -> entries.add(new VpcRouteEntry(dest, gw)));
return entries;
}
static String resolveInstanceGatewayIp(Instance instance) {
if (instance == null) {
return null;
}
if (instance.getState() != null) {
String state = instance.getState().getName();
if ("terminated".equalsIgnoreCase(state) || "shutting-down".equalsIgnoreCase(state)) {
return null;
}
}
if (isSet(instance.getContainerBridgeIp())) {
return instance.getContainerBridgeIp();
}
if (isSet(instance.getPrivateIpAddress())) {
return instance.getPrivateIpAddress();
}
return instance.getImdsSourceIp();
}
static String resolveEniGatewayIp(NetworkInterface ni, Ec2Service ec2Service, String accountId) {
if (ni == null) {
return null;
}
// An unattached ENI has no reachable host container
if (ni.getAttachment() == null
|| !isSet(ni.getAttachment().getInstanceId())
|| (ni.getAttachment().getStatus() != null && !"attached".equalsIgnoreCase(ni.getAttachment().getStatus()))) {
return null;
}
String instanceId = ni.getAttachment().getInstanceId();
Instance niInst = accountId != null
? ec2Service.findInstanceById(accountId, instanceId)
: ec2Service.findInstanceById(instanceId);
if (niInst == null) {
return null;
}
if (niInst.getState() != null) {
String state = niInst.getState().getName();
if ("terminated".equalsIgnoreCase(state) || "shutting-down".equalsIgnoreCase(state)) {
return null;
}
}
// When security-group enforcement is enabled, the target instance runs in a firewall
// helper's network namespace and is not attached to the VPC network at the ENI's private IP.
// In that case, or when VPC networking is disabled, the ENI's private IP is unreachable
// on the VPC network and the instance's reachable gateway IP must be used.
boolean vpcNetworkActive = ec2Service != null
&& ec2Service.isVpcNetworkEnabled()
&& !ec2Service.isSecurityGroupEnforcementEnabled();
if (vpcNetworkActive && isSet(ni.getPrivateIpAddress())) {
return ni.getPrivateIpAddress();
}
return resolveInstanceGatewayIp(niInst);
}
private static String clusterAccountId(Cluster cluster) {
if (cluster == null) {
return null;
}
if (isSet(cluster.getAccountId())) {
return cluster.getAccountId();
}
if (isSet(cluster.getArn())) {
String[] parts = cluster.getArn().split(":");
if (parts.length > 4 && isSet(parts[4])) {
return parts[4];
}
}
return null;
}
private static String clusterRegion(Cluster cluster) {
if (cluster != null && cluster.getArn() != null) {
String[] parts = cluster.getArn().split(":");
if (parts.length > 3 && !parts[3].isBlank()) {
return parts[3];
}
}
return "us-east-1"; // partition-literal: fallback only when the record carries no region; no resolver in scope (follow-up)
}
private static boolean isSet(String s) {
return s != null && !s.isBlank();
}
}