diff --git a/docs/services/ecs.md b/docs/services/ecs.md index 6500817aee..0167fd1961 100644 --- a/docs/services/ecs.md +++ b/docs/services/ecs.md @@ -744,10 +744,14 @@ The task set round-trips `networkConfiguration` (defaulting to the service's), ` scale percentage, always rounded up: 3 desired at 50 percent is 2 tasks, not 1. `UpdateTaskSet` recomputes it. -Floci places no tasks for a task set, so `runningCount` and `pendingCount` stay at 0 and -`stabilityStatus` is always `STEADY_STATE`. On AWS the status reaches `STEADY_STATE` once the -running count matches the computed one; reporting `STABILIZING` here would hang anything that -waits for the set to stabilise, since no count ever moves. +Creating a service with the `CODE_DEPLOY` controller creates an initial `PRIMARY` task set +at 100 percent scale. Its running and pending counts track the tasks launched by the service +scheduler. The set reaches `STEADY_STATE` when the running count matches its computed desired +count and no tasks are pending. `CreateService` and `DescribeServices` return the task set and an +empty `deployments` list for this controller. + +Sets added through `CreateTaskSet` have simulated scheduling. Their running and pending counts +stay at zero, and they report `STEADY_STATE` so deployment tools can continue. `DeleteTaskSet` refuses a task set that has not been scaled down to zero unless `force` is set. `DescribeTaskSets` returns tags only for `include: ["TAGS"]` and reports a reference naming no diff --git a/src/main/java/io/floci/aws/services/ecs/EcsService.java b/src/main/java/io/floci/aws/services/ecs/EcsService.java index b316144170..5a3f1de856 100644 --- a/src/main/java/io/floci/aws/services/ecs/EcsService.java +++ b/src/main/java/io/floci/aws/services/ecs/EcsService.java @@ -553,6 +553,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab // wipes CloudWatch, so the Container Insights samples it held must be written again. launchFailures.clear(); publishedInsights.clear(); + taskSets.clear(); } /** @@ -3081,6 +3082,9 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab svc.setTags(new LinkedHashMap<>(request.getTags())); } + if ("CODE_DEPLOY".equals(controller)) { + createInitialTaskSet(svc, cluster, region); + } services.put(key, svc); cluster.setActiveServicesCount(cluster.getActiveServicesCount() + 1); persistCluster(region, cluster); @@ -3473,6 +3477,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab if (serviceArn != null) { serviceDeployments.values().removeIf(d -> serviceArn.equals(d.getServiceArn())); serviceRevisions.values().removeIf(r -> serviceArn.equals(r.getServiceArn())); + taskSets.values().removeIf(ts -> serviceArn.equals(ts.getServiceArn())); launchFailures.remove(serviceArn); } return svc; @@ -4429,16 +4434,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab "Invalid scale unit: " + request.getScaleUnit(), 400); } - String setId = "ecs-svc/" + UUID.randomUUID().toString().replace("-", ""); - String taskSetArn = regionResolver.buildArn("ecs", region, "task-set/" - + cluster.getClusterName() + "/" + svc.getServiceName() + "/" + setId); - - TaskSet ts = new TaskSet(); - ts.setId(setId); - ts.setTaskSetArn(taskSetArn); - ts.setServiceArn(svc.getServiceArn()); - ts.setClusterArn(cluster.getClusterArn()); - ts.setTaskDefinition(taskDef.getTaskDefinitionArn()); + TaskSet ts = newTaskSet(newDeploymentId(), svc, cluster, taskDef.getTaskDefinitionArn(), region); ts.setStatus(STATUS_ACTIVE); ts.setScaleValue(request.getScaleValue() != null ? request.getScaleValue() : 100.0); ts.setScaleUnit(SCALE_UNIT_PERCENT); @@ -4463,10 +4459,71 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab ts.setUpdatedAt(Instant.now()); applyTaskSetScale(ts, svc); - taskSets.put(taskSetArn, ts); + taskSets.put(ts.getTaskSetArn(), ts); return ts; } + private TaskSet newTaskSet(String id, EcsServiceModel svc, EcsCluster cluster, + String taskDefinition, String region) { + TaskSet ts = new TaskSet(); + ts.setId(id); + ts.setTaskSetArn(regionResolver.buildArn("ecs", region, "task-set/" + + cluster.getClusterName() + "/" + svc.getServiceName() + "/" + id)); + ts.setServiceArn(svc.getServiceArn()); + ts.setClusterArn(cluster.getClusterArn()); + ts.setTaskDefinition(taskDefinition); + return ts; + } + + private void createInitialTaskSet(EcsServiceModel svc, EcsCluster cluster, String region) { + // The service scheduler already starts these tasks under this deployment ID. + TaskSet ts = newTaskSet(svc.getDeploymentId(), svc, cluster, svc.getTaskDefinition(), region); + ts.setStatus("PRIMARY"); + ts.setScaleUnit(SCALE_UNIT_PERCENT); + ts.setScaleValue(100.0); + ts.setCapacityProviderStrategy(svc.getCapacityProviderStrategy()); + ts.setLaunchType(svc.getLaunchType()); + ts.setNetworkConfiguration(svc.getNetworkConfiguration()); + ts.setLoadBalancers(svc.getLoadBalancers()); + ts.setServiceRegistries(svc.getServiceRegistries()); + if (svc.getLaunchType() == LaunchType.FARGATE) { + ts.setPlatformVersion(resolvePlatformVersion(svc.getPlatformVersion())); + ts.setPlatformFamily(svc.getPlatformFamily()); + } + ts.setCreatedAt(svc.getCreatedAt()); + ts.setUpdatedAt(svc.getCreatedAt()); + refreshScheduledTaskSet(ts, svc); + taskSets.put(ts.getTaskSetArn(), ts); + } + + private void refreshScheduledTaskSet(TaskSet ts, EcsServiceModel svc) { + if (!"CODE_DEPLOY".equals(svc.getDeploymentController()) || !ts.getId().equals(deploymentId(svc))) { + return; + } + int running = 0; + int pending = 0; + for (EcsTask task : tasks.values()) { + if (!ts.getId().equals(task.getStartedBy()) + || !ts.getServiceArn().equals(task.getOwningServiceArn())) { + continue; + } + if (TaskStatus.RUNNING.name().equals(task.getLastStatus())) { + running++; + } else if (PENDING_TASK_STATUSES.contains(task.getLastStatus())) { + pending++; + } + } + int desired = (int) Math.ceil(svc.getDesiredCount() * ts.getScaleValue() / 100.0); + ts.setComputedDesiredCount(desired); + ts.setRunningCount(running); + ts.setPendingCount(pending); + String stability = running == desired && pending == 0 ? STABILITY_STATUS_STEADY_STATE : "STABILIZING"; + if (!stability.equals(ts.getStabilityStatus())) { + ts.setStabilityStatus(stability); + ts.setStabilityStatusAt(Instant.now()); + } + } + /** * A task set that names neither a launch type nor a capacity provider strategy takes the * cluster's default strategy, and falls back to EC2 when the cluster has none, as AWS does. @@ -4491,10 +4548,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab * Stamps {@code computedDesiredCount}, which AWS derives from the service's desired count and * the task set's scale percentage, always rounding up: a computed 1.2 becomes 2 tasks. * - *
Floci places no tasks for a task set, so its running and pending counts stay at zero. A - * stability status derived from them would leave every set on a service with a desired count - * above zero in {@code STABILIZING} for ever, hanging anything that waits for the set to - * stabilise, so the set is reported {@code STEADY_STATE} until Floci tracks tasks for it. + *
Explicitly created task sets simulate scheduling and report {@code STEADY_STATE}.
+ * The initial CodeDeploy task set uses the service scheduler's real task counts.
*/
private void applyTaskSetScale(TaskSet ts, EcsServiceModel svc) {
ts.setComputedDesiredCount(
@@ -4516,6 +4571,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
ts.setScaleUnit(SCALE_UNIT_PERCENT);
ts.setUpdatedAt(Instant.now());
applyTaskSetScale(ts, svc);
+ refreshScheduledTaskSet(ts, svc);
return ts;
}
@@ -4554,11 +4610,13 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
if (svc == null) {
return List.of();
}
- return taskSets.values().stream()
+ List 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.
+ * service has no deployments. CodeDeploy services report their rollout in taskSets.
*/
public List