Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECS task definition cpu/memory #3267

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions moto/ecs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def __init__(
revision,
container_definitions,
region_name,
cpu=None,
memory=None,
network_mode=None,
volumes=None,
tags=None,
Expand All @@ -131,6 +133,8 @@ def __init__(
region_name, family, revision
)
self.container_definitions = container_definitions
self.cpu = cpu
self.memory = memory
self.tags = tags if tags is not None else []
if volumes is None:
self.volumes = []
Expand Down Expand Up @@ -667,6 +671,8 @@ def register_task_definition(
self,
family,
container_definitions,
cpu=None,
memory=None,
volumes=None,
network_mode=None,
tags=None,
Expand All @@ -683,6 +689,8 @@ def register_task_definition(
revision,
container_definitions,
self.region_name,
cpu=cpu,
memory=memory,
volumes=volumes,
network_mode=network_mode,
tags=tags,
Expand Down Expand Up @@ -777,16 +785,21 @@ def run_task(self, cluster_str, task_definition_str, count, overrides, started_b

@staticmethod
def _calculate_task_resource_requirements(task_definition):
resource_requirements = {"CPU": 0, "MEMORY": 0, "PORTS": [], "PORTS_UDP": []}
resource_requirements = {
"CPU": int(task_definition.cpu or "0"),
"MEMORY": int(task_definition.memory or "0"),
"PORTS": [],
"PORTS_UDP": [],
}
for container_definition in task_definition.container_definitions:
# cloudformation uses capitalized properties, while boto uses all lower case

# CPU is optional
resource_requirements["CPU"] += container_definition.get(
"cpu", container_definition.get("Cpu", 0)
"cpu", container_definition.get("Cpu", 0) or 0
)

# either memory or memory reservation must be provided
# either memory or memory reservation must be provided if not on the task level
if (
"Memory" in container_definition
or "MemoryReservation" in container_definition
Expand All @@ -795,9 +808,13 @@ def _calculate_task_resource_requirements(task_definition):
"Memory", container_definition.get("MemoryReservation")
)
else:
resource_requirements["MEMORY"] += container_definition.get(
"memory", container_definition.get("memoryReservation")
)
try:
resource_requirements["MEMORY"] += container_definition.get(
"memory", container_definition["memoryReservation"]
)
except KeyError:
if not task_definition.memory:
raise

port_mapping_key = (
"PortMappings"
Expand Down
4 changes: 4 additions & 0 deletions moto/ecs/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ def delete_cluster(self):
def register_task_definition(self):
family = self._get_param("family")
container_definitions = self._get_param("containerDefinitions")
cpu = self._get_param("cpu")
memory = self._get_param("memory")
volumes = self._get_param("volumes")
tags = self._get_param("tags")
network_mode = self._get_param("networkMode")
placement_constraints = self._get_param("placementConstraints")
task_definition = self.ecs_backend.register_task_definition(
family,
container_definitions,
cpu=cpu,
memory=memory,
volumes=volumes,
network_mode=network_mode,
tags=tags,
Expand Down