Merge pull request #2948 from rifqifatih/ecs-placement-constraints

ECS: add placement constraints
This commit is contained in:
Steve Pulec 2020-05-02 12:36:45 -05:00 committed by GitHub
commit 067b7f3f5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 1 deletions

View File

@ -121,6 +121,7 @@ class TaskDefinition(BaseObject):
network_mode=None, network_mode=None,
volumes=None, volumes=None,
tags=None, tags=None,
placement_constraints=None,
): ):
self.family = family self.family = family
self.revision = revision self.revision = revision
@ -137,6 +138,9 @@ class TaskDefinition(BaseObject):
self.network_mode = "bridge" self.network_mode = "bridge"
else: else:
self.network_mode = network_mode self.network_mode = network_mode
self.placement_constraints = (
placement_constraints if placement_constraints is not None else []
)
@property @property
def response_object(self): def response_object(self):
@ -558,7 +562,13 @@ class EC2ContainerServiceBackend(BaseBackend):
raise Exception("{0} is not a cluster".format(cluster_name)) raise Exception("{0} is not a cluster".format(cluster_name))
def register_task_definition( def register_task_definition(
self, family, container_definitions, volumes=None, network_mode=None, tags=None self,
family,
container_definitions,
volumes=None,
network_mode=None,
tags=None,
placement_constraints=None,
): ):
if family in self.task_definitions: if family in self.task_definitions:
last_id = self._get_last_task_definition_revision_id(family) last_id = self._get_last_task_definition_revision_id(family)
@ -574,6 +584,7 @@ class EC2ContainerServiceBackend(BaseBackend):
volumes=volumes, volumes=volumes,
network_mode=network_mode, network_mode=network_mode,
tags=tags, tags=tags,
placement_constraints=placement_constraints,
) )
self.task_definitions[family][revision] = task_definition self.task_definitions[family][revision] = task_definition

View File

@ -63,12 +63,14 @@ class EC2ContainerServiceResponse(BaseResponse):
volumes = self._get_param("volumes") volumes = self._get_param("volumes")
tags = self._get_param("tags") tags = self._get_param("tags")
network_mode = self._get_param("networkMode") network_mode = self._get_param("networkMode")
placement_constraints = self._get_param("placementConstraints")
task_definition = self.ecs_backend.register_task_definition( task_definition = self.ecs_backend.register_task_definition(
family, family,
container_definitions, container_definitions,
volumes=volumes, volumes=volumes,
network_mode=network_mode, network_mode=network_mode,
tags=tags, tags=tags,
placement_constraints=placement_constraints,
) )
return json.dumps({"taskDefinition": task_definition.response_object}) return json.dumps({"taskDefinition": task_definition.response_object})

View File

@ -2604,3 +2604,36 @@ def test_ecs_service_untag_resource_multiple_tags():
resourceArn=response["service"]["serviceArn"] resourceArn=response["service"]["serviceArn"]
) )
response["tags"].should.equal([{"key": "hello", "value": "world"}]) response["tags"].should.equal([{"key": "hello", "value": "world"}])
@mock_ecs
def test_ecs_task_definition_placement_constraints():
client = boto3.client("ecs", region_name="us-east-1")
response = client.register_task_definition(
family="test_ecs_task",
containerDefinitions=[
{
"name": "hello_world",
"image": "docker/hello-world:latest",
"cpu": 1024,
"memory": 400,
"essential": True,
"environment": [
{"name": "AWS_ACCESS_KEY_ID", "value": "SOME_ACCESS_KEY"}
],
"logConfiguration": {"logDriver": "json-file"},
}
],
networkMode="bridge",
tags=[
{"key": "createdBy", "value": "moto-unittest"},
{"key": "foo", "value": "bar"},
],
placementConstraints=[
{"type": "memberOf", "expression": "attribute:ecs.instance-type =~ t2.*"}
],
)
type(response["taskDefinition"]["placementConstraints"]).should.be(list)
response["taskDefinition"]["placementConstraints"].should.equal(
[{"type": "memberOf", "expression": "attribute:ecs.instance-type =~ t2.*"}]
)