Skip to content

Commit 93b599c

Browse files
authored
backport stable-5 - ecs_cluster capacity provider strategy #1640 #1673 (#1678)
backport stable-5 - ecs_cluster capacity provider strategy #1640 #1673 backport #1640 ecs_cluster capacity provider strategy #1640 (comment) backport #1676 waiting for #1676 SUMMARY Fixes #770 - Add AWS ECS_Cluster Capacity Provider Strategy Support ISSUE TYPE Feature Pull Request COMPONENT NAME ecs_cluster ADDITIONAL INFORMATION When creating or updating an ECS Cluster, configure the capacity providers and capacity provider strategy as provided by the user. Given playbook task: name: Create an ECS Cluster with Capacity Providers ecs_cluster: name: default state: present capacity_providers: - FARGATE - FARGATE_SPOT capacity_provider_strategy: - capacity_provider: FARGATE base: 1 weight: 1 - capacity_provider: FARGATE_SPOT weight: 100 Previously would throw "Unsupported parameter" and no other parameter exists to expose these features. Now you should see changed: [localhost] with the resultant created ECS Cluster having the same providers and provider_strategy fields as provided by the user. Reviewed-by: Markus Bergholz [email protected] Reviewed-by: Justin McCormick Reviewed-by: Alina Buzachis SUMMARY ISSUE TYPE Bugfix Pull Request Docs Pull Request Feature Pull Request New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Mark Chappell <None>
1 parent a2f6dbd commit 93b599c

File tree

4 files changed

+165
-5
lines changed

4 files changed

+165
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- ecs_cluster - append default value to documentation (https://github.com/ansible-collections/community.aws/pull/1636).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- ecs_cluster - add support for ``capacity_providers`` and ``capacity_provider_strategy`` features (https://github.com/ansible-collections/community.aws/pull/1640).

plugins/modules/ecs_cluster.py

Lines changed: 137 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,42 @@
4141
required: false
4242
type: int
4343
default: 10
44+
capacity_providers:
45+
version_added: 5.2.0
46+
description:
47+
- List of capacity providers to use for the cluster.
48+
required: false
49+
type: list
50+
elements: str
51+
capacity_provider_strategy:
52+
version_added: 5.2.0
53+
description:
54+
- List of capacity provider strategies to use for the cluster.
55+
required: false
56+
type: list
57+
elements: dict
58+
suboptions:
59+
capacity_provider:
60+
description:
61+
- Name of capacity provider.
62+
type: str
63+
weight:
64+
description:
65+
- The relative percentage of the total number of launched tasks that should use the specified provider.
66+
type: int
67+
base:
68+
description:
69+
- How many tasks, at a minimum, should use the specified provider.
70+
type: int
71+
default: 0
72+
purge_capacity_providers:
73+
version_added: 5.2.0
74+
description:
75+
- Toggle overwriting of existing capacity providers or strategy. This is needed for backwards compatibility.
76+
- By default I(purge_capacity_providers=false). In a release after 2024-06-01 this will be changed to I(purge_capacity_providers=true).
77+
required: false
78+
type: bool
79+
default: false
4480
extends_documentation_fragment:
4581
- amazon.aws.aws
4682
- amazon.aws.ec2
@@ -56,6 +92,21 @@
5692
name: default
5793
state: present
5894
95+
- name: Cluster creation with capacity providers and strategies.
96+
community.aws.ecs_cluster:
97+
name: default
98+
state: present
99+
capacity_providers:
100+
- FARGATE
101+
- FARGATE_SPOT
102+
capacity_provider_strategy:
103+
- capacity_provider: FARGATE
104+
base: 1
105+
weight: 1
106+
- capacity_provider: FARGATE_SPOT
107+
weight: 100
108+
purge_capacity_providers: True
109+
59110
- name: Cluster deletion
60111
community.aws.ecs_cluster:
61112
name: default
@@ -75,6 +126,16 @@
75126
description: how many services are active in this cluster
76127
returned: 0 if a new cluster
77128
type: int
129+
capacityProviders:
130+
version_added: 5.2.0
131+
description: list of capacity providers used in this cluster
132+
returned: always
133+
type: list
134+
defaultCapacityProviderStrategy:
135+
version_added: 5.2.0
136+
description: list of capacity provider strategies used in this cluster
137+
returned: always
138+
type: list
78139
clusterArn:
79140
description: the ARN of the cluster just created
80141
type: str
@@ -112,6 +173,8 @@
112173
pass # Handled by AnsibleAWSModule
113174

114175
from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
176+
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
177+
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
115178

116179

117180
class EcsClusterManager:
@@ -145,8 +208,26 @@ def describe_cluster(self, cluster_name):
145208
return c
146209
raise Exception("Unknown problem describing cluster %s." % cluster_name)
147210

148-
def create_cluster(self, clusterName='default'):
149-
response = self.ecs.create_cluster(clusterName=clusterName)
211+
def create_cluster(self, cluster_name, capacity_providers, capacity_provider_strategy):
212+
params = dict(clusterName=cluster_name)
213+
if capacity_providers:
214+
params['capacityProviders'] = snake_dict_to_camel_dict(capacity_providers)
215+
if capacity_provider_strategy:
216+
params['defaultCapacityProviderStrategy'] = snake_dict_to_camel_dict(capacity_provider_strategy)
217+
response = self.ecs.create_cluster(**params)
218+
return response['cluster']
219+
220+
def update_cluster(self, cluster_name, capacity_providers, capacity_provider_strategy):
221+
params = dict(cluster=cluster_name)
222+
if capacity_providers:
223+
params['capacityProviders'] = snake_dict_to_camel_dict(capacity_providers)
224+
else:
225+
params['capacityProviders'] = []
226+
if capacity_provider_strategy:
227+
params['defaultCapacityProviderStrategy'] = snake_dict_to_camel_dict(capacity_provider_strategy)
228+
else:
229+
params['defaultCapacityProviderStrategy'] = []
230+
response = self.ecs.put_cluster_capacity_providers(**params)
150231
return response['cluster']
151232

152233
def delete_cluster(self, clusterName):
@@ -159,7 +240,17 @@ def main():
159240
state=dict(required=True, choices=['present', 'absent', 'has_instances']),
160241
name=dict(required=True, type='str'),
161242
delay=dict(required=False, type='int', default=10),
162-
repeat=dict(required=False, type='int', default=10)
243+
repeat=dict(required=False, type='int', default=10),
244+
purge_capacity_providers=dict(required=False, type='bool', default=False),
245+
capacity_providers=dict(required=False, type='list', elements='str'),
246+
capacity_provider_strategy=dict(required=False,
247+
type='list',
248+
elements='dict',
249+
options=dict(capacity_provider=dict(type='str'),
250+
weight=dict(type='int'),
251+
base=dict(type='int', default=0)
252+
)
253+
),
163254
)
164255
required_together = [['state', 'name']]
165256

@@ -177,12 +268,53 @@ def main():
177268

178269
results = dict(changed=False)
179270
if module.params['state'] == 'present':
271+
# Pull requested and existing capacity providers and strategies.
272+
purge_capacity_providers = module.params['purge_capacity_providers']
273+
requested_cp = module.params['capacity_providers']
274+
requested_cps = module.params['capacity_provider_strategy']
180275
if existing and 'status' in existing and existing['status'] == "ACTIVE":
181-
results['cluster'] = existing
276+
existing_cp = existing['capacityProviders']
277+
existing_cps = existing['defaultCapacityProviderStrategy']
278+
279+
if requested_cp is None:
280+
requested_cp = []
281+
282+
# Check if capacity provider strategy needs to trigger an update.
283+
cps_update_needed = False
284+
if requested_cps is not None:
285+
for strategy in requested_cps:
286+
if snake_dict_to_camel_dict(strategy) not in existing_cps:
287+
cps_update_needed = True
288+
for strategy in existing_cps:
289+
if camel_dict_to_snake_dict(strategy) not in requested_cps:
290+
cps_update_needed = True
291+
elif requested_cps is None and existing_cps != []:
292+
cps_update_needed = True
293+
294+
# Unless purge_capacity_providers is true, we will not be updating the providers or strategy.
295+
if not purge_capacity_providers:
296+
module.deprecate('After 2024-06-01 the default value of purge_capacity_providers will change from false to true.'
297+
' To maintain the existing behaviour explicitly set purge_capacity_providers=true',
298+
date='2024-06-01', collection_name='community.aws')
299+
cps_update_needed = False
300+
requested_cp = existing_cp
301+
requested_cps = existing_cps
302+
303+
# If either the providers or strategy differ, update the cluster.
304+
if requested_cp != existing_cp or cps_update_needed:
305+
if not module.check_mode:
306+
results['cluster'] = cluster_mgr.update_cluster(cluster_name=module.params['name'],
307+
capacity_providers=requested_cp,
308+
capacity_provider_strategy=requested_cps)
309+
results['changed'] = True
310+
else:
311+
results['cluster'] = existing
182312
else:
183313
if not module.check_mode:
184314
# doesn't exist. create it.
185-
results['cluster'] = cluster_mgr.create_cluster(module.params['name'])
315+
results['cluster'] = cluster_mgr.create_cluster(cluster_name=module.params['name'],
316+
capacity_providers=requested_cp,
317+
capacity_provider_strategy=requested_cps)
186318
results['changed'] = True
187319

188320
# delete the cluster

tests/integration/targets/ecs_cluster/tasks/main.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@
6363
that:
6464
- not ecs_cluster_again.changed
6565

66+
- name: add capacity providers and strategy
67+
ecs_cluster:
68+
name: "{{ ecs_cluster_name }}"
69+
state: present
70+
purge_capacity_providers: True
71+
capacity_providers:
72+
- FARGATE
73+
- FARGATE_SPOT
74+
capacity_provider_strategy:
75+
- capacity_provider: FARGATE
76+
base: 1
77+
weight: 1
78+
- capacity_provider: FARGATE_SPOT
79+
weight: 100
80+
register: ecs_cluster_update
81+
82+
- name: check that ecs_cluster was correctly updated
83+
assert:
84+
that:
85+
- ecs_cluster_update.changed
86+
- ecs_cluster_update.cluster is defined
87+
- ecs_cluster_update.cluster.capacityProviders is defined
88+
- "'FARGATE' in ecs_cluster_update.cluster.capacityProviders"
89+
6690
- name: create a VPC to work in
6791
ec2_vpc_net:
6892
cidr_block: 10.0.0.0/16

0 commit comments

Comments
 (0)