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.java index 744af30a23..fab1bde7cb 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 @@ -143,7 +143,8 @@ public class EcsJsonHandler { private Response handleCreateCluster(JsonNode req, String region) { String name = req.path("clusterName").asText(null); Map tags = parseTagMap(req.path("tags")); - EcsCluster cluster = service.createCluster(name, tags, region); + List settings = parseClusterSettings(req.path("settings")); + EcsCluster cluster = service.createCluster(name, tags, settings, region); ObjectNode resp = objectMapper.createObjectNode(); resp.set("cluster", clusterNode(cluster)); return Response.ok(resp).build(); 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.java index 5aa7b40c98..634829caa4 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 @@ -221,6 +221,20 @@ public class EcsService implements ContainerTeardown, ResourceProvider { } public EcsCluster createCluster(String clusterName, Map tags, String region) { + return createCluster(clusterName, tags, null, region); + } + + /** + * Creates a cluster, applying any {@code settings} the request carried. + * + *

Real AWS accepts {@code settings} (containerInsights and friends) on CreateCluster itself, + * not only through UpdateClusterSettings. Dropping them here meant a client that configured the + * cluster at creation, such as terraform-provider-aws's {@code setting} block, saw + * DescribeClusters come back without them and proposed the same change on every subsequent + * plan, forever (issue #2806). + */ + public EcsCluster createCluster(String clusterName, Map tags, + List settings, String region) { String name = (clusterName == null || clusterName.isBlank()) ? DEFAULT_CLUSTER : clusterName; String key = clusterKey(region, name); if (clusters.containsKey(key)) { @@ -233,6 +247,9 @@ public class EcsService implements ContainerTeardown, ResourceProvider { if (tags != null && !tags.isEmpty()) { cluster.setTags(new LinkedHashMap<>(tags)); } + if (settings != null && !settings.isEmpty()) { + cluster.setSettings(new ArrayList<>(settings)); + } clusters.put(key, cluster); LOG.infov("Created ECS cluster: {0} in {1}", name, region); return cluster; diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsCreateClusterSettingsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsCreateClusterSettingsIntegrationTest.java new file mode 100644 index 0000000000..8fb0b0200e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsCreateClusterSettingsIntegrationTest.java @@ -0,0 +1,87 @@ +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; + +/** + * Issue #2806: CreateCluster dropped the request's {@code settings}, so a cluster configured at + * creation came back from DescribeClusters without them. Clients that set settings on create, such + * as terraform-provider-aws's {@code setting} block, then proposed the same change on every plan + * after apply, because the state they read never matched the configuration they sent. + * + *

Real AWS accepts settings on CreateCluster itself, not only through UpdateClusterSettings. + */ +@QuarkusTest +class EcsCreateClusterSettingsIntegrationTest { + + private static final String TARGET_PREFIX = "AmazonEC2ContainerServiceV20141113."; + private static final String CONTENT_TYPE = "application/x-amz-json-1.1"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + private static io.restassured.specification.RequestSpecification ecs(String action) { + return given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", TARGET_PREFIX + action); + } + + @Test + void settingsGivenOnCreateAreReturnedByCreateAndSurviveToDescribe() { + String cluster = "create-settings-" + Long.toString(System.nanoTime(), 36); + + ecs("CreateCluster") + .body(""" + {"clusterName": "%s", + "settings": [{"name": "containerInsights", "value": "enabled"}]} + """.formatted(cluster)) + .when() + .post("/") + .then() + .statusCode(200) + .body("cluster.clusterName", equalTo(cluster)) + .body("cluster.settings", hasSize(1)) + .body("cluster.settings[0].name", equalTo("containerInsights")) + .body("cluster.settings[0].value", equalTo("enabled")); + + // The drift the issue describes is not in the CreateCluster response but in what a later + // read reports, which is what a client compares its configuration against. + ecs("DescribeClusters") + .body(""" + {"clusters": ["%s"]} + """.formatted(cluster)) + .when() + .post("/") + .then() + .statusCode(200) + .body("clusters[0].settings", hasSize(1)) + .body("clusters[0].settings[0].name", equalTo("containerInsights")) + .body("clusters[0].settings[0].value", equalTo("enabled")); + } + + @Test + void aClusterCreatedWithoutSettingsReportsNone() { + // The absent case must stay absent rather than gaining an empty array, since a client + // diffing its configuration would see that as a change too. + String cluster = "create-nosettings-" + Long.toString(System.nanoTime(), 36); + + ecs("CreateCluster") + .body(""" + {"clusterName": "%s"} + """.formatted(cluster)) + .when() + .post("/") + .then() + .statusCode(200) + .body("cluster.clusterName", equalTo(cluster)) + .body("cluster.settings", org.hamcrest.Matchers.nullValue()); + } +}