mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(eks): support native kubernetes versions and network cidrs (#4064)
Support standard AWS EKS Kubernetes versions (1.28 and above) with strict 1.xx validation format, mapped to stable k3s images when requested. Fall back to default-image (with 1.29 cluster version metadata) when no version is specified. Support service IPv4 CIDR validation (RFC 1918 containment, /12 to /24 prefix, non-overlapping with VPC CIDR) with automatic fallback to alternative CIDR, and preserve standard k3s pod CIDR to avoid collisions with Docker networks.
This commit is contained in:
1 parent
4618d633e0
commit
49d28fb685
12 files changed
+610
-22
No files matched your search
@@ -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<String> imageTemplate();
|
||||
|
||||
@WithDefault("6500")
|
||||
int apiServerBasePort();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -133,7 +133,7 @@ public final class SecurityGroupPolicy {
|
||||
}
|
||||
}
|
||||
|
||||
static boolean validCidr(String cidr) {
|
||||
public static boolean validCidr(String cidr) {
|
||||
if (cidr == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -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<String, String> 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<String> 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<String> 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<String> buildServerArgs(boolean disableCni) {
|
||||
static List<String> buildServerArgs(boolean disableCni, String serviceCidr, String clusterCidr) {
|
||||
List<String> 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<String> 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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
Reference in new issue
Block a user