feat(ecs): honour task-definition volumes + container mountPoints (#1171)

* feat(ecs): honour task-def volumes + container mountPoints

Implements EC2-launch-type ECS host-bind semantics: a task definition may
declare task-level volumes ({name, host.sourcePath}) and each container
definition may reference them via mountPoints ({sourceVolume, containerPath,
readOnly}). At RunTask launch, each mountPoint resolves its source volume to
the host source path and bind-mounts it into the container (read-only via
withReadOnlyBind, read-write via withBind); unresolved volume references are
skipped with a warning.

This lets stock images be launched with per-task config bind-mounted at a
fixed container path, instead of baking config into per-image wrappers.

- model: Volume, MountPoint records; volumes on TaskDefinition, mountPoints on
  ContainerDefinition
- EcsJsonHandler: parse + serialize round-trip for both
- EcsContainerManager.startTask: resolve volumes and wire binds
- tests: JSON wire-path round-trip + bind-by-access-mode (Docker-free)

* fix(ecs): fail loudly when a task definition resolves zero container definitions

RunTask/StartTask on a task definition with no container definitions used to
launch zero containers silently: the per-container loop was a no-op, the task
still transitioned through PENDING towards RUNNING, and callers polling
DescribeTasks waited forever on a task with nothing behind it.

Validate up front in launchTasks (covers RunTask and StartTask, docker and
mock mode alike): WARN and throw a ClientException (HTTP 400) before any task
is created, consistent with how unresolved task definitions fail.
This commit is contained in:
Niraj authored and GitHub committed 2026-06-06 08:57:37 -05:00
1 parent 772467b7cc
commit b9dd0c38a9
10 files changed
+376

No files matched your search

@@ -15,6 +15,7 @@ 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.KeyValuePair;
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
import io.github.hectorvent.floci.services.ecs.model.MountPoint;
import io.github.hectorvent.floci.services.ecs.model.NetworkBinding;
import io.github.hectorvent.floci.services.ecs.model.NetworkConfiguration;
import io.github.hectorvent.floci.services.ecs.model.NetworkMode;
@@ -24,6 +25,7 @@ import io.github.hectorvent.floci.services.ecs.model.ServiceDeployment;
import io.github.hectorvent.floci.services.ecs.model.ServiceRevision;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.ecs.model.TaskSet;
import io.github.hectorvent.floci.services.ecs.model.Volume;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -209,6 +211,9 @@ public class EcsJsonHandler {
TaskDefinition td = service.registerTaskDefinition(family, containerDefs, networkMode, cpu, memory,
taskRoleArn, executionRoleArn, tags, region);
// Task-level volumes are not part of registerTaskDefinition's signature; set them on the
// returned (and stored) task definition so they round-trip and reach RunTask launches.
td.setVolumes(parseVolumes(req.path("volumes")));
ObjectNode resp = objectMapper.createObjectNode();
resp.set("taskDefinition", taskDefinitionNode(td));
@@ -925,6 +930,20 @@ public class EcsJsonHandler {
}
}
n.set("containerDefinitions", containers);
if (td.getVolumes() != null && !td.getVolumes().isEmpty()) {
ArrayNode vols = objectMapper.createArrayNode();
for (Volume v : td.getVolumes()) {
ObjectNode vNode = objectMapper.createObjectNode();
vNode.put("name", v.name());
if (v.hostSourcePath() != null) {
ObjectNode host = objectMapper.createObjectNode();
host.put("sourcePath", v.hostSourcePath());
vNode.set("host", host);
}
vols.add(vNode);
}
n.set("volumes", vols);
}
if (td.getTags() != null && !td.getTags().isEmpty()) {
n.set("tags", tagsNode(td.getTags()));
}
@@ -962,6 +981,18 @@ public class EcsJsonHandler {
n.set("environment", envArr);
}
if (def.getMountPoints() != null && !def.getMountPoints().isEmpty()) {
ArrayNode mps = objectMapper.createArrayNode();
for (MountPoint mp : def.getMountPoints()) {
ObjectNode mpNode = objectMapper.createObjectNode();
mpNode.put("sourceVolume", mp.sourceVolume());
mpNode.put("containerPath", mp.containerPath());
mpNode.put("readOnly", mp.readOnly());
mps.add(mpNode);
}
n.set("mountPoints", mps);
}
return n;
}
@@ -1200,6 +1231,7 @@ public class EcsJsonHandler {
def.setPortMappings(parsePortMappings(item.path("portMappings")));
def.setEnvironment(parseKeyValuePairs(item.path("environment")));
def.setMountPoints(parseMountPoints(item.path("mountPoints")));
if (item.has("command") && item.path("command").isArray()) {
List<String> cmd = new ArrayList<>();
@@ -1242,6 +1274,32 @@ public class EcsJsonHandler {
return result;
}
private List<Volume> parseVolumes(JsonNode node) {
List<Volume> result = new ArrayList<>();
if (!node.isArray()) {
return result;
}
for (JsonNode item : node) {
String hostSourcePath = item.path("host").path("sourcePath").asText(null);
result.add(new Volume(item.path("name").asText(), hostSourcePath));
}
return result;
}
private List<MountPoint> parseMountPoints(JsonNode node) {
List<MountPoint> result = new ArrayList<>();
if (!node.isArray()) {
return result;
}
for (JsonNode item : node) {
result.add(new MountPoint(
item.path("sourceVolume").asText(),
item.path("containerPath").asText(),
item.path("readOnly").asBoolean(false)));
}
return result;
}
private List<ContainerOverride> parseContainerOverrides(JsonNode node) {
List<ContainerOverride> result = new ArrayList<>();
if (!node.isArray()) {
@@ -292,6 +292,14 @@ public class EcsService {
LaunchType launchType, String group, String startedBy,
String containerInstanceArn,
List<ContainerOverride> containerOverrides, String region) {
// Fail loudly instead of silently launching zero containers and leaving
// a task that looks RUNNING with nothing behind it.
if (taskDef.getContainerDefinitions() == null || taskDef.getContainerDefinitions().isEmpty()) {
LOG.warnv("Task definition {0} has no container definitions; refusing to launch tasks",
taskDef.getTaskDefinitionArn());
throw new AwsException("ClientException",
"Task definition " + taskDef.getTaskDefinitionArn() + " has no container definitions.", 400);
}
List<EcsTask> launched = new ArrayList<>();
for (int i = 0; i < count; i++) {
String taskId = UUID.randomUUID().toString().replace("-", "");
@@ -12,9 +12,11 @@ import io.github.hectorvent.floci.services.ecs.model.Container;
import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition;
import io.github.hectorvent.floci.services.ecs.model.ContainerOverride;
import io.github.hectorvent.floci.services.ecs.model.EcsTask;
import io.github.hectorvent.floci.services.ecs.model.MountPoint;
import io.github.hectorvent.floci.services.ecs.model.NetworkBinding;
import io.github.hectorvent.floci.services.ecs.model.PortMapping;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import io.github.hectorvent.floci.services.ecs.model.Volume;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ExposedPort;
@@ -72,6 +74,16 @@ public class EcsContainerManager {
List<Closeable> logStreams = new ArrayList<>();
List<Container> runtimeContainers = new ArrayList<>();
// Task-level volumes: map volume name -> host source path, consumed by per-container mountPoints.
Map<String, String> volumeSourcePaths = new LinkedHashMap<>();
if (taskDef.getVolumes() != null) {
for (Volume v : taskDef.getVolumes()) {
if (v.name() != null && v.hostSourcePath() != null) {
volumeSourcePaths.put(v.name(), v.hostSourcePath());
}
}
}
for (ContainerDefinition def : taskDef.getContainerDefinitions()) {
String containerName = "floci-ecs-" + taskId + "-" + def.getName();
@@ -125,6 +137,25 @@ public class EcsContainerManager {
specBuilder.withEntrypoint(def.getEntryPoint());
}
// Bind-mount task-level volumes referenced by this container's mountPoints.
// The host source path resolves on the Docker daemon (sibling-container launch),
// so it must be an absolute host path. Unresolved volume references are skipped.
if (def.getMountPoints() != null) {
for (MountPoint mp : def.getMountPoints()) {
String sourcePath = volumeSourcePaths.get(mp.sourceVolume());
if (sourcePath == null || mp.containerPath() == null) {
LOG.warnv("Skipping mountPoint with unresolved volume {0} on container {1}",
mp.sourceVolume(), def.getName());
continue;
}
if (mp.readOnly()) {
specBuilder.withReadOnlyBind(sourcePath, mp.containerPath());
} else {
specBuilder.withBind(sourcePath, mp.containerPath());
}
}
}
ContainerSpec spec = specBuilder.build();
// Create and start container
@@ -17,6 +17,7 @@ public class ContainerDefinition {
private List<KeyValuePair> environment;
private List<String> command;
private List<String> entryPoint;
private List<MountPoint> mountPoints;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@@ -47,4 +48,7 @@ public class ContainerDefinition {
public List<String> getEntryPoint() { return entryPoint; }
public void setEntryPoint(List<String> entryPoint) { this.entryPoint = entryPoint; }
public List<MountPoint> getMountPoints() { return mountPoints; }
public void setMountPoints(List<MountPoint> mountPoints) { this.mountPoints = mountPoints; }
}
@@ -0,0 +1,10 @@
package io.github.hectorvent.floci.services.ecs.model;
/**
* A container mount point in an ECS container definition:
* {@code {"sourceVolume": ..., "containerPath": ..., "readOnly": ...}}.
* {@code sourceVolume} references a task-level {@link Volume} by name; the volume's
* host source path is bind-mounted into the container at {@code containerPath}.
*/
public record MountPoint(String sourceVolume, String containerPath, boolean readOnly) {
}
@@ -19,6 +19,7 @@ public class TaskDefinition {
private String taskRoleArn;
private String executionRoleArn;
private List<ContainerDefinition> containerDefinitions;
private List<Volume> volumes;
private Map<String, String> tags = new HashMap<>();
public String getTaskDefinitionArn() { return taskDefinitionArn; }
@@ -53,6 +54,9 @@ public class TaskDefinition {
this.containerDefinitions = containerDefinitions;
}
public List<Volume> getVolumes() { return volumes; }
public void setVolumes(List<Volume> volumes) { this.volumes = volumes; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}
@@ -0,0 +1,10 @@
package io.github.hectorvent.floci.services.ecs.model;
/**
* A task-level volume in an ECS task definition. Only the EC2-launch-type
* {@code host} volume shape is modelled: {@code {"name": ..., "host": {"sourcePath": ...}}}.
* {@code hostSourcePath} is the absolute path on the Docker host that the volume
* binds to; a container references this volume by {@code name} via a {@link MountPoint}.
*/
public record Volume(String name, String hostSourcePath) {
}