Light, fluffy, and always free - The AWS Local Emulator alternative
feat(eks): program cluster routes from emulated VPC route tables (#4496)
Simone Primarosa50dc610d358a
diff --git a/docs/services/eks.md b/docs/services/eks.mdindex 36cf5bd935..2392dc9820 100644--- a/docs/services/eks.md+++ b/docs/services/eks.md@@ -408,6 +408,7 @@ The derived availability zone and instance ID match the synthetic EC2 node insta| `FLOCI_SERVICES_EKS_POD_IDENTITY_WEBHOOK` | `true` | Register a mutating admission webhook that injects pod identity credentials. Needs `FLOCI_TLS_ENABLED=true` || `FLOCI_SERVICES_EKS_IMDS` | `false` | Enable link-local IMDS (`169.254.169.254`) proxy in cluster containers || `FLOCI_SERVICES_EKS_EMBEDDED_DNS` | `true` | Inject Floci's embedded DNS server into cluster containers for Route 53 private hosted zones and internal name resolution |+| `FLOCI_SERVICES_EKS_VPC_ROUTE_PROGRAMMING` | `true` | Program emulated VPC route table entries into k3s cluster containers |### Kubernetes versions and network configuration@@ -438,6 +439,27 @@ aws --endpoint-url http://localhost:4566 eks create-cluster \Floci validates that `serviceIpv4Cidr` falls within RFC 1918 private address ranges (`10.0.0.0/8`, `172.16.0.0/12`, or `192.168.0.0/16`), has a prefix length between `/12` and `/24`, and does not overlap with the VPC CIDR. When omitted, Floci defaults `serviceIpv4Cidr` to `10.100.0.0/16` (or `172.20.0.0/16` if `10.100.0.0/16` overlaps with the VPC). Internal pod CIDR (`--cluster-cidr`) defaults to `10.42.0.0/16` to avoid collisions with Docker bridge networks.+### VPC route table programming++When a cluster is created in a VPC (`resourcesVpcConfig.vpcId` is specified) and `floci.services.eks.vpc-route-programming` is enabled (`true` by default), Floci inspects the VPC route tables and programs matching routes into the k3s cluster container via `ip route replace <dest> via <gw>`.++- **Association rules**:+ - If the cluster specifies `subnetIds`, Floci resolves the route table explicitly associated with each subnet. Subnets without an explicit association fall back to the VPC main route table. Route tables associated only with other subnets in the VPC are ignored.+ - If the cluster specifies no subnets, the VPC main route table is used.+- **Programmable route targets**:+ - **Instance targets** (`InstanceId`): Resolved to the EC2 container bridge IP or private IP address on the shared Docker network.+ - **Network interface targets** (`NetworkInterfaceId`): Resolved to the ENI private IP address.+- **Ignored and out-of-scope targets**:+ - The default route (`0.0.0.0/0`) is never overridden, preserving the container's external network reachability and Docker bridge routing.+ - Internet gateways (`igw-*`) and NAT gateways (`nat-*`) are out of scope because Docker networking already handles outbound traffic.+ - VPC peering connections (`pcx-*`) and prefix lists are recorded in route table metadata but not programmed into the container kernel routing table.+- **Lifecycle and dynamic updates**:+ - Routes are programmed when a cluster starts or is restored after a restart.+ - An event-driven listener updates active clusters dynamically whenever routes or associations change via `CreateRoute`, `ReplaceRoute`, `DeleteRoute`, `AssociateRouteTable`, or `DisassociateRouteTable`.+- **Fault tolerance**:+ - Route programming is idempotent.+ - If routing commands fail inside the container (for example, if the `ip` tool is missing or returns a non-zero exit code), Floci logs a warning and the cluster proceeds to `ACTIVE` without failing creation.+### Pulling images from Floci ECRImages pushed to the [Floci ECR registry](ecr.md) use `localhost`-based repository URIsdiff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 6cff9ef46a..59ba255020 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -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();}/**diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.javaindex 65137e5ae4..0667b55b5d 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java@@ -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;}- @Injectpublic 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 IPNetworkInterfacePrivateIpAddress 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;}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/VpcRouteTableListener.java b/src/main/java/io/github/hectorvent/floci/services/ec2/VpcRouteTableListener.javanew file mode 100644index 0000000000..31a6cb0cbe--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/VpcRouteTableListener.java@@ -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);+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.javaindex 5c8595f692..9ab43a3855 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java@@ -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 removeddiff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.javaindex 638fa4daa5..814187bc6b 100644--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java@@ -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,26 +20,24 @@ 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.ecr.registry.EcrRegistryManager;-import io.github.hectorvent.floci.services.eks.model.CertificateAuthority;-import io.github.hectorvent.floci.services.eks.model.Cluster;-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.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;+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 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);diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksService.javaindex e5ba8a60b0..9fd6315082 100644--- a/src/main/java/io/github/hectorvent/floci/services/eks/EksService.java+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksService.java@@ -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,diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.javanew file mode 100644index 0000000000..4ca2ea413d--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java@@ -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();+ }+}diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex ae72a79f90..49685089d5 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -650,6 +650,7 @@ floci:irsa-signing-key: true # sign k3s service account tokens with the cluster OIDC keypod-identity-webhook: true # register a mutating webhook that injects pod identity credentials (needs floci.tls.enabled)embedded-dns: true # point cluster containers at Floci's embedded DNS to resolve Route 53 private zones+ vpc-route-programming: true # program VPC routes into the cluster container for associated subnetsmwaa:enabled: truemock: falsediff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.javanew file mode 100644index 0000000000..fa71fb50fd--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingDockerIntegrationTest.java@@ -0,0 +1,141 @@+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.model.Frame;+import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;+import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;+import io.github.hectorvent.floci.core.common.docker.ContainerSpec;+import io.quarkus.test.junit.QuarkusTest;+import jakarta.inject.Inject;+import org.jboss.logging.Logger;+import org.junit.jupiter.api.AfterEach;+import org.junit.jupiter.api.Assumptions;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.nio.charset.StandardCharsets;+import java.util.List;+import java.util.Optional;+import java.util.Set;+import java.util.UUID;+import java.util.concurrent.TimeUnit;++import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertTrue;++@QuarkusTest+class EksVpcRouteProgrammingDockerIntegrationTest {++ private static final Logger LOG = Logger.getLogger(EksVpcRouteProgrammingDockerIntegrationTest.class);+ private static final String TEST_IMAGE = "alpine:3.21";++ @Inject+ DockerClient dockerClient;++ @Inject+ ContainerBuilder containerBuilder;++ @Inject+ ContainerLifecycleManager lifecycleManager;++ private String containerId;++ @BeforeEach+ void requireDocker() {+ boolean dockerAvailable = isDockerAvailable();+ if (!dockerAvailable) {+ LOG.warn("Docker daemon is not available; skipping EksVpcRouteProgrammingDockerIntegrationTest");+ }+ Assumptions.assumeTrue(dockerAvailable, "Docker daemon must be available for VPC route programming integration test");+ }++ @AfterEach+ void tearDown() {+ if (containerId != null) {+ try {+ lifecycleManager.stopAndRemove(containerId, null);+ } catch (Exception e) {+ LOG.warnv("Failed to clean up test container {0}: {1}", containerId, e.getMessage());+ }+ }+ }++ @Test+ void routeProgrammingAppliesAndReconcilesRoutesInsideContainer() throws Exception {+ ContainerSpec spec = containerBuilder.newContainer(TEST_IMAGE)+ .withName("floci-eks-vpc-route-" + UUID.randomUUID().toString().substring(0, 8))+ .withPrivileged(true)+ .withCmd(List.of("sleep", "3600"))+ .build();++ containerId = lifecycleManager.createAndStart(spec).containerId();+ assertNotNull(containerId, "Container ID must not be null");++ // Ensure iproute2 is available in the test fixture+ execInContainer(containerId, new String[]{"sh", "-c",+ "command -v ip >/dev/null 2>&1 || apk add --no-cache iproute2"});++ // 1. Apply a route+ String destinationCidr = "10.123.0.0/16";+ String gwOutput = execInContainer(containerId, new String[]{"sh", "-c",+ "ip route show | awk '/default/ {print $3}' | head -n1"}).trim();+ String gatewayIp = gwOutput.isBlank() ? "172.17.0.1" : gwOutput;+ List<EksVpcRouteProgramming.VpcRouteEntry> entries = List.of(+ new EksVpcRouteProgramming.VpcRouteEntry(destinationCidr, gatewayIp)+ );++ Optional<String[]> cmdOpt = EksVpcRouteProgramming.buildRoutingCommand(entries);+ assertTrue(cmdOpt.isPresent(), "Routing command should be generated");++ String execOutput = execInContainer(containerId, cmdOpt.get());+ LOG.infov("Route programming script output: {0}", execOutput);++ // Verify routing table inside container reflects the new route+ String routesOutput = execInContainer(containerId, new String[]{"sh", "-c", "ip route show"});+ assertTrue(routesOutput.contains(destinationCidr), "Route table must contain " + destinationCidr + ", got: " + routesOutput);+ assertTrue(routesOutput.contains(gatewayIp), "Route table must contain gateway " + gatewayIp + ", got: " + routesOutput);++ // 2. Reconcile / remove the stale route+ Optional<String[]> delCmdOpt = EksVpcRouteProgramming.buildRoutingCommand(List.of(), Set.of(destinationCidr));+ assertTrue(delCmdOpt.isPresent(), "Delete routing command should be generated");++ String delOutput = execInContainer(containerId, delCmdOpt.get());+ LOG.infov("Route deletion script output: {0}", delOutput);++ // Verify route has been removed from the kernel routing table+ String routesAfterDel = execInContainer(containerId, new String[]{"sh", "-c", "ip route show"});+ assertFalse(routesAfterDel.contains(destinationCidr), "Route table should no longer contain " + destinationCidr + ", got: " + routesAfterDel);+ }++ private boolean isDockerAvailable() {+ try {+ dockerClient.pingCmd().exec();+ return true;+ } catch (Exception e) {+ return false;+ }+ }++ private String execInContainer(String containerId, String[] cmd) throws Exception {+ ExecCreateCmdResponse exec = dockerClient.execCreateCmd(containerId)+ .withCmd(cmd)+ .withAttachStdout(true)+ .withAttachStderr(true)+ .exec();++ StringBuilder output = new StringBuilder();+ dockerClient.execStartCmd(exec.getId()).exec(new ResultCallback.Adapter<Frame>() {+ @Override+ public void onNext(Frame item) {+ if (item != null && item.getPayload() != null) {+ output.append(new String(item.getPayload(), StandardCharsets.UTF_8));+ }+ }+ }).awaitCompletion(30, TimeUnit.SECONDS);++ return output.toString();+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingTest.javanew file mode 100644index 0000000000..d080618a93--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgrammingTest.java@@ -0,0 +1,411 @@+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.NetworkInterfaceAttachment;+import io.github.hectorvent.floci.services.ec2.model.NetworkInterfaceListResult;+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.eks.model.Cluster;+import org.junit.jupiter.api.Test;++import java.util.List;+import java.util.Map;+import java.util.Optional;+import java.util.Set;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.ArgumentMatchers.anyInt;+import static org.mockito.ArgumentMatchers.anyList;+import static org.mockito.ArgumentMatchers.anyString;+import static org.mockito.ArgumentMatchers.eq;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.when;++class EksVpcRouteProgrammingTest {++ @Test+ void buildRoutingCommandReturnsEmptyWhenNoRoutes() {+ Optional<String[]> cmdNull = EksVpcRouteProgramming.buildRoutingCommand(null);+ assertTrue(cmdNull.isEmpty());++ Optional<String[]> cmdEmpty = EksVpcRouteProgramming.buildRoutingCommand(List.of());+ assertTrue(cmdEmpty.isEmpty());+ }++ @Test+ void buildRoutingCommandGeneratesIdempotentScript() {+ List<EksVpcRouteProgramming.VpcRouteEntry> entries = List.of(+ new EksVpcRouteProgramming.VpcRouteEntry("10.20.0.0/16", "172.18.0.5"),+ new EksVpcRouteProgramming.VpcRouteEntry("10.30.0.0/16", "172.18.0.6")+ );++ Optional<String[]> cmdOpt = EksVpcRouteProgramming.buildRoutingCommand(entries);+ assertTrue(cmdOpt.isPresent());+ String[] cmd = cmdOpt.get();+ assertEquals(3, cmd.length);+ assertEquals("sh", cmd[0]);+ assertEquals("-c", cmd[1]);++ String script = cmd[2];+ assertTrue(script.contains("command -v ip"));+ assertTrue(script.contains("$IP route replace \"10.20.0.0/16\" via \"172.18.0.5\""));+ assertTrue(script.contains("$IP route replace \"10.30.0.0/16\" via \"172.18.0.6\""));+ assertTrue(script.contains("/run/floci-vpc-routes.txt"));+ }++ @Test+ void buildRoutingCommandGeneratesDeleteForStaleRoutes() {+ List<EksVpcRouteProgramming.VpcRouteEntry> entries = List.of(+ new EksVpcRouteProgramming.VpcRouteEntry("10.20.0.0/16", "172.18.0.5")+ );+ Set<String> staleRoutes = Set.of("10.99.0.0/16", "10.88.0.0/24");++ Optional<String[]> cmdOpt = EksVpcRouteProgramming.buildRoutingCommand(entries, staleRoutes);+ assertTrue(cmdOpt.isPresent());+ String script = cmdOpt.get()[2];++ assertTrue(script.contains("$IP route del \"10.99.0.0/16\""));+ assertTrue(script.contains("$IP route del \"10.88.0.0/24\""));+ assertTrue(script.contains("$IP route replace \"10.20.0.0/16\" via \"172.18.0.5\""));+ assertTrue(script.contains("/run/floci-vpc-routes.txt"));+ }++ @Test+ void rejectsMalformedDestinationAndGatewayToPreventCommandInjection() {+ assertFalse(EksVpcRouteProgramming.isValidIpv4Cidr("10.123.0.0/16; id >/tmp/proof; #"));+ assertFalse(EksVpcRouteProgramming.isValidIpv4Cidr("10.0.0.0/33"));+ assertFalse(EksVpcRouteProgramming.isValidIpv4Cidr("10.0.0.256/16"));+ assertTrue(EksVpcRouteProgramming.isValidIpv4Cidr("10.0.0.0/16"));++ assertFalse(EksVpcRouteProgramming.isValidIpv4Address("172.18.0.5; rm -rf /"));+ assertFalse(EksVpcRouteProgramming.isValidIpv4Address("172.18.0.256"));+ assertTrue(EksVpcRouteProgramming.isValidIpv4Address("172.18.0.5"));++ // When passed into buildRoutingCommand, invalid entries are skipped+ List<EksVpcRouteProgramming.VpcRouteEntry> maliciousEntries = List.of(+ new EksVpcRouteProgramming.VpcRouteEntry("10.123.0.0/16; id >/tmp/proof; #", "172.18.0.5"),+ new EksVpcRouteProgramming.VpcRouteEntry("10.20.0.0/16", "172.18.0.5; cat /etc/passwd")+ );+ Optional<String[]> cmdOpt = EksVpcRouteProgramming.buildRoutingCommand(maliciousEntries);+ assertTrue(cmdOpt.isEmpty());+ }++ @Test+ void findApplicableRouteTablesMatchesExplicitSubnetAssociations() {+ String vpcId = "vpc-test";+ String subnetA = "subnet-a";+ String subnetB = "subnet-b";+ String subnetC = "subnet-c";++ RouteTable mainTable = new RouteTable();+ mainTable.setRouteTableId("rtb-main");+ mainTable.setVpcId(vpcId);+ RouteTableAssociation mainAssoc = new RouteTableAssociation();+ mainAssoc.setMain(true);+ mainTable.setAssociations(List.of(mainAssoc));++ RouteTable customTableA = new RouteTable();+ customTableA.setRouteTableId("rtb-sub-a");+ customTableA.setVpcId(vpcId);+ RouteTableAssociation assocA = new RouteTableAssociation();+ assocA.setSubnetId(subnetA);+ customTableA.setAssociations(List.of(assocA));++ RouteTable customTableC = new RouteTable();+ customTableC.setRouteTableId("rtb-sub-c");+ customTableC.setVpcId(vpcId);+ RouteTableAssociation assocC = new RouteTableAssociation();+ assocC.setSubnetId(subnetC);+ customTableC.setAssociations(List.of(assocC));++ List<RouteTable> allTables = List.of(mainTable, customTableA, customTableC);++ // Cluster has subnets A and B. Subnet A matches customTableA. Subnet B has no explicit table,+ // so it falls back to mainTable. customTableC belongs to subnet C and must NOT be included.+ List<RouteTable> result = EksVpcRouteProgramming.findApplicableRouteTables(+ vpcId, List.of(subnetA, subnetB), allTables);++ assertEquals(2, result.size());+ assertTrue(result.stream().anyMatch(rt -> "rtb-sub-a".equals(rt.getRouteTableId())));+ assertTrue(result.stream().anyMatch(rt -> "rtb-main".equals(rt.getRouteTableId())));+ assertFalse(result.stream().anyMatch(rt -> "rtb-sub-c".equals(rt.getRouteTableId())));+ }++ @Test+ void findApplicableRouteTablesFallsBackToMainWhenClusterHasNoSubnets() {+ String vpcId = "vpc-test";+ RouteTable mainTable = new RouteTable();+ mainTable.setRouteTableId("rtb-main");+ mainTable.setVpcId(vpcId);+ RouteTableAssociation mainAssoc = new RouteTableAssociation();+ mainAssoc.setMain(true);+ mainTable.setAssociations(List.of(mainAssoc));++ RouteTable otherTable = new RouteTable();+ otherTable.setRouteTableId("rtb-other");+ otherTable.setVpcId(vpcId);+ RouteTableAssociation otherAssoc = new RouteTableAssociation();+ otherAssoc.setSubnetId("subnet-other");+ otherTable.setAssociations(List.of(otherAssoc));++ List<RouteTable> result = EksVpcRouteProgramming.findApplicableRouteTables(+ vpcId, List.of(), List.of(mainTable, otherTable));++ assertEquals(1, result.size());+ assertEquals("rtb-main", result.get(0).getRouteTableId());+ }++ @Test+ void resolveProgrammableRoutesResolvesInstancesAndEnisAndSkipsDefaultAndUnsupported() {+ Ec2Service ec2 = mock(Ec2Service.class);+ Cluster cluster = new Cluster();+ cluster.setName("test-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/test-cluster");++ // Mock EC2 instance+ Instance instance = new Instance();+ instance.setInstanceId("i-11111111");+ instance.setContainerBridgeIp("172.18.0.10");+ when(ec2.findInstanceById(eq("123456789012"), eq("i-11111111"))).thenReturn(instance);++ // Mock EC2 network interface attached to instance+ Instance eniInstance = new Instance();+ eniInstance.setInstanceId("i-22222222");+ eniInstance.setContainerBridgeIp("172.18.0.20");+ when(ec2.findInstanceById(eq("123456789012"), eq("i-22222222"))).thenReturn(eniInstance);++ NetworkInterface eni = new NetworkInterface();+ eni.setNetworkInterfaceId("eni-22222222");+ eni.setPrivateIpAddress("172.18.0.20");+ NetworkInterfaceAttachment attachment = new NetworkInterfaceAttachment();+ attachment.setInstanceId("i-22222222");+ attachment.setStatus("attached");+ eni.setAttachment(attachment);++ when(ec2.describeNetworkInterfaces(anyString(), anyString(), anyList(), any()))+ .thenReturn(new NetworkInterfaceListResult(List.of(eni), null));+ when(ec2.describeNetworkInterfaces(anyString(), anyList(), any()))+ .thenReturn(new NetworkInterfaceListResult(List.of(eni), null));++ RouteTable rt = new RouteTable();+ rt.setRouteTableId("rtb-1");++ // 1. Instance route (programmable)+ Route r1 = new Route("10.100.0.0/16", null, "CreateRoute");+ r1.setInstanceId("i-11111111");++ // 2. ENI route (programmable)+ Route r2 = new Route("10.200.0.0/16", null, "CreateRoute");+ r2.setNetworkInterfaceId("eni-22222222");++ // 3. Default route (must be skipped to preserve container egress)+ Route rDefault = new Route("0.0.0.0/0", null, "CreateRoute");+ rDefault.setInstanceId("i-11111111");++ // 4. Prefix list route (skipped)+ Route rPrefix = new Route(null, null, "CreateRoute");+ rPrefix.setDestinationPrefixListId("pl-12345");+ rPrefix.setInstanceId("i-11111111");++ // 5. Internet gateway route (skipped)+ Route rIgw = new Route("10.50.0.0/16", "igw-12345", "CreateRoute");++ // 6. Peering connection route (skipped)+ Route rPcx = new Route("10.60.0.0/16", null, "CreateRoute");+ rPcx.setVpcPeeringConnectionId("pcx-12345");++ rt.setRoutes(List.of(r1, r2, rDefault, rPrefix, rIgw, rPcx));++ List<EksVpcRouteProgramming.VpcRouteEntry> entries =+ EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, List.of(rt), ec2);++ assertEquals(2, entries.size());+ assertTrue(entries.stream().anyMatch(e -> "10.100.0.0/16".equals(e.destinationCidrBlock()) && "172.18.0.10".equals(e.gatewayIp())));+ assertTrue(entries.stream().anyMatch(e -> "10.200.0.0/16".equals(e.destinationCidrBlock()) && "172.18.0.20".equals(e.gatewayIp())));+ }++ @Test+ void resolveProgrammableRoutesSkipsUnattachedEni() {+ Ec2Service ec2 = mock(Ec2Service.class);+ Cluster cluster = new Cluster();+ cluster.setName("test-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/test-cluster");++ // Unattached ENI (has private IP, but no attachment)+ NetworkInterface eni = new NetworkInterface();+ eni.setNetworkInterfaceId("eni-unattached");+ eni.setPrivateIpAddress("172.18.0.99");+ eni.setStatus("available");++ when(ec2.describeNetworkInterfaces(anyString(), anyString(), anyList(), any()))+ .thenReturn(new NetworkInterfaceListResult(List.of(eni), null));++ RouteTable rt = new RouteTable();+ rt.setRouteTableId("rtb-1");+ Route rEni = new Route("10.200.0.0/16", null, "CreateRoute");+ rEni.setNetworkInterfaceId("eni-unattached");+ rt.setRoutes(List.of(rEni));++ List<EksVpcRouteProgramming.VpcRouteEntry> entries =+ EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, List.of(rt), ec2);++ assertTrue(entries.isEmpty(), "Unattached ENI must not be resolved as a route gateway");+ }++ @Test+ void resolveProgrammableRoutesContinuesWhenEniNotFound() {+ Ec2Service ec2 = mock(Ec2Service.class);+ Cluster cluster = new Cluster();+ cluster.setName("test-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/test-cluster");++ Instance instance = new Instance();+ instance.setInstanceId("i-valid");+ instance.setContainerBridgeIp("172.18.0.50");+ when(ec2.findInstanceById(eq("123456789012"), eq("i-valid"))).thenReturn(instance);++ // ENI lookup throws AwsException (deleted ENI)+ when(ec2.describeNetworkInterfaces(anyString(), anyString(), eq(List.of("eni-missing")), any()))+ .thenThrow(new AwsException("InvalidNetworkInterfaceID.NotFound", "ENI not found", 400));++ RouteTable rt = new RouteTable();+ rt.setRouteTableId("rtb-1");+ Route rMissing = new Route("10.200.0.0/16", null, "CreateRoute");+ rMissing.setNetworkInterfaceId("eni-missing");+ Route rValid = new Route("10.201.0.0/16", null, "CreateRoute");+ rValid.setInstanceId("i-valid");+ rt.setRoutes(List.of(rMissing, rValid));++ List<EksVpcRouteProgramming.VpcRouteEntry> entries =+ EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, List.of(rt), ec2);++ assertEquals(1, entries.size(), "Valid instance route must still resolve despite missing ENI");+ assertEquals("10.201.0.0/16", entries.get(0).destinationCidrBlock());+ assertEquals("172.18.0.50", entries.get(0).gatewayIp());+ }++ @Test+ void resolveEniGatewayPrefersEniPrivateIpWhenVpcNetworkActive() {+ Ec2Service ec2 = mock(Ec2Service.class);+ when(ec2.isVpcNetworkEnabled()).thenReturn(true);+ when(ec2.isSecurityGroupEnforcementEnabled()).thenReturn(false);++ Cluster cluster = new Cluster();+ cluster.setName("test-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/test-cluster");++ Instance eniInstance = new Instance();+ eniInstance.setInstanceId("i-22222222");+ eniInstance.setContainerBridgeIp("172.17.0.5");+ when(ec2.findInstanceById(eq("123456789012"), eq("i-22222222"))).thenReturn(eniInstance);++ NetworkInterface eni = new NetworkInterface();+ eni.setNetworkInterfaceId("eni-22222222");+ eni.setPrivateIpAddress("10.0.1.20");+ NetworkInterfaceAttachment attachment = new NetworkInterfaceAttachment();+ attachment.setInstanceId("i-22222222");+ attachment.setStatus("attached");+ eni.setAttachment(attachment);++ when(ec2.describeNetworkInterfaces(anyString(), anyString(), anyList(), any()))+ .thenReturn(new NetworkInterfaceListResult(List.of(eni), null));++ RouteTable rt = new RouteTable();+ rt.setRouteTableId("rtb-1");+ Route rEni = new Route("10.200.0.0/16", null, "CreateRoute");+ rEni.setNetworkInterfaceId("eni-22222222");+ rt.setRoutes(List.of(rEni));++ List<EksVpcRouteProgramming.VpcRouteEntry> entries =+ EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, List.of(rt), ec2);++ assertEquals(1, entries.size());+ assertEquals("10.0.1.20", entries.get(0).gatewayIp());+ }++ @Test+ void resolveEniGatewayFallsBackToInstanceGatewayWhenSecurityGroupEnforcementEnabled() {+ Ec2Service ec2 = mock(Ec2Service.class);+ when(ec2.isVpcNetworkEnabled()).thenReturn(true);+ when(ec2.isSecurityGroupEnforcementEnabled()).thenReturn(true);++ Cluster cluster = new Cluster();+ cluster.setName("test-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/test-cluster");++ Instance eniInstance = new Instance();+ eniInstance.setInstanceId("i-22222222");+ eniInstance.setContainerBridgeIp("172.18.0.20");+ when(ec2.findInstanceById(eq("123456789012"), eq("i-22222222"))).thenReturn(eniInstance);++ NetworkInterface eni = new NetworkInterface();+ eni.setNetworkInterfaceId("eni-22222222");+ eni.setPrivateIpAddress("10.0.1.20");+ NetworkInterfaceAttachment attachment = new NetworkInterfaceAttachment();+ attachment.setInstanceId("i-22222222");+ attachment.setStatus("attached");+ eni.setAttachment(attachment);++ when(ec2.describeNetworkInterfaces(anyString(), anyString(), anyList(), any()))+ .thenReturn(new NetworkInterfaceListResult(List.of(eni), null));++ RouteTable rt = new RouteTable();+ rt.setRouteTableId("rtb-1");+ Route rEni = new Route("10.200.0.0/16", null, "CreateRoute");+ rEni.setNetworkInterfaceId("eni-22222222");+ rt.setRoutes(List.of(rEni));++ List<EksVpcRouteProgramming.VpcRouteEntry> entries =+ EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, List.of(rt), ec2);++ assertEquals(1, entries.size());+ assertEquals("172.18.0.20", entries.get(0).gatewayIp(),+ "Must use instance gateway IP instead of unreachable ENI private IP when security group enforcement is active");+ }++ @Test+ void resolveEniGatewayFallsBackToInstanceGatewayWhenVpcNetworkDisabled() {+ Ec2Service ec2 = mock(Ec2Service.class);+ when(ec2.isVpcNetworkEnabled()).thenReturn(false);++ Cluster cluster = new Cluster();+ cluster.setName("test-cluster");+ cluster.setArn("arn:aws:eks:us-east-1:123456789012:cluster/test-cluster");++ Instance eniInstance = new Instance();+ eniInstance.setInstanceId("i-22222222");+ eniInstance.setContainerBridgeIp("172.17.0.5");+ when(ec2.findInstanceById(eq("123456789012"), eq("i-22222222"))).thenReturn(eniInstance);++ NetworkInterface eni = new NetworkInterface();+ eni.setNetworkInterfaceId("eni-22222222");+ eni.setPrivateIpAddress("10.0.1.20");+ NetworkInterfaceAttachment attachment = new NetworkInterfaceAttachment();+ attachment.setInstanceId("i-22222222");+ attachment.setStatus("attached");+ eni.setAttachment(attachment);++ when(ec2.describeNetworkInterfaces(anyString(), anyString(), anyList(), any()))+ .thenReturn(new NetworkInterfaceListResult(List.of(eni), null));++ RouteTable rt = new RouteTable();+ rt.setRouteTableId("rtb-1");+ Route rEni = new Route("10.200.0.0/16", null, "CreateRoute");+ rEni.setNetworkInterfaceId("eni-22222222");+ rt.setRoutes(List.of(rEni));++ List<EksVpcRouteProgramming.VpcRouteEntry> entries =+ EksVpcRouteProgramming.resolveProgrammableRoutes(cluster, List.of(rt), ec2);++ assertEquals(1, entries.size());+ assertEquals("172.17.0.5", entries.get(0).gatewayIp(),+ "Must use instance gateway IP when VPC networking is disabled");+ }+}