feat(ecr): advertise TLS registry URIs on opt-in (#4252)

This commit is contained in:
XuJian authored and GitHub committed 2026-09-23 16:46:10 -04:00
1 parent eb039eb71c
commit 22c56157ee
13 files changed
+506 -19

No files matched your search

@@ -2487,6 +2487,10 @@ public interface EmulatorConfig {
@WithDefault("hostname")
String uriStyle();
/** Advertise TLS registry hostnames when Floci's global TLS listener is enabled. */
@WithDefault("false")
boolean tlsUri();
/**
* When true, an AWS-shaped ECR image URI that names an image already present on the Docker
* daemon is used as-is instead of being rewritten to Floci's loopback registry.
@@ -269,6 +269,7 @@ public class EcrService implements ResourceProvider {
registryManager.pruneStorage();
}
LOG.infov("Deleted ECR repository {0}/{1}/{2}", region, account, repositoryName);
repo.setRepositoryUri(registryManager.getRepositoryUri(account, region, repositoryName));
return repo;
}
@@ -124,7 +124,7 @@ public class EcrRegistryManager {
String region = m.group(2);
String repoAndTag = m.group(3);
ensureStarted();
String rewritten = getRepositoryUri(account, region, repoAndTag);
String rewritten = repositoryUri(account, region, repoAndTag, "localhost");
LOG.infov("Rewriting ECR image URI {0} -> {1}", image, rewritten);
return rewritten;
}
@@ -144,16 +144,31 @@ public class EcrRegistryManager {
/** Returns the docker-pullable repository URI for the given account/region/name. */
public String getRepositoryUri(String accountId, String region, String repoName) {
return repositoryUri(accountId, region, repoName, tlsUriEnabled() ? "localhost.floci.io" : "localhost");
}
private String repositoryUri(String accountId, String region, String repoName, String domain) {
int port = config.port();
String style = config.services().ecr().uriStyle();
if ("path".equalsIgnoreCase(style)) {
return "localhost:" + port + "/" + accountId + "/" + region + "/" + repoName;
return domain + ":" + port + "/" + accountId + "/" + region + "/" + repoName;
}
return accountId + ".dkr.ecr." + region + ".localhost:" + port + "/" + repoName;
return accountId + ".dkr.ecr." + region + "." + domain + ":" + port + "/" + repoName;
}
private boolean tlsUriEnabled() {
return config.services().ecr().tlsUri() && config.tls().enabled();
}
/** Returns the proxy endpoint a docker daemon should log into for any ECR repo. */
public String getProxyEndpoint() {
if (tlsUriEnabled()) {
String host = "path".equalsIgnoreCase(config.services().ecr().uriStyle())
? "localhost.floci.io"
: regionResolver.getAccountId() + ".dkr.ecr."
+ regionResolver.getRegion() + ".localhost.floci.io";
return "https://" + host + ":" + config.port();
}
String scheme = config.services().ecr().tlsEnabled() ? "https" : "http";
return scheme + "://" + regionResolver.getAccountId() + ".dkr.ecr."
+ regionResolver.getDefaultRegion() + ".localhost:" + config.port();
@@ -1357,7 +1357,8 @@ 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: the default account across
* the full region catalog and the path-style {@code localhost:<port>} form, to Floci's
* the full region catalog and 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.
*/
@@ -1377,7 +1378,8 @@ public class EksClusterManager {
regions.add(config.defaultRegion());
}
String endpoint = "http://" + dockerHostResolver.resolve() + ":" + config.port();
String content = buildRegistriesYaml(config.defaultAccountId(), regions, config.port(), endpoint);
boolean tlsUri = config.services().ecr().tlsUri() && config.tls().enabled();
String content = buildRegistriesYaml(config.defaultAccountId(), regions, config.port(), endpoint, tlsUri);
writeLocalCopy(Paths.get(config.services().eks().dataPath(), "registries", clusterName,
"registries.yaml"), content, clusterName);
try {
@@ -1516,16 +1518,28 @@ public class EksClusterManager {
/**
* 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 Floci's in-network data plane. k3s supports
* ({@code localhost:<port>}), all pointing at Floci's in-network data plane. 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.
*/
static String buildRegistriesYaml(String accountId, List<String> regions, int dataPlanePort, String endpoint) {
return buildRegistriesYaml(accountId, regions, dataPlanePort, endpoint, false);
}
static String buildRegistriesYaml(String accountId, List<String> regions, int dataPlanePort,
String endpoint, boolean tlsUri) {
StringBuilder yaml = new StringBuilder("mirrors:\n");
for (String region : regions) {
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + dataPlanePort, endpoint);
if (tlsUri) {
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + dataPlanePort, endpoint);
}
}
appendMirror(yaml, "localhost:" + dataPlanePort, endpoint);
if (tlsUri) {
appendMirror(yaml, "localhost.floci.io:" + dataPlanePort, endpoint);
}
return yaml.toString();
}