diff --git a/docs/services/eks.md b/docs/services/eks.md index 08f2827e84..2349a69f44 100644 --- a/docs/services/eks.md +++ b/docs/services/eks.md @@ -486,6 +486,15 @@ run. `floci.services.eks.max-memory-mib` and `floci.services.eks.max-vcpus` prov ceilings; zero means no extra ceiling. Their environment variables are `FLOCI_SERVICES_EKS_MAX_MEMORY_MIB` and `FLOCI_SERVICES_EKS_MAX_VCPUS`. +Those ceilings can only lower the node. To give a workload more room locally than its node group +asks for on AWS, set `floci.services.eks.node-instance-type` (`FLOCI_SERVICES_EKS_NODE_INSTANCE_TYPE`) +to a catalog type such as `t2.2xlarge`. The node container's memory and CPU and its kubelet +reservations are then sized from that type (k3s `maxPods` does not change), while +`DescribeNodegroup`, the node's `node.kubernetes.io/instance-type` label and the synthesized EC2 +instance keep the requested type, so the same Terraform plans no changes. The two ceilings still apply on top. A type absent from the catalog is ignored with a +warning. Changing the setting recreates a surviving node on the next start, keeping its data volume. +This setting is Floci-only; it has no AWS equivalent. + The same calculation sets kubelet's `kube-reserved`, `system-reserved`, and `eviction-hard` flags. The EKS AMI reserves `255 + 11 * maxPods` MiB and a tiered CPU fraction for kubelet and the container runtime. Floci derives `maxPods` from the catalog's primary network card and IPv4 @@ -597,6 +606,7 @@ does not enable it; omitted or false values keep the simulated behavior. | `FLOCI_SERVICES_EKS_MOCK` | `false` | Metadata-only mode (no Docker) | | `FLOCI_SERVICES_EKS_MAX_MEMORY_MIB` | `0` | Optional k3s container memory ceiling in MiB | | `FLOCI_SERVICES_EKS_MAX_VCPUS` | `0` | Optional k3s container vCPU ceiling | +| `FLOCI_SERVICES_EKS_NODE_INSTANCE_TYPE` | *(unset)* | Catalog instance type the k3s node container is sized from; the API keeps the node group's type | | `FLOCI_SERVICES_EKS_DEFAULT_IMAGE` | `rancher/k3s:latest` | k3s Docker image fallback | | `FLOCI_SERVICES_EKS_IMAGE_TEMPLATE` | *(unset)* | Format string for custom k3s images (e.g. `myregistry.io/k3s:v%s`), taking cluster version | | `FLOCI_SERVICES_EKS_API_SERVER_BASE_PORT` | `6500` | First port in the k3s API server range | @@ -771,7 +781,7 @@ services: ``` ## Instance Metadata Service (IMDS) -The cluster container is registered as a synthesized EC2 instance node (image `ami-eks-k3s`) regardless of whether IMDS is enabled, making it visible to the EC2 service (`DescribeInstances`, `DescribeInstanceStatus`, volume attachments, tagging). Its instance type is the same type used for the container limits. +The cluster container is registered as a synthesized EC2 instance node (image `ami-eks-k3s`) regardless of whether IMDS is enabled, making it visible to the EC2 service (`DescribeInstances`, `DescribeInstanceStatus`, volume attachments, tagging). Its instance type is the node group's type, which also sizes the container unless `floci.services.eks.node-instance-type` is set. When IMDS is enabled (`FLOCI_SERVICES_EKS_IMDS=true`), each k3s cluster container exposes the AWS Instance Metadata Service on the link-local address `169.254.169.254:80`. Inside the container, Floci adds `169.254.169.254/32` to the loopback interface (`lo`) and runs a lightweight `socat` TCP relay forwarding metadata requests to Floci's IMDS server. diff --git a/src/main/java/io/floci/aws/config/EmulatorConfig.java b/src/main/java/io/floci/aws/config/EmulatorConfig.java index 5104cdbce7..90e22c17d1 100644 --- a/src/main/java/io/floci/aws/config/EmulatorConfig.java +++ b/src/main/java/io/floci/aws/config/EmulatorConfig.java @@ -3755,6 +3755,14 @@ public interface EmulatorConfig { @WithDefault("0") int maxVcpus(); + /** + * Optional EC2 catalog instance type the k3s node container is sized from, in place of its + * first node group's type. The node group, the node's {@code node.kubernetes.io/instance-type} + * label and the synthesized EC2 instance keep the requested type. A type absent from the + * catalog is ignored with a warning; {@link #maxMemoryMib()} and {@link #maxVcpus()} still apply. + */ + Optional nodeInstanceType(); + @WithDefault("k3s") String provider(); diff --git a/src/main/java/io/floci/aws/services/eks/EksClusterManager.java b/src/main/java/io/floci/aws/services/eks/EksClusterManager.java index 0dc65f537c..f70d75bbbf 100644 --- a/src/main/java/io/floci/aws/services/eks/EksClusterManager.java +++ b/src/main/java/io/floci/aws/services/eks/EksClusterManager.java @@ -1152,7 +1152,7 @@ public class EksClusterManager private EksNodeCapacity.Limits resolveNodeCapacity(Cluster cluster) { try { Info host = lifecycleManager.getDockerClient().infoCmd().exec(); - CatalogInstanceType type = instanceTypeCatalog.find(nodeInstanceType(cluster)).orElseThrow(); + CatalogInstanceType type = instanceTypeCatalog.find(sizingInstanceType(cluster)).orElseThrow(); EksNodeCapacity.Limits limits = EksNodeCapacity.calculate(type, host.getMemTotal(), host.getNCPU(), config.services().eks().maxMemoryMib(), config.services().eks().maxVcpus()); if (limits == null) { @@ -1181,7 +1181,7 @@ public class EksClusterManager if (maxMemoryMib <= 0 && maxVcpus <= 0) { return null; } - int vcpus = instanceTypeCatalog.find(nodeInstanceType(cluster)) + int vcpus = instanceTypeCatalog.find(sizingInstanceType(cluster)) .map(type -> maxVcpus > 0 ? Math.min(type.vcpu, maxVcpus) : 0) .orElse(maxVcpus); LOG.warnv("Applying explicit EKS cluster {0} ceiling without kubelet reservations" @@ -1189,17 +1189,45 @@ public class EksClusterManager return EksNodeCapacity.explicitCeilingWithoutHostInfo(maxMemoryMib, vcpus); } - private static String capacityLabel(Cluster cluster, EksNodeCapacity.Limits limits) { + /** + * Carries the sizing type only when it is configured, so a survivor created without the + * setting keeps its label and is not recreated after an upgrade. Changing the setting changes + * the label, which recreates the node on the next start with its data volume. + */ + private String capacityLabel(Cluster cluster, EksNodeCapacity.Limits limits) { + String types = nodeInstanceType(cluster) + configuredSizingInstanceType().map(type -> "@" + type).orElse(""); if (limits == null) { - return nodeInstanceType(cluster) + ":unbounded"; + return types + ":unbounded"; } - return nodeInstanceType(cluster) + ":" + limits.memoryBytes() + ":" + limits.vcpus() + return types + ":" + limits.memoryBytes() + ":" + limits.vcpus() + ":" + limits.systemMemoryMib() + ":" + limits.kubeMemoryMib() + ":" + limits.evictionMemoryMib() + ":" + limits.systemCpuMilli() + ":" + limits.kubeCpuMilli() + ":" + limits.reducedReservations() + ":" + limits.kubeletArgsEnabled(); } + /** + * The catalog type the node container is sized from: {@code floci.services.eks.node-instance-type} + * when set, otherwise the node group's. Only the container's memory, CPU and kubelet reservations + * follow it; the node group, the node's instance-type label and the synthesized EC2 instance keep + * {@link #nodeInstanceType(Cluster)}. + */ + String sizingInstanceType(Cluster cluster) { + return configuredSizingInstanceType().orElseGet(() -> nodeInstanceType(cluster)); + } + + private Optional configuredSizingInstanceType() { + Optional configured = config.services().eks().nodeInstanceType() + .map(String::trim) + .filter(type -> !type.isEmpty()); + if (configured.isEmpty() || instanceTypeCatalog.find(configured.get()).isPresent()) { + return configured; + } + LOG.warnv("floci.services.eks.node-instance-type {0} is absent from the EC2 catalog; sizing EKS nodes " + + "from their node group's instance type", configured.get()); + return Optional.empty(); + } + static String nodeInstanceType(Cluster cluster) { return cluster != null && cluster.getNodeInstanceType() != null ? cluster.getNodeInstanceType() : DEFAULT_NODE_INSTANCE_TYPE; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f541ba51a4..72797934db 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -854,6 +854,7 @@ floci: mock: false max-memory-mib: 0 # optional k3s container memory ceiling; 0 follows the node type max-vcpus: 0 # optional k3s container CPU ceiling; 0 follows the node type + # node-instance-type: t2.2xlarge # optional catalog type to size the k3s node from; the API keeps the node group's type provider: k3s default-image: "rancher/k3s:latest" # image-template: "myregistry.io/k3s:v%s" # optional custom image format template taking cluster version diff --git a/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java b/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java index 822ba50b8c..6fe5d2af7d 100644 --- a/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/floci/aws/services/eks/EksClusterManagerTest.java @@ -833,6 +833,36 @@ class EksClusterManagerTest { assertEquals("https://aws.example.test:6440", cluster.getEndpoint()); } + @Test + void configuringTheSizingTypeReplacesASurvivorKeepingItsVolume() { + when(config.services().eks().nodeInstanceType()).thenReturn(Optional.of("t2.2xlarge")); + when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(survivingContainer("cid-old"))); + when(lifecycleManager.adopt(eq("cid-old"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-old", Map.of(), Map.of(6443, 6440))); + stubFreshStart("cid-new", 6440); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-new", cluster.getContainerId()); + verify(lifecycleManager, never()).removeVolume(anyString()); + } + + @Test + void aSurvivorSizedFromTheConfiguredTypeIsKept() { + when(config.services().eks().nodeInstanceType()).thenReturn(Optional.of("t2.2xlarge")); + when(lifecycleManager.findByName("floci-eks-demo")).thenReturn(Optional.of(containerFromJson( + "{\"Id\":\"cid-1\",\"Labels\":{\"io.floci.eks.node-capacity\":\"m5.large@t2.2xlarge:unbounded\"}}"))); + when(lifecycleManager.adopt(eq("cid-1"), eq(List.of(6443)), any())) + .thenReturn(new ContainerInfo("cid-1", Map.of(), Map.of(6443, 6440))); + + Cluster cluster = cluster(); + manager.restoreCluster(cluster); + + assertEquals("cid-1", cluster.getContainerId()); + verify(lifecycleManager, never()).create(any()); + } + @Test void networkModeIgnoresTheEndpointHost() { when(containerDetector.isRunningInContainer()).thenReturn(true); @@ -3275,6 +3305,66 @@ class EksClusterManagerTest { cluster, "172.17.0.2", "us-east-1", "000000000000").getInstanceType()); } + @Test + void configuredSizingTypeSizesTheContainerButNotWhatTheApiReports() { + when(eks.nodeInstanceType()).thenReturn(Optional.of(" t2.2xlarge ")); + Info host = stubDockerHost(64L * 1024 * 1024 * 1024, 16); + + Cluster cluster = new Cluster(); + cluster.setName("sized-cluster"); + cluster.setNodeInstanceType(manager.selectNodeInstanceType("t3.small")); + manager.startCluster(cluster); + + Ec2InstanceTypeCatalog catalog = new Ec2InstanceTypeCatalog(); + EksNodeCapacity.Limits limits = EksNodeCapacity.calculate( + catalog.find("t2.2xlarge").orElseThrow(), host.getMemTotal(), host.getNCPU(), 0, 0); + verify(builder).withMemoryBytes(limits.memoryBytes()); + verify(builder).withCpuUnits(limits.vcpus() * 1024); + assertEquals("t3.small", cluster.getNodeInstanceType()); + assertEquals("t3.small", manager.synthesizeClusterNodeInstance( + cluster, "172.17.0.2", "us-east-1", "000000000000").getInstanceType()); + assertEquals("t2.2xlarge", manager.sizingInstanceType(cluster)); + } + + @Test + void ceilingsStillApplyOnTopOfTheConfiguredSizingType() { + when(eks.nodeInstanceType()).thenReturn(Optional.of("t2.2xlarge")); + when(eks.maxMemoryMib()).thenReturn(4096); + when(eks.maxVcpus()).thenReturn(2); + Info host = stubDockerHost(64L * 1024 * 1024 * 1024, 16); + + Cluster cluster = new Cluster(); + cluster.setName("capped-sized-cluster"); + manager.startCluster(cluster); + + EksNodeCapacity.Limits limits = EksNodeCapacity.calculate(new Ec2InstanceTypeCatalog() + .find("t2.2xlarge").orElseThrow(), host.getMemTotal(), host.getNCPU(), 4096, 2); + verify(builder).withMemoryBytes(limits.memoryBytes()); + verify(builder).withCpuUnits(2 * 1024); + } + + @Test + void sizingTypeAbsentFromTheCatalogIsIgnored() { + when(eks.nodeInstanceType()).thenReturn(Optional.of("x9.imaginary")); + Cluster cluster = new Cluster(); + cluster.setNodeInstanceType("t3.small"); + + assertEquals("t3.small", manager.sizingInstanceType(cluster)); + + when(eks.nodeInstanceType()).thenReturn(Optional.of(" ")); + assertEquals("t3.small", manager.sizingInstanceType(cluster)); + } + + private Info stubDockerHost(long memoryBytes, int cpus) { + DockerClient dockerClient = Mockito.mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS); + Info host = Mockito.mock(Info.class); + when(host.getMemTotal()).thenReturn(memoryBytes); + when(host.getNCPU()).thenReturn(cpus); + when(dockerClient.infoCmd().exec()).thenReturn(host); + when(lifecycleManager.getDockerClient()).thenReturn(dockerClient); + return host; + } + @Test void hostTooSmallLeavesClusterUnboundedAndStarting() { DockerClient dockerClient = Mockito.mock(DockerClient.class, Mockito.RETURNS_DEEP_STUBS);