mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(ecs): add network mode and resources validation for fargate task … (#1274)
* fix(ecs): add network mode and resources validation for fargate task definitions * fix(ecs): add validation for ECS with fargate for task definition * fix(ecs): add exact error messages and compatibilities default to EC2 when requiresCompatibilities not provided
This commit is contained in:
1 parent
22a50b3c80
commit
8ba258c37a
6 files changed
+245
-6
No files matched your search
+2
-1
@@ -1864,10 +1864,11 @@ public class CloudFormationResourceProvisioner {
|
||||
String memory = resolveOptional(props, "Memory", engine);
|
||||
String taskRoleArn = resolveOptional(props, "TaskRoleArn", engine);
|
||||
String executionRoleArn = resolveOptional(props, "ExecutionRoleArn", engine);
|
||||
List<String> requiresCompatibilities = resolveStringListOrEmpty(props, "RequiresCompatibilities", engine);
|
||||
|
||||
// Task definitions are immutable; each CFN update registers a fresh revision.
|
||||
TaskDefinition td = ecsService.registerTaskDefinition(family, containerDefs, networkMode, cpu, memory,
|
||||
taskRoleArn, executionRoleArn, region);
|
||||
taskRoleArn, executionRoleArn, requiresCompatibilities, region);
|
||||
|
||||
r.setPhysicalId(td.getTaskDefinitionArn());
|
||||
r.getAttributes().put("TaskDefinitionArn", td.getTaskDefinitionArn());
|
||||
|
||||
@@ -207,10 +207,11 @@ public class EcsJsonHandler {
|
||||
String memory = req.has("memory") ? req.path("memory").asText() : null;
|
||||
String taskRoleArn = req.hasNonNull("taskRoleArn") ? req.path("taskRoleArn").asText() : null;
|
||||
String executionRoleArn = req.hasNonNull("executionRoleArn") ? req.path("executionRoleArn").asText() : null;
|
||||
List<String> requiresCompatibilities = jsonArrayToList(req.path("requiresCompatibilities"));
|
||||
Map<String, String> tags = parseTagMap(req.path("tags"));
|
||||
|
||||
TaskDefinition td = service.registerTaskDefinition(family, containerDefs, networkMode, cpu, memory,
|
||||
taskRoleArn, executionRoleArn, tags, region);
|
||||
taskRoleArn, executionRoleArn, requiresCompatibilities, 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")));
|
||||
@@ -922,6 +923,16 @@ public class EcsJsonHandler {
|
||||
if (td.getMemory() != null) { n.put("memory", td.getMemory()); }
|
||||
if (td.getTaskRoleArn() != null) { n.put("taskRoleArn", td.getTaskRoleArn()); }
|
||||
if (td.getExecutionRoleArn() != null) { n.put("executionRoleArn", td.getExecutionRoleArn()); }
|
||||
if (td.getRequiresCompatibilities() != null && !td.getRequiresCompatibilities().isEmpty()) {
|
||||
ArrayNode arr = objectMapper.createArrayNode();
|
||||
td.getRequiresCompatibilities().forEach(arr::add);
|
||||
n.set("requiresCompatibilities", arr);
|
||||
}
|
||||
if (td.getCompatibilities() != null && !td.getCompatibilities().isEmpty()) {
|
||||
ArrayNode arr = objectMapper.createArrayNode();
|
||||
td.getCompatibilities().forEach(arr::add);
|
||||
n.set("compatibilities", arr);
|
||||
}
|
||||
|
||||
ArrayNode containers = objectMapper.createArrayNode();
|
||||
if (td.getContainerDefinitions() != null) {
|
||||
|
||||
@@ -193,15 +193,31 @@ public class EcsService {
|
||||
public TaskDefinition registerTaskDefinition(String family, List<ContainerDefinition> containerDefs,
|
||||
NetworkMode networkMode, String cpu, String memory,
|
||||
String taskRoleArn, String executionRoleArn,
|
||||
List<String> requiresCompatibilities,
|
||||
String region) {
|
||||
return registerTaskDefinition(family, containerDefs, networkMode, cpu, memory,
|
||||
taskRoleArn, executionRoleArn, null, region);
|
||||
taskRoleArn, executionRoleArn, requiresCompatibilities, null, region);
|
||||
}
|
||||
|
||||
public TaskDefinition registerTaskDefinition(String family, List<ContainerDefinition> containerDefs,
|
||||
NetworkMode networkMode, String cpu, String memory,
|
||||
String taskRoleArn, String executionRoleArn,
|
||||
List<String> requiresCompatibilities,
|
||||
Map<String, String> tags, String region) {
|
||||
if (requiresCompatibilities != null && requiresCompatibilities.contains("FARGATE")) {
|
||||
if (networkMode != NetworkMode.awsvpc) {
|
||||
throw new AwsException("ClientException", "Fargate only supports network mode 'awsvpc'.", 400);
|
||||
}
|
||||
if (cpu == null) {
|
||||
throw new AwsException("ClientException", "Fargate requires that 'cpu' be defined at the task level.", 400);
|
||||
}
|
||||
if (memory == null) {
|
||||
throw new AwsException("ClientException", "Fargate requires that 'memory' be defined at the task level.", 400);
|
||||
}
|
||||
if (!isValidFargateCpuMemory(cpu, memory)) {
|
||||
throw new AwsException("ClientException", "No Fargate configuration exists for given values.", 400);
|
||||
}
|
||||
}
|
||||
int revision = latestRevisions.merge(family, 1, Integer::sum);
|
||||
|
||||
TaskDefinition td = new TaskDefinition();
|
||||
@@ -214,6 +230,14 @@ public class EcsService {
|
||||
td.setTaskRoleArn(taskRoleArn);
|
||||
td.setExecutionRoleArn(executionRoleArn);
|
||||
td.setContainerDefinitions(containerDefs != null ? containerDefs : List.of());
|
||||
td.setRequiresCompatibilities(requiresCompatibilities);
|
||||
|
||||
if (requiresCompatibilities != null && !requiresCompatibilities.isEmpty()) {
|
||||
td.setCompatibilities(new java.util.ArrayList<>(requiresCompatibilities));
|
||||
} else {
|
||||
td.setCompatibilities(java.util.List.of("EC2"));
|
||||
}
|
||||
|
||||
td.setTaskDefinitionArn(regionResolver.buildArn("ecs", region,
|
||||
"task-definition/" + family + ":" + revision));
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
@@ -225,6 +249,63 @@ public class EcsService {
|
||||
return td;
|
||||
}
|
||||
|
||||
private boolean isValidFargateCpuMemory(String cpuStr, String memStr) {
|
||||
if (cpuStr == null || memStr == null) return false;
|
||||
int cpu = parseCpu(cpuStr);
|
||||
int memory = parseMemory(memStr);
|
||||
if (cpu <= 0 || memory <= 0) return false;
|
||||
|
||||
if (cpu == 256) return memory == 512 || memory == 1024 || memory == 2048;
|
||||
if (cpu == 512) return memory >= 1024 && memory <= 4096 && memory % 1024 == 0;
|
||||
if (cpu == 1024) return memory >= 2048 && memory <= 8192 && memory % 1024 == 0;
|
||||
if (cpu == 2048) return memory >= 4096 && memory <= 16384 && memory % 1024 == 0;
|
||||
if (cpu == 4096) return memory >= 8192 && memory <= 30720 && memory % 1024 == 0;
|
||||
if (cpu == 8192) return memory >= 16384 && memory <= 61440 && memory % 4096 == 0;
|
||||
if (cpu == 16384) return memory >= 32768 && memory <= 122880 && memory % 8192 == 0;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private int parseCpu(String cpu) {
|
||||
if (cpu.toUpperCase().endsWith("VCPU")) {
|
||||
try {
|
||||
double v = Double.parseDouble(cpu.substring(0, cpu.length() - 4).trim());
|
||||
return (int) Math.round(v * 1024);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(cpu.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private int parseMemory(String mem) {
|
||||
if (mem.toUpperCase().endsWith("GB")) {
|
||||
try {
|
||||
double v = Double.parseDouble(mem.substring(0, mem.length() - 2).trim());
|
||||
return (int) Math.round(v * 1024);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (mem.toUpperCase().endsWith("MB") || mem.toUpperCase().endsWith("MIB")) {
|
||||
try {
|
||||
double v = Double.parseDouble(mem.replaceAll("(?i)mib|mb", "").trim());
|
||||
return (int) Math.round(v);
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(mem.trim());
|
||||
} catch (NumberFormatException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public TaskDefinition describeTaskDefinition(String taskDefinitionRef, String region) {
|
||||
return resolveTaskDefinitionOrThrow(taskDefinitionRef, region);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ public class TaskDefinition {
|
||||
private String executionRoleArn;
|
||||
private List<ContainerDefinition> containerDefinitions;
|
||||
private List<Volume> volumes;
|
||||
private List<String> requiresCompatibilities;
|
||||
private List<String> compatibilities;
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
|
||||
public String getTaskDefinitionArn() { return taskDefinitionArn; }
|
||||
@@ -57,6 +59,12 @@ public class TaskDefinition {
|
||||
public List<Volume> getVolumes() { return volumes; }
|
||||
public void setVolumes(List<Volume> volumes) { this.volumes = volumes; }
|
||||
|
||||
public List<String> getRequiresCompatibilities() { return requiresCompatibilities; }
|
||||
public void setRequiresCompatibilities(List<String> requiresCompatibilities) { this.requiresCompatibilities = requiresCompatibilities; }
|
||||
|
||||
public List<String> getCompatibilities() { return compatibilities; }
|
||||
public void setCompatibilities(List<String> compatibilities) { this.compatibilities = compatibilities; }
|
||||
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) { this.tags = tags; }
|
||||
}
|
||||
Reference in new issue
Block a user