mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 01:43:02 -05:00
fix(ecs): keep task definitions and their revisions per region (#5078)
* fix(ecs): keep task definitions and their revisions per region Task definitions were keyed family:revision and revision counters by family alone, so registering a family in one region bumped its revision in every other, and ListTaskDefinitions, ListTaskDefinitionFamilies and the describe, deregister and delete lookups saw every region's definitions. Both stores now key by region (region::family:revision, region::family), as clusters already do, and every read keeps to the request region. A definition's region is read from its own ARN, so definitions persisted under the old keys still resolve; the next write re-files one under its new key, so none is listed twice. * fix(ecs): drop a task definition's old storage key only for that same definition The old family:revision key named no region, so re-filing a definition under its region's key removed whatever sat under that old key, even another region's not-yet-migrated definition. The old entry is now removed only when its ARN is the definition being written; EcsTaskDefinitionLegacyKeyTest seeds an old-key definition and covers both the re-filing and another region's registration of the same family and revision. * fix(ecs): read a task definition's region without failing on an unreadable ARN Every task definition list and lookup read each definition's region with AwsArnUtils.parse, which throws on a null or malformed ARN, so one bad stored record failed every call. The region now comes from regionOrDefault, as elsewhere in the tree: a definition whose ARN cannot be read counts as the request's region, and ListTaskDefinitions skips one with no ARN to list. * fix(ecs): keep a task definition with an unreadable ARN in the default region The fallback for a definition whose ARN cannot be read was the requesting region, so such a definition was seen from every region: its family's next revision in any region counted it, and lists and lookups everywhere included it. It now belongs to the default region, as a task with an unreadable ARN already does, so it still breaks no lookup and is seen in one region only. EcsTaskDefinitionLegacyKeyTest covers the same family registered in another region.
This commit is contained in:
1 parent
d9b2707ad4
commit
e22e24a810
4 files changed
+334
-30
No files matched your search
+153
@@ -0,0 +1,153 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
|
||||
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
|
||||
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Task definitions persisted before their storage key carried the region ({@code family:revision})
|
||||
* stay in their own region: they still resolve there, are re-filed on their next write, and are not
|
||||
* dropped when another region registers the same family and revision.
|
||||
*/
|
||||
class EcsTaskDefinitionLegacyKeyTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String OTHER = "eu-west-1";
|
||||
private static final String FAMILY = "legacy-fam";
|
||||
private static final String LEGACY_ARN =
|
||||
"arn:aws:ecs:" + REGION + ":000000000000:task-definition/" + FAMILY + ":1";
|
||||
|
||||
@Test
|
||||
void anotherRegionsRegistrationKeepsALegacyDefinition() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
storage.taskDefinitions().put(FAMILY + ":1", legacyDefinition());
|
||||
EcsService service = newService(storage);
|
||||
|
||||
TaskDefinition other = register(service, OTHER);
|
||||
|
||||
assertEquals(1, other.getRevision());
|
||||
assertEquals(List.of(LEGACY_ARN), arns(service, REGION));
|
||||
assertEquals(List.of(other.getTaskDefinitionArn()), arns(service, OTHER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLegacyDefinitionIsRefiledOnItsNextWriteAndListedOnce() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
storage.taskDefinitions().put(FAMILY + ":1", legacyDefinition());
|
||||
EcsService service = newService(storage);
|
||||
|
||||
service.deregisterTaskDefinition(FAMILY + ":1", REGION);
|
||||
|
||||
assertEquals(List.of(REGION + "::" + FAMILY + ":1"), List.copyOf(storage.taskDefinitions().keys()));
|
||||
assertEquals(List.of(LEGACY_ARN), service.listTaskDefinitions(REGION, FAMILY, "INACTIVE", null, null, null)
|
||||
.arns());
|
||||
assertEquals(2, register(service, REGION).getRevision());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStoredDefinitionWithoutAnArnBreaksNoLookup() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
TaskDefinition unreadable = legacyDefinition();
|
||||
unreadable.setFamily(FAMILY + "-unreadable");
|
||||
unreadable.setTaskDefinitionArn(null);
|
||||
storage.taskDefinitions().put(FAMILY + "-unreadable:1", unreadable);
|
||||
EcsService service = newService(storage);
|
||||
|
||||
TaskDefinition registered = register(service, REGION);
|
||||
|
||||
assertEquals(List.of(registered.getTaskDefinitionArn()), arns(service, REGION));
|
||||
assertEquals(registered.getTaskDefinitionArn(),
|
||||
service.describeTaskDefinition(FAMILY + ":1", REGION).getTaskDefinitionArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStoredDefinitionWithoutAnArnStaysInTheDefaultRegion() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
TaskDefinition unreadable = legacyDefinition();
|
||||
unreadable.setTaskDefinitionArn(null);
|
||||
storage.taskDefinitions().put(FAMILY + ":1", unreadable);
|
||||
EcsService service = newService(storage);
|
||||
|
||||
TaskDefinition other = register(service, OTHER);
|
||||
|
||||
assertEquals(1, other.getRevision());
|
||||
assertEquals(other.getTaskDefinitionArn(),
|
||||
service.describeTaskDefinition(FAMILY + ":1", OTHER).getTaskDefinitionArn());
|
||||
assertEquals(List.of(other.getTaskDefinitionArn()), arns(service, OTHER));
|
||||
assertEquals(2, register(service, REGION).getRevision());
|
||||
}
|
||||
|
||||
private static TaskDefinition legacyDefinition() {
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
td.setFamily(FAMILY);
|
||||
td.setRevision(1);
|
||||
td.setStatus("ACTIVE");
|
||||
td.setTaskDefinitionArn(LEGACY_ARN);
|
||||
return td;
|
||||
}
|
||||
|
||||
private static TaskDefinition register(EcsService service, String region) {
|
||||
ContainerDefinition cd = new ContainerDefinition();
|
||||
cd.setName("app");
|
||||
cd.setImage("app:1");
|
||||
return service.registerTaskDefinition(FAMILY, List.of(cd), null, null, null, null, null, List.of(), region);
|
||||
}
|
||||
|
||||
private static List<String> arns(EcsService service, String region) {
|
||||
return service.listTaskDefinitions(region, FAMILY, null, null, null, null).arns();
|
||||
}
|
||||
|
||||
private static EcsService newService(StorageFactory storage) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
EcsService service = new EcsService(
|
||||
new RegionResolver(REGION, "000000000000"),
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storage,
|
||||
null);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
AccountAwareStorageBackend<TaskDefinition> taskDefinitions() {
|
||||
return (AccountAwareStorageBackend<TaskDefinition>) stores.computeIfAbsent("ecs-task-definitions.json",
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* Task definitions are regional: a family registered in two regions has its own revisions in each,
|
||||
* and a region lists and describes only its own.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class EcsTaskDefinitionRegionIntegrationTest {
|
||||
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String TARGET_PREFIX = "AmazonEC2ContainerServiceV20141113.";
|
||||
private static final String HOME = "eu-west-1";
|
||||
private static final String OTHER = "us-west-2";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private static RequestSpecification ecs(String region, String action) {
|
||||
return given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region
|
||||
+ "/ecs/aws4_request, SignedHeaders=host, Signature=abc")
|
||||
.header("X-Amz-Target", TARGET_PREFIX + action);
|
||||
}
|
||||
|
||||
private static String register(String region, String family) {
|
||||
return ecs(region, "RegisterTaskDefinition")
|
||||
.body("""
|
||||
{"family": "%s", "containerDefinitions": [{"name": "app", "image": "nginx", "memory": 128}]}
|
||||
""".formatted(family))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinition.revision", equalTo(1))
|
||||
.body("taskDefinition.taskDefinitionArn", containsString(":" + region + ":"))
|
||||
.extract().path("taskDefinition.taskDefinitionArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
void taskDefinitionsStayInTheirRegion() {
|
||||
String family = "region-td-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String homeArn = register(HOME, family);
|
||||
String otherArn = register(OTHER, family);
|
||||
|
||||
ecs(HOME, "ListTaskDefinitions")
|
||||
.body("{\"familyPrefix\": \"" + family + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinitionArns", contains(homeArn));
|
||||
ecs(OTHER, "ListTaskDefinitions")
|
||||
.body("{\"familyPrefix\": \"" + family + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("taskDefinitionArns", contains(otherArn));
|
||||
ecs("ap-southeast-1", "ListTaskDefinitionFamilies")
|
||||
.body("{\"familyPrefix\": \"" + family + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("families", empty());
|
||||
ecs(OTHER, "DescribeTaskDefinition")
|
||||
.body("{\"taskDefinition\": \"" + homeArn + "\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", endsWith("ClientException"));
|
||||
|
||||
for (String region : new String[] {HOME, OTHER}) {
|
||||
ecs(region, "DeregisterTaskDefinition")
|
||||
.body("{\"taskDefinition\": \"" + family + ":1\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
ecs(region, "DeleteTaskDefinitions")
|
||||
.body("{\"taskDefinitions\": [\"" + family + ":1\"]}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user