feat(ecs): serve the task metadata endpoint v4 (#4120)

An application in an ECS task, the AWS SDK integrations that read task
identity, and the aws-for-fluent-bit init process all expect
ECS_CONTAINER_METADATA_URI_V4 to resolve. Under Floci it was never set,
so any of them fell back or failed.

AWS serves the endpoint on the link-local address 169.254.170.2, which a
local container cannot be given. Floci serves the same paths on its own
port instead: clients read the environment variable rather than the
address, so nothing has to know the difference.

The id is minted per container before the container is created, the way
the ECS agent mints one, because the container's own environment has to
carry the URI and so it cannot be derived from the Docker id the daemon
returns afterwards. EcsService.findByMetadataId resolves an id back to
its container and task.

The task document carries Cluster, TaskARN, Family, Revision, the
statuses, Limits in vCPUs, the pull timestamps, AvailabilityZone,
LaunchType, ServiceName for a service's task, VPCID for an EC2 task,
and, on Fargate, ClockDrift and EphemeralStorageMetrics. Floci has no
clock drift and does not meter the disk, so those report a synchronized
clock and zero usage. An awsvpc container's Networks object describes
the task ENI and the subnet it sits in; a local ENI has no DHCP option
set behind it, so the gateway, the resolver and the search domain are
derived the way a real VPC assigns them.

The stats paths read the daemon when the request arrives and return
Docker's own ContainerStats document, plus the network_rate_stats the
agent adds. Docker reports cumulative counters only, so the rates are a
delta across two consecutive samples, which is also why AWS documents a
container as having to run for about a second before its stats appear. A
container with no running Docker container behind it reports an empty
document rather than an error.

Because that pair costs a collection tick, task/stats samples the task's
containers in one pass: every stream is opened before any of them is
waited on, and the waits share one deadline. A sidecar polls that path
for network metrics, so the response has to cost about one tick however
many containers the task has, not one tick per container.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-22 08:18:55 -04:00
1 parent 93f8d1421a
commit 7cd441231f
12 files changed
+1918 -7

No files matched your search

@@ -75,6 +75,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.Set;
import java.util.Comparator;
@@ -1657,6 +1658,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
deregisterTaskFromLoadBalancers(task, region);
Map<String, Integer> exitCodes = Map.of();
Map<String, Instant> finishedAt = Map.of();
if (dockerMode) {
EcsTaskHandle handle = taskHandles.remove(task.getTaskArn());
try {
@@ -1664,6 +1666,9 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
} finally {
retainUnresolvedLogHandle(task.getTaskArn(), handle);
}
if (handle != null) {
finishedAt = handle.getFinishedAt();
}
}
task.setLastStatus(TaskStatus.STOPPED.name());
@@ -1673,12 +1678,17 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
containerManager.releaseTaskNetwork(task, region);
if (task.getContainers() != null) {
final Map<String, Integer> codes = exitCodes;
final Map<String, Instant> finishTimes = finishedAt;
task.getContainers().forEach(c -> {
c.setLastStatus("STOPPED");
Integer code = codes.get(c.getName());
if (code != null) {
c.setExitCode(code);
}
Instant finished = finishTimes.get(c.getName());
if (finished != null) {
c.setFinishedAt(finished);
}
});
}
@@ -1736,6 +1746,42 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
}
}
/** A container reached through the task metadata endpoint, with the task it belongs to. */
public record MetadataTarget(EcsTask task, Container container, TaskDefinition taskDefinition) {}
/**
* Resolves the {@code ECS_CONTAINER_METADATA_URI_V4} id a container was given at launch. The id
* is minted per container and never reused, so it identifies both the container and its task.
*/
public Optional<MetadataTarget> findByMetadataId(String metadataId) {
if (metadataId == null || metadataId.isBlank()) {
return Optional.empty();
}
for (EcsTask task : tasks.values()) {
if (task.getContainers() == null) {
continue;
}
for (Container container : task.getContainers()) {
if (metadataId.equals(container.getMetadataId())) {
return Optional.of(new MetadataTarget(task, container,
taskDefinitionOf(task)));
}
}
}
return Optional.empty();
}
/** The task's definition, or null when it has since been deleted. */
private TaskDefinition taskDefinitionOf(EcsTask task) {
try {
return resolveTaskDefinitionOrThrow(task.getTaskDefinitionArn(), taskRegion(task));
} catch (AwsException e) {
LOG.debugv("Task {0} references a task definition that is gone: {1}",
task.getTaskArn(), e.getMessage());
return null;
}
}
public List<EcsTask> describeTasks(String clusterRef, List<String> taskRefs, String region) {
return describeTasksDetailed(clusterRef, taskRefs, region).tasks();
}
@@ -3772,14 +3818,20 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
updateHealth(task, handle);
// Inspect every container; abort if any are still running.
// Inspect every container; abort if any are still running. A container that exited on its
// own has a finish time of its own, read here while the daemon still remembers it.
Map<String, Integer> exitCodes = new LinkedHashMap<>();
Map<String, Instant> finishedAt = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : handle.getContainerIds().entrySet()) {
Integer code = containerManager.getExitCodeIfStopped(entry.getValue());
if (code == null) {
return;
}
exitCodes.put(entry.getKey(), code);
Instant finished = containerManager.getFinishedAtIfStopped(entry.getValue());
if (finished != null) {
finishedAt.put(entry.getKey(), finished);
}
}
// All containers have exited. Atomically claim the handle to avoid
@@ -3799,6 +3851,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
if (code != null) {
c.setExitCode(code);
}
Instant finished = finishedAt.get(c.getName());
if (finished != null) {
c.setFinishedAt(finished);
}
});
}
@@ -0,0 +1,548 @@
package io.github.hectorvent.floci.services.ecs;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager;
import io.github.hectorvent.floci.services.ecs.model.AwsVpcConfiguration;
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.EcsTask;
import io.github.hectorvent.floci.services.ecs.model.LaunchType;
import io.github.hectorvent.floci.services.ecs.model.LogConfiguration;
import io.github.hectorvent.floci.services.ecs.model.TaskDefinition;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
/**
* The ECS task metadata endpoint, version 4.
*
* <p>On AWS this is served at {@code http://169.254.170.2/v4/<id>} and every container is told
* where to find it through {@code ECS_CONTAINER_METADATA_URI_V4}. A local task cannot be given a
* link-local address of its own, so Floci serves the same paths on its own port and injects the
* matching URI into each container it launches; an application, the AWS SDKs' ECS credential and
* metadata clients, and the aws-for-fluent-bit init process all read the variable rather than the
* address, so they work unchanged.
*
* <p>The two {@code /stats} paths return the Docker stats AWS documents them as returning: one
* sample per container, taken from the daemon when the request arrives. A container Floci has no
* running Docker container for reports an empty document rather than an error, because the path
* itself still exists for it.
*/
@Path("/v4")
@ApplicationScoped
public class EcsTaskMetadataController {
private static final Logger LOG = Logger.getLogger(EcsTaskMetadataController.class);
private static final DateTimeFormatter TIMESTAMP = DateTimeFormatter.ISO_INSTANT;
private static final String SERVICE_GROUP_PREFIX = "service:";
/** The search domain an instance gets, which us-east-1 spells differently from every other region. */
private static final String LEGACY_SEARCH_DOMAIN_REGION = "us-east-1";
/**
* Docker's stats document, written back out the way the daemon sent it. docker-java's model
* carries a field for every member of every API version, so the members this daemon left out
* are dropped rather than written as nulls.
*/
private static final ObjectMapper STATS_MAPPER = JsonMapper.builder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.build();
private final EcsService service;
private final EcsContainerManager containerManager;
private final Ec2Service ec2Service;
private final ObjectMapper objectMapper;
@Inject
public EcsTaskMetadataController(EcsService service, EcsContainerManager containerManager,
Ec2Service ec2Service, ObjectMapper objectMapper) {
this.service = service;
this.containerManager = containerManager;
this.ec2Service = ec2Service;
this.objectMapper = objectMapper;
}
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response container(@PathParam("id") String id) {
Optional<EcsService.MetadataTarget> target = service.findByMetadataId(id);
if (target.isEmpty()) {
return notFound();
}
EcsService.MetadataTarget found = target.get();
return Response.ok(containerNode(found.task(), found.container(), found.taskDefinition())).build();
}
@GET
@Path("/{id}/task")
@Produces(MediaType.APPLICATION_JSON)
public Response task(@PathParam("id") String id) {
Optional<EcsService.MetadataTarget> target = service.findByMetadataId(id);
if (target.isEmpty()) {
return notFound();
}
EcsService.MetadataTarget found = target.get();
return Response.ok(taskNode(found.task(), found.taskDefinition())).build();
}
/**
* The task document with the task's own tags and, for a task placed on a container instance,
* that instance's. The container agent serves this path, so it exists for the EC2 launch type
* only; a Fargate task gets the 404 AWS gives it.
*/
@GET
@Path("/{id}/taskWithTags")
@Produces(MediaType.APPLICATION_JSON)
public Response taskWithTags(@PathParam("id") String id) {
Optional<EcsService.MetadataTarget> target = service.findByMetadataId(id);
if (target.isEmpty() || target.get().task().getLaunchType() != LaunchType.EC2) {
return notFound();
}
EcsService.MetadataTarget found = target.get();
ObjectNode node = taskNode(found.task(), found.taskDefinition());
ArrayNode errors = objectMapper.createArrayNode();
putTags(node, "TaskTags", found.task().getTaskArn(), errors);
putTags(node, "ContainerInstanceTags", found.task().getContainerInstanceArn(), errors);
if (!errors.isEmpty()) {
node.set("Errors", errors);
}
return Response.ok(node).build();
}
/**
* One tag collection, omitted when the resource carries none and reported under {@code Errors}
* the way the agent reports a tag lookup it could not make.
*/
private void putTags(ObjectNode target, String field, String resourceArn, ArrayNode errors) {
if (resourceArn == null) {
return;
}
try {
Map<String, String> tags = service.listTagsForResource(resourceArn);
if (!tags.isEmpty()) {
ObjectNode node = target.putObject(field);
tags.forEach(node::put);
}
} catch (AwsException e) {
ObjectNode error = errors.addObject();
error.put("ErrorField", field);
error.put("ErrorCode", e.getErrorCode());
error.put("ErrorMessage", e.getMessage());
error.put("StatusCode", e.getHttpStatus());
error.put("ResourceARN", resourceArn);
}
}
@GET
@Path("/{id}/stats")
@Produces(MediaType.APPLICATION_JSON)
public Response containerStats(@PathParam("id") String id) {
Optional<EcsService.MetadataTarget> target = service.findByMetadataId(id);
if (target.isEmpty()) {
return notFound();
}
EcsContainerManager.ContainerStats sample = containerManager
.sampleContainerStats(target.get().container().getDockerId()).orElse(null);
return Response.ok(statsNode(sample)).build();
}
@GET
@Path("/{id}/task/stats")
@Produces(MediaType.APPLICATION_JSON)
public Response taskStats(@PathParam("id") String id) {
Optional<EcsService.MetadataTarget> target = service.findByMetadataId(id);
if (target.isEmpty()) {
return notFound();
}
List<Container> containers = target.get().task().getContainers();
if (containers == null) {
containers = List.of();
}
// The task's containers are sampled in one pass rather than one after another: Docker's
// collection tick is a second, and this is the path a sidecar polls for network metrics.
Map<String, EcsContainerManager.ContainerStats> sampled = containerManager.sampleContainerStats(
containers.stream().map(Container::getDockerId).filter(Objects::nonNull).toList());
ObjectNode stats = objectMapper.createObjectNode();
for (Container container : containers) {
stats.set(statsKey(container), statsNode(sampled.get(container.getDockerId())));
}
return Response.ok(stats).build();
}
/** Docker's stats for one container, or an empty document when the daemon had none to give. */
private ObjectNode statsNode(EcsContainerManager.ContainerStats stats) {
if (stats == null) {
return objectMapper.createObjectNode();
}
ObjectNode node = STATS_MAPPER.valueToTree(stats.statistics());
// Docker reports cumulative counters only. The per-second rates alongside them are the
// agent's own, and are what a sidecar reads /task/stats for.
if (stats.rxBytesPerSecond() != null && stats.txBytesPerSecond() != null) {
ObjectNode rates = node.putObject("network_rate_stats");
rates.put("rx_bytes_per_sec", stats.rxBytesPerSecond());
rates.put("tx_bytes_per_sec", stats.txBytesPerSecond());
}
return node;
}
/** Task stats are keyed by Docker id, the way the daemon keys a container. */
private static String statsKey(Container container) {
return container.getDockerId() != null ? container.getDockerId() : container.getName();
}
private Response notFound() {
ObjectNode error = objectMapper.createObjectNode();
error.put("error", "Unable to get metadata for the requested id");
return Response.status(Response.Status.NOT_FOUND).entity(error).build();
}
private ObjectNode containerNode(EcsTask task, Container container, TaskDefinition taskDef) {
ContainerDefinition definition = definitionOf(taskDef, container.getName());
ObjectNode n = objectMapper.createObjectNode();
n.put("DockerId", container.getDockerId() != null ? container.getDockerId() : container.getMetadataId());
n.put("Name", container.getName());
n.put("DockerName", container.getName());
n.put("Image", container.getImage());
if (container.getImageDigest() != null && !container.getImageDigest().isBlank()) {
n.put("ImageID", container.getImageDigest());
}
ArrayNode ports = portsNode(container);
if (!ports.isEmpty()) {
n.set("Ports", ports);
}
n.set("Labels", labelsNode(task, taskDef, container));
n.put("DesiredStatus", task.getDesiredStatus());
n.put("KnownStatus", container.getLastStatus());
if (container.getExitCode() != null) {
n.put("ExitCode", container.getExitCode());
}
ObjectNode limits = containerLimitsNode(definition);
if (!limits.isEmpty()) {
n.set("Limits", limits);
}
// The container's own clock when Docker gave Floci one, the task's otherwise: a mock-mode
// task, or one restored from storage, has containers that never went through a daemon.
putTimestamp(n, "CreatedAt", firstOf(container.getCreatedAt(), task.getCreatedAt()));
putTimestamp(n, "StartedAt", firstOf(container.getStartedAt(), task.getStartedAt()));
putTimestamp(n, "FinishedAt", firstOf(container.getFinishedAt(), task.getStoppedAt()));
n.put("Type", "NORMAL");
n.put("ContainerARN", container.getContainerArn());
if (container.getHealthStatus() != null) {
n.putObject("Health").put("status", container.getHealthStatus());
}
if (definition != null && definition.getLogConfiguration() != null) {
LogConfiguration logConfiguration = definition.getLogConfiguration();
n.put("LogDriver", logConfiguration.logDriver());
if (logConfiguration.options() != null) {
ObjectNode options = n.putObject("LogOptions");
logConfiguration.options().forEach(options::put);
}
}
n.set("Networks", networksNode(task, taskDef));
if (task.getPlatformVersion() != null) {
n.put("Snapshotter", "overlayfs");
}
return n;
}
private ArrayNode portsNode(Container container) {
ArrayNode ports = objectMapper.createArrayNode();
if (container.getNetworkBindings() != null) {
container.getNetworkBindings().forEach(binding -> {
ObjectNode port = ports.addObject();
port.put("ContainerPort", binding.containerPort());
port.put("Protocol", binding.protocol());
if (binding.hostPort() > 0) {
port.put("HostPort", binding.hostPort());
}
});
}
return ports;
}
private ObjectNode taskNode(EcsTask task, TaskDefinition taskDef) {
ObjectNode n = objectMapper.createObjectNode();
n.put("Cluster", task.getClusterArn());
n.put("TaskARN", task.getTaskArn());
if (taskDef != null) {
n.put("Family", taskDef.getFamily());
n.put("Revision", String.valueOf(taskDef.getRevision()));
}
n.put("DesiredStatus", task.getDesiredStatus());
n.put("KnownStatus", task.getLastStatus());
ObjectNode limits = taskLimitsNode(task);
if (!limits.isEmpty()) {
n.set("Limits", limits);
}
putTimestamp(n, "PullStartedAt", task.getPullStartedAt());
putTimestamp(n, "PullStoppedAt", task.getPullStoppedAt());
putTimestamp(n, "ExecutionStoppedAt", task.getExecutionStoppedAt());
if (task.getAvailabilityZone() != null) {
n.put("AvailabilityZone", task.getAvailabilityZone());
}
// A service's tasks are grouped as "service:<name>", which is where the endpoint's
// ServiceName comes from; a standalone task has no service and reports none.
if (task.getGroup() != null && task.getGroup().startsWith(SERVICE_GROUP_PREFIX)) {
n.put("ServiceName", task.getGroup().substring(SERVICE_GROUP_PREFIX.length()));
}
if (task.getLaunchType() != null) {
n.put("LaunchType", task.getLaunchType().name());
}
// VPCID is the container instance's, so AWS reports it for the EC2 launch type only.
if (task.getLaunchType() == LaunchType.EC2) {
Subnet subnet = taskSubnet(task);
if (subnet != null && subnet.getVpcId() != null) {
n.put("VPCID", subnet.getVpcId());
}
}
ArrayNode containers = n.putArray("Containers");
if (task.getContainers() != null) {
task.getContainers().forEach(container ->
containers.add(containerNode(task, container, taskDef)));
}
// Fargate reports the clock's accuracy and the task's ephemeral storage use. Floci has no
// drift to report and does not meter the disk, so both are reported as a healthy baseline.
if (task.getPlatformVersion() != null) {
ObjectNode clockDrift = n.putObject("ClockDrift");
clockDrift.put("ClockErrorBound", 0.0);
clockDrift.put("ReferenceTimestamp", TIMESTAMP.format(Instant.now()));
clockDrift.put("ClockSynchronizationStatus", "SYNCHRONIZED");
if (task.getEphemeralStorage() != null) {
ObjectNode storage = n.putObject("EphemeralStorageMetrics");
storage.put("Utilized", 0);
storage.put("Reserved", task.getEphemeralStorage().sizeInGiB() * 1024);
}
}
return n;
}
private ObjectNode labelsNode(EcsTask task, TaskDefinition taskDef, Container container) {
ObjectNode labels = objectMapper.createObjectNode();
labels.put("com.amazonaws.ecs.cluster", task.getClusterArn());
labels.put("com.amazonaws.ecs.container-name", container.getName());
labels.put("com.amazonaws.ecs.task-arn", task.getTaskArn());
if (taskDef != null) {
labels.put("com.amazonaws.ecs.task-definition-family", taskDef.getFamily());
labels.put("com.amazonaws.ecs.task-definition-version", String.valueOf(taskDef.getRevision()));
}
return labels;
}
/** The container's own limits, omitted member by member when the definition sets none. */
private ObjectNode containerLimitsNode(ContainerDefinition definition) {
ObjectNode limits = objectMapper.createObjectNode();
if (definition == null) {
return limits;
}
if (definition.getCpu() != null) {
limits.put("CPU", definition.getCpu());
}
if (definition.getMemory() != null) {
limits.put("Memory", definition.getMemory());
}
return limits;
}
/** The task's own limits: CPU as a vCPU count, memory in MiB, the way the endpoint reports them. */
private ObjectNode taskLimitsNode(EcsTask task) {
ObjectNode limits = objectMapper.createObjectNode();
Integer cpuUnits = parseInteger(task.getCpu());
if (cpuUnits != null) {
limits.put("CPU", cpuUnits / 1024.0);
}
Integer memoryMb = parseInteger(task.getMemory());
if (memoryMb != null) {
limits.put("Memory", memoryMb);
}
return limits;
}
private ArrayNode networksNode(EcsTask task, TaskDefinition taskDef) {
ArrayNode networks = objectMapper.createArrayNode();
ObjectNode network = objectMapper.createObjectNode();
network.put("NetworkMode", taskDef != null && taskDef.getNetworkMode() != null
? taskDef.getNetworkMode().name() : "bridge");
ArrayNode addresses = network.putArray("IPv4Addresses");
if (task.getPrivateIpAddress() != null) {
addresses.add(task.getPrivateIpAddress());
}
if (taskSubnetId(task) != null) {
network.put("AttachmentIndex", 0);
}
// The rest of the network object describes the task ENI and the subnet it sits in. Each
// member is written only when the interface Floci allocated actually carries it.
if (task.getMacAddress() != null) {
network.put("MACAddress", task.getMacAddress());
}
putSubnetMembers(network, task);
if (task.getPrivateDnsName() != null) {
network.put("PrivateDNSName", task.getPrivateDnsName());
}
networks.add(network);
return networks;
}
/**
* The subnet's own view of the network: its CIDR, the gateway VPC reserves as the first
* address in it, the resolver at the VPC's third address, and the search domain the region
* hands out. All four are derived, since a local ENI has no DHCP option set behind it.
*/
private void putSubnetMembers(ObjectNode network, EcsTask task) {
Subnet subnet = taskSubnet(task);
if (subnet == null || subnet.getCidrBlock() == null) {
return;
}
network.put("IPv4SubnetCIDRBlock", subnet.getCidrBlock());
String gateway = addressWithinCidr(subnet.getCidrBlock(), 1);
if (gateway != null) {
network.put("SubnetGatewayIpv4Address", gateway + "/" + prefixLengthOf(subnet.getCidrBlock()));
}
Vpc vpc = vpcOf(task, subnet);
String resolver = vpc != null ? addressWithinCidr(vpc.getCidrBlock(), 2) : null;
if (resolver != null) {
network.putArray("DomainNameServers").add(resolver);
}
String region = taskRegion(task);
if (region != null) {
network.putArray("DomainNameSearchList").add(searchDomain(region));
}
}
private Subnet taskSubnet(EcsTask task) {
String subnetId = taskSubnetId(task);
String region = taskRegion(task);
if (subnetId == null || region == null) {
return null;
}
try {
return ec2Service.describeSubnets(region, List.of(subnetId), Map.of()).stream()
.findFirst()
.orElse(null);
} catch (AwsException e) {
LOG.debugv("Task {0} references a subnet that is gone: {1}", task.getTaskArn(), e.getMessage());
return null;
}
}
private Vpc vpcOf(EcsTask task, Subnet subnet) {
String region = taskRegion(task);
if (subnet.getVpcId() == null || region == null) {
return null;
}
try {
return ec2Service.describeVpcs(region, List.of(subnet.getVpcId()), Map.of()).stream()
.findFirst()
.orElse(null);
} catch (AwsException e) {
LOG.debugv("Subnet {0} references a VPC that is gone: {1}",
subnet.getSubnetId(), e.getMessage());
return null;
}
}
private static String taskSubnetId(EcsTask task) {
AwsVpcConfiguration awsvpc = task.getNetworkConfiguration() != null
? task.getNetworkConfiguration().getAwsvpcConfiguration() : null;
if (awsvpc == null || awsvpc.getSubnets() == null || awsvpc.getSubnets().isEmpty()) {
return null;
}
return awsvpc.getSubnets().getFirst();
}
private static String taskRegion(EcsTask task) {
return AwsArnUtils.regionOrDefault(task.getTaskArn(), null);
}
/** The nth address of a CIDR block, or null when the block is not a readable IPv4 one. */
private static String addressWithinCidr(String cidr, int offset) {
if (cidr == null) {
return null;
}
String[] parts = cidr.split("/");
String[] octets = parts[0].split("\\.");
if (octets.length != 4) {
return null;
}
try {
long address = 0;
for (String octet : octets) {
int value = Integer.parseInt(octet);
if (value < 0 || value > 255) {
return null;
}
address = (address << 8) | value;
}
address += offset;
return "%d.%d.%d.%d".formatted((address >> 24) & 0xFF, (address >> 16) & 0xFF,
(address >> 8) & 0xFF, address & 0xFF);
} catch (NumberFormatException e) {
LOG.debugv("CIDR block {0} is not a readable IPv4 block: {1}", cidr, e.getMessage());
return null;
}
}
private static String prefixLengthOf(String cidr) {
int slash = cidr.indexOf('/');
return slash < 0 ? "32" : cidr.substring(slash + 1);
}
private static String searchDomain(String region) {
return LEGACY_SEARCH_DOMAIN_REGION.equals(region) ? "ec2.internal" : region + ".compute.internal";
}
private static ContainerDefinition definitionOf(TaskDefinition taskDef, String containerName) {
if (taskDef == null || taskDef.getContainerDefinitions() == null) {
return null;
}
return taskDef.getContainerDefinitions().stream()
.filter(definition -> containerName.equals(definition.getName()))
.findFirst()
.orElse(null);
}
private static Integer parseInteger(String value) {
if (value == null || value.isBlank()) {
return null;
}
try {
return Integer.parseInt(value.trim());
} catch (NumberFormatException e) {
return null;
}
}
private static Instant firstOf(Instant preferred, Instant fallback) {
return preferred != null ? preferred : fallback;
}
private static void putTimestamp(ObjectNode target, String field, Instant value) {
if (value != null) {
target.put(field, TIMESTAMP.format(value));
}
}
}
@@ -43,11 +43,15 @@ import io.github.hectorvent.floci.services.s3.model.S3Object;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.ssm.SsmService;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.StatsCmd;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.LogConfig;
import com.github.dockerjava.api.model.StatisticNetworksConfig;
import com.github.dockerjava.api.model.Statistics;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
@@ -61,7 +65,9 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HexFormat;
@@ -70,7 +76,11 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Stream;
/**
@@ -92,6 +102,13 @@ public class EcsContainerManager {
private static final long DEPENDENCY_POLL_MILLIS = 200;
/** How long a killed container gets to register as exited before its code is read. */
private static final int KILL_SETTLE_SECONDS = 5;
/** How long a stats sample gets before the task metadata endpoint answers without one. */
private static final int STATS_SAMPLE_SECONDS = 5;
/**
* Docker writes {@code 0001-01-01T00:00:00Z} for an event that has not happened. Anything at
* or before the Unix epoch is that zero value rather than a time a container reached.
*/
private static final Instant DOCKER_ZERO_TIMESTAMP = Instant.EPOCH;
private final ContainerBuilder containerBuilder;
private final ContainerLifecycleManager lifecycleManager;
@@ -199,8 +216,14 @@ public class EcsContainerManager {
Map<ContainerDefinition, List<String>> envVarsByContainer = new LinkedHashMap<>();
// Resolved before any container is created, so a registry-startup failure can't leak one already started.
Map<ContainerDefinition, String> imagesByContainer = new LinkedHashMap<>();
// The task metadata id has to exist before the container does: its own environment carries
// the URI, so it cannot be derived from the Docker id the daemon hands back afterwards.
Map<String, String> metadataIdsByContainer = new LinkedHashMap<>();
for (ContainerDefinition def : launchOrder) {
envVarsByContainer.put(def, buildEnvVars(def, overridesByName.get(def.getName()), region));
String metadataId = UUID.randomUUID().toString().replace("-", "");
metadataIdsByContainer.put(def.getName(), metadataId);
envVarsByContainer.put(def, buildEnvVars(def, overridesByName.get(def.getName()), region,
metadataId));
imagesByContainer.put(def, ecrRegistryManager.rewriteImageUri(def.getImage()));
}
@@ -410,7 +433,8 @@ public class EcsContainerManager {
protectedNetwork == null ? dockerId : protectedNetwork.namespace().helperId(), def);
// Build ECS container model
Container container = buildContainer(task.getTaskArn(), def, dockerId, networkBindings, region);
Container container = buildContainer(task.getTaskArn(), def, dockerId, networkBindings, region,
metadataIdsByContainer.get(def.getName()));
runtimeContainers.add(container);
containerIds.put(def.getName(), dockerId);
@@ -606,6 +630,211 @@ public class EcsContainerManager {
}
}
/**
* A container's Docker stats as the task metadata endpoint reports them: the latest sample,
* plus the network byte rates. The rates are null when they cannot be derived, and Docker
* never reports them itself: they are a delta the ECS agent takes across two samples.
*/
public record ContainerStats(Statistics statistics, Double rxBytesPerSecond, Double txBytesPerSecond) {}
/**
* Reads a container's stats from the daemon. Empty when there is nothing to read: the
* container has no Docker id, it has already gone, or the daemon is unreachable.
*
* <p>The stats stream is read rather than a single {@code stream=false} document, because the
* network rates are a delta between consecutive samples. Docker sends the first sample at once
* and the next on its collection tick a second later, which is the same second AWS documents a
* container as having to run before its stats are available.
*/
public Optional<ContainerStats> sampleContainerStats(String dockerId) {
if (dockerId == null || dockerId.isBlank()) {
return Optional.empty();
}
return Optional.ofNullable(sampleContainerStats(List.of(dockerId)).get(dockerId));
}
/**
* Reads several containers' stats in one pass, keyed by Docker id. A container the daemon has
* nothing to say about is absent from the map rather than present with an empty sample.
*
* <p>Every stream is opened before any of them is waited on, and the waits share one deadline.
* Docker's stats stream is asynchronous, so the containers tick alongside each other and the
* call costs about the single collection tick one container costs, however many containers the
* task has. That matters on {@code /task/stats}, which is the path a sidecar polls: sampling
* the containers one after another would cost a tick each.
*/
public Map<String, ContainerStats> sampleContainerStats(List<String> dockerIds) {
Map<String, ContainerStats> sampled = new LinkedHashMap<>();
if (dockerIds == null || dockerIds.isEmpty()) {
return sampled;
}
Map<String, OpenStatsStream> streams = openStatsStreams(dockerIds);
try {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(STATS_SAMPLE_SECONDS);
for (Map.Entry<String, OpenStatsStream> stream : streams.entrySet()) {
awaitPair(stream.getKey(), stream.getValue().samples(), deadline);
containerStats(stream.getValue().samples().collected())
.ifPresent(stats -> sampled.put(stream.getKey(), stats));
}
} finally {
for (OpenStatsStream stream : streams.values()) {
closeQuietly(stream.samples());
closeQuietly(stream.command());
}
}
return sampled;
}
/** A stats stream the daemon is already filling, and the command that has to be closed with it. */
private record OpenStatsStream(StatsCmd command, ConsecutiveStatsSamples samples) {}
/** Starts every container's stats stream, skipping the ones the daemon will not open. */
private Map<String, OpenStatsStream> openStatsStreams(List<String> dockerIds) {
Map<String, OpenStatsStream> streams = new LinkedHashMap<>();
for (String dockerId : dockerIds) {
if (dockerId == null || dockerId.isBlank() || streams.containsKey(dockerId)) {
continue;
}
StatsCmd command = null;
try {
command = lifecycleManager.getDockerClient().statsCmd(dockerId);
ConsecutiveStatsSamples samples = new ConsecutiveStatsSamples();
command.exec(samples);
streams.put(dockerId, new OpenStatsStream(command, samples));
} catch (RuntimeException e) {
closeQuietly(command);
LOG.debugv("Could not sample stats for container {0}: {1}", dockerId, e.getMessage());
}
}
return streams;
}
/** Waits for one stream's pair of samples, never past the deadline the whole pass shares. */
private static void awaitPair(String dockerId, ConsecutiveStatsSamples samples, long deadlineNanos) {
long remaining = deadlineNanos - System.nanoTime();
try {
if (remaining <= 0 || !samples.awaitPair(remaining, TimeUnit.NANOSECONDS)) {
LOG.debugv("Docker sent fewer than two stats samples for container {0} within {1}s",
dockerId, STATS_SAMPLE_SECONDS);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.debugv("Interrupted while sampling stats for container {0}", dockerId);
}
}
private static void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException | RuntimeException e) {
LOG.debugv("Could not close a Docker stats stream: {0}", e.getMessage());
}
}
/** The latest sample, with rates when two samples a measurable interval apart arrived. */
private static Optional<ContainerStats> containerStats(List<Statistics> samples) {
if (samples.isEmpty()) {
return Optional.empty();
}
Statistics latest = samples.getLast();
if (samples.size() < 2) {
return Optional.of(new ContainerStats(latest, null, null));
}
Statistics earlier = samples.getFirst();
double seconds = secondsBetween(earlier.getRead(), latest.getRead());
Long rxDelta = byteDelta(earlier, latest, StatisticNetworksConfig::getRxBytes);
Long txDelta = byteDelta(earlier, latest, StatisticNetworksConfig::getTxBytes);
if (seconds <= 0 || rxDelta == null || txDelta == null) {
return Optional.of(new ContainerStats(latest, null, null));
}
return Optional.of(new ContainerStats(latest, rxDelta / seconds, txDelta / seconds));
}
/** The counter's movement across the two samples, or null when neither reports an interface. */
private static Long byteDelta(Statistics earlier, Statistics latest,
Function<StatisticNetworksConfig, Long> counter) {
Long earlierBytes = totalBytes(earlier, counter);
Long latestBytes = totalBytes(latest, counter);
if (earlierBytes == null || latestBytes == null) {
return null;
}
return Math.max(0, latestBytes - earlierBytes);
}
private static Long totalBytes(Statistics statistics,
Function<StatisticNetworksConfig, Long> counter) {
Map<String, StatisticNetworksConfig> networks = statistics.getNetworks();
if (networks == null || networks.isEmpty()) {
return null;
}
long total = 0;
for (StatisticNetworksConfig network : networks.values()) {
Long bytes = counter.apply(network);
total += bytes != null ? bytes : 0;
}
return total;
}
/** The interval between two Docker read timestamps, or 0 when either cannot be read. */
private static double secondsBetween(String earlier, String latest) {
if (earlier == null || latest == null) {
return 0;
}
try {
return Duration.between(Instant.parse(earlier), Instant.parse(latest)).toNanos() / 1_000_000_000.0;
} catch (DateTimeParseException e) {
LOG.debugv("Docker stats carried an unreadable read timestamp: {0}", e.getMessage());
return 0;
}
}
/** Docker's stats stream never ends on its own; this takes the first two samples and stops. */
private static final class ConsecutiveStatsSamples extends ResultCallback.Adapter<Statistics> {
private static final int WANTED = 2;
private final CountDownLatch enough = new CountDownLatch(WANTED);
private final List<Statistics> collected = new CopyOnWriteArrayList<>();
@Override
public void onNext(Statistics statistics) {
if (collected.size() < WANTED) {
collected.add(statistics);
enough.countDown();
}
}
@Override
public void onError(Throwable throwable) {
releaseWaiter();
super.onError(throwable);
}
@Override
public void onComplete() {
releaseWaiter();
super.onComplete();
}
/** A stream that ends or fails early must not hold the caller until the timeout. */
private void releaseWaiter() {
while (enough.getCount() > 0) {
enough.countDown();
}
}
boolean awaitPair(long timeout, TimeUnit unit) throws InterruptedException {
return enough.await(timeout, unit);
}
List<Statistics> collected() {
return List.copyOf(collected);
}
}
private List<ContainerDefinition> orderForDependencies(List<ContainerDefinition> definitions,
ContainerDefinition firelensRouter) {
Map<String, ContainerDefinition> definitionsByName = new LinkedHashMap<>();
@@ -899,6 +1128,8 @@ public class EcsContainerManager {
String name = entry.getKey();
String dockerId = entry.getValue();
exitCodes.put(name, getExitCodeIfStopped(dockerId));
// Read before the removal below, which is the last moment the daemon still knows it.
handle.recordFinishedAt(name, getFinishedAtIfStopped(dockerId));
try {
lifecycleManager.getDockerClient().removeContainerCmd(dockerId).withForce(true).exec();
terminatedContainerIds.add(dockerId);
@@ -1231,7 +1462,8 @@ public class EcsContainerManager {
}
}
private List<String> buildEnvVars(ContainerDefinition def, ContainerOverride override, String region) {
private List<String> buildEnvVars(ContainerDefinition def, ContainerOverride override, String region,
String metadataId) {
// AWS SDK baseline (endpoint + region + credentials) first so the task can reach the
// emulator, then the task-def environment, then task-def secrets, then the override
// environment. Later entries win on key conflict, so an explicit task-def value or
@@ -1243,6 +1475,12 @@ public class EcsContainerManager {
envMap.put(kv.substring(0, eq), kv.substring(eq + 1));
}
}
// The task metadata endpoint, which an application, the ECS SDK integrations and the
// aws-for-fluent-bit init process all read from this variable.
String flociEndpoint = awsEnv.flociEndpoint();
if (metadataId != null && flociEndpoint != null) {
envMap.put("ECS_CONTAINER_METADATA_URI_V4", flociEndpoint + "/v4/" + metadataId);
}
if (def.getEnvironment() != null) {
for (var kv : def.getEnvironment()) {
envMap.put(kv.name(), kv.value());
@@ -1440,7 +1678,8 @@ public class EcsContainerManager {
}
private Container buildContainer(String taskArn, ContainerDefinition def, String dockerId,
List<NetworkBinding> networkBindings, String region) {
List<NetworkBinding> networkBindings, String region,
String metadataId) {
Container container = new Container();
container.setTaskArn(taskArn);
container.setName(def.getName());
@@ -1449,6 +1688,7 @@ public class EcsContainerManager {
container.setNetworkBindings(networkBindings);
container.setDockerId(dockerId);
container.setRuntimeId(dockerId);
container.setMetadataId(metadataId);
container.setHealthStatus(def.getHealthCheck() != null ? "UNKNOWN" : null);
if (def.getCpu() != null) {
container.setCpu(String.valueOf(def.getCpu()));
@@ -1461,9 +1701,62 @@ public class EcsContainerManager {
}
container.setContainerArn(regionResolver.buildArn("ecs", region,
"container/" + extractTaskId(taskArn) + "/" + def.getName()));
applyLaunchTimestamps(container, dockerId);
return container;
}
/**
* Stamps the container with Docker's own create and start times. A task's containers start one
* at a time, holding for each other's {@code dependsOn} conditions, so these are the container's
* and not the task's.
*/
private void applyLaunchTimestamps(Container container, String dockerId) {
InspectContainerResponse inspect = inspectOrNull(dockerId);
if (inspect == null) {
return;
}
container.setCreatedAt(dockerTimestamp(inspect.getCreated()));
if (inspect.getState() != null) {
container.setStartedAt(dockerTimestamp(inspect.getState().getStartedAt()));
}
}
/**
* When a container that has already stopped finished. Read before the container is removed,
* since the daemon forgets it afterwards.
*/
public Instant getFinishedAtIfStopped(String dockerId) {
InspectContainerResponse inspect = inspectOrNull(dockerId);
if (inspect == null || inspect.getState() == null
|| Boolean.TRUE.equals(inspect.getState().getRunning())) {
return null;
}
return dockerTimestamp(inspect.getState().getFinishedAt());
}
private InspectContainerResponse inspectOrNull(String dockerId) {
try {
return lifecycleManager.getDockerClient().inspectContainerCmd(dockerId).exec();
} catch (Exception e) {
LOG.debugv("Could not inspect container {0}: {1}", dockerId, e.getMessage());
return null;
}
}
/** A Docker RFC 3339 timestamp, or null for its zero value, which means "never happened". */
private static Instant dockerTimestamp(String value) {
if (value == null || value.isBlank()) {
return null;
}
try {
Instant instant = Instant.parse(value);
return instant.isAfter(DOCKER_ZERO_TIMESTAMP) ? instant : null;
} catch (DateTimeParseException e) {
LOG.debugv("Docker reported an unreadable timestamp {0}: {1}", value, e.getMessage());
return null;
}
}
private static String extractTaskId(String arn) {
if (arn == null) {
return "";
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.ecs.container;
import java.io.Closeable;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -28,6 +29,11 @@ public class EcsTaskHandle {
private final String region;
/** Per-container {@code stopTimeout}, so teardown waits as long as the task definition asked. */
private final Map<String, Integer> stopTimeouts;
/**
* When each container finished, read during teardown. Docker forgets a container once it is
* removed, so the time has to be kept here for the caller that stamps the task's containers.
*/
private final Map<String, Instant> finishedAt = new LinkedHashMap<>();
public EcsTaskHandle(String taskArn, Map<String, String> containerIds,
Map<String, Closeable> logStreamsByContainerId) {
@@ -67,6 +73,18 @@ public class EcsTaskHandle {
return configured != null ? configured : DEFAULT_STOP_TIMEOUT_SECONDS;
}
/** Notes when a container finished. A null is ignored, so a failed read leaves no entry. */
public void recordFinishedAt(String containerName, Instant instant) {
if (instant != null) {
finishedAt.put(containerName, instant);
}
}
/** When each container finished, for the containers teardown could read a time for. */
public Map<String, Instant> getFinishedAt() {
return finishedAt;
}
/** Removes and returns the log stream that no longer needs task-level ownership. */
public Closeable removeLogStream(String containerId) {
return logStreamsByContainerId.remove(containerId);
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ecs.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
import java.util.List;
@RegisterForReflection
@@ -19,10 +20,24 @@ public class Container {
private List<ManagedAgent> managedAgents;
private String healthStatus;
private String runtimeId;
/**
* The opaque id in this container's {@code ECS_CONTAINER_METADATA_URI_V4}. Minted before the
* container is created, the way the ECS agent mints one, so the URI can be injected into the
* container's own environment.
*/
private String metadataId;
private String imageDigest;
private String cpu;
private String memory;
private String memoryReservation;
/**
* When Docker created, started and finished this container. The ECS API's own container shape
* carries none of these, so they never reach a DescribeTasks response; the task metadata
* endpoint reports them, and its containers start and stop one at a time.
*/
private Instant createdAt;
private Instant startedAt;
private Instant finishedAt;
// transient — not persisted
private transient String dockerId;
@@ -65,6 +80,9 @@ public class Container {
public String getRuntimeId() { return runtimeId; }
public void setRuntimeId(String runtimeId) { this.runtimeId = runtimeId; }
public String getMetadataId() { return metadataId; }
public void setMetadataId(String metadataId) { this.metadataId = metadataId; }
public String getImageDigest() { return imageDigest; }
public void setImageDigest(String imageDigest) { this.imageDigest = imageDigest; }
@@ -77,6 +95,15 @@ public class Container {
public String getMemoryReservation() { return memoryReservation; }
public void setMemoryReservation(String memoryReservation) { this.memoryReservation = memoryReservation; }
public Instant getCreatedAt() { return createdAt; }
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
public Instant getStartedAt() { return startedAt; }
public void setStartedAt(Instant startedAt) { this.startedAt = startedAt; }
public Instant getFinishedAt() { return finishedAt; }
public void setFinishedAt(Instant finishedAt) { this.finishedAt = finishedAt; }
public String getDockerId() { return dockerId; }
public void setDockerId(String dockerId) { this.dockerId = dockerId; }
}