diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index 8df59ba96c..1200f00b5a 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -455,6 +455,7 @@ These services spawn Docker containers. They require access to the Docker socket | `FLOCI_SERVICES_EKS_MOCK` | `false` | When `true`, clusters are created instantly without a real container | | `FLOCI_SERVICES_EKS_PROVIDER` | `k3s` | Kubernetes provider (`k3s`) | | `FLOCI_SERVICES_EKS_DEFAULT_IMAGE` | `rancher/k3s:latest` | Docker image for EKS clusters | +| `FLOCI_SERVICES_EKS_IMAGE_TEMPLATE` | _(none)_ | Format string for custom k3s images (e.g. `myrepo/k3s:v%s`), taking cluster version | | `FLOCI_SERVICES_EKS_API_SERVER_BASE_PORT` | `6500` | First port in the Kubernetes API server range | | `FLOCI_SERVICES_EKS_API_SERVER_MAX_PORT` | `6599` | Last port in the Kubernetes API server range | | `FLOCI_SERVICES_EKS_KEEP_RUNNING_ON_SHUTDOWN` | `false` | Keep EKS containers running when Floci stops | diff --git a/docs/services/eks.md b/docs/services/eks.md index d5343a3552..1c7f290cb4 100644 --- a/docs/services/eks.md +++ b/docs/services/eks.md @@ -256,7 +256,8 @@ back (for example Docker is unavailable), the cluster is marked `FAILED` instead |---|---|---| | `FLOCI_SERVICES_EKS_ENABLED` | `true` | Enable the EKS service | | `FLOCI_SERVICES_EKS_MOCK` | `false` | Metadata-only mode (no Docker) | -| `FLOCI_SERVICES_EKS_DEFAULT_IMAGE` | `rancher/k3s:latest` | k3s Docker image | +| `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 | | `FLOCI_SERVICES_EKS_API_SERVER_MAX_PORT` | `6599` | Last port in the k3s API server range | | `FLOCI_SERVICES_EKS_DATA_PATH` | `./data/eks` | Host bind-mount root for cluster data | @@ -268,6 +269,35 @@ back (for example Docker is unavailable), the cluster is marked `FAILED` instead | `FLOCI_SERVICES_EKS_IRSA_SIGNING_KEY` | `true` | Pass the cluster OIDC signing key to k3s so in-cluster projected service account tokens can assume IAM roles via Floci STS | | `FLOCI_SERVICES_EKS_IMDS` | `false` | Enable link-local IMDS (`169.254.169.254`) proxy in cluster containers | +### Kubernetes versions and network configuration + +Floci supports standard AWS EKS Kubernetes versions (`1.xx`, 1.28 and above). Known releases are mapped to stable pinned k3s images, while newer or unpinned versions dynamically resolve to upstream k3s release images (`rancher/k3s:v.0-k3s1`): + +- `1.28` (`rancher/k3s:v1.28.15-k3s1`) +- `1.29` (`rancher/k3s:v1.29.14-k3s1`, default version metadata) +- `1.30` (`rancher/k3s:v1.30.10-k3s1`) +- `1.31` (`rancher/k3s:v1.31.5-k3s1`) +- `1.32` (`rancher/k3s:v1.32.2-k3s1`) +- `1.33` (`rancher/k3s:v1.33.1-k3s1`) +- `1.34` (`rancher/k3s:v1.34.1-k3s1`) +- `1.35` (`rancher/k3s:v1.35.0-k3s1`) +- `1.36` (`rancher/k3s:v1.36.0-k3s1`) +- `1.37+` (dynamically resolved to `rancher/k3s:v.0-k3s1`) + +When `--version` is omitted, the cluster version defaults to `1.29` and the container image resolution uses `FLOCI_SERVICES_EKS_DEFAULT_IMAGE` (`rancher/k3s:latest` by default). When a version is explicitly requested, it maps to the corresponding pinned image or template. + +You can specify standard EKS `version` and `kubernetesNetworkConfig.serviceIpv4Cidr` when creating a cluster: + +```bash +aws --endpoint-url http://localhost:4566 eks create-cluster \ + --name my-cluster \ + --role-arn arn:aws:iam::000000000000:role/eks-role \ + --version 1.31 \ + --kubernetes-network-config serviceIpv4Cidr=172.20.0.0/16 +``` + +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. + ### Pulling images from Floci ECR Images pushed to the [Floci ECR registry](ecr.md) use `localhost`-based repository URIs diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index b25ab16079..5c8c72383c 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -2800,6 +2800,13 @@ public interface EmulatorConfig { @WithDefault("rancher/k3s:latest") String defaultImage(); + /** + * Optional image template for k3s images when version is specified. + * For example: "custom-registry.internal/k3s:v%s". + * If omitted, Floci maps supported Kubernetes versions to stable upstream k3s images. + */ + Optional imageTemplate(); + @WithDefault("6500") int apiServerBasePort(); 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.java index ff90127b43..acd45a57af 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 @@ -3033,6 +3033,11 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider { return subnet; } + public Vpc requireVpc(String region, String vpcId) { + ensureDefaultResources(region); + return getRequiredVpc(region, vpcId); + } + /** * Resolves a subnet for an EC2 placement availability zone. Prefer the seeded default subnet * when several local subnets share the zone, but allow user-created subnets to make custom diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java index 9298063c91..5a24acb0db 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ipv4Cidrs.java @@ -8,19 +8,19 @@ import java.util.List; * Minimal IPv4 CIDR math backing the IPAM emulation: containment, overlap and * first-free-block scans over a pool's provisioned space. */ -final class Ipv4Cidrs { +public final class Ipv4Cidrs { private Ipv4Cidrs() {} /** True when {@code inner} lies entirely within {@code outer}. */ - static boolean contains(String outer, String inner) { + public static boolean contains(String outer, String inner) { long[] o = parse(outer); long[] i = parse(inner); return o[0] <= i[0] && i[1] <= o[1]; } /** True when the two blocks share any address. */ - static boolean overlaps(String a, String b) { + public static boolean overlaps(String a, String b) { long[] x = parse(a); long[] y = parse(b); return x[0] <= y[1] && y[0] <= x[1]; diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java b/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java index 7af14b313d..27103f3391 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupPolicy.java @@ -133,7 +133,7 @@ public final class SecurityGroupPolicy { } } - static boolean validCidr(String cidr) { + public static boolean validCidr(String cidr) { if (cidr == null) { return false; } diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index 6fd2d7fb74..070dae1882 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 @@ -75,6 +75,19 @@ public class EksClusterManager { // exist yet in a created-but-not-started k3s container. private static final String REGISTRIES_TAR_ENTRY = "rancher/k3s/registries.yaml"; private static final String ENDPOINT_MODE_NETWORK = "network"; + public static final String DEFAULT_POD_CIDR = "10.42.0.0/16"; + + public static final Map SUPPORTED_K8S_VERSIONS = Map.of( + "1.28", "rancher/k3s:v1.28.15-k3s1", + "1.29", "rancher/k3s:v1.29.14-k3s1", + "1.30", "rancher/k3s:v1.30.10-k3s1", + "1.31", "rancher/k3s:v1.31.5-k3s1", + "1.32", "rancher/k3s:v1.32.2-k3s1", + "1.33", "rancher/k3s:v1.33.1-k3s1", + "1.34", "rancher/k3s:v1.34.1-k3s1", + "1.35", "rancher/k3s:v1.35.0-k3s1", + "1.36", "rancher/k3s:v1.36.0-k3s1" + ); static final String SA_SIGNING_KEY_FILE = "sa-signing-key.pem"; static final String SA_PUBLIC_KEY_FILE = "sa-public-key.pem"; @@ -187,7 +200,7 @@ public class EksClusterManager { * {@link #isReady(Cluster)} returns true and {@link #finalizeCluster(Cluster)} is called. */ public void startCluster(Cluster cluster) { - String image = config.services().eks().defaultImage(); + String image = resolveClusterImage(cluster); if (cluster.getDockerName() == null) { cluster.setDockerName(accountQualifiedName(cluster)); } @@ -217,7 +230,15 @@ public class EksClusterManager { // filesystem, so chmod works correctly and data persists across container restarts. String volumeName = cluster.getDockerName(); - List serverArgs = buildServerArgs(config.services().eks().disableCni()); + String serviceCidr = cluster.getKubernetesNetworkConfig() != null + && cluster.getKubernetesNetworkConfig().getServiceIpv4Cidr() != null + ? cluster.getKubernetesNetworkConfig().getServiceIpv4Cidr() + : EksService.DEFAULT_SERVICE_IPV4_CIDR; + String clusterCidr = cluster.getPodCidr() != null && !cluster.getPodCidr().isBlank() + ? cluster.getPodCidr() + : DEFAULT_POD_CIDR; + + List serverArgs = buildServerArgs(config.services().eks().disableCni(), serviceCidr, clusterCidr); // The account label comes from the cluster record when set (restore runs with no request // context); regionResolver is the fallback for the create path. @@ -296,7 +317,13 @@ public class EksClusterManager { if (signingKeyFiles != null) { copySigningKeysIntoContainer(containerId, signingKeyFiles, cluster.getName()); } - ContainerInfo info = lifecycleManager.startCreated(containerId, spec); + ContainerInfo info; + try { + info = lifecycleManager.startCreated(containerId, spec); + } catch (Exception e) { + lifecycleManager.removeIfExists(containerName); + throw e; + } applyEndpoints(cluster, containerName, hostPort, info); configureLinkLocalMetadataEndpoint(cluster, containerId); @@ -558,12 +585,39 @@ public class EksClusterManager { } } + /** + * Resolves the k3s container image for a cluster. + * Uses imageTemplate if configured, otherwise maps explicitly requested Kubernetes versions + * to stable k3s images, falling back to the configured defaultImage when no version was requested. + */ + String resolveClusterImage(Cluster cluster) { + String configuredDefault = config.services().eks().defaultImage(); + boolean hasExplicitVersion = cluster != null + && (cluster.isExplicitVersion() + || (cluster.getVersion() != null && !EksService.DEFAULT_K8S_VERSION.equals(cluster.getVersion()))); + if (hasExplicitVersion) { + String version = cluster.getVersion(); + if (config.services().eks().imageTemplate().isPresent()) { + String template = config.services().eks().imageTemplate().get(); + return template.contains("%s") ? String.format(template, version) : template; + } + String mapped = SUPPORTED_K8S_VERSIONS.get(version); + if (mapped != null) { + return mapped; + } + return "rancher/k3s:v" + version + ".0-k3s1"; + } + return configuredDefault != null && !configuredDefault.isBlank() + ? configuredDefault + : "rancher/k3s:latest"; + } + /** * Builds the k3s {@code server} command-line args. When {@code disableCni} is true, flannel, - * k3s's default network policy controller, and kube-proxy are all disabled up front — see the + * k3s's default network policy controller, and kube-proxy are all disabled up front: see the * {@code disableCni} config javadoc for why this must happen at startup, not after the fact. */ - static List buildServerArgs(boolean disableCni) { + static List buildServerArgs(boolean disableCni, String serviceCidr, String clusterCidr) { List serverArgs = new ArrayList<>(List.of("server", "--disable=traefik", "--tls-san=localhost")); @@ -572,9 +626,19 @@ public class EksClusterManager { serverArgs.add("--disable-network-policy"); serverArgs.add("--disable-kube-proxy"); } + if (serviceCidr != null && !serviceCidr.isBlank()) { + serverArgs.add("--service-cidr=" + serviceCidr); + } + if (clusterCidr != null && !clusterCidr.isBlank()) { + serverArgs.add("--cluster-cidr=" + clusterCidr); + } return serverArgs; } + static List buildServerArgs(boolean disableCni) { + return buildServerArgs(disableCni, null, null); + } + /** * Overrides the image's default {@code ["/bin/k3s"]} entrypoint so a {@code mount * --make-rshared /} can run immediately before k3s starts (see the disableCni branch 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.java index 0e8234597b..890cc8f92f 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 @@ -10,10 +10,13 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.ec2.Ec2Service; +import io.github.hectorvent.floci.services.ec2.Ipv4Cidrs; +import io.github.hectorvent.floci.services.ec2.SecurityGroupPolicy; import io.github.hectorvent.floci.services.ec2.model.IpPermission; import io.github.hectorvent.floci.services.ec2.model.SecurityGroup; import io.github.hectorvent.floci.services.ec2.model.Tag; import io.github.hectorvent.floci.services.ec2.model.UserIdGroupPair; +import io.github.hectorvent.floci.services.ec2.model.Vpc; 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.AccessConfig; @@ -57,6 +60,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import io.github.hectorvent.floci.core.resource.ExplorerResource; import io.github.hectorvent.floci.core.resource.ResourceProvider; @@ -171,6 +176,7 @@ public class EksService implements TagHandler, ResourceProvider { try { LOG.infov("Restoring k3s container for persisted EKS cluster {0}", cluster.getName()); cluster.setStatus(ClusterStatus.CREATING); + cluster.setPodCidr(EksClusterManager.DEFAULT_POD_CIDR); clusterManager.restoreCluster(cluster); } catch (Exception e) { if (!clusterManager.isDockerReachable()) { @@ -428,7 +434,27 @@ public class EksService implements TagHandler, ResourceProvider { cluster.setArn(arn); cluster.setAccountId(accountId); cluster.setCreatedAt(Instant.now()); - cluster.setVersion(request.getVersion() != null ? request.getVersion() : "1.29"); + + if (request.getVersion() != null && !request.getVersion().isBlank()) { + String requestedVersion = request.getVersion().trim(); + Matcher matcher = K8S_VERSION_PATTERN.matcher(requestedVersion); + if (!matcher.matches()) { + throw new AwsException("InvalidParameterException", + "The specified parameter version is not valid: " + requestedVersion, 400); + } + int minor = Integer.parseInt(matcher.group(1)); + if (minor < MIN_SUPPORTED_K8S_MINOR) { + throw new AwsException("InvalidParameterException", + "Unsupported Kubernetes version '" + requestedVersion + "'. Supported versions are 1." + + MIN_SUPPORTED_K8S_MINOR + " and above.", 400); + } + cluster.setVersion(requestedVersion); + cluster.setExplicitVersion(true); + } else { + cluster.setVersion(DEFAULT_K8S_VERSION); + cluster.setExplicitVersion(false); + } + cluster.setRoleArn(request.getRoleArn()); ResourcesVpcConfig vpcConfig = buildVpcConfigResponse(request.getResourcesVpcConfig(), resolvedVpcId); SecurityGroup clusterSg = null; @@ -445,7 +471,9 @@ public class EksService implements TagHandler, ResourceProvider { } } cluster.setResourcesVpcConfig(vpcConfig); - cluster.setKubernetesNetworkConfig(buildNetworkConfig(request.getKubernetesNetworkConfig())); + String vpcCidr = resolveClusterVpcCidr(region, vpcConfig.getVpcId()); + cluster.setKubernetesNetworkConfig(buildNetworkConfig(request.getKubernetesNetworkConfig(), vpcCidr)); + cluster.setPodCidr(EksClusterManager.DEFAULT_POD_CIDR); cluster.setLogging(buildLogging(request.getLogging())); cluster.setEncryptionConfig(buildEncryptionConfig(request.getEncryptionConfig())); cluster.setStatus(ClusterStatus.CREATING); @@ -818,15 +846,76 @@ public class EksService implements TagHandler, ResourceProvider { return response; } - private KubernetesNetworkConfig buildNetworkConfig(KubernetesNetworkConfig request) { - KubernetesNetworkConfig config = new KubernetesNetworkConfig(); - if (request != null) { - config.setServiceIpv4Cidr(request.getServiceIpv4Cidr() != null ? request.getServiceIpv4Cidr() : "10.100.0.0/16"); - config.setIpFamily(request.getIpFamily() != null ? request.getIpFamily() : "ipv4"); - } else { - config.setServiceIpv4Cidr("10.100.0.0/16"); - config.setIpFamily("ipv4"); + public static final String DEFAULT_SERVICE_IPV4_CIDR = "10.100.0.0/16"; + public static final String ALTERNATIVE_SERVICE_IPV4_CIDR = "172.20.0.0/16"; + public static final String DEFAULT_K8S_VERSION = "1.29"; + public static final Pattern K8S_VERSION_PATTERN = Pattern.compile("^1\\.(\\d+)$"); + public static final int MIN_SUPPORTED_K8S_MINOR = 28; + + static void validateServiceIpv4Cidr(String cidr) { + if (cidr == null || !SecurityGroupPolicy.validCidr(cidr)) { + throw new AwsException("InvalidParameterException", + "The specified parameter kubernetesNetworkConfig.serviceIpv4Cidr is not valid: " + cidr, 400); } + int slash = cidr.indexOf('/'); + int prefix; + try { + prefix = Integer.parseInt(cidr.substring(slash + 1)); + } catch (NumberFormatException e) { + throw new AwsException("InvalidParameterException", + "The specified parameter kubernetesNetworkConfig.serviceIpv4Cidr is not valid: " + cidr, 400); + } + if (prefix < 12 || prefix > 24) { + throw new AwsException("InvalidParameterException", + "The specified parameter kubernetesNetworkConfig.serviceIpv4Cidr must have a prefix between /12 and /24: " + cidr, 400); + } + if (!Ipv4Cidrs.contains("10.0.0.0/8", cidr) + && !Ipv4Cidrs.contains("172.16.0.0/12", cidr) + && !Ipv4Cidrs.contains("192.168.0.0/16", cidr)) { + throw new AwsException("InvalidParameterException", + "The specified parameter kubernetesNetworkConfig.serviceIpv4Cidr must fall within RFC 1918 private address ranges: " + cidr, 400); + } + } + + String resolveClusterVpcCidr(String region, String vpcId) { + if (ec2Service != null && vpcId != null && !vpcId.isBlank()) { + try { + Vpc vpc = ec2Service.requireVpc(region, vpcId); + if (vpc.getCidrBlock() != null && !vpc.getCidrBlock().isBlank()) { + return vpc.getCidrBlock(); + } + } catch (Exception e) { + LOG.debugv("Could not resolve VPC CIDR for vpc {0}: {1}", vpcId, e.getMessage()); + } + } + return null; + } + + private KubernetesNetworkConfig buildNetworkConfig(KubernetesNetworkConfig request, String vpcCidr) { + KubernetesNetworkConfig config = new KubernetesNetworkConfig(); + String requestedCidr = request != null ? request.getServiceIpv4Cidr() : null; + String resolvedCidr; + if (requestedCidr != null && !requestedCidr.isBlank()) { + validateServiceIpv4Cidr(requestedCidr); + if (vpcCidr != null && Ipv4Cidrs.overlaps(requestedCidr, vpcCidr)) { + throw new AwsException("InvalidParameterException", + "The specified service IPv4 CIDR " + requestedCidr + " overlaps with the VPC CIDR " + vpcCidr, 400); + } + resolvedCidr = requestedCidr; + } else { + if (vpcCidr != null && Ipv4Cidrs.overlaps(DEFAULT_SERVICE_IPV4_CIDR, vpcCidr)) { + if (Ipv4Cidrs.overlaps(ALTERNATIVE_SERVICE_IPV4_CIDR, vpcCidr)) { + throw new AwsException("InvalidParameterException", + "Default service IPv4 CIDR blocks (10.100.0.0/16 and 172.20.0.0/16) overlap with the VPC CIDR " + + vpcCidr + ". Please specify a non-overlapping serviceIpv4Cidr.", 400); + } + resolvedCidr = ALTERNATIVE_SERVICE_IPV4_CIDR; + } else { + resolvedCidr = DEFAULT_SERVICE_IPV4_CIDR; + } + } + config.setServiceIpv4Cidr(resolvedCidr); + config.setIpFamily(request != null && request.getIpFamily() != null ? request.getIpFamily() : "ipv4"); return config; } diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/model/Cluster.java b/src/main/java/io/github/hectorvent/floci/services/eks/model/Cluster.java index 08da352b36..173a08199a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/model/Cluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/model/Cluster.java @@ -74,6 +74,12 @@ public class Cluster { @JsonIgnore private int hostPort; + @JsonIgnore + private String podCidr; + + @JsonIgnore + private boolean explicitVersion; + /** * Resolved Docker container/volume name for this cluster's k3s resources. In-memory only * (never part of the AWS response shape): assigned when the container is started, or @@ -148,4 +154,10 @@ public class Cluster { public String getDockerName() { return dockerName; } public void setDockerName(String dockerName) { this.dockerName = dockerName; } + + public String getPodCidr() { return podCidr; } + public void setPodCidr(String podCidr) { this.podCidr = podCidr; } + + public boolean isExplicitVersion() { return explicitVersion; } + public void setExplicitVersion(boolean explicitVersion) { this.explicitVersion = explicitVersion; } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 673803bd4c..16ab62d24e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -591,6 +591,7 @@ floci: mock: false provider: k3s default-image: "rancher/k3s:latest" + # image-template: "myregistry.io/k3s:v%s" # optional custom image format template taking cluster version api-server-base-port: 6500 api-server-max-port: 6599 data-path: ./data/eks diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java index 07f198ea7a..ce52ad3a3d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java @@ -1137,4 +1137,164 @@ class EksClusterManagerTest { assertEquals("pub-key-3", Files.readString(files3.publicKeyPath())); } } + + @Nested + class NativeClusterRuntime { + + private EmulatorConfig config; + private EmulatorConfig.EksServiceConfig eks; + private ContainerLifecycleManager lifecycleManager; + private ContainerBuilder containerBuilder; + private ContainerBuilder.Builder builder; + private PortAllocator portAllocator; + private EksClusterManager manager; + + @BeforeEach + void setUp() { + config = Mockito.mock(EmulatorConfig.class); + EmulatorConfig.ServicesConfig services = Mockito.mock(EmulatorConfig.ServicesConfig.class); + eks = Mockito.mock(EmulatorConfig.EksServiceConfig.class); + when(config.services()).thenReturn(services); + when(services.eks()).thenReturn(eks); + when(eks.defaultImage()).thenReturn("rancher/k3s:latest"); + when(eks.imageTemplate()).thenReturn(Optional.empty()); + when(eks.apiServerBasePort()).thenReturn(6500); + when(eks.apiServerMaxPort()).thenReturn(6599); + when(eks.dockerNetwork()).thenReturn(Optional.empty()); + when(eks.disableCni()).thenReturn(false); + when(eks.iamAuthWebhook()).thenReturn(false); + when(eks.ecrRegistryMirror()).thenReturn(false); + when(eks.imds()).thenReturn(false); + when(eks.endpointMode()).thenReturn("host"); + when(config.defaultAccountId()).thenReturn("000000000000"); + + lifecycleManager = Mockito.mock(ContainerLifecycleManager.class); + when(lifecycleManager.create(any())).thenReturn("container-id"); + when(lifecycleManager.startCreated(any(), any())).thenReturn( + new ContainerInfo("container-id", Map.of())); + + containerBuilder = Mockito.mock(ContainerBuilder.class); + builder = Mockito.mock(ContainerBuilder.Builder.class, Mockito.RETURNS_SELF); + when(containerBuilder.newContainer(anyString())).thenReturn(builder); + when(builder.build()).thenReturn(Mockito.mock(ContainerSpec.class)); + + portAllocator = Mockito.mock(PortAllocator.class); + when(portAllocator.allocate(6500, 6599)).thenReturn(6500); + + RegionResolver regionResolver = Mockito.mock(RegionResolver.class); + when(regionResolver.getAccountId()).thenReturn("000000000000"); + when(regionResolver.getDefaultRegion()).thenReturn("us-east-1"); + + manager = new EksClusterManager(containerBuilder, lifecycleManager, + Mockito.mock(ContainerDetector.class), portAllocator, + Mockito.mock(DockerHostResolver.class), Mockito.mock(EcrRegistryManager.class), + config, regionResolver, null, Mockito.mock(EksOidcService.class)); + } + + @Test + void resolveClusterImageMapsSupportedVersions() { + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + cluster.setExplicitVersion(true); + + cluster.setVersion("1.28"); + assertEquals("rancher/k3s:v1.28.15-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.29"); + assertEquals("rancher/k3s:v1.29.14-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.30"); + assertEquals("rancher/k3s:v1.30.10-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.31"); + assertEquals("rancher/k3s:v1.31.5-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.32"); + assertEquals("rancher/k3s:v1.32.2-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.33"); + assertEquals("rancher/k3s:v1.33.1-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.34"); + assertEquals("rancher/k3s:v1.34.1-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.35"); + assertEquals("rancher/k3s:v1.35.0-k3s1", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.36"); + assertEquals("rancher/k3s:v1.36.0-k3s1", manager.resolveClusterImage(cluster)); + } + + @Test + void resolveClusterImageDynamicallyFormatsUnmappedVersions() { + Cluster cluster = new Cluster(); + cluster.setName("future-cluster"); + cluster.setExplicitVersion(true); + cluster.setVersion("1.37"); + + assertEquals("rancher/k3s:v1.37.0-k3s1", manager.resolveClusterImage(cluster)); + } + + @Test + void resolveClusterImageFallsBackToDefaultWhenVersionNullOrDefaultWithoutExplicit() { + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + cluster.setVersion(null); + assertEquals("rancher/k3s:latest", manager.resolveClusterImage(cluster)); + + cluster.setVersion("1.29"); + cluster.setExplicitVersion(false); + assertEquals("rancher/k3s:latest", manager.resolveClusterImage(cluster)); + + cluster.setExplicitVersion(true); + assertEquals("rancher/k3s:v1.29.14-k3s1", manager.resolveClusterImage(cluster)); + } + + @Test + void resolveClusterImageUsesConfiguredImageTemplate() { + when(eks.imageTemplate()).thenReturn(Optional.of("internal.registry.io/k3s:v%s-custom")); + + Cluster cluster = new Cluster(); + cluster.setName("test-cluster"); + cluster.setVersion("1.31"); + + assertEquals("internal.registry.io/k3s:v1.31-custom", manager.resolveClusterImage(cluster)); + } + + @Test + void defaultPodCidrConstantIsK3sStandard() { + assertEquals("10.42.0.0/16", EksClusterManager.DEFAULT_POD_CIDR); + } + + @Test + void buildServerArgsPropagatesServiceAndPodCidr() { + List args = EksClusterManager.buildServerArgs(false, "172.20.0.0/16", "10.44.0.0/16"); + assertTrue(args.contains("--service-cidr=172.20.0.0/16")); + assertTrue(args.contains("--cluster-cidr=10.44.0.0/16")); + assertFalse(args.contains("--flannel-backend=none")); + } + + @Test + void buildServerArgsWithDisableCniAndCidrs() { + List args = EksClusterManager.buildServerArgs(true, "10.100.0.0/16", "10.42.0.0/16"); + assertTrue(args.contains("--flannel-backend=none")); + assertTrue(args.contains("--disable-network-policy")); + assertTrue(args.contains("--disable-kube-proxy")); + assertTrue(args.contains("--service-cidr=10.100.0.0/16")); + assertTrue(args.contains("--cluster-cidr=10.42.0.0/16")); + } + + @Test + void startClusterCleansUpContainerOnStartFailure() { + when(lifecycleManager.startCreated(any(), any())) + .thenThrow(new RuntimeException("Container failed to boot")); + + Cluster cluster = new Cluster(); + cluster.setName("fail-cluster"); + cluster.setVersion("1.30"); + + assertThrows(RuntimeException.class, () -> manager.startCluster(cluster)); + verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-eks-fail-cluster"); + } + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java index 352576b79c..9fa0796e28 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksServiceTest.java @@ -26,6 +26,7 @@ import io.github.hectorvent.floci.services.eks.model.CreateNodeGroupRequest; import io.github.hectorvent.floci.services.eks.model.EncryptionConfig; import io.github.hectorvent.floci.services.eks.model.FargateProfile; import io.github.hectorvent.floci.services.eks.model.FargateProfileStatus; +import io.github.hectorvent.floci.services.eks.model.KubernetesNetworkConfig; import io.github.hectorvent.floci.services.eks.model.Cluster; import io.github.hectorvent.floci.services.eks.model.LogSetup; import io.github.hectorvent.floci.services.eks.model.Logging; @@ -59,10 +60,12 @@ import static org.mockito.Mockito.when; class EksServiceTest { private EksService eksService; + private StorageFactory storageFactory; + private RegionResolver regionResolver; @BeforeEach void setUp() { - StorageFactory storageFactory = new StorageFactory(null, null) { + storageFactory = new StorageFactory(null, null) { @Override public AccountAwareStorageBackend create(String serviceName, String fileName, TypeReference> typeReference) { @@ -73,7 +76,7 @@ class EksServiceTest { EmulatorConfig config = testConfig(); EksClusterManager clusterManager = null; Ec2Service ec2Service = null; - RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000"); + regionResolver = new RegionResolver("us-east-1", "000000000000"); eksService = new EksService(storageFactory, config, regionResolver, clusterManager, ec2Service, new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class), mock(EksPodIdentityAssociationService.class)); @@ -1749,4 +1752,220 @@ class EksServiceTest { assertEquals(created.getEncryptionConfig(), described.getEncryptionConfig()); assertEquals(created.getLogging(), described.getLogging()); } + + @Test + void createClusterWithSupportedVersion() { + CreateClusterRequest req = createTestClusterRequest("v30-cluster"); + req.setVersion("1.30"); + + Cluster created = eksService.createCluster(req); + assertEquals("1.30", created.getVersion()); + + Cluster described = eksService.describeCluster("v30-cluster"); + assertEquals("1.30", described.getVersion()); + } + + @Test + void createClusterWithFutureVersionSucceeds() { + CreateClusterRequest req = createTestClusterRequest("v37-cluster"); + req.setVersion("1.37"); + + Cluster created = eksService.createCluster(req); + assertEquals("1.37", created.getVersion()); + + Cluster described = eksService.describeCluster("v37-cluster"); + assertEquals("1.37", described.getVersion()); + } + + @Test + void createClusterWithoutVersionDefaultsTo129() { + CreateClusterRequest req = createTestClusterRequest("default-ver-cluster"); + req.setVersion(null); + Cluster created = eksService.createCluster(req); + assertEquals("1.29", created.getVersion()); + assertFalse(created.isExplicitVersion()); + } + + @Test + void createClusterWithInvalidVersionFormatThrowsInvalidParameterException() { + CreateClusterRequest req1 = createTestClusterRequest("v-prefix-cluster"); + req1.setVersion("v1.30"); + AwsException ex1 = assertThrows(AwsException.class, () -> eksService.createCluster(req1)); + assertEquals("InvalidParameterException", ex1.getErrorCode()); + assertTrue(ex1.getMessage().contains("The specified parameter version is not valid: v1.30")); + + CreateClusterRequest req2 = createTestClusterRequest("patch-version-cluster"); + req2.setVersion("1.30.2"); + AwsException ex2 = assertThrows(AwsException.class, () -> eksService.createCluster(req2)); + assertEquals("InvalidParameterException", ex2.getErrorCode()); + assertTrue(ex2.getMessage().contains("The specified parameter version is not valid: 1.30.2")); + } + + @Test + void createClusterWithUnsupportedVersionThrowsInvalidParameterException() { + CreateClusterRequest req = createTestClusterRequest("bad-version-cluster"); + req.setVersion("1.15"); + + AwsException ex = assertThrows(AwsException.class, () -> eksService.createCluster(req)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + assertTrue(ex.getMessage().contains("Unsupported Kubernetes version '1.15'")); + } + + @Test + void createClusterWithCustomServiceIpv4Cidr() { + CreateClusterRequest req = createTestClusterRequest("cidr-cluster"); + KubernetesNetworkConfig netConfig = new KubernetesNetworkConfig(); + netConfig.setServiceIpv4Cidr("172.20.0.0/16"); + req.setKubernetesNetworkConfig(netConfig); + + Cluster created = eksService.createCluster(req); + assertNotNull(created.getKubernetesNetworkConfig()); + assertEquals("172.20.0.0/16", created.getKubernetesNetworkConfig().getServiceIpv4Cidr()); + + Cluster described = eksService.describeCluster("cidr-cluster"); + assertEquals("172.20.0.0/16", described.getKubernetesNetworkConfig().getServiceIpv4Cidr()); + } + + @Test + void createClusterWithInvalidServiceIpv4CidrThrowsInvalidParameterException() { + CreateClusterRequest req = createTestClusterRequest("bad-cidr-cluster"); + KubernetesNetworkConfig netConfig = new KubernetesNetworkConfig(); + netConfig.setServiceIpv4Cidr("invalid-cidr"); + req.setKubernetesNetworkConfig(netConfig); + + AwsException ex = assertThrows(AwsException.class, () -> eksService.createCluster(req)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + assertTrue(ex.getMessage().contains("kubernetesNetworkConfig.serviceIpv4Cidr is not valid")); + } + + @Test + void createClusterWithNonRfc1918ServiceIpv4CidrThrowsInvalidParameterException() { + CreateClusterRequest req = createTestClusterRequest("public-ip-cluster"); + KubernetesNetworkConfig netConfig = new KubernetesNetworkConfig(); + netConfig.setServiceIpv4Cidr("8.8.8.0/24"); + req.setKubernetesNetworkConfig(netConfig); + + AwsException ex = assertThrows(AwsException.class, () -> eksService.createCluster(req)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + assertTrue(ex.getMessage().contains("must fall within RFC 1918 private address ranges")); + } + + @Test + void createClusterWithInvalidPrefixServiceIpv4CidrThrowsInvalidParameterException() { + CreateClusterRequest req1 = createTestClusterRequest("wide-cidr-cluster"); + KubernetesNetworkConfig netConfig1 = new KubernetesNetworkConfig(); + netConfig1.setServiceIpv4Cidr("10.0.0.0/8"); + req1.setKubernetesNetworkConfig(netConfig1); + + AwsException ex1 = assertThrows(AwsException.class, () -> eksService.createCluster(req1)); + assertEquals("InvalidParameterException", ex1.getErrorCode()); + assertTrue(ex1.getMessage().contains("must have a prefix between /12 and /24")); + + CreateClusterRequest req2 = createTestClusterRequest("narrow-cidr-cluster"); + KubernetesNetworkConfig netConfig2 = new KubernetesNetworkConfig(); + netConfig2.setServiceIpv4Cidr("10.0.0.0/28"); + req2.setKubernetesNetworkConfig(netConfig2); + + AwsException ex2 = assertThrows(AwsException.class, () -> eksService.createCluster(req2)); + assertEquals("InvalidParameterException", ex2.getErrorCode()); + assertTrue(ex2.getMessage().contains("must have a prefix between /12 and /24")); + } + + @Test + void createClusterUsesStandardK3sPodCidrToAvoidDockerCollisions() { + Ec2Service ec2 = realEc2Service(); + ec2.ensureDefaultResources("us-east-1"); + String vpcId = ec2.createVpc("us-east-1", "172.31.0.0/16", false).getVpcId(); + String subnetId = ec2.createSubnet("us-east-1", vpcId, "172.31.1.0/24", "us-east-1a").getSubnetId(); + + EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, ec2, + new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class), + mock(EksPodIdentityAssociationService.class)); + + CreateClusterRequest req = createTestClusterRequest("vpc-cidr-cluster"); + ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig(); + vpcConfig.setSubnetIds(List.of(subnetId)); + req.setResourcesVpcConfig(vpcConfig); + + Cluster cluster = service.createCluster(req); + assertEquals("10.42.0.0/16", cluster.getPodCidr()); + } + + @Test + void createClusterWithOverlappingServiceIpv4CidrThrowsInvalidParameterException() { + Ec2Service ec2 = realEc2Service(); + ec2.ensureDefaultResources("us-east-1"); + String vpcId = ec2.createVpc("us-east-1", "10.0.0.0/16", false).getVpcId(); + String subnetId = ec2.createSubnet("us-east-1", vpcId, "10.0.1.0/24", "us-east-1a").getSubnetId(); + + EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, ec2, + new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class), + mock(EksPodIdentityAssociationService.class)); + + CreateClusterRequest req = createTestClusterRequest("overlap-cidr-cluster"); + ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig(); + vpcConfig.setSubnetIds(List.of(subnetId)); + req.setResourcesVpcConfig(vpcConfig); + + KubernetesNetworkConfig netConfig = new KubernetesNetworkConfig(); + netConfig.setServiceIpv4Cidr("10.0.50.0/24"); + req.setKubernetesNetworkConfig(netConfig); + + AwsException ex = assertThrows(AwsException.class, () -> service.createCluster(req)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + assertTrue(ex.getMessage().contains("overlaps with the VPC CIDR")); + } + + @Test + void createClusterWithDefaultServiceCidrOverlappingVpcPicksAlternative() { + Ec2Service ec2 = realEc2Service(); + ec2.ensureDefaultResources("us-east-1"); + String vpcId = ec2.createVpc("us-east-1", "10.100.0.0/16", false).getVpcId(); + String subnetId = ec2.createSubnet("us-east-1", vpcId, "10.100.1.0/24", "us-east-1a").getSubnetId(); + + EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, ec2, + new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class), + mock(EksPodIdentityAssociationService.class)); + + CreateClusterRequest req = createTestClusterRequest("alt-cidr-cluster"); + ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig(); + vpcConfig.setSubnetIds(List.of(subnetId)); + req.setResourcesVpcConfig(vpcConfig); + + Cluster cluster = service.createCluster(req); + assertEquals(EksService.ALTERNATIVE_SERVICE_IPV4_CIDR, cluster.getKubernetesNetworkConfig().getServiceIpv4Cidr()); + } + + @Test + void createClusterWithBothDefaultServiceCidrsOverlappingVpcThrowsInvalidParameterException() { + Ec2Service ec2 = realEc2Service(); + ec2.ensureDefaultResources("us-east-1"); + String vpcId = ec2.createVpc("us-east-1", "0.0.0.0/0", false).getVpcId(); + String subnetId = ec2.createSubnet("us-east-1", vpcId, "0.0.0.0/24", "us-east-1a").getSubnetId(); + + EksService service = new EksService(storageFactory, testConfig(), regionResolver, null, ec2, + new EksOidcService(storageFactory, new ObjectMapper()), mock(EksAccessEntryService.class), + mock(EksPodIdentityAssociationService.class)); + + CreateClusterRequest req = createTestClusterRequest("both-overlap-cluster"); + ResourcesVpcConfig vpcConfig = new ResourcesVpcConfig(); + vpcConfig.setSubnetIds(List.of(subnetId)); + req.setResourcesVpcConfig(vpcConfig); + + AwsException ex = assertThrows(AwsException.class, () -> service.createCluster(req)); + assertEquals("InvalidParameterException", ex.getErrorCode()); + assertEquals(400, ex.getHttpStatus()); + assertTrue(ex.getMessage().contains("Default service IPv4 CIDR blocks")); + } + + @Test + void createClusterWithoutVpcDefaultsToK3sPodCidr() { + CreateClusterRequest req = createTestClusterRequest("no-vpc-cluster"); + Cluster cluster = eksService.createCluster(req); + assertEquals("10.42.0.0/16", cluster.getPodCidr()); + } }