fix(ecs): report service deployments and MISSING failures on DescribeServices (#2176)

* fix(ecs): report service deployments and MISSING failures on DescribeServices

DescribeServices omitted services[].deployments entirely and always returned an
empty failures list. Both are read by AWS's own ServicesStable waiter, which
accepts on

  length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`

and fails fast on failures[].reason == "MISSING" (botocore
ecs/2014-11-13/waiters-2.json). Because that file is codegen'd into every SDK,
`aws ecs wait services-stable` and every SDK waiter polled for the full
40 x 15s before giving up, and Terraform's aws_ecs_service never converged
(terraform-provider-aws internal/service/ecs/service.go:2206 applies the same
len(Deployments) == 1 test). Evaluating the acceptor against the old payload
raises JMESPathTypeError, so it could not even be evaluated.

An ACTIVE service now reports exactly one PRIMARY deployment derived from its
current state, with rolloutState COMPLETED once runningCount reaches
desiredCount. The deployment id is derived from the service ARN and task
definition rather than generated per request, so it is stable across calls and
restarts and rolls over when the task definition changes. An unresolvable
service reference is returned as a MISSING failure carrying the ARN it would
have had, instead of being dropped.

Known deviations are documented in docs/services/ecs.md: a single deployment
with no draining ACTIVE alongside it, no deploymentController gating, and
pendingCount always 0.

Closes #2174

* fix(ecs): stamp a deployment with its own start time, not the service's

A task-definition change mints a new deployment id, but both deployment
timestamps were still taken from the service's creation time, so the payload
contradicted itself: a new deployment claiming to have been created when the
service was. botocore documents Deployment.createdAt as "the time when the
service deployment was created", and moto stamps it at deployment construction
(moto/ecs/models.py:540) rather than reusing the service's.

Track when the current deployment began on the service model, set at creation
and on every task-definition change, and report that. Services persisted before
this field existed fall back to the service creation time.

Raised in review on #2176.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-08-08 21:37:59 -05:00
1 parent aed50cc2b2
commit 8b8f837728
8 files changed
+552 -17

No files matched your search

@@ -8,6 +8,8 @@ import io.github.hectorvent.floci.services.ecs.model.CapacityProvider;
import io.github.hectorvent.floci.services.ecs.model.ClusterSetting;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.ContainerInstance;
import io.github.hectorvent.floci.services.ecs.model.Deployment;
import io.github.hectorvent.floci.services.ecs.model.Failure;
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
@@ -497,13 +499,16 @@ public class EcsJsonHandler {
String cluster = req.has("cluster") ? req.path("cluster").asText() : null;
List<String> serviceIds = jsonArrayToList(req.path("services"));
List<EcsServiceModel> found = service.describeServices(cluster, serviceIds, region);
EcsService.DescribeServicesResult found =
service.describeServicesDetailed(cluster, serviceIds, region);
ObjectNode resp = objectMapper.createObjectNode();
ArrayNode arr = objectMapper.createArrayNode();
found.forEach(s -> arr.add(serviceNode(s)));
found.services().forEach(s -> arr.add(serviceNode(s)));
resp.set("services", arr);
resp.set("failures", objectMapper.createArrayNode());
ArrayNode failures = objectMapper.createArrayNode();
found.failures().forEach(f -> failures.add(failureNode(f)));
resp.set("failures", failures);
return Response.ok(resp).build();
}
@@ -1149,6 +1154,34 @@ public class EcsJsonHandler {
networkConfig.set("awsvpcConfiguration", awsvpcNode);
n.set("networkConfiguration", networkConfig);
}
ArrayNode deployments = objectMapper.createArrayNode();
service.deploymentsFor(s).forEach(d -> deployments.add(deploymentNode(d)));
n.set("deployments", deployments);
return n;
}
private ObjectNode failureNode(Failure f) {
ObjectNode n = objectMapper.createObjectNode();
n.put("arn", f.arn());
n.put("reason", f.reason());
if (f.detail() != null) { n.put("detail", f.detail()); }
return n;
}
private ObjectNode deploymentNode(Deployment d) {
ObjectNode n = objectMapper.createObjectNode();
n.put("id", d.getId());
n.put("status", d.getStatus());
n.put("taskDefinition", d.getTaskDefinition());
n.put("desiredCount", d.getDesiredCount());
n.put("pendingCount", d.getPendingCount());
n.put("runningCount", d.getRunningCount());
n.put("failedTasks", d.getFailedTasks());
n.put("rolloutState", d.getRolloutState());
n.put("rolloutStateReason", d.getRolloutStateReason());
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); }
return n;
}
@@ -14,6 +14,8 @@ import io.github.hectorvent.floci.services.ecs.model.CapacityProvider;
import io.github.hectorvent.floci.services.ecs.model.ClusterSetting;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.ContainerInstance;
import io.github.hectorvent.floci.services.ecs.model.Deployment;
import io.github.hectorvent.floci.services.ecs.model.Failure;
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
@@ -705,6 +707,7 @@ public class EcsService implements ContainerTeardown {
svc.setNetworkConfiguration(networkConfiguration);
svc.setStatus("ACTIVE");
svc.setCreatedAt(Instant.now());
svc.setLastDeploymentAt(svc.getCreatedAt());
if (tags != null && !tags.isEmpty()) {
svc.setTags(new LinkedHashMap<>(tags));
}
@@ -745,6 +748,7 @@ public class EcsService implements ContainerTeardown {
if (taskDefinition != null) {
resolveTaskDefinitionOrThrow(taskDefinition, region);
svc.setTaskDefinition(taskDefinition);
svc.setLastDeploymentAt(Instant.now());
recordServiceDeployment(svc, taskDefinition, region);
}
services.put(key, svc);
@@ -791,16 +795,43 @@ public class EcsService implements ContainerTeardown {
return svc;
}
/** The services that resolved, plus a {@code MISSING} failure for each reference that did not. */
public record DescribeServicesResult(List<EcsServiceModel> services, List<Failure> failures) {}
public List<EcsServiceModel> describeServices(String clusterRef, List<String> serviceIds, String region) {
return describeServicesDetailed(clusterRef, serviceIds, region).services();
}
/**
* Resolves each reference, reporting the ones that do not exist as {@code MISSING} failures
* rather than dropping them. ECS answers a describe for an unknown service with a failure
* entry, not an error, and its {@code ServicesStable} waiter fails fast on
* {@code failures[].reason == "MISSING"} — so silently returning an empty list leaves the
* waiter polling for its full timeout instead of reporting the service does not exist.
*/
public DescribeServicesResult describeServicesDetailed(String clusterRef, List<String> serviceIds,
String region) {
EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);
List<EcsServiceModel> result = new ArrayList<>();
List<Failure> failures = new ArrayList<>();
for (String id : serviceIds) {
EcsServiceModel svc = resolveService(cluster.getClusterName(), id, region);
if (svc != null) {
result.add(svc);
} else {
failures.add(Failure.missing(missingServiceArn(cluster, id, region)));
}
}
return result;
return new DescribeServicesResult(result, failures);
}
/** Echoes an ARN the caller supplied, otherwise builds the ARN the service would have had. */
private String missingServiceArn(EcsCluster cluster, String serviceRef, String region) {
if (serviceRef != null && serviceRef.startsWith("arn:")) {
return serviceRef;
}
return regionResolver.buildArn("ecs", region,
"service/" + cluster.getClusterName() + "/" + serviceRef);
}
public List<String> listServices(String clusterRef, String region) {
@@ -1257,6 +1288,64 @@ public class EcsService implements ContainerTeardown {
.toList();
}
/**
* The deployments of a service, derived from its current state rather than tracked as a
* rollout. An ACTIVE service always has exactly one PRIMARY deployment: AWS's own
* {@code ServicesStable} waiter accepts on
* {@code length(services[?!(length(deployments) == `1` && runningCount == desiredCount)]) == `0`},
* so reporting none leaves every SDK waiter polling until it times out.
*
* <p>A task-definition change is applied in place rather than draining a previous
* deployment, so there is never a second ACTIVE entry beside the PRIMARY one. An INACTIVE
* service has no deployments.
*/
public List<Deployment> deploymentsFor(EcsServiceModel svc) {
if (svc == null || !"ACTIVE".equals(svc.getStatus())) {
return List.of();
}
String deploymentId = deploymentId(svc);
boolean converged = svc.getRunningCount() >= svc.getDesiredCount();
Deployment d = new Deployment();
d.setId(deploymentId);
d.setStatus("PRIMARY");
d.setTaskDefinition(svc.getTaskDefinition());
d.setDesiredCount(svc.getDesiredCount());
d.setPendingCount(svc.getPendingCount());
d.setRunningCount(svc.getRunningCount());
d.setFailedTasks(0);
d.setRolloutState(converged ? "COMPLETED" : "IN_PROGRESS");
d.setRolloutStateReason("ECS deployment " + deploymentId
+ (converged ? " completed." : " in progress."));
d.setLaunchType(svc.getLaunchType());
// 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.
Instant startedAt = svc.getLastDeploymentAt() != null
? svc.getLastDeploymentAt()
: svc.getCreatedAt();
d.setCreatedAt(startedAt);
d.setUpdatedAt(startedAt);
return List.of(d);
}
/**
* A stable {@code ecs-svc/<id>} identifier derived from the service ARN and its task
* definition. Deployments are derived per request, so a random id would differ on every
* {@code DescribeServices} call and read as perpetual drift in clients that persist it.
* Deriving it from persisted state keeps it identical across calls and across restarts,
* while still rolling over when the task definition changes.
*/
private String deploymentId(EcsServiceModel svc) {
String seed = svc.getServiceArn() + ":" + svc.getTaskDefinition();
long hash = 0xcbf29ce484222325L;
for (int i = 0; i < seed.length(); i++) {
hash = (hash ^ seed.charAt(i)) * 0x100000001b3L;
}
return "ecs-svc/" + Long.toUnsignedString(hash);
}
private void recordServiceDeployment(EcsServiceModel svc, String taskDefinition, String region) {
String deploymentId = UUID.randomUUID().toString().replace("-", "");
String deploymentArn = regionResolver.buildArn("ecs", region,
@@ -0,0 +1,53 @@
package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
/**
* A deployment of an ECS service, as reported in {@code DescribeServices}'
* {@code services[].deployments}. Distinct from {@link ServiceDeployment}, which is the
* separate {@code DescribeServiceDeployments} API shape and carries neither counts nor a
* rollout state.
*/
@RegisterForReflection
public class Deployment {
private String id;
private String status;
private String taskDefinition;
private int desiredCount;
private int pendingCount;
private int runningCount;
private int failedTasks;
private String rolloutState;
private String rolloutStateReason;
private LaunchType launchType;
private Instant createdAt;
private Instant updatedAt;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getTaskDefinition() { return taskDefinition; }
public void setTaskDefinition(String taskDefinition) { this.taskDefinition = taskDefinition; }
public int getDesiredCount() { return desiredCount; }
public void setDesiredCount(int desiredCount) { this.desiredCount = desiredCount; }
public int getPendingCount() { return pendingCount; }
public void setPendingCount(int pendingCount) { this.pendingCount = pendingCount; }
public int getRunningCount() { return runningCount; }
public void setRunningCount(int runningCount) { this.runningCount = runningCount; }
public int getFailedTasks() { return failedTasks; }
public void setFailedTasks(int failedTasks) { this.failedTasks = failedTasks; }
public String getRolloutState() { return rolloutState; }
public void setRolloutState(String rolloutState) { this.rolloutState = rolloutState; }
public String getRolloutStateReason() { return rolloutStateReason; }
public void setRolloutStateReason(String rolloutStateReason) { this.rolloutStateReason = rolloutStateReason; }
public LaunchType getLaunchType() { return launchType; }
public void setLaunchType(LaunchType launchType) { this.launchType = launchType; }
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
public Instant getUpdatedAt() { return updatedAt; }
public void setUpdatedAt(Instant updatedAt) { this.updatedAt = updatedAt; }
}
@@ -21,6 +21,8 @@ public class EcsServiceModel {
private int pendingCount;
private String status;
private Instant createdAt;
/** When the current deployment began: service creation, or the last task-definition change. */
private Instant lastDeploymentAt;
private String namespace;
private String deploymentController;
private Map<String, String> tags = new HashMap<>();
@@ -56,6 +58,8 @@ public class EcsServiceModel {
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
public Instant getLastDeploymentAt() { return lastDeploymentAt; }
public void setLastDeploymentAt(Instant lastDeploymentAt) { this.lastDeploymentAt = lastDeploymentAt; }
public String getNamespace() { return namespace; }
public void setNamespace(String namespace) { this.namespace = namespace; }
@@ -0,0 +1,16 @@
package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
/**
* A per-resource failure returned alongside the results of an ECS {@code Describe*} call.
* ECS reports a resource it could not find here rather than throwing, so callers get partial
* results; {@code reason} is {@code MISSING} for a resource that does not exist.
*/
@RegisterForReflection
public record Failure(String arn, String reason, String detail) {
public static Failure missing(String arn) {
return new Failure(arn, "MISSING", null);
}
}