mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(emr-serverless): run a job run in its application's custom image (#6975)
In the docker runner mode an application's imageConfiguration, and a
worker type's, was stored and returned, but every run started from the
release's public image. So a job whose libraries are baked into its own
image, the user guide's "Customizing an EMR Serverless image", could not
run. Now a run starts from the application's custom image:
- the Driver worker type's imageConfiguration, else the application's,
which "EMR Serverless applies ... to all worker types". Local mode
runs the executors in the driver's container, so an Executor image is
not used;
- an <account>.dkr.ecr.<region>.amazonaws.com URI leads to Floci's ECR,
as a Lambda function's or an ECS task's image does, and the
repositoryUri Floci's ECR hands out works too;
- the image is pulled for every run, as an ECS task's is, so a tag
pushed again is what the next run gets ("New workloads submitted after
the update automatically will use the new image"), and the driver
starts from the image id that pull resolved. When the pull fails, the
image the Docker daemon has runs; without one the run is FAILED.
SparkJobRunner.Job carries the custom image beside the template, which
classic EMR's steps leave null.
The image configurations, the application's and each worker type's,
are checked as the user guide and the model describe them, in Floci's
own wording as no source gives AWS's:
- CreateApplication refuses one without imageUri, "This field is
required when you create a new application";
- CreateApplication and UpdateApplication refuse an image whose ECR
repository, AWS-shaped or a repositoryUri from Floci's ECR, is in
another Region: "The Amazon ECR repository must be in the same AWS
Region that you use to launch EMR Serverless applications";
- UpdateApplication with one that leaves imageUri out removes it: "If
you leave this field blank in an update, Amazon EMR will remove the
image configuration."
EcrRegistryManager.imageRegion reads the Region from either URI form.
Floci does not check imageUri against the model's pattern, which would
refuse the repository URIs its own ECR hands out, nor the image's
release or size, and reports no resolvedImageDigest; the service doc
lists these.
This commit is contained in:
1 parent
6e9ac904c8
commit
931b2d26a7
15 files changed
+387
-34
No files matched your search
+22
@@ -725,4 +725,26 @@ class EcrRegistryManagerTest {
|
||||
"/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_manifests",
|
||||
"/var/lib/registry/docker/registry/v2/repositories/000000000000/us-east-1/team/app/_uploads");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"123456789012.dkr.ecr.eu-west-1.amazonaws.com/spark/custom:7.5.0, eu-west-1",
|
||||
"123456789012.dkr.ecr.cn-north-1.amazonaws.com.cn/spark/custom:7.5.0, cn-north-1",
|
||||
"000000000000.dkr.ecr.us-east-1.localhost:4566/spark/custom:7.5.0, us-east-1",
|
||||
"000000000000.dkr.ecr.us-east-1.localhost.floci.io:4566/spark/custom:7.5.0, us-east-1",
|
||||
"localhost:4566/000000000000/us-west-2/spark/custom:7.5.0, us-west-2",
|
||||
"localhost.floci.io:4566/000000000000/us-west-2/spark/custom:7.5.0, us-west-2"
|
||||
})
|
||||
void anImageInAnEcrRepositoryHasItsRepositorysRegion(String image, String region) {
|
||||
assertEquals(Optional.of(region), EcrRegistryManager.imageRegion(image));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anImageOutsideEcrHasNoRegion() {
|
||||
for (String image : List.of("public.ecr.aws/emr-serverless/spark/emr-7.5.0:latest", "python:3.12",
|
||||
"localhost:5000/spark/custom:7.5.0", "registry.internal/123456789012/us-east-1/spark:7.5.0")) {
|
||||
assertEquals(Optional.empty(), EcrRegistryManager.imageRegion(image), image);
|
||||
}
|
||||
assertEquals(Optional.empty(), EcrRegistryManager.imageRegion(null));
|
||||
}
|
||||
}
|
||||
+27
@@ -429,4 +429,31 @@ public class EmrServerlessIntegrationTest {
|
||||
equalTo("arn:aws:sso:::instance/ssoins-1234567890abcdef"))
|
||||
.body("application.jobLevelCostAllocationConfiguration.enabled", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void createApplicationRefusesACustomImageInAnotherRegion() {
|
||||
givenReq()
|
||||
.body("""
|
||||
{
|
||||
"releaseLabel": "emr-7.5.0",
|
||||
"type": "SPARK",
|
||||
"clientToken": "%s",
|
||||
"workerTypeSpecifications": {
|
||||
"Driver": {
|
||||
"imageConfiguration": {
|
||||
"imageUri": "000000000000.dkr.ecr.eu-west-1.amazonaws.com/spark/custom:7.5.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(UUID.randomUUID()))
|
||||
.when()
|
||||
.post("/applications")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", equalTo("Image 000000000000.dkr.ecr.eu-west-1.amazonaws.com/spark/custom:7.5.0 is in "
|
||||
+ "an Amazon ECR repository in eu-west-1; it must be in the application's Region, us-east-1."));
|
||||
}
|
||||
}
|
||||
+28
@@ -255,6 +255,7 @@ class EmrServerlessJobRunServiceTest {
|
||||
assertEquals(REGION, job.region());
|
||||
assertEquals("emr-7.5.0", job.releaseLabel());
|
||||
assertNull(job.imageTemplate());
|
||||
assertNull(job.customImage());
|
||||
assertNull(job.dockerNetwork());
|
||||
assertEquals(ROLE, job.executionRole());
|
||||
assertEquals(List.of("s3://bucket/jobs/app.jar"), job.application());
|
||||
@@ -264,6 +265,33 @@ class EmrServerlessJobRunServiceTest {
|
||||
assertNull(job.cloudWatchLogs());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theRunnerGetsTheDriverWorkerTypesCustomImageElseTheApplicationsElseTheReleases() {
|
||||
FakeRunner runner = new FakeRunner();
|
||||
EmrServerlessJobRunService service = dockerService(runner);
|
||||
String applicationImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0";
|
||||
String driverImage = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/driver:7.5.0";
|
||||
CreateApplicationRequest wholeApplication = applicationRequest("custom", "emr-7.5.0", "SPARK");
|
||||
wholeApplication.setImageConfiguration(EmrServerlessServiceTest.image(applicationImage));
|
||||
// Local mode runs the executors in the driver's container, so an Executor image is not used.
|
||||
wholeApplication.setWorkerTypeSpecifications(
|
||||
Map.of("Executor", EmrServerlessServiceTest.workerType(driverImage)));
|
||||
CreateApplicationRequest perWorkerType = applicationRequest("per-worker-type", "emr-7.5.0", "SPARK");
|
||||
perWorkerType.setImageConfiguration(EmrServerlessServiceTest.image(applicationImage));
|
||||
perWorkerType.setWorkerTypeSpecifications(Map.of("Driver", EmrServerlessServiceTest.workerType(driverImage)));
|
||||
|
||||
service.startJobRun(applications.createApplication(wholeApplication).getApplicationId(), request(null, null));
|
||||
service.startJobRun(applications.createApplication(perWorkerType).getApplicationId(), request(null, null));
|
||||
service.startJobRun(applicationId, request(null, null));
|
||||
for (int i = 0; i < 3; i++) {
|
||||
runNext();
|
||||
}
|
||||
|
||||
assertEquals(applicationImage, runner.jobs.get(0).customImage());
|
||||
assertEquals(driverImage, runner.jobs.get(1).customImage());
|
||||
assertNull(runner.jobs.get(2).customImage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunsDriverOutputGoesToCloudWatchWhenItsOwnOrTheApplicationsLoggingIsEnabled() {
|
||||
FakeRunner runner = new FakeRunner();
|
||||
|
||||
+113
@@ -2,10 +2,14 @@ package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.Application;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.CreateApplicationRequest;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.ImageConfiguration;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.UpdateApplicationRequest;
|
||||
import io.github.hectorvent.floci.services.emrserverless.model.WorkerTypeSpecification;
|
||||
import io.github.hectorvent.floci.testing.InMemoryStorageFactory;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -16,10 +20,13 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -28,6 +35,7 @@ class EmrServerlessServiceTest {
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final TypeReference<Map<String, Application>> APPLICATIONS = new TypeReference<>() {};
|
||||
private static final String CUSTOM_IMAGE = "123456789012.dkr.ecr.us-east-1.amazonaws.com/spark/custom:7.5.0";
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
@@ -90,4 +98,109 @@ class EmrServerlessServiceTest {
|
||||
assertEquals(app.getUpdatedAt(), reloaded.getUpdatedAt());
|
||||
assertEquals("create-00abc", reloaded.getClientToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUpdateLeavingTheImageUriOutRemovesTheImageConfiguration() {
|
||||
EmrServerlessService service = service();
|
||||
CreateApplicationRequest create = createRequest();
|
||||
create.setImageConfiguration(image(CUSTOM_IMAGE));
|
||||
create.setWorkerTypeSpecifications(Map.of("Driver", workerType(CUSTOM_IMAGE)));
|
||||
String applicationId = service.createApplication(create).getApplicationId();
|
||||
ImageConfiguration withoutUri = new ImageConfiguration();
|
||||
withoutUri.setApplicationLevelDigestResolution(true);
|
||||
UpdateApplicationRequest update = new UpdateApplicationRequest();
|
||||
update.setImageConfiguration(withoutUri);
|
||||
update.setWorkerTypeSpecifications(Map.of("Driver", workerType(null)));
|
||||
|
||||
Application updated = service.updateApplication(applicationId, update);
|
||||
|
||||
assertNull(updated.getImageConfiguration());
|
||||
assertNull(updated.getWorkerTypeSpecifications().get("Driver").getImageConfiguration());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aNewApplicationsImageConfigurationNeedsAnImageUri() {
|
||||
EmrServerlessService service = service();
|
||||
CreateApplicationRequest application = createRequest();
|
||||
application.setImageConfiguration(image(null));
|
||||
CreateApplicationRequest workerType = createRequest();
|
||||
workerType.setWorkerTypeSpecifications(Map.of("Driver", workerType(null)));
|
||||
|
||||
AwsException noApplicationImage = assertThrows(AwsException.class,
|
||||
() -> service.createApplication(application));
|
||||
AwsException noDriverImage = assertThrows(AwsException.class, () -> service.createApplication(workerType));
|
||||
|
||||
assertEquals("ValidationException", noApplicationImage.getErrorCode());
|
||||
assertEquals("imageConfiguration.imageUri is required", noApplicationImage.getMessage());
|
||||
assertEquals("workerTypeSpecifications.Driver.imageConfiguration.imageUri is required",
|
||||
noDriverImage.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCustomImageMustBeInTheApplicationsRegion() {
|
||||
EmrServerlessService service = service();
|
||||
CreateApplicationRequest elsewhere = createRequest();
|
||||
elsewhere.setImageConfiguration(image("123456789012.dkr.ecr.us-west-2.amazonaws.com/spark/custom:7.5.0"));
|
||||
CreateApplicationRequest driverElsewhere = createRequest();
|
||||
driverElsewhere.setWorkerTypeSpecifications(
|
||||
Map.of("Driver", workerType("localhost:4566/000000000000/eu-west-1/spark/custom:7.5.0")));
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class, () -> service.createApplication(elsewhere));
|
||||
assertThrows(AwsException.class, () -> service.createApplication(driverElsewhere));
|
||||
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertEquals("Image 123456789012.dkr.ecr.us-west-2.amazonaws.com/spark/custom:7.5.0 is in an Amazon ECR "
|
||||
+ "repository in us-west-2; it must be in the application's Region, us-east-1.", refused.getMessage());
|
||||
// The repositoryUri Floci's ECR hands out, in the Region, and an image outside ECR, which has none.
|
||||
for (String imageUri : List.of("000000000000.dkr.ecr.us-east-1.localhost:4566/spark/custom:7.5.0",
|
||||
"python:3.12")) {
|
||||
CreateApplicationRequest accepted = createRequest();
|
||||
accepted.setImageConfiguration(image(imageUri));
|
||||
assertEquals(imageUri, service.createApplication(accepted).getImageConfiguration().getImageUri());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUpdateNamingAnImageInAnotherRegionChangesNothing() {
|
||||
EmrServerlessService service = service();
|
||||
CreateApplicationRequest create = createRequest();
|
||||
create.setImageConfiguration(image(CUSTOM_IMAGE));
|
||||
String applicationId = service.createApplication(create).getApplicationId();
|
||||
UpdateApplicationRequest update = new UpdateApplicationRequest();
|
||||
update.setReleaseLabel("emr-7.10.0");
|
||||
update.setImageConfiguration(image("123456789012.dkr.ecr.us-west-2.amazonaws.com/spark/custom:7.10.0"));
|
||||
|
||||
assertThrows(AwsException.class, () -> service.updateApplication(applicationId, update));
|
||||
|
||||
Application unchanged = service.getApplication(applicationId);
|
||||
assertEquals("emr-7.5.0", unchanged.getReleaseLabel());
|
||||
assertEquals(CUSTOM_IMAGE, unchanged.getImageConfiguration().getImageUri());
|
||||
}
|
||||
|
||||
private static EmrServerlessService service() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.defaultAccountId()).thenReturn(ACCOUNT_ID);
|
||||
return new EmrServerlessService(config, new InMemoryStorageFactory(ACCOUNT_ID),
|
||||
new RegionResolver(REGION, ACCOUNT_ID), new MutableClock());
|
||||
}
|
||||
|
||||
private static CreateApplicationRequest createRequest() {
|
||||
CreateApplicationRequest create = new CreateApplicationRequest();
|
||||
create.setReleaseLabel("emr-7.5.0");
|
||||
create.setType("SPARK");
|
||||
create.setClientToken(UUID.randomUUID().toString());
|
||||
return create;
|
||||
}
|
||||
|
||||
static ImageConfiguration image(String imageUri) {
|
||||
ImageConfiguration image = new ImageConfiguration();
|
||||
image.setImageUri(imageUri);
|
||||
return image;
|
||||
}
|
||||
|
||||
static WorkerTypeSpecification workerType(String imageUri) {
|
||||
WorkerTypeSpecification specification = new WorkerTypeSpecification();
|
||||
specification.setImageConfiguration(image(imageUri));
|
||||
return specification;
|
||||
}
|
||||
}
|
||||
+40
-4
@@ -7,12 +7,15 @@ import com.github.dockerjava.api.model.Container;
|
||||
import com.github.dockerjava.api.model.Frame;
|
||||
import com.github.dockerjava.api.model.StreamType;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig.EcsServiceConfig.ImagePullBehavior;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
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.lambda.launcher.ImageCacheService.LaunchImage;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.LambdaExecutionRoleCredentials;
|
||||
import io.github.hectorvent.floci.services.s3.S3Service;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
@@ -50,6 +53,7 @@ import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -63,6 +67,7 @@ class EmrServerlessSparkRunnerTest {
|
||||
private final SparkJobRunner.Progress progress = mock(SparkJobRunner.Progress.class);
|
||||
private final S3Service s3 = mock(S3Service.class);
|
||||
private final ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class);
|
||||
private final EcrRegistryManager ecr = mock(EcrRegistryManager.class);
|
||||
|
||||
@Test
|
||||
void sparkSubmitRunsLocallyOnFlocisS3ThenTheRunsOwnSettings() {
|
||||
@@ -145,6 +150,31 @@ class EmrServerlessSparkRunnerTest {
|
||||
// Its JVMs trust Floci's CA, the trust store going in with the CA bundle as the container is created.
|
||||
verify(spec).withJavaTrustStore();
|
||||
verify(lifecycle, never()).copyJavaTrustStore(anyString());
|
||||
// The release's image is not in an ECR repository, so it is pulled once, as the container is created.
|
||||
verify(lifecycle, never()).resolveImageForLaunch(anyString(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anImageInAnEcrRepositoryRunsFromFlocisRegistryPulledForEveryRun() {
|
||||
ContainerBuilder builder = mock(ContainerBuilder.class);
|
||||
ContainerBuilder.Builder spec = mock(ContainerBuilder.Builder.class, RETURNS_SELF);
|
||||
String custom = "123456789012.dkr.ecr.eu-west-1.amazonaws.com/spark/custom:7.5.0";
|
||||
String inFlocisRegistry = "localhost:5100/123456789012/eu-west-1/spark/custom:7.5.0";
|
||||
String pulled = "sha256:" + "3".repeat(64);
|
||||
when(builder.newContainer(anyString())).thenReturn(spec);
|
||||
when(builder.resolveImage(inFlocisRegistry)).thenReturn(inFlocisRegistry);
|
||||
when(ecr.rewriteImageUri(custom)).thenReturn(inFlocisRegistry);
|
||||
when(lifecycle.resolveImageForLaunch(inFlocisRegistry, ImagePullBehavior.DEFAULT, null))
|
||||
.thenReturn(new LaunchImage(pulled, "sha256:" + "4".repeat(64)));
|
||||
EmrServerlessSparkRunner runner = runner(builder);
|
||||
|
||||
runner.run(customImageJob(custom), progress);
|
||||
runner.run(customImageJob(custom), progress);
|
||||
|
||||
// By the id each pull resolved, which a push under the same tag cannot move.
|
||||
verify(lifecycle, times(2)).resolveImageForLaunch(inFlocisRegistry, ImagePullBehavior.DEFAULT, null);
|
||||
verify(builder, times(2)).newContainer(pulled);
|
||||
verify(builder, never()).newContainer(custom);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -511,7 +541,7 @@ class EmrServerlessSparkRunnerTest {
|
||||
when(docker.inspectContainerCmd("driver").exec().getState().getRunning()).thenReturn(false);
|
||||
when(docker.inspectContainerCmd("driver").exec().getState().getExitCodeLong()).thenReturn(0L);
|
||||
return new EmrServerlessSparkRunner(builder, lifecycle, logStreamer,
|
||||
mock(LaunchedContainerAwsEnv.class), mock(LambdaExecutionRoleCredentials.class), s3, config,
|
||||
mock(LaunchedContainerAwsEnv.class), mock(LambdaExecutionRoleCredentials.class), s3, ecr, config,
|
||||
new MutableClock(), Duration.ZERO);
|
||||
}
|
||||
|
||||
@@ -579,13 +609,19 @@ class EmrServerlessSparkRunnerTest {
|
||||
Map<String, String> sparkDefaults, Duration timeout, String logFolder,
|
||||
SparkJobRunner.CloudWatchLogs cloudWatchLogs) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
imageTemplate, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, application,
|
||||
null, timeout, logFolder, cloudWatchLogs);
|
||||
imageTemplate, null, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults,
|
||||
application, null, timeout, logFolder, cloudWatchLogs);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job customImageJob(String customImage) {
|
||||
return new SparkJobRunner.Job("emr-serverless", "00f0000000000001", "123456789012", "eu-west-1", "emr-7.5.0",
|
||||
null, customImage, "floci-network", "arn:aws:iam::123456789012:role/job", Map.of(),
|
||||
List.of("s3://bucket/jobs/app.jar"), null, Duration.ZERO, null, null);
|
||||
}
|
||||
|
||||
private static SparkJobRunner.Job commandJob(List<String> command, Map<String, String> sparkDefaults) {
|
||||
return new SparkJobRunner.Job("emr", "s-1A2B3C4D5E6F7", "123456789012", "eu-west-1", "emr-7.5.0", null,
|
||||
"floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, List.of(), command,
|
||||
null, "floci-network", "arn:aws:iam::123456789012:role/job", sparkDefaults, List.of(), command,
|
||||
Duration.ZERO, null, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user