mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ecr): advertise the registry at the published address (#4863)
ECR built repositoryUri, the GetAuthorizationToken proxyEndpoint and the Lambda and ECS image rewrites from floci.port, the port Floci listens on inside its own container. The Docker daemon performs login, push and pull, and reaches Floci through the host port that container publishes. When Floci ran as docker run -p 54321:4566, every advertised address named port 4566 on the host, where nothing answers, so docker login, CDK container asset publishing and ECS or Lambda image pulls failed. EcrRegistryManager now has advertisedPort(): the host port Floci's container publishes for floci.port, read through the existing CurrentContainerNetworkResolver.resolvePublishedPort as RDS does for its proxy endpoint, or floci.port when Floci is not in a container or the port is not published. repositoryUri (hostname and path style, plain and TLS URI mode), proxyEndpoint and rewriteImageUri all use it. The EKS registry mirror keys must match the URIs pods name, so they use the same advertised port. The mirror endpoint stays Floci's in-network address on floci.port. The resolver now caches every published-port answer, "not published" and a failed inspect included, as it already caches the network: a running container's port bindings cannot change, and an inspect that fails fails the same way next time. ECR requests therefore do not inspect the container each time.
This commit is contained in:
1 parent
e22bd2c59a
commit
c1cd5af2bd
8 files changed
+169
-32
No files matched your search
@@ -88,6 +88,8 @@ services:
|
||||
|
||||
`docker login 000000000000.dkr.ecr.us-east-1.localhost:4566` works once Floci starts the registry sidecar.
|
||||
|
||||
When Floci runs in a container that publishes its port on another host port (for example `docker run -p 54321:4566`), the `repositoryUri`, the `GetAuthorizationToken` `proxyEndpoint` and the rewritten Lambda and ECS image URIs all name that host port (`000000000000.dkr.ecr.us-east-1.localhost:54321`), because the Docker daemon performs login, push and pull through it. Floci reads the published port from its own container through the mounted Docker socket, and uses `floci.port` (`4566`) when it does not run in a container or the port is not published.
|
||||
|
||||
## Examples
|
||||
|
||||
```bash
|
||||
|
||||
@@ -679,6 +679,8 @@ Requirements and limits:
|
||||
EKS service network variable.
|
||||
- Only Floci-mintable hostnames are mirrored; public registries (docker.io, ghcr.io, …)
|
||||
are never touched.
|
||||
- Mirrored hostnames carry the port the repository URIs advertise, which is the host port
|
||||
when Floci's container publishes `4566` on another one (see [ECR](ecr.md#docker-compose-port-mapping)).
|
||||
- Repository URIs using a non-default `registryId` (account) are not covered.
|
||||
- The mirror set is snapshotted when the cluster is created. Clusters created before this
|
||||
feature can be fixed manually because the k3s container filesystem survives a restart:
|
||||
|
||||
+12
-6
@@ -32,7 +32,7 @@ public class CurrentContainerNetworkResolver {
|
||||
|
||||
private final DockerClient dockerClient;
|
||||
private final ContainerDetector containerDetector;
|
||||
private final Map<Integer, Integer> cachedPublishedPorts = new ConcurrentHashMap<>();
|
||||
private final Map<Integer, OptionalInt> cachedPublishedPorts = new ConcurrentHashMap<>();
|
||||
|
||||
private volatile Optional<CurrentContainerNetwork> cachedNetwork;
|
||||
|
||||
@@ -59,15 +59,14 @@ public class CurrentContainerNetworkResolver {
|
||||
}
|
||||
|
||||
public OptionalInt resolvePublishedPort(int containerPort) {
|
||||
Integer cachedPublishedPort = cachedPublishedPorts.get(containerPort);
|
||||
OptionalInt cachedPublishedPort = cachedPublishedPorts.get(containerPort);
|
||||
if (cachedPublishedPort != null) {
|
||||
return OptionalInt.of(cachedPublishedPort);
|
||||
return cachedPublishedPort;
|
||||
}
|
||||
|
||||
OptionalInt resolvedPublishedPort = detectPublishedPort(containerPort);
|
||||
resolvedPublishedPort.ifPresent(port -> cachedPublishedPorts.putIfAbsent(containerPort, port));
|
||||
Integer publishedPort = cachedPublishedPorts.get(containerPort);
|
||||
return publishedPort == null ? OptionalInt.empty() : OptionalInt.of(publishedPort);
|
||||
OptionalInt publishedPort = cachedPublishedPorts.putIfAbsent(containerPort, resolvedPublishedPort);
|
||||
return publishedPort == null ? resolvedPublishedPort : publishedPort;
|
||||
}
|
||||
|
||||
Optional<CurrentContainerNetwork> resolve() {
|
||||
@@ -79,6 +78,13 @@ public class CurrentContainerNetworkResolver {
|
||||
return cachedNetwork;
|
||||
}
|
||||
|
||||
/**
|
||||
* The host port Floci's own container publishes for {@code containerPort}, empty when it is
|
||||
* not published, Floci is not in a container, or the container cannot be inspected. Every
|
||||
* result is cached, a failure included, as {@link #resolve()} does: a running container's
|
||||
* port bindings never change, and an inspect that fails (no Docker access, no container id)
|
||||
* fails the same way on the next call.
|
||||
*/
|
||||
private OptionalInt detectPublishedPort(int containerPort) {
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
return OptionalInt.empty();
|
||||
|
||||
+18
-5
@@ -45,8 +45,10 @@ import java.util.regex.Pattern;
|
||||
* lazily on first use and reused across restarts.
|
||||
*
|
||||
* <p>Methods that compute URIs ({@link #getRepositoryUri}, {@link #getProxyEndpoint})
|
||||
* do not require Docker — they read the configured port and account/region from
|
||||
* {@link EmulatorConfig}. Only {@link #ensureStarted()} talks to the daemon.
|
||||
* never start the registry. They read the account and region from {@link EmulatorConfig}
|
||||
* and the port from {@link #advertisedPort()}, which inspects Floci's own container only
|
||||
* when Floci runs in one and falls back to the configured port. Only
|
||||
* {@link #ensureStarted()} manages the registry container.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class EcrRegistryManager {
|
||||
@@ -161,7 +163,7 @@ public class EcrRegistryManager {
|
||||
}
|
||||
|
||||
private String repositoryUri(String accountId, String region, String repoName, String domain) {
|
||||
int port = config.port();
|
||||
int port = advertisedPort();
|
||||
String style = config.services().ecr().uriStyle();
|
||||
if ("path".equalsIgnoreCase(style)) {
|
||||
return domain + ":" + port + "/" + accountId + "/" + region + "/" + repoName;
|
||||
@@ -187,11 +189,22 @@ public class EcrRegistryManager {
|
||||
? "localhost.floci.io"
|
||||
: regionResolver.getAccountId() + ".dkr.ecr."
|
||||
+ regionResolver.getRegion() + ".localhost.floci.io";
|
||||
return "https://" + host + ":" + config.port();
|
||||
return "https://" + host + ":" + advertisedPort();
|
||||
}
|
||||
String scheme = config.services().ecr().tlsEnabled() ? "https" : "http";
|
||||
return scheme + "://" + regionResolver.getAccountId() + ".dkr.ecr."
|
||||
+ regionResolver.getRegion() + ".localhost:" + config.port();
|
||||
+ regionResolver.getRegion() + ".localhost:" + advertisedPort();
|
||||
}
|
||||
|
||||
/**
|
||||
* The port in every registry address Floci hands out. The Docker daemon performs login, push
|
||||
* and pull, and reaches Floci through the host port Floci's own container publishes for
|
||||
* {@code floci.port}. Outside a container, or when that port is not published, it is
|
||||
* {@code floci.port} itself.
|
||||
*/
|
||||
public int advertisedPort() {
|
||||
int port = config.port();
|
||||
return currentContainerNetworkResolver.resolvePublishedPort(port).orElse(port);
|
||||
}
|
||||
|
||||
/** Returns the effective registry port. Stable across calls once {@link #ensureStarted} runs. */
|
||||
|
||||
@@ -1977,7 +1977,8 @@ public class EksClusterManager
|
||||
}
|
||||
String endpoint = "http://" + dockerHostResolver.resolve() + ":" + config.port();
|
||||
boolean tlsUri = config.services().ecr().tlsUri() && config.tls().enabled();
|
||||
String content = buildRegistriesYaml(config.defaultAccountId(), regions, config.port(), endpoint, tlsUri);
|
||||
String content = buildRegistriesYaml(config.defaultAccountId(), regions,
|
||||
ecrRegistryManager.advertisedPort(), endpoint, tlsUri);
|
||||
writeLocalCopy(Paths.get(config.services().eks().dataPath(), "registries", clusterName,
|
||||
"registries.yaml"), content, clusterName);
|
||||
try {
|
||||
@@ -2160,27 +2161,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. 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:<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.
|
||||
*/
|
||||
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 advertisedPort, String endpoint) {
|
||||
return buildRegistriesYaml(accountId, regions, advertisedPort, endpoint, false);
|
||||
}
|
||||
|
||||
static String buildRegistriesYaml(String accountId, List<String> regions, int dataPlanePort,
|
||||
static String buildRegistriesYaml(String accountId, 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:" + dataPlanePort, endpoint);
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost:" + advertisedPort, endpoint);
|
||||
if (tlsUri) {
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + dataPlanePort, endpoint);
|
||||
appendMirror(yaml, accountId + ".dkr.ecr." + region + ".localhost.floci.io:" + advertisedPort, endpoint);
|
||||
}
|
||||
}
|
||||
appendMirror(yaml, "localhost:" + dataPlanePort, endpoint);
|
||||
appendMirror(yaml, "localhost:" + advertisedPort, endpoint);
|
||||
if (tlsUri) {
|
||||
appendMirror(yaml, "localhost.floci.io:" + dataPlanePort, endpoint);
|
||||
appendMirror(yaml, "localhost.floci.io:" + advertisedPort, endpoint);
|
||||
}
|
||||
return yaml.toString();
|
||||
}
|
||||
|
||||
+51
-8
@@ -17,6 +17,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class CurrentContainerNetworkResolverTest {
|
||||
@@ -80,7 +82,7 @@ class CurrentContainerNetworkResolverTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvePublishedPort_retriesUntilSuccessfulAndCachesResult() {
|
||||
void resolvePublishedPort_skipsUnboundEntryAndCachesResult() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
ContainerDetector containerDetector = mock(ContainerDetector.class);
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
@@ -88,7 +90,10 @@ class CurrentContainerNetworkResolverTest {
|
||||
InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);
|
||||
InspectContainerResponse inspect = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);
|
||||
Ports ports = new Ports();
|
||||
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {null});
|
||||
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {
|
||||
null,
|
||||
Ports.Binding.bindPort(49173)
|
||||
});
|
||||
when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);
|
||||
when(inspectCmd.exec()).thenReturn(inspect);
|
||||
when(inspect.getNetworkSettings().getPorts()).thenReturn(ports);
|
||||
@@ -96,18 +101,56 @@ class CurrentContainerNetworkResolverTest {
|
||||
CurrentContainerNetworkResolver resolver =
|
||||
new TestResolver(dockerClient, containerDetector, "floci-container");
|
||||
|
||||
assertTrue(resolver.resolvePublishedPort(7000).isEmpty());
|
||||
|
||||
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {
|
||||
null,
|
||||
Ports.Binding.bindPort(49173)
|
||||
});
|
||||
assertEquals(OptionalInt.of(49173), resolver.resolvePublishedPort(7000));
|
||||
|
||||
ports.getBindings().put(ExposedPort.tcp(7000), new Ports.Binding[] {Ports.Binding.bindPort(49174)});
|
||||
assertEquals(OptionalInt.of(49173), resolver.resolvePublishedPort(7000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvePublishedPort_portNotPublished_cachesTheEmptyResult() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
ContainerDetector containerDetector = mock(ContainerDetector.class);
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
// EXPOSE 4566 without -p: the port is listed with no host binding.
|
||||
Ports ports = new Ports();
|
||||
ports.getBindings().put(ExposedPort.tcp(4566), null);
|
||||
InspectContainerCmd inspectCmd = inspectReturning(dockerClient, ports);
|
||||
|
||||
CurrentContainerNetworkResolver resolver =
|
||||
new TestResolver(dockerClient, containerDetector, "floci-container");
|
||||
|
||||
assertTrue(resolver.resolvePublishedPort(4566).isEmpty());
|
||||
assertTrue(resolver.resolvePublishedPort(4566).isEmpty());
|
||||
verify(inspectCmd, times(1)).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvePublishedPort_inspectFails_cachesTheEmptyResult() {
|
||||
DockerClient dockerClient = mock(DockerClient.class);
|
||||
ContainerDetector containerDetector = mock(ContainerDetector.class);
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(true);
|
||||
InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);
|
||||
when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);
|
||||
when(inspectCmd.exec()).thenThrow(new RuntimeException("Docker daemon unreachable"));
|
||||
|
||||
CurrentContainerNetworkResolver resolver =
|
||||
new TestResolver(dockerClient, containerDetector, "floci-container");
|
||||
|
||||
assertTrue(resolver.resolvePublishedPort(4566).isEmpty());
|
||||
assertTrue(resolver.resolvePublishedPort(4566).isEmpty());
|
||||
verify(inspectCmd, times(1)).exec();
|
||||
}
|
||||
|
||||
private static InspectContainerCmd inspectReturning(DockerClient dockerClient, Ports ports) {
|
||||
InspectContainerCmd inspectCmd = mock(InspectContainerCmd.class);
|
||||
InspectContainerResponse inspect = mock(InspectContainerResponse.class, RETURNS_DEEP_STUBS);
|
||||
when(dockerClient.inspectContainerCmd("floci-container")).thenReturn(inspectCmd);
|
||||
when(inspectCmd.exec()).thenReturn(inspect);
|
||||
when(inspect.getNetworkSettings().getPorts()).thenReturn(ports);
|
||||
return inspectCmd;
|
||||
}
|
||||
|
||||
private static Map<String, ContainerNetwork> networks(String firstName, String firstIp,
|
||||
String secondName, String secondIp) {
|
||||
Map<String, ContainerNetwork> networks = new LinkedHashMap<>();
|
||||
|
||||
+53
@@ -35,6 +35,7 @@ import java.io.Closeable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -464,6 +465,58 @@ class EcrRegistryManagerTest {
|
||||
verify(regionResolver, never()).getDefaultRegion();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"hostname, false, 000000000000.dkr.ecr.us-east-1.localhost:54321/app, http://000000000000.dkr.ecr.us-east-1.localhost:54321",
|
||||
"path, false, localhost:54321/000000000000/us-east-1/app, http://000000000000.dkr.ecr.us-east-1.localhost:54321",
|
||||
"hostname, true, 000000000000.dkr.ecr.us-east-1.localhost.floci.io:54321/app, https://000000000000.dkr.ecr.us-east-1.localhost.floci.io:54321",
|
||||
"path, true, localhost.floci.io:54321/000000000000/us-east-1/app, https://localhost.floci.io:54321"
|
||||
})
|
||||
void advertisedUrisUseTheHostPortTheFlociContainerPublishes(String style, boolean tlsUris,
|
||||
String repositoryUri, String proxyEndpoint) {
|
||||
EmulatorConfig.TlsConfig tls = Mockito.mock(EmulatorConfig.TlsConfig.class);
|
||||
when(config.tls()).thenReturn(tls);
|
||||
when(tls.enabled()).thenReturn(tlsUris);
|
||||
when(ecr.uriStyle()).thenReturn(style);
|
||||
when(ecr.tlsUri()).thenReturn(tlsUris);
|
||||
// docker run -p 54321:4566: the Docker daemon logs in, pushes and pulls through the host port.
|
||||
when(currentContainerNetworkResolver.resolvePublishedPort(4566)).thenReturn(OptionalInt.of(54321));
|
||||
|
||||
assertEquals(repositoryUri, manager.getRepositoryUri("000000000000", "us-east-1", "app"));
|
||||
assertEquals(proxyEndpoint, manager.getProxyEndpoint());
|
||||
verify(lifecycleManager, never()).createAndStart(any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"hostname, 123456789012.dkr.ecr.us-east-1.localhost:54321/backend-user:1",
|
||||
"path, localhost:54321/123456789012/us-east-1/backend-user:1"
|
||||
})
|
||||
void rewriteImageUri_flociPublishedOnAnotherHostPort_pointsTheDaemonAtThePublishedPort(String style,
|
||||
String expectedImage) {
|
||||
when(ecr.uriStyle()).thenReturn(style);
|
||||
when(currentContainerNetworkResolver.resolvePublishedPort(4566)).thenReturn(OptionalInt.of(54321));
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(
|
||||
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
|
||||
|
||||
assertEquals(expectedImage, manager.rewriteImageUri(AWS_ECR_IMAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void advertisedUrisKeepFlociPortWhenItIsNotPublishedFromAContainer() {
|
||||
when(config.port()).thenReturn(4577);
|
||||
when(currentContainerNetworkResolver.resolvePublishedPort(4577)).thenReturn(OptionalInt.empty());
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(
|
||||
new ContainerLifecycleManager.ContainerInfo("container-id", Map.of()));
|
||||
|
||||
assertEquals("000000000000.dkr.ecr.us-east-1.localhost:4577/app",
|
||||
manager.getRepositoryUri("000000000000", "us-east-1", "app"));
|
||||
assertEquals("http://000000000000.dkr.ecr.us-east-1.localhost:4577", manager.getProxyEndpoint());
|
||||
assertEquals("123456789012.dkr.ecr.us-east-1.localhost:4577/backend-user:1",
|
||||
manager.rewriteImageUri(AWS_ECR_IMAGE));
|
||||
verify(currentContainerNetworkResolver, Mockito.atLeastOnce()).resolvePublishedPort(4577);
|
||||
}
|
||||
|
||||
@Test
|
||||
void tlsUriOptInDoesNotReplaceTheLegacyEcrTlsSetting() {
|
||||
when(ecr.tlsEnabled()).thenReturn(true);
|
||||
|
||||
@@ -1060,7 +1060,7 @@ class EksClusterManagerTest {
|
||||
when(dockerClient.copyArchiveToContainerCmd(anyString())).thenReturn(copyCmd);
|
||||
|
||||
registryManager = Mockito.mock(EcrRegistryManager.class);
|
||||
|
||||
when(registryManager.advertisedPort()).thenReturn(4566);
|
||||
config = Mockito.mock(EmulatorConfig.class);
|
||||
eks = Mockito.mock(EmulatorConfig.EksServiceConfig.class);
|
||||
ecr = Mockito.mock(EmulatorConfig.EcrServiceConfig.class);
|
||||
@@ -1095,6 +1095,22 @@ class EksClusterManagerTest {
|
||||
assertFalse(yaml.contains("localhost.floci.io"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mirrorsTheAdvertisedRegistryPortToTheInNetworkDataPlane() throws Exception {
|
||||
// Floci published as -p 54321:4566: pods name the advertised repository URI, while
|
||||
// k3s still reaches Floci on its own port inside the Docker network.
|
||||
when(registryManager.advertisedPort()).thenReturn(54321);
|
||||
|
||||
manager.injectEcrRegistryMirror("container-1", "demo");
|
||||
|
||||
String yaml = Files.readString(tempDir.resolve("registries/demo/registries.yaml"));
|
||||
assertTrue(yaml.contains("\"000000000000.dkr.ecr.us-east-1.localhost:54321\":"));
|
||||
assertTrue(yaml.contains("\"localhost:54321\":"));
|
||||
assertFalse(yaml.contains("localhost:4566\":"));
|
||||
assertTrue(yaml.contains("- \"http://floci:4566\""));
|
||||
verify(copyCmd).exec();
|
||||
}
|
||||
|
||||
@Test
|
||||
void injectsTlsAliasesWhenTlsAndTlsUrisAreEnabled() throws Exception {
|
||||
EmulatorConfig.TlsConfig tls = Mockito.mock(EmulatorConfig.TlsConfig.class);
|
||||
|
||||
Reference in new issue
Block a user