mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(ecs): honour RunTask containerOverrides (command + environment) (#1151)
RunTask accepts overrides.containerOverrides[] in the AWS ECS API, but floci parsed RunTask without reading them, so a caller's per-container command and environment overrides were silently dropped — the launched container always ran the task-definition's command/env. Thread containerOverrides from EcsJsonHandler.runTask through EcsService.runTask/launchTasks into EcsContainerManager.startTask, matched to a container definition by name: - command (if present and non-empty) replaces the task-def command - environment is merged over the task-def environment (override wins on key) StartTask and the service reconciler pass null (no overrides), unchanged. Adds model/ContainerOverride (name, command, environment), @RegisterForReflection for native builds.
This commit is contained in:
1 parent
357a64243b
commit
a9297b8fec
5 files changed
+233
-15
No files matched your search
@@ -8,6 +8,7 @@ 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.ContainerOverride;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel;
|
||||
@@ -271,9 +272,11 @@ public class EcsJsonHandler {
|
||||
LaunchType launchType = parseEnum(req, "launchType", LaunchType.class);
|
||||
String group = req.has("group") ? req.path("group").asText() : null;
|
||||
String startedBy = req.has("startedBy") ? req.path("startedBy").asText() : null;
|
||||
List<ContainerOverride> containerOverrides =
|
||||
parseContainerOverrides(req.path("overrides").path("containerOverrides"));
|
||||
|
||||
List<EcsTask> launched = service.runTask(cluster, taskDefinition, count,
|
||||
launchType, group, startedBy, region);
|
||||
launchType, group, startedBy, containerOverrides, region);
|
||||
|
||||
ObjectNode resp = objectMapper.createObjectNode();
|
||||
ArrayNode arr = objectMapper.createArrayNode();
|
||||
@@ -1239,6 +1242,25 @@ public class EcsJsonHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ContainerOverride> parseContainerOverrides(JsonNode node) {
|
||||
List<ContainerOverride> result = new ArrayList<>();
|
||||
if (!node.isArray()) {
|
||||
return result;
|
||||
}
|
||||
for (JsonNode item : node) {
|
||||
ContainerOverride co = new ContainerOverride();
|
||||
co.setName(item.path("name").asText());
|
||||
if (item.has("command") && item.path("command").isArray()) {
|
||||
List<String> cmd = new ArrayList<>();
|
||||
item.path("command").forEach(c -> cmd.add(c.asText()));
|
||||
co.setCommand(cmd);
|
||||
}
|
||||
co.setEnvironment(parseKeyValuePairs(item.path("environment")));
|
||||
result.add(co);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<ClusterSetting> parseClusterSettings(JsonNode node) {
|
||||
List<ClusterSetting> result = new ArrayList<>();
|
||||
if (!node.isArray()) {
|
||||
|
||||
@@ -10,6 +10,7 @@ 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.ContainerOverride;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsCluster;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsLoadBalancer;
|
||||
import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel;
|
||||
@@ -265,10 +266,12 @@ public class EcsService {
|
||||
// ── Tasks ─────────────────────────────────────────────────────────────────
|
||||
|
||||
public List<EcsTask> runTask(String clusterRef, String taskDefinitionRef, int count,
|
||||
LaunchType launchType, String group, String startedBy, String region) {
|
||||
LaunchType launchType, String group, String startedBy,
|
||||
List<ContainerOverride> containerOverrides, String region) {
|
||||
EcsCluster cluster = resolveClusterOrDefault(clusterRef, region);
|
||||
TaskDefinition taskDef = resolveTaskDefinitionOrThrow(taskDefinitionRef, region);
|
||||
return launchTasks(cluster, taskDef, count, launchType, group, startedBy, null, region);
|
||||
return launchTasks(cluster, taskDef, count, launchType, group, startedBy, null,
|
||||
containerOverrides, region);
|
||||
}
|
||||
|
||||
public List<EcsTask> startTask(String clusterRef, List<String> containerInstanceRefs,
|
||||
@@ -279,7 +282,7 @@ public class EcsService {
|
||||
for (String instanceRef : containerInstanceRefs) {
|
||||
ContainerInstance instance = resolveContainerInstanceOrThrow(cluster.getClusterArn(), instanceRef);
|
||||
List<EcsTask> launched = launchTasks(cluster, taskDef, 1, LaunchType.EC2,
|
||||
group, startedBy, instance.getContainerInstanceArn(), region);
|
||||
group, startedBy, instance.getContainerInstanceArn(), null, region);
|
||||
result.addAll(launched);
|
||||
}
|
||||
return result;
|
||||
@@ -287,7 +290,8 @@ public class EcsService {
|
||||
|
||||
private List<EcsTask> launchTasks(EcsCluster cluster, TaskDefinition taskDef, int count,
|
||||
LaunchType launchType, String group, String startedBy,
|
||||
String containerInstanceArn, String region) {
|
||||
String containerInstanceArn,
|
||||
List<ContainerOverride> containerOverrides, String region) {
|
||||
List<EcsTask> launched = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
String taskId = UUID.randomUUID().toString().replace("-", "");
|
||||
@@ -313,7 +317,7 @@ public class EcsService {
|
||||
|
||||
if (dockerMode) {
|
||||
try {
|
||||
EcsTaskHandle handle = containerManager.startTask(task, taskDef, region);
|
||||
EcsTaskHandle handle = containerManager.startTask(task, taskDef, containerOverrides, region);
|
||||
taskHandles.put(taskArn, handle);
|
||||
cluster.setRunningTasksCount(cluster.getRunningTasksCount() + 1);
|
||||
LOG.infov("Started ECS task (docker): {0}", taskArn);
|
||||
@@ -1153,7 +1157,7 @@ public class EcsService {
|
||||
for (int i = 0; i < toStart; i++) {
|
||||
try {
|
||||
List<EcsTask> launched = runTask(clusterName, svc.getTaskDefinition(), 1,
|
||||
svc.getLaunchType(), svc.getServiceName(), "ecs-svc", region);
|
||||
svc.getLaunchType(), svc.getServiceName(), "ecs-svc", null, region);
|
||||
LOG.infov("Service reconciler started task {0} for service {1}",
|
||||
launched.getFirst().getTaskArn(), svc.getServiceName());
|
||||
} catch (Exception e) {
|
||||
|
||||
+37
-8
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
|
||||
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.NetworkBinding;
|
||||
import io.github.hectorvent.floci.services.ecs.model.PortMapping;
|
||||
@@ -63,7 +64,8 @@ public class EcsContainerManager {
|
||||
* Starts Docker containers for all container definitions in a task.
|
||||
* Updates the task's container list in-place with runtime network bindings and docker IDs.
|
||||
*/
|
||||
public EcsTaskHandle startTask(EcsTask task, TaskDefinition taskDef, String region) {
|
||||
public EcsTaskHandle startTask(EcsTask task, TaskDefinition taskDef,
|
||||
List<ContainerOverride> containerOverrides, String region) {
|
||||
String taskId = extractTaskId(task.getTaskArn());
|
||||
|
||||
Map<String, String> containerIds = new LinkedHashMap<>();
|
||||
@@ -73,10 +75,22 @@ public class EcsContainerManager {
|
||||
for (ContainerDefinition def : taskDef.getContainerDefinitions()) {
|
||||
String containerName = "floci-ecs-" + taskId + "-" + def.getName();
|
||||
|
||||
// RunTask containerOverrides matched by container name: command replaces
|
||||
// the task-def command; environment is merged over the task-def environment.
|
||||
ContainerOverride override = null;
|
||||
if (containerOverrides != null) {
|
||||
for (ContainerOverride co : containerOverrides) {
|
||||
if (def.getName() != null && def.getName().equals(co.getName())) {
|
||||
override = co;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build container spec
|
||||
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(def.getImage())
|
||||
.withName(containerName)
|
||||
.withEnv(buildEnvVars(def))
|
||||
.withEnv(buildEnvVars(def, override))
|
||||
.withDockerNetwork(config.services().ecs().dockerNetwork())
|
||||
.withLogRotation();
|
||||
|
||||
@@ -98,9 +112,14 @@ public class EcsContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Add command and entrypoint if specified
|
||||
if (def.getCommand() != null && !def.getCommand().isEmpty()) {
|
||||
specBuilder.withCmd(def.getCommand());
|
||||
// Add command and entrypoint if specified. An override command (from
|
||||
// RunTask containerOverrides) takes precedence over the task-def command.
|
||||
List<String> effectiveCommand =
|
||||
(override != null && override.getCommand() != null && !override.getCommand().isEmpty())
|
||||
? override.getCommand()
|
||||
: def.getCommand();
|
||||
if (effectiveCommand != null && !effectiveCommand.isEmpty()) {
|
||||
specBuilder.withCmd(effectiveCommand);
|
||||
}
|
||||
if (def.getEntryPoint() != null && !def.getEntryPoint().isEmpty()) {
|
||||
specBuilder.withEntrypoint(def.getEntryPoint());
|
||||
@@ -232,13 +251,23 @@ public class EcsContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> buildEnvVars(ContainerDefinition def) {
|
||||
List<String> envVars = new ArrayList<>();
|
||||
private List<String> buildEnvVars(ContainerDefinition def, ContainerOverride override) {
|
||||
// Task-def environment first, then override environment (override wins on key conflict).
|
||||
Map<String, String> envMap = new LinkedHashMap<>();
|
||||
if (def.getEnvironment() != null) {
|
||||
for (var kv : def.getEnvironment()) {
|
||||
envVars.add(kv.name() + "=" + kv.value());
|
||||
envMap.put(kv.name(), kv.value());
|
||||
}
|
||||
}
|
||||
if (override != null && override.getEnvironment() != null) {
|
||||
for (var kv : override.getEnvironment()) {
|
||||
envMap.put(kv.name(), kv.value());
|
||||
}
|
||||
}
|
||||
List<String> envVars = new ArrayList<>();
|
||||
for (var entry : envMap.entrySet()) {
|
||||
envVars.add(entry.getKey() + "=" + entry.getValue());
|
||||
}
|
||||
return envVars;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.github.hectorvent.floci.services.ecs.model;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A per-container override supplied on a RunTask request
|
||||
* (overrides.containerOverrides[]). When present and matched by {@code name}
|
||||
* to a container definition, its {@code command} replaces the task-def command
|
||||
* and its {@code environment} is merged over the task-def environment.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public class ContainerOverride {
|
||||
|
||||
private String name;
|
||||
private List<String> command;
|
||||
private List<KeyValuePair> environment;
|
||||
|
||||
public String getName() { return name; }
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
public List<String> getCommand() { return command; }
|
||||
public void setCommand(List<String> command) { this.command = command; }
|
||||
|
||||
public List<KeyValuePair> getEnvironment() { return environment; }
|
||||
public void setEnvironment(List<KeyValuePair> environment) { this.environment = environment; }
|
||||
}
|
||||
Reference in new issue
Block a user