mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eks): mirror the cluster's and default account's AWS ECR hosts to Floci (#9297)
A chart written for EKS names its own account's images by the AWS registry host, <account>.dkr.ecr.<region>.amazonaws.com. The registries.yaml Floci gives a k3s node mirrored only Floci's localhost forms, so containerd sent those pulls to AWS, where they fail. The generated registries.yaml now also mirrors the cluster's and the default account's registry hosts as AWS names them, across the regions of the cluster's partition, under that partition's DNS suffix (amazonaws.com.cn in the China regions). containerd still tries AWS last, so an image Floci does not hold is looked for there. Other accounts' registries, such as the ones EKS add-on images come from, and public registries are not mirrored. Floci's registry endpoint serves a mirror request whose ns names an account's AWS registry host from that account's repositories in that region, when the region belongs to the suffix's partition. A request sent with an AWS registry host as its Host is not routed there. A node now records the digest of the registries.yaml it was started with in a label. When Floci starts, a surviving node whose label does not match the registries.yaml Floci gives it now, such as one started before these entries existed, is replaced on its data volume, as one with another capacity is, so existing clusters get the new mirrors too. It is kept while the registry is unavailable or the mirror is off. EksVpcPodNetworkNightlyIntegrationTest, which #9288 added, now skips without -Dnightly.checks.enabled=true, as every other nightly check does, so a local test run with Docker no longer starts its k3s node. Closes #5711
This commit is contained in:
1 parent
422c78866d
commit
9c21d7c64c
12 files changed
+624
-238
No files matched your search
@@ -4270,8 +4270,9 @@ public interface EmulatorConfig {
|
||||
/**
|
||||
* When true (and ECR is enabled), each new k3s cluster gets a generated
|
||||
* {@code /etc/rancher/k3s/registries.yaml} that mirrors every ECR repository URI the
|
||||
* emulator can mint to the registry container's in-network endpoint, so pods can pull
|
||||
* images pushed to Floci ECR without any manual containerd configuration.
|
||||
* emulator can mint, and the cluster's and default account's registry hosts as AWS names
|
||||
* them, to the registry container's in-network endpoint, so pods can pull images pushed to
|
||||
* Floci ECR without any manual containerd configuration.
|
||||
*/
|
||||
@WithDefault("true")
|
||||
boolean ecrRegistryMirror();
|
||||
|
||||
@@ -3,6 +3,8 @@ package io.floci.aws.services.ecr.registry;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.config.NetworkExposureGuard;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsPartitions;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.services.ecr.EcrService;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
@@ -42,6 +44,12 @@ public class EcrRegistryDataPlane {
|
||||
private static final Logger LOG = Logger.getLogger(EcrRegistryDataPlane.class);
|
||||
private static final Pattern ECR_HOST = Pattern.compile(
|
||||
"^([0-9]{12})\\.dkr\\.ecr\\.([a-z0-9-]+)\\.localhost(?:\\.floci\\.io)?(?::[0-9]+)?$");
|
||||
/**
|
||||
* An account's registry host as AWS names it, {@code <account>.dkr.ecr.<region>.<dnsSuffix>}, which containerd sends
|
||||
* as a mirror request's {@code ns} when an EKS node's registries.yaml mirrors it to Floci.
|
||||
*/
|
||||
private static final Pattern AWS_ECR_HOST = Pattern.compile(
|
||||
"^([0-9]{12})\\.dkr\\.ecr\\.([a-z0-9-]+)\\.(" + AwsPartitions.dnsSuffixRegex() + ")(?::[0-9]+)?$");
|
||||
private static final String MANIFESTS_PATH = "/manifests/";
|
||||
private static final long REPOSITORY_LOCK_POLL_MILLIS = 5;
|
||||
/**
|
||||
@@ -695,11 +703,8 @@ public class EcrRegistryDataPlane {
|
||||
if (!registryPath.startsWith("/v2/")) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Matcher hostMatch = ECR_HOST.matcher(host == null ? "" : host);
|
||||
if (!hostMatch.matches()) {
|
||||
hostMatch = ECR_HOST.matcher(queryParameter(query, "ns"));
|
||||
}
|
||||
if (hostMatch.matches()) {
|
||||
Matcher hostMatch = registryHost(host, query);
|
||||
if (hostMatch != null) {
|
||||
String repository = "/v2/".equals(registryPath) ? "" : repositoryName(registryPath.substring("/v2/".length()));
|
||||
String storageRepositoryName = hostMatch.group(1) + "/" + hostMatch.group(2) + "/" + repository;
|
||||
String backendUri = repository.isEmpty()
|
||||
@@ -734,6 +739,25 @@ public class EcrRegistryDataPlane {
|
||||
registryPath + querySuffix(query, true), tag(registryPath)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The registry a request is for: its host when that is a Floci repository host, else the registry a mirror request
|
||||
* names in {@code ns}, a Floci repository host or an account's AWS registry host in that region's partition. A request
|
||||
* with an AWS registry host in its {@code Host} header is not routed here.
|
||||
*/
|
||||
private static Matcher registryHost(String host, String query) {
|
||||
Matcher byHost = ECR_HOST.matcher(host == null ? "" : host);
|
||||
if (byHost.matches()) {
|
||||
return byHost;
|
||||
}
|
||||
String namespace = queryParameter(query, "ns");
|
||||
Matcher byNamespace = ECR_HOST.matcher(namespace);
|
||||
if (byNamespace.matches()) {
|
||||
return byNamespace;
|
||||
}
|
||||
Matcher mirrored = AWS_ECR_HOST.matcher(namespace);
|
||||
return mirrored.matches() && AwsRegions.dnsSuffixFor(mirrored.group(2)).equals(mirrored.group(3)) ? mirrored : null;
|
||||
}
|
||||
|
||||
private static boolean isLoopbackHost(String host) {
|
||||
HostAndPort authority = host == null ? null : HostAndPort.parseAuthority(host, -1);
|
||||
return authority != null && NetworkExposureGuard.isLoopback(authority.host());
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.floci.aws.config.FlociCertificateAuthority;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StripedLocks;
|
||||
import io.floci.aws.core.common.dns.DnsAnswer;
|
||||
@@ -137,6 +138,8 @@ public class EksClusterManager
|
||||
private static final String SHARED_MOUNTS_LABEL = "io.floci.eks.shared-mounts";
|
||||
/** The pod network a node was started with: {@link #FLANNEL_POD_NETWORK}, or {@code vpc:} and its prefixes. */
|
||||
private static final String POD_NETWORK_LABEL = "io.floci.eks.pod-network";
|
||||
/** The digest of the registries.yaml a node was started with; a node without one has none, or predates the label. */
|
||||
private static final String REGISTRY_MIRROR_LABEL = "io.floci.eks.registry-mirror";
|
||||
private static final String FLANNEL_POD_NETWORK = "flannel";
|
||||
private static final String VPC_POD_NETWORK_LABEL_PREFIX = "vpc:";
|
||||
/** The k3s arguments that leave pod addresses to the VPC pod network's bridge configuration. */
|
||||
@@ -778,6 +781,7 @@ public class EksClusterManager
|
||||
SigningKeyFiles signingKeyFiles = null;
|
||||
String auditPolicyLocalFile = null;
|
||||
String podResolvConf = null;
|
||||
String registries = null;
|
||||
ContainerSpec spec;
|
||||
String containerId;
|
||||
try {
|
||||
@@ -842,6 +846,10 @@ public class EksClusterManager
|
||||
labels.put(DEFAULT_STORAGE_CLASS_LABEL, String.valueOf(defaultStorageClass));
|
||||
labels.put(SHARED_MOUNTS_LABEL, "true");
|
||||
labels.put(POD_NETWORK_LABEL, podNetworkLabel(podNetwork));
|
||||
registries = startingRegistriesYaml(cluster);
|
||||
if (registries != null) {
|
||||
labels.put(REGISTRY_MIRROR_LABEL, registryMirrorLabel(registries));
|
||||
}
|
||||
if (!"localhost".equals(endpointHost)) {
|
||||
labels.put(ENDPOINT_HOST_LABEL, endpointHost);
|
||||
}
|
||||
@@ -996,7 +1004,7 @@ public class EksClusterManager
|
||||
if (auditPolicyLocalFile != null) {
|
||||
copyAuditPolicyIntoContainer(containerId, auditPolicyLocalFile, cluster.getName());
|
||||
}
|
||||
injectEcrRegistryMirror(containerId, cluster);
|
||||
injectEcrRegistryMirror(containerId, cluster, registries);
|
||||
linkContainerdCertsDir(containerId, cluster.getName());
|
||||
registerPodIdentityWebhook(containerId, cluster);
|
||||
if (signingKeyFiles != null) {
|
||||
@@ -1102,9 +1110,10 @@ public class EksClusterManager
|
||||
// A node started before its mounts were shared keeps private mounts, on which a CSI node plugin's
|
||||
// Bidirectional mounts fail: it is replaced on the same data volume, as one with another capacity is.
|
||||
boolean privateMounts = existingLabels == null || !"true".equals(existingLabels.get(SHARED_MOUNTS_LABEL));
|
||||
// One started with another pod network is replaced too.
|
||||
// One started with another pod network or registry mirror is replaced too.
|
||||
if (capacityChanged || privateMounts || onAnotherNetwork(existing.get())
|
||||
|| !endpointHost.equals(certificateHost(existingLabels)) || podNetworkChanged(cluster, existingLabels)) {
|
||||
|| !endpointHost.equals(certificateHost(existingLabels)) || podNetworkChanged(cluster, existingLabels)
|
||||
|| registryMirrorChanged(cluster, existingLabels)) {
|
||||
replaceClusterContainer(cluster, null);
|
||||
}
|
||||
}
|
||||
@@ -2780,44 +2789,83 @@ public class EksClusterManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and injects {@code /etc/rancher/k3s/registries.yaml} into the (created,
|
||||
* not-yet-started) k3s container so its containerd can pull images pushed to the Floci ECR
|
||||
* registry. Mirrors every repository hostname the cluster can pull from Floci: its own account
|
||||
* and the default account, across every region of the cluster's partition, plus the path-style
|
||||
* {@code localhost:<port>} form, including {@code localhost.floci.io} aliases when TLS registry
|
||||
* URIs are enabled, to Floci's in-network data plane. Public registries are never matched. A
|
||||
* failure disables the mirror for this cluster but does not abort its startup, matching the
|
||||
* webhook contract.
|
||||
* The {@code /etc/rancher/k3s/registries.yaml} Floci gives {@code cluster}'s k3s container so its containerd can pull
|
||||
* images pushed to the Floci ECR registry, or null when it gives none: the mirror or ECR is off. It mirrors every
|
||||
* repository hostname the cluster can pull from Floci: its own account and the default account, across every region
|
||||
* of the cluster's partition, plus the path-style {@code localhost:<port>} form, including {@code localhost.floci.io}
|
||||
* aliases when TLS registry URIs are enabled, to Floci's in-network data plane. The same two accounts' registry hosts
|
||||
* as AWS names them are mirrored as well. Public registries and other accounts' are never matched.
|
||||
*/
|
||||
void injectEcrRegistryMirror(String containerId, Cluster cluster) {
|
||||
String clusterName = cluster.getName();
|
||||
String registriesYaml(Cluster cluster) {
|
||||
if (!config.services().eks().ecrRegistryMirror() || !config.services().ecr().enabled()) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
String region = regionResolver.regionOf(cluster.getArn());
|
||||
String endpoint = "http://" + dockerHostResolver.resolve() + ":" + config.port();
|
||||
boolean tlsUri = config.services().ecr().tlsUri() && config.tls().enabled();
|
||||
return buildRegistriesYaml(mirrorAccounts(resolveClusterAccountId(cluster), config.defaultAccountId()),
|
||||
mirrorRegions(region), ecrRegistryManager.advertisedPort(), endpoint, tlsUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* The registries.yaml a k3s container created now gets: {@link #registriesYaml}, or none when the registry is
|
||||
* unavailable, matching the webhook contract: the cluster starts without a mirror.
|
||||
*/
|
||||
String startingRegistriesYaml(Cluster cluster) {
|
||||
String content = registriesYaml(cluster);
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
ecrRegistryManager.ensureStarted();
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("EKS cluster {0} gets no ECR registry mirror: registry unavailable: {1}",
|
||||
clusterName, e.getMessage());
|
||||
cluster.getName(), e.getMessage());
|
||||
return null;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Injects {@code content}, from {@link #startingRegistriesYaml}, into the created, not yet started k3s container. A
|
||||
* failure leaves this cluster without the mirror but does not abort its startup.
|
||||
*/
|
||||
void injectEcrRegistryMirror(String containerId, Cluster cluster, String content) {
|
||||
if (content == null) {
|
||||
return;
|
||||
}
|
||||
String region = regionResolver.regionOf(cluster.getArn());
|
||||
String endpoint = "http://" + dockerHostResolver.resolve() + ":" + config.port();
|
||||
boolean tlsUri = config.services().ecr().tlsUri() && config.tls().enabled();
|
||||
String content = buildRegistriesYaml(mirrorAccounts(resolveClusterAccountId(cluster), config.defaultAccountId()),
|
||||
mirrorRegions(region), ecrRegistryManager.advertisedPort(), endpoint, tlsUri);
|
||||
String clusterName = cluster.getName();
|
||||
writeLocalCopy(localClusterFile(cluster, "registries", "registries.yaml"), content, clusterName);
|
||||
try {
|
||||
RetryingTarCopier.copyBytes(lifecycleManager.getDockerClient(), containerId, "/etc",
|
||||
REGISTRIES_TAR_ENTRY, content.getBytes(StandardCharsets.UTF_8), 0644,
|
||||
RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, archiveRetryBackoffMillis);
|
||||
LOG.infov("Injected ECR registry mirror ({0}) into k3s cluster {1}", endpoint, clusterName);
|
||||
LOG.infov("Injected ECR registry mirror into k3s cluster {0}", clusterName);
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("EKS cluster {0} gets no ECR registry mirror: could not copy registries.yaml "
|
||||
+ "into the k3s container: {1}", clusterName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a surviving node runs another registries.yaml than the one Floci gives it now, such as one written before
|
||||
* the mirror set grew, or none: it is replaced then, on its data volume, and its replacement gets the current one.
|
||||
* It is not replaced while the registry is unavailable, as the replacement would get no mirror either, nor to take
|
||||
* a mirror away when the setting is off.
|
||||
*/
|
||||
boolean registryMirrorChanged(Cluster cluster, Map<String, String> existingLabels) {
|
||||
String wanted = registriesYaml(cluster);
|
||||
if (wanted == null || existingLabels != null
|
||||
&& registryMirrorLabel(wanted).equals(existingLabels.get(REGISTRY_MIRROR_LABEL))) {
|
||||
return false;
|
||||
}
|
||||
return startingRegistriesYaml(cluster) != null;
|
||||
}
|
||||
|
||||
static String registryMirrorLabel(String registriesYaml) {
|
||||
return Digests.sha256Hex(registriesYaml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Symlinks {@code /etc/containerd/certs.d} to k3s containerd's certs directory
|
||||
* ({@code /var/lib/rancher/k3s/agent/etc/containerd/certs.d}) inside the container before start.
|
||||
@@ -3079,8 +3127,11 @@ public class EksClusterManager
|
||||
* ({@code localhost:<port>}), all pointing at Floci's in-network data plane. The port is the
|
||||
* one the repository URIs advertise, which differs from the endpoint's port when Floci's
|
||||
* container publishes its port on another host port. The TLS URI mode adds the corresponding
|
||||
* {@code localhost.floci.io} aliases. k3s supports no partial wildcards and a {@code "*"}
|
||||
* catch-all would also intercept public registries, so the hostnames are enumerated explicitly.
|
||||
* {@code localhost.floci.io} aliases. Each account's registry host as AWS names it,
|
||||
* {@code <account>.dkr.ecr.<region>.<dnsSuffix>}, is mirrored too, so an image a chart names
|
||||
* that way is pulled from Floci; containerd still tries AWS last, for an image Floci does not
|
||||
* hold. k3s supports no partial wildcards and a {@code "*"} catch-all would also intercept
|
||||
* public registries and other accounts' registries, so the hostnames are enumerated explicitly.
|
||||
*/
|
||||
static String buildRegistriesYaml(String accountId, List<String> regions, int advertisedPort, String endpoint) {
|
||||
return buildRegistriesYaml(accountId, regions, advertisedPort, endpoint, false);
|
||||
@@ -3101,6 +3152,7 @@ public class EksClusterManager
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + advertisedPort,
|
||||
endpoint);
|
||||
}
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + "." + AwsRegions.dnsSuffixFor(region), endpoint);
|
||||
}
|
||||
}
|
||||
appendMirror(yaml, "localhost:" + advertisedPort, endpoint);
|
||||
|
||||
Reference in new issue
Block a user