mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(eks): let pods pull images pushed to the Floci ECR registry (#1762)
k3s containerd could not reach the emulated ECR registry: repository URIs embed localhost plus the host-published port, which resolves to the k3s container itself, and containerd requires a registries.yaml entry before it will speak plain HTTP to a registry. New clusters now get a generated /etc/rancher/k3s/registries.yaml, injected through the Docker API before the container starts, that mirrors every repository hostname the emulator can mint (the default account across the region catalog, plus the path-style form) to the registry container's in-network endpoint. Public registries are never matched, and any failure skips the mirror without aborting cluster creation. Opt out with FLOCI_SERVICES_EKS_ECR_REGISTRY_MIRROR=false. Closes #1761
This commit is contained in:
1 parent
2c746b25a1
commit
7586a01cf9
10 files changed
+344
-7
No files matched your search
@@ -1396,6 +1396,15 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
@WithDefault("true")
|
||||
boolean iamAuthWebhook();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@WithDefault("true")
|
||||
boolean ecrRegistryMirror();
|
||||
}
|
||||
|
||||
interface InitHooksConfig {
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Catalog of AWS regions the emulator advertises. Single source of truth for
|
||||
* every feature that enumerates regions (EC2 DescribeRegions, EKS ECR-mirror
|
||||
* hostnames, ...).
|
||||
*/
|
||||
public final class AwsRegions {
|
||||
|
||||
public static final List<String> ALL = List.of(
|
||||
"us-east-1", "us-east-2", "us-west-1", "us-west-2",
|
||||
"eu-west-1", "eu-west-2", "eu-west-3", "eu-central-1",
|
||||
"ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2",
|
||||
"ap-south-1", "sa-east-1", "ca-central-1");
|
||||
|
||||
private AwsRegions() {
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Address;
|
||||
@@ -2406,12 +2407,7 @@ public class Ec2Service {
|
||||
}
|
||||
|
||||
public List<String> describeRegions() {
|
||||
return List.of(
|
||||
"us-east-1", "us-east-2", "us-west-1", "us-west-2",
|
||||
"eu-west-1", "eu-west-2", "eu-west-3", "eu-central-1",
|
||||
"ap-northeast-1", "ap-northeast-2", "ap-southeast-1", "ap-southeast-2",
|
||||
"ap-south-1", "sa-east-1", "ca-central-1"
|
||||
);
|
||||
return AwsRegions.ALL;
|
||||
}
|
||||
|
||||
public Map<String, String> describeAccountAttributes(String region) {
|
||||
|
||||
+9
-1
@@ -109,10 +109,18 @@ public class EcrRegistryManager {
|
||||
return accountId + "/" + region + "/" + repoName;
|
||||
}
|
||||
|
||||
/**
|
||||
* The registry endpoint reachable from other containers on the Docker network:
|
||||
* the container name plus the container-internal port (not the published host port).
|
||||
*/
|
||||
public String internalEndpoint() {
|
||||
return "http://" + registryContainerName() + ":" + CONTAINER_INTERNAL_PORT;
|
||||
}
|
||||
|
||||
/** Returns a {@link RegistryHttpClient} bound to the current registry endpoint. */
|
||||
public RegistryHttpClient httpClient() {
|
||||
if (containerDetector.isRunningInContainer()) {
|
||||
return new RegistryHttpClient("http://" + registryContainerName() + ":" + CONTAINER_INTERNAL_PORT);
|
||||
return new RegistryHttpClient(internalEndpoint());
|
||||
}
|
||||
return new RegistryHttpClient("http://localhost:" + effectivePort());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
@@ -9,6 +10,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
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.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.eks.model.CertificateAuthority;
|
||||
import io.github.hectorvent.floci.services.eks.model.Cluster;
|
||||
import com.github.dockerjava.api.async.ResultCallback;
|
||||
@@ -16,8 +18,12 @@ import com.github.dockerjava.api.command.ExecCreateCmdResponse;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
@@ -42,6 +48,9 @@ public class EksClusterManager {
|
||||
private static final String WEBHOOK_CONFIG_DIR = "/etc";
|
||||
private static final String WEBHOOK_CONFIG_FILE = "token-webhook.yaml";
|
||||
private static final String WEBHOOK_CONFIG_PATH = WEBHOOK_CONFIG_DIR + "/" + WEBHOOK_CONFIG_FILE;
|
||||
// Tar entry extracted at /etc; the archive path creates /etc/rancher/k3s, which does not
|
||||
// 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";
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
@@ -49,6 +58,7 @@ public class EksClusterManager {
|
||||
private final ContainerDetector containerDetector;
|
||||
private final PortAllocator portAllocator;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final EcrRegistryManager ecrRegistryManager;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@Inject
|
||||
@@ -57,12 +67,14 @@ public class EksClusterManager {
|
||||
ContainerDetector containerDetector,
|
||||
PortAllocator portAllocator,
|
||||
DockerHostResolver dockerHostResolver,
|
||||
EcrRegistryManager ecrRegistryManager,
|
||||
EmulatorConfig config) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.containerDetector = containerDetector;
|
||||
this.portAllocator = portAllocator;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.ecrRegistryManager = ecrRegistryManager;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@@ -137,6 +149,7 @@ public class EksClusterManager {
|
||||
if (webhookLocalFile != null) {
|
||||
copyWebhookIntoContainer(containerId, webhookLocalFile, cluster.getName());
|
||||
}
|
||||
injectEcrRegistryMirror(containerId, cluster.getName());
|
||||
ContainerInfo info = lifecycleManager.startCreated(containerId, spec);
|
||||
|
||||
// Public endpoint: see floci.services.eks.endpoint-mode. `host` (default) is the host-reachable
|
||||
@@ -287,6 +300,99 @@ 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 emulator can mint — default account across
|
||||
* the full region catalog, plus the path-style {@code localhost:<port>} form — to the registry
|
||||
* container's in-network endpoint. Public registries are never matched. A failure disables the
|
||||
* mirror for this cluster but does not abort its startup, matching the webhook contract.
|
||||
*/
|
||||
void injectEcrRegistryMirror(String containerId, String clusterName) {
|
||||
if (!config.services().eks().ecrRegistryMirror() || !config.services().ecr().enabled()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
ecrRegistryManager.ensureStarted();
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("EKS cluster {0} gets no ECR registry mirror: registry unavailable: {1}",
|
||||
clusterName, e.getMessage());
|
||||
return;
|
||||
}
|
||||
List<String> regions = new ArrayList<>(AwsRegions.ALL);
|
||||
if (!regions.contains(config.defaultRegion())) {
|
||||
regions.add(config.defaultRegion());
|
||||
}
|
||||
String content = buildRegistriesYaml(config.defaultAccountId(), regions,
|
||||
ecrRegistryManager.effectivePort(), ecrRegistryManager.internalEndpoint());
|
||||
writeRegistriesYaml(clusterName, content);
|
||||
try {
|
||||
lifecycleManager.getDockerClient()
|
||||
.copyArchiveToContainerCmd(containerId)
|
||||
.withTarInputStream(new ByteArrayInputStream(tarSingleFile(REGISTRIES_TAR_ENTRY, content)))
|
||||
.withRemotePath("/etc")
|
||||
.exec();
|
||||
LOG.infov("Injected ECR registry mirror ({0}) into k3s cluster {1}",
|
||||
ecrRegistryManager.internalEndpoint(), 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());
|
||||
}
|
||||
}
|
||||
|
||||
/** Best-effort local copy for inspection/debugging; the container copy streams from memory. */
|
||||
private void writeRegistriesYaml(String clusterName, String content) {
|
||||
Path localFile = Paths.get(config.services().eks().dataPath(), "registries", clusterName, "registries.yaml")
|
||||
.toAbsolutePath().normalize();
|
||||
try {
|
||||
Files.createDirectories(localFile.getParent());
|
||||
Files.writeString(localFile, content);
|
||||
} catch (IOException e) {
|
||||
LOG.debugv("Could not write local registries.yaml copy for cluster {0}: {1}",
|
||||
clusterName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] tarSingleFile(String entryName, String content) {
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] data = content.getBytes(StandardCharsets.UTF_8);
|
||||
try (TarArchiveOutputStream tar = new TarArchiveOutputStream(out)) {
|
||||
TarArchiveEntry entry = new TarArchiveEntry(entryName);
|
||||
entry.setSize(data.length);
|
||||
entry.setMode(0644);
|
||||
tar.putArchiveEntry(entry);
|
||||
tar.write(data);
|
||||
tar.closeArchiveEntry();
|
||||
}
|
||||
return out.toByteArray();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Could not build in-memory tar for " + entryName, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the k3s registries.yaml content. One mirror entry per hostname-style repository URI
|
||||
* ({@code <account>.dkr.ecr.<region>.localhost:<port>}) plus one for the path-style form
|
||||
* ({@code localhost:<port>}), all pointing at the registry's in-network endpoint. k3s supports
|
||||
* no partial wildcards and a {@code "*"} catch-all would also intercept public registries,
|
||||
* so the hostnames are enumerated explicitly.
|
||||
*/
|
||||
static String buildRegistriesYaml(String accountId, List<String> regions, int hostPort, String endpoint) {
|
||||
StringBuilder yaml = new StringBuilder("mirrors:\n");
|
||||
for (String region : regions) {
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + hostPort, endpoint);
|
||||
}
|
||||
appendMirror(yaml, "localhost:" + hostPort, endpoint);
|
||||
return yaml.toString();
|
||||
}
|
||||
|
||||
private static void appendMirror(StringBuilder yaml, String host, String endpoint) {
|
||||
yaml.append(" \"").append(host).append("\":\n")
|
||||
.append(" endpoint:\n")
|
||||
.append(" - \"").append(endpoint).append("\"\n");
|
||||
}
|
||||
|
||||
/** The Floci token-webhook URL as reachable from inside the k3s container. */
|
||||
String webhookUrl() {
|
||||
return "http://" + dockerHostResolver.resolve() + ":" + config.port() + "/_floci/eks/token-webhook";
|
||||
|
||||
Reference in new issue
Block a user