mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ecs): register ECS service containers as ELBv2 targets (#929)
* feat(ecs): add loadBalancers to the ECS service model * feat(ecs): register ECS service containers as ELBv2 targets * test(ecs): cover loadBalancers parsing and ELBv2 target registration * fix(ecs): reject malformed loadBalancers entries on CreateService * refactor(ecs): gate loadBalancer deregister hook on dockerMode * refactor(ecs): collapse EcsService to a single constructor * fix(ecs): prefer a routable network when resolving container IP
This commit is contained in:
1 parent
9e7cbe012f
commit
eb1ca7b674
8 files changed
+544
-16
No files matched your search
+109
@@ -0,0 +1,109 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import io.github.hectorvent.floci.services.ecs.model.Container;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
|
||||
import io.github.hectorvent.floci.services.ecs.model.NetworkBinding;
|
||||
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;
|
||||
import io.github.hectorvent.floci.services.elbv2.model.TargetHealth;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Component test for {@link EcsLoadBalancerRegistrar}: drives the register/deregister
|
||||
* logic directly with a synthetic task and a real {@link ElbV2Service}, since ECS runs
|
||||
* in mock mode (no Docker containers) under {@code @QuarkusTest}.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class EcsLoadBalancerRegistrarTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Inject
|
||||
EcsLoadBalancerRegistrar registrar;
|
||||
|
||||
@Inject
|
||||
ElbV2Service elbV2Service;
|
||||
|
||||
private String createTargetGroup(String name) {
|
||||
TargetGroup tg = elbV2Service.createTargetGroup(REGION, name, "HTTP", "HTTP1",
|
||||
8080, "vpc-regtest", "ip",
|
||||
null, null, false, null, null, null, null, null, null, null, Map.of());
|
||||
return tg.getTargetGroupArn();
|
||||
}
|
||||
|
||||
private EcsTask taskWithContainer(String containerName, int containerPort, int hostPort) {
|
||||
Container container = new Container();
|
||||
container.setName(containerName);
|
||||
container.setNetworkBindings(List.of(
|
||||
new NetworkBinding("0.0.0.0", containerPort, hostPort, "tcp")));
|
||||
EcsTask task = new EcsTask();
|
||||
task.setTaskArn("arn:aws:ecs:" + REGION + ":000000000000:task/c/regtest");
|
||||
task.setGroup("regtest-svc");
|
||||
task.setContainers(List.of(container));
|
||||
return task;
|
||||
}
|
||||
|
||||
private EcsServiceModel serviceWithLb(String tgArn, String containerName, int containerPort) {
|
||||
EcsLoadBalancer lb = new EcsLoadBalancer();
|
||||
lb.setTargetGroupArn(tgArn);
|
||||
lb.setContainerName(containerName);
|
||||
lb.setContainerPort(containerPort);
|
||||
EcsServiceModel svc = new EcsServiceModel();
|
||||
svc.setServiceName("regtest-svc");
|
||||
svc.setLoadBalancers(List.of(lb));
|
||||
return svc;
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerThenDeregisterTaskContainer() {
|
||||
String tgArn = createTargetGroup("reg-tg-1");
|
||||
EcsTask task = taskWithContainer("web", 8080, 34567);
|
||||
EcsServiceModel svc = serviceWithLb(tgArn, "web", 8080);
|
||||
|
||||
registrar.registerTask(task, svc, REGION);
|
||||
|
||||
List<TargetHealth> health = elbV2Service.describeTargetHealth(REGION, tgArn, null);
|
||||
assertEquals(1, health.size(), "one target should be registered");
|
||||
// ECS containers are reached at 127.0.0.1:<hostPort> in native mode.
|
||||
assertEquals("127.0.0.1", health.get(0).getTarget().getId());
|
||||
assertEquals(34567, health.get(0).getTarget().getPort());
|
||||
|
||||
registrar.deregisterTask(task, svc, REGION);
|
||||
assertTrue(elbV2Service.describeTargetHealth(REGION, tgArn, null).isEmpty(),
|
||||
"target should be deregistered");
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceWithoutLoadBalancersRegistersNothing() {
|
||||
String tgArn = createTargetGroup("reg-tg-2");
|
||||
EcsTask task = taskWithContainer("web", 8080, 40000);
|
||||
EcsServiceModel svc = new EcsServiceModel(); // no loadBalancers
|
||||
svc.setServiceName("regtest-svc");
|
||||
|
||||
registrar.registerTask(task, svc, REGION);
|
||||
assertTrue(elbV2Service.describeTargetHealth(REGION, tgArn, null).isEmpty(),
|
||||
"no loadBalancers block -> no target registered");
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerPortMismatchRegistersNothing() {
|
||||
String tgArn = createTargetGroup("reg-tg-3");
|
||||
// task container exposes 8080, but the loadBalancers block points at 9999
|
||||
EcsTask task = taskWithContainer("web", 8080, 41000);
|
||||
EcsServiceModel svc = serviceWithLb(tgArn, "web", 9999);
|
||||
|
||||
registrar.registerTask(task, svc, REGION);
|
||||
assertTrue(elbV2Service.describeTargetHealth(REGION, tgArn, null).isEmpty(),
|
||||
"no network binding for the declared containerPort -> no target");
|
||||
}
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* Integration tests for the ECS service {@code loadBalancers} block (mock mode).
|
||||
* Verifies the field is parsed on CreateService and echoed on DescribeServices.
|
||||
* Actual ELBv2 target registration requires Docker-backed tasks and is covered by
|
||||
* {@link EcsLoadBalancerRegistrarTest} (registrar logic) and the end-to-end scenario.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class EcsLoadBalancersIntegrationTest {
|
||||
|
||||
private static final String TARGET = "AmazonEC2ContainerServiceV20141113.";
|
||||
private static final String CT = "application/x-amz-json-1.1";
|
||||
|
||||
@BeforeAll
|
||||
static void configure() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private static void call(String action, String body) {
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + action)
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private void seedClusterAndTaskDef(String cluster, String family) {
|
||||
call("CreateCluster", "{\"clusterName\":\"" + cluster + "\"}");
|
||||
call("RegisterTaskDefinition", "{\"family\":\"" + family + "\","
|
||||
+ "\"containerDefinitions\":[{\"name\":\"web\",\"image\":\"nginx\","
|
||||
+ "\"memory\":128,\"portMappings\":[{\"containerPort\":8080}]}]}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createServiceParsesAndEchoesLoadBalancers() {
|
||||
seedClusterAndTaskDef("lb-cluster-1", "lb-td-1");
|
||||
String tgArn = "arn:aws:elasticloadbalancing:us-east-1:000000000000:targetgroup/lbtg1/abc123";
|
||||
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + "CreateService")
|
||||
.body("{\"cluster\":\"lb-cluster-1\",\"serviceName\":\"lb-svc-1\","
|
||||
+ "\"taskDefinition\":\"lb-td-1\",\"desiredCount\":1,"
|
||||
+ "\"loadBalancers\":[{\"targetGroupArn\":\"" + tgArn + "\","
|
||||
+ "\"containerName\":\"web\",\"containerPort\":8080}]}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("service.serviceName", equalTo("lb-svc-1"))
|
||||
.body("service.loadBalancers", hasSize(1))
|
||||
.body("service.loadBalancers[0].targetGroupArn", equalTo(tgArn))
|
||||
.body("service.loadBalancers[0].containerName", equalTo("web"))
|
||||
.body("service.loadBalancers[0].containerPort", equalTo(8080));
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeServicesEchoesLoadBalancers() {
|
||||
seedClusterAndTaskDef("lb-cluster-2", "lb-td-2");
|
||||
String tgArn = "arn:aws:elasticloadbalancing:us-east-1:000000000000:targetgroup/lbtg2/def456";
|
||||
call("CreateService", "{\"cluster\":\"lb-cluster-2\",\"serviceName\":\"lb-svc-2\","
|
||||
+ "\"taskDefinition\":\"lb-td-2\",\"desiredCount\":1,"
|
||||
+ "\"loadBalancers\":[{\"targetGroupArn\":\"" + tgArn + "\","
|
||||
+ "\"containerName\":\"web\",\"containerPort\":8080}]}");
|
||||
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + "DescribeServices")
|
||||
.body("{\"cluster\":\"lb-cluster-2\",\"services\":[\"lb-svc-2\"]}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("services", hasSize(1))
|
||||
.body("services[0].loadBalancers", hasSize(1))
|
||||
.body("services[0].loadBalancers[0].targetGroupArn", equalTo(tgArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createServiceWithoutLoadBalancersOmitsField() {
|
||||
seedClusterAndTaskDef("lb-cluster-3", "lb-td-3");
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + "CreateService")
|
||||
.body("{\"cluster\":\"lb-cluster-3\",\"serviceName\":\"lb-svc-3\","
|
||||
+ "\"taskDefinition\":\"lb-td-3\",\"desiredCount\":1}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("service.serviceName", equalTo("lb-svc-3"))
|
||||
.body("service.loadBalancers", org.hamcrest.Matchers.nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createServiceRejectsLoadBalancerEntryMissingRequiredFields() {
|
||||
seedClusterAndTaskDef("lb-cluster-5", "lb-td-5");
|
||||
// entry has containerName but no containerPort and no targetGroupArn/loadBalancerName
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + "CreateService")
|
||||
.body("{\"cluster\":\"lb-cluster-5\",\"serviceName\":\"lb-svc-5\","
|
||||
+ "\"taskDefinition\":\"lb-td-5\",\"desiredCount\":1,"
|
||||
+ "\"loadBalancers\":[{\"containerName\":\"web\"}]}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", org.hamcrest.Matchers.containsString("InvalidParameterException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createServiceRejectsLoadBalancerEntryWithoutTargetGroup() {
|
||||
seedClusterAndTaskDef("lb-cluster-6", "lb-td-6");
|
||||
// containerName + containerPort present, but neither targetGroupArn nor loadBalancerName
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + "CreateService")
|
||||
.body("{\"cluster\":\"lb-cluster-6\",\"serviceName\":\"lb-svc-6\","
|
||||
+ "\"taskDefinition\":\"lb-td-6\",\"desiredCount\":1,"
|
||||
+ "\"loadBalancers\":[{\"containerName\":\"web\",\"containerPort\":8080}]}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", org.hamcrest.Matchers.containsString("InvalidParameterException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serviceWithLoadBalancersIsStableInMockMode() {
|
||||
// Mock mode launches no real containers; the registrar must no-op cleanly.
|
||||
seedClusterAndTaskDef("lb-cluster-4", "lb-td-4");
|
||||
String tgArn = "arn:aws:elasticloadbalancing:us-east-1:000000000000:targetgroup/lbtg4/ghi789";
|
||||
call("CreateService", "{\"cluster\":\"lb-cluster-4\",\"serviceName\":\"lb-svc-4\","
|
||||
+ "\"taskDefinition\":\"lb-td-4\",\"desiredCount\":1,"
|
||||
+ "\"loadBalancers\":[{\"targetGroupArn\":\"" + tgArn + "\","
|
||||
+ "\"containerName\":\"web\",\"containerPort\":8080}]}");
|
||||
// Service is describable and intact — no crash from the registrar hook.
|
||||
given().contentType(CT).header("X-Amz-Target", TARGET + "DescribeServices")
|
||||
.body("{\"cluster\":\"lb-cluster-4\",\"services\":[\"lb-svc-4\"]}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("services[0].serviceArn", notNullValue())
|
||||
.body("services[0].status", equalTo("ACTIVE"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user