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_service supports constraints and strategy update #1601

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ecs_service supports constraints and strategy update (https://github.com/ansible-collections/community.aws/pull/1601).
markuman marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 13 additions & 3 deletions plugins/modules/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,14 +819,22 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan
response = self.ecs.create_service(**params)
return self.jsonize(response['service'])

def update_service(self, service_name, cluster_name, task_definition,
desired_count, deployment_configuration, network_configuration,
health_check_grace_period_seconds, force_new_deployment, capacity_provider_strategy):
def update_service(self, service_name, cluster_name, task_definition, desired_count,
deployment_configuration, placement_constraints, placement_strategy,
network_configuration, health_check_grace_period_seconds,
force_new_deployment, capacity_provider_strategy):
params = dict(
cluster=cluster_name,
service=service_name,
taskDefinition=task_definition,
deploymentConfiguration=deployment_configuration)
# filter placement_constraint and left only those where value is not None
# use-case: `distinctInstance` type should never contain `expression`, but None will fail `str` type validation
if placement_constraints:
params['placementConstraints'] = [{key: value for key, value in constraint.items() if value is not None}
for constraint in placement_constraints]
if placement_strategy:
params['placementStrategy'] = placement_strategy
markuman marked this conversation as resolved.
Show resolved Hide resolved
if network_configuration:
params['networkConfiguration'] = network_configuration
if force_new_deployment:
Expand Down Expand Up @@ -1038,6 +1046,8 @@ def main():
task_definition,
module.params['desired_count'],
deploymentConfiguration,
module.params['placement_constraints'],
module.params['placement_strategy'],
network_configuration,
module.params['health_check_grace_period_seconds'],
module.params['force_new_deployment'],
Expand Down
116 changes: 116 additions & 0 deletions tests/integration/targets/ecs_cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,104 @@
that:
- ecs_task_definition_constraints_delete is not changed

- name: Create ecs service with placement constraints
ecs_service:
name: "{{ ecs_service_name }}-constraint"
cluster: "{{ ecs_cluster_name }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
containerName: "{{ ecs_task_name }}"
containerPort: "{{ ecs_task_container_port }}"
task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}"
scheduling_strategy: "REPLICA"
placement_constraints:
- type: distinctInstance
desired_count: 1
state: present
register: ecs_service_creation_constraints

- name: Assert ecs service constraint
assert:
that:
- ecs_service_creation_constraints.changed
- "{{ ecs_service_creation_constraints.service.placementConstraints | length }} == 1"
- "{{ ecs_service_creation_constraints.service.placementConstraints[0].type }} == distinctInstance"

- name: Update ecs service's placement constraints
ecs_service:
name: "{{ ecs_service_name }}-constraint"
cluster: "{{ ecs_cluster_name }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
containerName: "{{ ecs_task_name }}"
containerPort: "{{ ecs_task_container_port }}"
task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}"
scheduling_strategy: "REPLICA"
placement_constraints:
- type: memberOf
expression: 'attribute:ecs.instance-type == t3.micro'
desired_count: 1
state: present
register: ecs_service_update_constraints

- name: Assert ecs service constraint
assert:
that:
- ecs_service_update_constraints.changed
- "{{ ecs_service_update_constraints.service.placementConstraints | length }} == 1"
- "{{ ecs_service_update_constraints.service.placementConstraints[0].type }} == memberOf"
- "{{ ecs_service_update_constraints.service.placementConstraints[0].expression }} == 'attribute:ecs.instance-type == t3.micro'"

- name: Create ecs service with placement strategy
ecs_service:
name: "{{ ecs_service_name }}-strategy"
cluster: "{{ ecs_cluster_name }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
containerName: "{{ ecs_task_name }}"
containerPort: "{{ ecs_task_container_port }}"
task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}"
scheduling_strategy: "REPLICA"
placement_strategy:
- type: binpack
field: MEMORY
desired_count: 1
state: present
register: ecs_service_creation_strategy

- name: Assert ecs service strategy
assert:
that:
- ecs_service_creation_strategy.changed
- "{{ ecs_service_creation_strategy.service.placementplacementStrategy | length }} == 1"
- "{{ ecs_service_creation_strategy.service.placementConstraints[0].type }} == binpack"
- "{{ ecs_service_creation_strategy.service.placementConstraints[0].field }} == MEMORY"

- name: Update ecs service's placement strategy
ecs_service:
name: "{{ ecs_service_name }}-strategy"
cluster: "{{ ecs_cluster_name }}"
load_balancers:
- targetGroupArn: "{{ elb_target_group_instance.target_group_arn }}"
containerName: "{{ ecs_task_name }}"
containerPort: "{{ ecs_task_container_port }}"
task_definition: "{{ ecs_task_name }}:{{ ecs_task_definition.taskdefinition.revision }}"
scheduling_strategy: "REPLICA"
placement_strategy:
- type: spread
field: instanceId
desired_count: 1
state: present
register: ecs_service_update_strategy

- name: Assert ecs service strategy
assert:
that:
- ecs_service_update_strategy.changed
- "{{ ecs_service_update_strategy.service.placementplacementStrategy | length }} == 1"
- "{{ ecs_service_update_strategy.service.placementConstraints[0].type }} == spread"
- "{{ ecs_service_update_strategy.service.placementConstraints[0].field }} == instanceId"

# ============================================================
# Begin tests for Fargate

Expand Down Expand Up @@ -985,6 +1083,24 @@
wait: yes
ignore_errors: yes

- name: remove constraints ecs service
ecs_service:
state: absent
cluster: "{{ ecs_cluster_name }}"
name: "{{ ecs_service_name }}-constraint"
force_deletion: yes
wait: yes
ignore_errors: yes

- name: remove strategy ecs service
ecs_service:
state: absent
cluster: "{{ ecs_cluster_name }}"
name: "{{ ecs_service_name }}-strategy"
force_deletion: yes
wait: yes
ignore_errors: yes

- name: remove scheduling_strategy ecs service
ecs_service:
state: absent
Expand Down