mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ecs): rewrite ECR image URIs on RunTask like Lambda already does (#2641)
Extracts the AWS-ECR-URI rewrite out of Lambda's ContainerLauncher into EcrRegistryManager.rewriteImageUri(), now shared by Lambda and ECS so RunTask images resolve through Floci's emulated registry instead of the real amazonaws.com host. Closes #2568 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
149ac635f2
commit
c4050936a8
12 files changed
+259
-44
No files matched your search
+28
@@ -47,6 +47,10 @@ public class EcrRegistryManager {
|
||||
private static final int CONTAINER_INTERNAL_PORT = 5000;
|
||||
private static final String NAMED_VOLUME = "floci-ecr-registry-data";
|
||||
|
||||
/** Matches an AWS-shaped ECR image URI: {@code <account>.dkr.ecr.<region>.amazonaws.com/<repo>[:tag]}. */
|
||||
private static final java.util.regex.Pattern AWS_ECR_URI =
|
||||
java.util.regex.Pattern.compile("^([0-9]{12})\\.dkr\\.ecr\\.([a-z0-9-]+)\\.amazonaws\\.com/(.+)$");
|
||||
|
||||
private final ContainerBuilder containerBuilder;
|
||||
private final ContainerLifecycleManager lifecycleManager;
|
||||
private final ContainerLogStreamer logStreamer;
|
||||
@@ -83,6 +87,30 @@ public class EcrRegistryManager {
|
||||
this.hostPort = config.services().ecr().registryBasePort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrites a real-AWS-shaped ECR image URI to point at Floci's loopback registry,
|
||||
* starting the backing registry container on first use. Any image that doesn't
|
||||
* match the AWS ECR URI shape (e.g. a plain Docker Hub reference or an image
|
||||
* already pointing at Floci's registry) is returned unchanged, without starting
|
||||
* the registry container.
|
||||
*/
|
||||
public String rewriteImageUri(String image) {
|
||||
if (image == null) {
|
||||
return null;
|
||||
}
|
||||
java.util.regex.Matcher m = AWS_ECR_URI.matcher(image);
|
||||
if (!m.matches()) {
|
||||
return image;
|
||||
}
|
||||
String account = m.group(1);
|
||||
String region = m.group(2);
|
||||
String repoAndTag = m.group(3);
|
||||
ensureStarted();
|
||||
String rewritten = getRepositoryUri(account, region, repoAndTag);
|
||||
LOG.infov("Rewriting ECR image URI {0} -> {1}", image, rewritten);
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
/** Returns the docker-pullable repository URI for the given account/region/name. */
|
||||
public String getRepositoryUri(String accountId, String region, String repoName) {
|
||||
int port = effectivePort();
|
||||
|
||||
+9
-2
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
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.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.Container;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
|
||||
@@ -60,6 +61,7 @@ public class EcsContainerManager {
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
private final SsmService ssmService;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
private final EcrRegistryManager ecrRegistryManager;
|
||||
|
||||
@Inject
|
||||
public EcsContainerManager(ContainerBuilder containerBuilder,
|
||||
@@ -70,7 +72,8 @@ public class EcsContainerManager {
|
||||
RegionResolver regionResolver,
|
||||
LaunchedContainerAwsEnv awsEnv,
|
||||
SsmService ssmService,
|
||||
SecretsManagerService secretsManagerService) {
|
||||
SecretsManagerService secretsManagerService,
|
||||
EcrRegistryManager ecrRegistryManager) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.logStreamer = logStreamer;
|
||||
@@ -80,6 +83,7 @@ public class EcsContainerManager {
|
||||
this.awsEnv = awsEnv;
|
||||
this.ssmService = ssmService;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.ecrRegistryManager = ecrRegistryManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,8 +118,11 @@ public class EcsContainerManager {
|
||||
|
||||
Map<String, ContainerOverride> overridesByName = overridesByName(containerOverrides);
|
||||
Map<ContainerDefinition, List<String>> envVarsByContainer = new LinkedHashMap<>();
|
||||
// Resolved before any container is created, so a registry-startup failure can't leak one already started.
|
||||
Map<ContainerDefinition, String> imagesByContainer = new LinkedHashMap<>();
|
||||
for (ContainerDefinition def : taskDef.getContainerDefinitions()) {
|
||||
envVarsByContainer.put(def, buildEnvVars(def, overridesByName.get(def.getName()), region));
|
||||
imagesByContainer.put(def, ecrRegistryManager.rewriteImageUri(def.getImage()));
|
||||
}
|
||||
|
||||
for (ContainerDefinition def : taskDef.getContainerDefinitions()) {
|
||||
@@ -126,7 +133,7 @@ public class EcsContainerManager {
|
||||
ContainerOverride override = overridesByName.get(def.getName());
|
||||
|
||||
// Build container spec
|
||||
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(def.getImage())
|
||||
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(imagesByContainer.get(def))
|
||||
.withName(containerName)
|
||||
.withEnv(envVarsByContainer.get(def))
|
||||
.withDockerNetwork(config.services().ecs().dockerNetwork())
|
||||
|
||||
+1
-27
@@ -123,10 +123,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
private final LaunchedContainerAwsEnv awsEnv;
|
||||
private final LambdaExecutionRoleCredentials executionRoleCredentials;
|
||||
|
||||
/** Matches an AWS-shaped ECR image URI: {@code <account>.dkr.ecr.<region>.amazonaws.com/<repo>[:tag]}. */
|
||||
private static final java.util.regex.Pattern AWS_ECR_URI =
|
||||
java.util.regex.Pattern.compile("^([0-9]{12})\\.dkr\\.ecr\\.([a-z0-9-]+)\\.amazonaws\\.com/(.+)$");
|
||||
|
||||
@Inject
|
||||
public ContainerLauncher(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
@@ -163,28 +159,6 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
volumeCleanupScheduler.shutdownNow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrites real-AWS-shaped ECR image URIs to point at Floci's loopback registry.
|
||||
* Stored ImageUri is preserved (so describe-function returns the original);
|
||||
* the rewrite is only applied immediately before the docker pull.
|
||||
*/
|
||||
private String rewriteForEmulatedRegistry(String image) {
|
||||
if (image == null) {
|
||||
return null;
|
||||
}
|
||||
java.util.regex.Matcher m = AWS_ECR_URI.matcher(image);
|
||||
if (!m.matches()) {
|
||||
return image;
|
||||
}
|
||||
String account = m.group(1);
|
||||
String region = m.group(2);
|
||||
String repoAndTag = m.group(3);
|
||||
ecrRegistryManager.ensureStarted();
|
||||
String rewritten = ecrRegistryManager.getRepositoryUri(account, region, repoAndTag);
|
||||
LOG.infov("Rewriting ECR image URI {0} -> {1}", image, rewritten);
|
||||
return rewritten;
|
||||
}
|
||||
|
||||
public ContainerHandle launch(LambdaFunction fn) {
|
||||
LOG.infov("Launching container for function: {0}", fn.getFunctionName());
|
||||
|
||||
@@ -236,7 +210,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
: imageResolver.resolve(fn.getRuntime());
|
||||
|
||||
// If this is an AWS-shaped ECR URI, rewrite it to Floci's loopback registry
|
||||
image = rewriteForEmulatedRegistry(image);
|
||||
image = ecrRegistryManager.rewriteImageUri(image);
|
||||
|
||||
// Determine host address reachable from container
|
||||
String hostAddress = dockerHostResolver.resolve();
|
||||
|
||||
Reference in new issue
Block a user