mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(eks): mirror the cluster's own region and account in the registry config (#5399)
The k3s registries.yaml listed the default account across the regions of the deployment's partition, so a cluster in another account, or in a region of another partition served by the same process, could not pull from Floci's ECR through its own repository hostnames. Build the mirror from the cluster's account (then the default account) across every region of the cluster's partition. A commercial cluster in the default account gets the same file as before.
This commit is contained in:
1 parent
3ff5529fad
commit
8b83892dff
3 files changed
+144
-27
No files matched your search
@@ -654,8 +654,9 @@ on HTTPS for anything it doesn't recognize as loopback: so, out of the box, k3s
|
||||
pull from the registry even though `docker push` from the host works.
|
||||
|
||||
Floci solves this at cluster creation: each new k3s container gets a generated
|
||||
`/etc/rancher/k3s/registries.yaml` that mirrors every repository hostname the emulator
|
||||
can mint: the default account across the full region catalog, plus the path-style
|
||||
`/etc/rancher/k3s/registries.yaml` that mirrors every repository hostname the cluster
|
||||
can pull: the cluster's own account and the default account, across every region of the
|
||||
cluster's partition, plus the path-style
|
||||
`localhost:<port>` form used by `FLOCI_SERVICES_ECR_URI_STYLE=path`, to Floci's
|
||||
in-network data plane. The same image
|
||||
reference then works for the host-side push and the in-cluster pull, with no retagging
|
||||
|
||||
@@ -724,7 +724,7 @@ public class EksClusterManager
|
||||
if (auditPolicyLocalFile != null) {
|
||||
copyAuditPolicyIntoContainer(containerId, auditPolicyLocalFile, cluster.getName());
|
||||
}
|
||||
injectEcrRegistryMirror(containerId, cluster.getName());
|
||||
injectEcrRegistryMirror(containerId, cluster);
|
||||
linkContainerdCertsDir(containerId, cluster.getName());
|
||||
registerPodIdentityWebhook(containerId, cluster);
|
||||
if (signingKeyFiles != null) {
|
||||
@@ -1954,13 +1954,15 @@ 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, 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.
|
||||
* 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.
|
||||
*/
|
||||
void injectEcrRegistryMirror(String containerId, String clusterName) {
|
||||
void injectEcrRegistryMirror(String containerId, Cluster cluster) {
|
||||
String clusterName = cluster.getName();
|
||||
if (!config.services().eks().ecrRegistryMirror() || !config.services().ecr().enabled()) {
|
||||
return;
|
||||
}
|
||||
@@ -1971,14 +1973,11 @@ public class EksClusterManager
|
||||
clusterName, e.getMessage());
|
||||
return;
|
||||
}
|
||||
List<String> regions = new ArrayList<>(AwsRegions.advertised(AwsRegions.partitionFor(config.defaultRegion())));
|
||||
if (!regions.contains(config.defaultRegion())) {
|
||||
regions.add(config.defaultRegion());
|
||||
}
|
||||
String region = AwsArnUtils.regionOrDefault(cluster.getArn(), config.defaultRegion());
|
||||
String endpoint = "http://" + dockerHostResolver.resolve() + ":" + config.port();
|
||||
boolean tlsUri = config.services().ecr().tlsUri() && config.tls().enabled();
|
||||
String content = buildRegistriesYaml(config.defaultAccountId(), regions,
|
||||
ecrRegistryManager.advertisedPort(), endpoint, tlsUri);
|
||||
String content = buildRegistriesYaml(mirrorAccounts(resolveClusterAccountId(cluster), config.defaultAccountId()),
|
||||
mirrorRegions(region), ecrRegistryManager.advertisedPort(), endpoint, tlsUri);
|
||||
writeLocalCopy(Paths.get(config.services().eks().dataPath(), "registries", clusterName,
|
||||
"registries.yaml"), content, clusterName);
|
||||
try {
|
||||
@@ -2158,6 +2157,25 @@ public class EksClusterManager
|
||||
}
|
||||
}
|
||||
|
||||
/** Every region of the cluster region's partition, and the cluster region itself if it is unpublished. */
|
||||
static List<String> mirrorRegions(String clusterRegion) {
|
||||
List<String> regions = new ArrayList<>(AwsRegions.advertised(AwsRegions.partitionFor(clusterRegion)));
|
||||
if (!regions.contains(clusterRegion)) {
|
||||
regions.add(clusterRegion);
|
||||
}
|
||||
return regions;
|
||||
}
|
||||
|
||||
/** The cluster's account first, then the default account when it differs. */
|
||||
static List<String> mirrorAccounts(String clusterAccountId, String defaultAccountId) {
|
||||
Set<String> accounts = new LinkedHashSet<>();
|
||||
accounts.add(clusterAccountId);
|
||||
if (defaultAccountId != null && !defaultAccountId.isBlank()) {
|
||||
accounts.add(defaultAccountId);
|
||||
}
|
||||
return List.copyOf(accounts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -2173,11 +2191,19 @@ public class EksClusterManager
|
||||
|
||||
static String buildRegistriesYaml(String accountId, List<String> regions, int advertisedPort,
|
||||
String endpoint, boolean tlsUri) {
|
||||
return buildRegistriesYaml(List.of(accountId), regions, advertisedPort, endpoint, tlsUri);
|
||||
}
|
||||
|
||||
static String buildRegistriesYaml(List<String> accountIds, List<String> regions, int advertisedPort,
|
||||
String endpoint, boolean tlsUri) {
|
||||
StringBuilder yaml = new StringBuilder("mirrors:\n");
|
||||
for (String region : regions) {
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + advertisedPort, endpoint);
|
||||
if (tlsUri) {
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + advertisedPort, endpoint);
|
||||
for (String accountId : accountIds) {
|
||||
for (String region : regions) {
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + advertisedPort, endpoint);
|
||||
if (tlsUri) {
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + advertisedPort,
|
||||
endpoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
appendMirror(yaml, "localhost:" + advertisedPort, endpoint);
|
||||
|
||||
@@ -225,6 +225,52 @@ class EksClusterManagerTest {
|
||||
assertEquals(2L * (AwsRegions.advertised("aws").size() + 1), endpoints);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registriesYamlForACommercialClusterInTheDefaultAccountIsUnchanged() {
|
||||
String yaml = EksClusterManager.buildRegistriesYaml(
|
||||
EksClusterManager.mirrorAccounts("000000000000", "000000000000"),
|
||||
EksClusterManager.mirrorRegions("us-east-1"), 4566, "http://floci:4566", false);
|
||||
|
||||
assertEquals(EksClusterManager.buildRegistriesYaml(
|
||||
"000000000000", AwsRegions.advertised("aws"), 4566, "http://floci:4566"), yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registriesYamlForAChinaClusterMirrorsTheChinaRegions() {
|
||||
String yaml = EksClusterManager.buildRegistriesYaml(
|
||||
EksClusterManager.mirrorAccounts("000000000000", "000000000000"),
|
||||
EksClusterManager.mirrorRegions("cn-north-1"), 4566, "http://floci:4566", false);
|
||||
|
||||
for (String region : AwsRegions.advertised("aws-cn")) {
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr." + region + ".localhost:4566\":"), region);
|
||||
}
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.cn-north-1.localhost:4566\":"));
|
||||
assertFalse(yaml.contains(".us-east-1."));
|
||||
assertFalse(yaml.contains("amazonaws"), "repository URIs are Floci's localhost hosts in every partition");
|
||||
assertEquals(AwsRegions.advertised("aws-cn").size() + 1,
|
||||
yaml.lines().filter(line -> line.contains("- \"http://floci:4566\"")).count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registriesYamlForANonDefaultAccountMirrorsBothAccounts() {
|
||||
String yaml = EksClusterManager.buildRegistriesYaml(
|
||||
EksClusterManager.mirrorAccounts("111122223333", "000000000000"),
|
||||
List.of("eu-central-1"), 4566, "http://floci:4566", false);
|
||||
|
||||
assertEquals("""
|
||||
mirrors:
|
||||
"111122223333.dkr.ecr.eu-central-1.localhost:4566":
|
||||
endpoint:
|
||||
- "http://floci:4566"
|
||||
"000000000000.dkr.ecr.eu-central-1.localhost:4566":
|
||||
endpoint:
|
||||
- "http://floci:4566"
|
||||
"localhost:4566":
|
||||
endpoint:
|
||||
- "http://floci:4566"
|
||||
""", yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
void registriesYamlKeepsTheExistingOutputWhenTlsUrisAreDisabled() {
|
||||
String yaml = EksClusterManager.buildRegistriesYaml(
|
||||
@@ -1082,9 +1128,53 @@ class EksClusterManagerTest {
|
||||
Mockito.mock(RegionResolver.class));
|
||||
}
|
||||
|
||||
private Cluster demoCluster() {
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setName("demo");
|
||||
return cluster;
|
||||
}
|
||||
|
||||
private Cluster clusterWithArn(String arn) {
|
||||
Cluster cluster = demoCluster();
|
||||
cluster.setArn(arn);
|
||||
return cluster;
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDefaultCommercialClusterKeepsTheDeploymentWideMirror() throws Exception {
|
||||
manager.injectEcrRegistryMirror("container-1",
|
||||
clusterWithArn("arn:aws:eks:us-east-1:000000000000:cluster/demo"));
|
||||
|
||||
assertEquals(EksClusterManager.buildRegistriesYaml("000000000000", AwsRegions.advertised("aws"),
|
||||
4566, "http://floci:4566"), Files.readString(tempDir.resolve("registries/demo/registries.yaml")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aChinaClusterInACommercialDeploymentMirrorsItsOwnPartitionsRegions() throws Exception {
|
||||
manager.injectEcrRegistryMirror("container-1",
|
||||
clusterWithArn("arn:aws-cn:eks:cn-north-1:000000000000:cluster/demo"));
|
||||
|
||||
String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.cn-north-1.localhost:4566\":"));
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.cn-northwest-1.localhost:4566\":"));
|
||||
assertFalse(yaml.contains(".us-east-1."), "a China cluster pulls from China regions only");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterInAnotherAccountMirrorsItsOwnAccountAndTheDefaultOne() throws Exception {
|
||||
Cluster cluster = clusterWithArn("arn:aws:eks:eu-west-1:111122223333:cluster/demo");
|
||||
cluster.setAccountId("111122223333");
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", cluster);
|
||||
|
||||
String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));
|
||||
assertTrue(yaml.contains("\"111122223333.dkr.ecr.eu-west-1.localhost:4566\":"));
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.eu-west-1.localhost:4566\":"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void injectsTheMirrorIntoTheContainer() throws Exception {
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
verify(registryManager).ensureStarted();
|
||||
verify(copyCmd).withRemotePath("/etc");
|
||||
@@ -1100,7 +1190,7 @@ class EksClusterManagerTest {
|
||||
// k3s still reaches Floci on its own port inside the Docker network.
|
||||
when(registryManager.advertisedPort()).thenReturn(54321);
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.us-east-1.localhost:54321\":"));
|
||||
@@ -1117,7 +1207,7 @@ class EksClusterManagerTest {
|
||||
when(tls.enabled()).thenReturn(true);
|
||||
when(ecr.tlsUri()).thenReturn(true);
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.us-east-1.localhost.floci.io:4566\":"));
|
||||
@@ -1133,7 +1223,7 @@ class EksClusterManagerTest {
|
||||
when(config.tls()).thenReturn(tls);
|
||||
when(ecr.tlsUri()).thenReturn(true);
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));
|
||||
assertFalse(yaml.contains("localhost.floci.io"));
|
||||
@@ -1145,7 +1235,7 @@ class EksClusterManagerTest {
|
||||
void skipsWhenTheKnobIsOff() {
|
||||
when(eks.ecrRegistryMirror()).thenReturn(false);
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
verifyNoInteractions(registryManager);
|
||||
verify(lifecycleManager, never()).getDockerClient();
|
||||
@@ -1155,7 +1245,7 @@ class EksClusterManagerTest {
|
||||
void skipsWhenEcrIsDisabled() {
|
||||
when(ecr.enabled()).thenReturn(false);
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
verifyNoInteractions(registryManager);
|
||||
verify(lifecycleManager, never()).getDockerClient();
|
||||
@@ -1165,7 +1255,7 @@ class EksClusterManagerTest {
|
||||
void registryStartupFailureSkipsTheMirrorWithoutAborting() {
|
||||
Mockito.doThrow(new RuntimeException("no docker")).when(registryManager).ensureStarted();
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
verify(lifecycleManager, never()).getDockerClient();
|
||||
}
|
||||
@@ -1174,7 +1264,7 @@ class EksClusterManagerTest {
|
||||
void copyFailureDoesNotAbortClusterCreation() {
|
||||
when(copyCmd.exec()).thenThrow(new RuntimeException("copy failed"));
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
manager.injectEcrRegistryMirror("container-1", demoCluster());
|
||||
|
||||
verify(copyCmd).exec();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user