Light, fluffy, and always free - The AWS Local Emulator alternative
fix(ecs): report serviceConnectConfiguration on the service's deployment (#3662)
lex007da4ee0b6b1a
diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.javaindex de694cbc68..6261dd3bea 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsJsonHandler.java@@ -413,10 +413,13 @@ public class EcsJsonHandler {String deploymentControllerType = parseChoice(req.path("deploymentController"), "type","deploymentController.type", DEPLOYMENT_CONTROLLER_TYPES);String availabilityZoneRebalancing = parseChoice(req, "availabilityZoneRebalancing", AZ_REBALANCING);+ Map<String, Object> serviceConnectConfiguration =+ parseServiceConnectConfiguration(req.path("serviceConnectConfiguration"));EcsServiceModel svc = service.createService(cluster, serviceName, taskDefinition,desiredCount, launchType, loadBalancers, networkConfiguration, tags,- schedulingStrategy, deploymentControllerType, availabilityZoneRebalancing, region);+ schedulingStrategy, deploymentControllerType, availabilityZoneRebalancing,+ serviceConnectConfiguration, region);ObjectNode resp = objectMapper.createObjectNode();resp.set("service", serviceNode(svc));@@ -493,9 +496,12 @@ public class EcsJsonHandler {NetworkConfiguration networkConfiguration = parseNetworkConfiguration(req.path("networkConfiguration"));String availabilityZoneRebalancing = parseChoice(req, "availabilityZoneRebalancing", AZ_REBALANCING);boolean forceNewDeployment = req.path("forceNewDeployment").asBoolean(false);+ Map<String, Object> serviceConnectConfiguration =+ parseServiceConnectConfiguration(req.path("serviceConnectConfiguration"));EcsServiceModel svc = service.updateService(cluster, serviceName, taskDefinition, desiredCount,- networkConfiguration, availabilityZoneRebalancing, forceNewDeployment, region);+ networkConfiguration, availabilityZoneRebalancing, forceNewDeployment,+ serviceConnectConfiguration, region);ObjectNode resp = objectMapper.createObjectNode();resp.set("service", serviceNode(svc));@@ -1317,9 +1323,25 @@ public class EcsJsonHandler {if (d.getLaunchType() != null) { n.put("launchType", d.getLaunchType().name()); }if (d.getCreatedAt() != null) { n.put("createdAt", d.getCreatedAt().toEpochMilli() / 1000.0); }if (d.getUpdatedAt() != null) { n.put("updatedAt", d.getUpdatedAt().toEpochMilli() / 1000.0); }+ if (d.getServiceConnectConfiguration() != null) {+ n.set("serviceConnectConfiguration", objectMapper.valueToTree(d.getServiceConnectConfiguration()));+ }return n;}+ /**+ * Keeps the caller's Service Connect configuration as given. AWS's {@code Service} shape has+ * no member for it, so DescribeServices reports it on each deployment rather than on the+ * service, and a generated client drops anything written anywhere else.+ */+ @SuppressWarnings("unchecked")+ private Map<String, Object> parseServiceConnectConfiguration(JsonNode node) {+ if (node == null || !node.isObject()) {+ return null;+ }+ return objectMapper.convertValue(node, Map.class);+ }+private ObjectNode containerInstanceNode(ContainerInstance ci) {ObjectNode n = objectMapper.createObjectNode();n.put("containerInstanceArn", ci.getContainerInstanceArn());diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.javaindex 95c177ace7..0e710c855d 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java@@ -808,6 +808,25 @@ public class EcsService implements ContainerTeardown, ResourceProvider {Map<String, String> tags, String schedulingStrategy,String deploymentControllerType, String availabilityZoneRebalancing,String region) {+ return createService(clusterRef, serviceName, taskDefinition, desiredCount, launchType,+ loadBalancers, networkConfiguration, tags, schedulingStrategy, deploymentControllerType,+ availabilityZoneRebalancing, null, region);+ }++ /**+ * @param schedulingStrategy {@code REPLICA} (default) or {@code DAEMON}+ * @param deploymentControllerType {@code ECS} (default), {@code CODE_DEPLOY} or {@code EXTERNAL}+ * @param availabilityZoneRebalancing {@code ENABLED} or {@code DISABLED} (default)+ * @param serviceConnectConfiguration reported back on each {@code deployments[]} entry+ */+ public EcsServiceModel createService(String clusterRef, String serviceName, String taskDefinition,+ int desiredCount, LaunchType launchType,+ List<EcsLoadBalancer> loadBalancers,+ NetworkConfiguration networkConfiguration,+ Map<String, String> tags, String schedulingStrategy,+ String deploymentControllerType, String availabilityZoneRebalancing,+ Map<String, Object> serviceConnectConfiguration,+ String region) {EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);// AWS resolves family / family:revision at create time and stores the ARN; the// reconciler compares it with each task's taskDefinitionArn, so pin it here.@@ -852,6 +871,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider {svc.setDeploymentController(controller);svc.setAvailabilityZoneRebalancing(availabilityZoneRebalancing != null? availabilityZoneRebalancing : DEFAULT_AZ_REBALANCING_ON_CREATE);+ svc.setServiceConnectConfiguration(serviceConnectConfiguration);svc.setStatus("ACTIVE");svc.setCreatedAt(Instant.now());svc.setLastDeploymentAt(svc.getCreatedAt());@@ -890,6 +910,15 @@ public class EcsService implements ContainerTeardown, ResourceProvider {Integer desiredCount, NetworkConfiguration networkConfiguration,String availabilityZoneRebalancing, boolean forceNewDeployment,String region) {+ return updateService(clusterRef, serviceName, taskDefinition, desiredCount, networkConfiguration,+ availabilityZoneRebalancing, forceNewDeployment, null, region);+ }++ public EcsServiceModel updateService(String clusterRef, String serviceName, String taskDefinition,+ Integer desiredCount, NetworkConfiguration networkConfiguration,+ String availabilityZoneRebalancing, boolean forceNewDeployment,+ Map<String, Object> serviceConnectConfiguration,+ String region) {EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);serviceName = extractServiceName(serviceName);@@ -915,13 +944,21 @@ public class EcsService implements ContainerTeardown, ResourceProvider {if (availabilityZoneRebalancing != null) {svc.setAvailabilityZoneRebalancing(availabilityZoneRebalancing);}+ // UpdateServiceRequest.serviceConnectConfiguration is documented as "This parameter+ // triggers a new service deployment", so a real change rolls the deployment the way a+ // task-definition change does. An omitted parameter is not a change and rolls nothing.+ boolean serviceConnectChanged = serviceConnectConfiguration != null+ && !serviceConnectConfiguration.equals(svc.getServiceConnectConfiguration());+ if (serviceConnectConfiguration != null) {+ svc.setServiceConnectConfiguration(serviceConnectConfiguration);+ }boolean taskDefChanged = false;if (taskDefinition != null) {String resolvedArn = resolveTaskDefinitionOrThrow(taskDefinition, region).getTaskDefinitionArn();taskDefChanged = !resolvedArn.equals(svc.getTaskDefinition());svc.setTaskDefinition(resolvedArn);}- if (taskDefChanged || forceNewDeployment) {+ if (taskDefChanged || forceNewDeployment || serviceConnectChanged) {svc.setDeploymentId(newDeploymentId());svc.setLastDeploymentAt(Instant.now());recordServiceDeployment(svc, svc.getTaskDefinition(), region);@@ -1496,6 +1533,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider {d.setRolloutStateReason("ECS deployment " + deploymentId+ (converged ? " completed." : " in progress."));d.setLaunchType(svc.getLaunchType());+ d.setServiceConnectConfiguration(svc.getServiceConnectConfiguration());// The deployment's own start time, not the service's: a task-definition change mints a// new deployment id, so reporting service creation here would contradict it. Older// persisted services predate the field and fall back to the service creation time.diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/model/Deployment.java b/src/main/java/io/github/hectorvent/floci/services/ecs/model/Deployment.javaindex ff7665138e..933f280973 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecs/model/Deployment.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/model/Deployment.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ecs.model;import io.quarkus.runtime.annotations.RegisterForReflection;import java.time.Instant;+import java.util.Map;/*** A deployment of an ECS service, as reported in {@code DescribeServices}'@@ -25,6 +26,12 @@ public class Deployment {private LaunchType launchType;private Instant createdAt;private Instant updatedAt;+ /**+ * Raw passthrough of the service's Service Connect configuration. AWS's own {@code Service}+ * shape carries no {@code serviceConnectConfiguration} member, only {@code Deployment} does,+ * so this is the single place a generated client can read it back from.+ */+ private Map<String, Object> serviceConnectConfiguration;public String getId() { return id; }public void setId(String id) { this.id = id; }@@ -50,4 +57,8 @@ public class Deployment {public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }public Instant getUpdatedAt() { return updatedAt; }public void setUpdatedAt(Instant updatedAt) { this.updatedAt = updatedAt; }+ public Map<String, Object> getServiceConnectConfiguration() { return serviceConnectConfiguration; }+ public void setServiceConnectConfiguration(Map<String, Object> serviceConnectConfiguration) {+ this.serviceConnectConfiguration = serviceConnectConfiguration;+ }}diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/model/EcsServiceModel.java b/src/main/java/io/github/hectorvent/floci/services/ecs/model/EcsServiceModel.javaindex 16a7e59b93..448bd057b1 100644--- a/src/main/java/io/github/hectorvent/floci/services/ecs/model/EcsServiceModel.java+++ b/src/main/java/io/github/hectorvent/floci/services/ecs/model/EcsServiceModel.java@@ -34,6 +34,12 @@ public class EcsServiceModel {private Map<String, String> tags = new HashMap<>();private List<EcsLoadBalancer> loadBalancers = new ArrayList<>();private NetworkConfiguration networkConfiguration;+ /**+ * The Service Connect configuration given on CreateService or UpdateService, kept raw.+ * DescribeServices reports it on each deployments[] entry, which is where AWS's model puts+ * it; the Service shape itself has no member for it.+ */+ private Map<String, Object> serviceConnectConfiguration;public String getServiceArn() { return serviceArn; }public void setServiceArn(String serviceArn) { this.serviceArn = serviceArn; }@@ -101,4 +107,9 @@ public class EcsServiceModel {public void setNetworkConfiguration(NetworkConfiguration networkConfiguration) {this.networkConfiguration = networkConfiguration;}++ public Map<String, Object> getServiceConnectConfiguration() { return serviceConnectConfiguration; }+ public void setServiceConnectConfiguration(Map<String, Object> serviceConnectConfiguration) {+ this.serviceConnectConfiguration = serviceConnectConfiguration;+ }}diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceConnectConfigurationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceConnectConfigurationIntegrationTest.javanew file mode 100644index 0000000000..16b935797f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServiceConnectConfigurationIntegrationTest.java@@ -0,0 +1,180 @@+package io.github.hectorvent.floci.services.ecs;++import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.response.Response;+import org.junit.jupiter.api.BeforeAll;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertTrue;++/**+ * DescribeServices has to report a Service Connect configuration on each {@code deployments[]}+ * entry. AWS's {@code Service} shape declares no {@code serviceConnectConfiguration} member, so a+ * generated client parses the field only at the deployment location and drops it anywhere else.+ * Terraform's provider reads it from there too, and reports a service with no readable value as a+ * pending change on every plan.+ */+@QuarkusTest+class EcsServiceConnectConfigurationIntegrationTest {++ private static final String TARGET = "AmazonEC2ContainerServiceV20141113.";+ private static final String CT = "application/x-amz-json-1.1";+ private static final String CLUSTER = "sc-cluster";+ private static final String FAMILY = "sc-fam";++ private static final String CONFIG = "{\"enabled\":true,\"namespace\":\"sc-ns\","+ + "\"services\":[{\"portName\":\"http\",\"discoveryName\":\"web\","+ + "\"clientAliases\":[{\"port\":8080,\"dnsName\":\"web.sc-ns\"}]}]}";++ @BeforeAll+ static void configure() {+ RestAssuredJsonUtils.configureAwsContentTypes();+ }++ private static Response call(String action, String body) {+ return given().contentType(CT).header("X-Amz-Target", TARGET + action)+ .body(body)+ .when().post("/")+ .then().statusCode(200)+ .extract().response();+ }++ private static void seed() {+ call("CreateCluster", "{\"clusterName\":\"" + CLUSTER + "\"}");+ call("RegisterTaskDefinition", "{\"family\":\"" + FAMILY + "\","+ + "\"containerDefinitions\":[{\"name\":\"web\",\"image\":\"nginx\",\"memory\":128}]}");+ }++ private static Response describe(String serviceName) {+ return call("DescribeServices", "{\"cluster\":\"" + CLUSTER + "\","+ + "\"services\":[\"" + serviceName + "\"]}");+ }++ @Test+ void createService_reportsServiceConnectConfigurationOnTheDeployment() {+ seed();+ String name = "sc-create";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0,"+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");++ Response described = describe(name);+ String base = "services[0].deployments[0].serviceConnectConfiguration.";++ assertEquals(true, described.jsonPath().getBoolean(base + "enabled"));+ assertEquals("sc-ns", described.jsonPath().getString(base + "namespace"));+ assertEquals("http", described.jsonPath().getString(base + "services[0].portName"));+ assertEquals("web", described.jsonPath().getString(base + "services[0].discoveryName"));+ assertEquals("web.sc-ns",+ described.jsonPath().getString(base + "services[0].clientAliases[0].dnsName"));+ assertEquals(8080, described.jsonPath().getInt(base + "services[0].clientAliases[0].port"));++ // The Service shape has no member for it, so nothing should appear at the top level.+ assertNull(described.jsonPath().get("services[0].serviceConnectConfiguration"));+ }++ @Test+ void updateService_replacesTheStoredServiceConnectConfiguration() {+ seed();+ String name = "sc-update";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0,"+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");++ call("UpdateService", "{\"cluster\":\"" + CLUSTER + "\",\"service\":\"" + name + "\","+ + "\"serviceConnectConfiguration\":{\"enabled\":false}}");++ Response described = describe(name);+ String base = "services[0].deployments[0].serviceConnectConfiguration.";+ assertEquals(false, described.jsonPath().getBoolean(base + "enabled"));+ assertNull(described.jsonPath().get(base + "namespace"));+ }++ @Test+ void updateService_withoutTheField_keepsTheStoredConfiguration() {+ seed();+ String name = "sc-keep";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0,"+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");++ call("UpdateService", "{\"cluster\":\"" + CLUSTER + "\",\"service\":\"" + name + "\","+ + "\"desiredCount\":0}");++ Response described = describe(name);+ assertEquals("sc-ns", described.jsonPath()+ .getString("services[0].deployments[0].serviceConnectConfiguration.namespace"));+ }++ private static String deploymentId(String serviceName) {+ return describe(serviceName).jsonPath().getString("services[0].deployments[0].id");+ }++ // UpdateServiceRequest.serviceConnectConfiguration is documented as triggering a new service+ // deployment, so a changed configuration must not be reported under the old deployment id.+ @Test+ void updateService_changingTheConfiguration_rollsTheDeployment() {+ seed();+ String name = "sc-rolls";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0,"+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");+ String before = deploymentId(name);++ call("UpdateService", "{\"cluster\":\"" + CLUSTER + "\",\"service\":\"" + name + "\","+ + "\"serviceConnectConfiguration\":{\"enabled\":false}}");++ assertNotEquals(before, deploymentId(name),+ "a Service Connect change starts a new deployment");+ }++ @Test+ void updateService_resendingTheSameConfiguration_keepsTheDeployment() {+ seed();+ String name = "sc-same";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0,"+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");+ String before = deploymentId(name);++ call("UpdateService", "{\"cluster\":\"" + CLUSTER + "\",\"service\":\"" + name + "\","+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");++ assertEquals(before, deploymentId(name),+ "resending an identical configuration is not a change");+ }++ @Test+ void updateService_withoutTheField_keepsTheDeployment() {+ seed();+ String name = "sc-untouched";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0,"+ + "\"serviceConnectConfiguration\":" + CONFIG + "}");+ String before = deploymentId(name);++ call("UpdateService", "{\"cluster\":\"" + CLUSTER + "\",\"service\":\"" + name + "\","+ + "\"desiredCount\":0}");++ assertEquals(before, deploymentId(name),+ "omitting the parameter is not a change");+ }++ @Test+ void createService_withoutTheField_reportsNoServiceConnectConfiguration() {+ seed();+ String name = "sc-absent";+ call("CreateService", "{\"cluster\":\"" + CLUSTER + "\",\"serviceName\":\"" + name + "\","+ + "\"taskDefinition\":\"" + FAMILY + "\",\"desiredCount\":0}");++ Response described = describe(name);+ assertTrue(described.jsonPath().getList("services[0].deployments").size() == 1);+ assertNull(described.jsonPath()+ .get("services[0].deployments[0].serviceConnectConfiguration"));+ }+}