8000 ecs_cluster capacity provider strategy by karcadia · Pull Request #1640 · ansible-collections/community.aws · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ecs_cluster capacity provider strategy #1640

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8cfea5d
Merge conflict.
karcadia Jan 10, 2023
f1b34f5
Some lines are too long.
karcadia Jan 4, 2023
d03d6d5
Provide more detail to argument spec and documentation.
karcadia Jan 5, 2023
19400ee
PEP8 Indentation Rules.
karcadia Jan 5, 2023
4ac5d21
Indentation Rules.
karcadia Jan 5, 2023
bfa1fc5
Spaces everywhere.
karcadia Jan 5, 2023
efc3336
Probably trading an indent error for a line long error.
karcadia Jan 5, 2023
fd8521c
refactor to avoid line splitting
karcadia Jan 5, 2023
1c7ff8c
Linting / Indentation
karcadia Jan 5, 2023
fd17f51
Linting / Indentation
karcadia Jan 5, 2023
15b9f32
Fix idempotency
karcadia Jan 5, 2023
2f52d93
Refactor to avoid assert.
karcadia Jan 6, 2023
e429046
Merge conflicts / rebase.
karcadia Jan 10, 2023
7e655bd
Merge conflicts / rebase.
karcadia Jan 10, 2023
083ef77
Merge conflicts / rebase.
karcadia Jan 10, 2023
a5ffc34
Linting / Indentation
karcadia Jan 5, 2023
8d1dc43
Formatting / Whitespace
karcadia Jan 10, 2023
f600cc4
Update changelogs/fragments/770-ecs-capacity-provider-strategy.yml
markuman Jan 12, 2023
1009ed3
Update plugins/modules/ecs_cluster.py
markuman Jan 12, 2023
016e426
Update plugins/modules/ecs_cluster.py
markuman Jan 12, 2023
87b6584
Update plugins/modules/ecs_cluster.py
markuman Jan 12, 2023
020cd62
Add toggle for purge_capacity_providers and ensure a default base is …
karcadia Jan 12, 2023
52b943e
Catch purge_capacity_providers earlier so we can retain idempotency.
karcadia Jan 12, 2023
89ed2b9
Create cluster also needs to use our variables with the base default …
karcadia Jan 12, 2023
37f3976
Flip the logic on purge_capacity_providers and catch a condition wher…
karcadia Jan 12, 2023
ca37ba7
Add import for camel_dict_to_snake_dict.
karcadia Jan 12, 2023
1dead45
Both loops check requested_cps so that cannot be None.
karcadia Jan 12, 2023
52fd322
Need to also trigger an update when a strategy is in existing but now…
karcadia Jan 12, 2023
400211c
Add purge_capacity_providers to the documentation.
karcadia Jan 12, 2023
e330238
Let the argument_spec handle defaulting to 0.
karcadia Jan 12, 2023
6af6c41
Add purge_capacity_providers default to the documentation.
karcadia Jan 12, 2023
a68d816
Provide a warning to the user that some of their request will be igno…
karcadia Jan 12, 2023
f957ebf
Update changelogs/fragments/770-ecs-capacity-provider-strategy.yml
markuman Jan 18, 2023
d31c8af
Integration test should use purge_capacity_providers True for now.
karcadia Jan 18, 2023
ffe6d23
Only extract detail from existing when existing cluster exists and is…
karcadia Jan 18, 2023
67be965
Fix idempotency -- override cps_update_needed to false as part of pur…
karcadia Jan 19, 2023
7b353b1
Update plugins/modules/ecs_cluster.py
markuman Jan 20, 2023
265df83
Update plugins/modules/ecs_cluster.py
markuman Jan 20, 2023
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
2 changes: 2 additions & 0 deletions changelogs/fragments/770-ecs-capacity-provider-strategy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ecs_cluster - add support for ``capacity_providers`` and ``capacity_provider_strategy`` features (https://github.com/ansible-collections/community.aws/pull/1640).
141 changes: 136 additions & 5 deletions plugins/modules/ecs_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,41 @@
required: false
type: int
default: 10
capacity_providers:
version_added: 5.2.0
description:
- List of capacity providers to use for the cluster.
required: false
type: list
elements: str
capacity_provider_strategy:
version_added: 5.2.0
description:
- List of capacity provider strategies to use for the cluster.
required: false
type: list
elements: dict
suboptions:
capacity_provider:
description:
- Name of capacity provider.
type: str
weight:
description:
- The relative percentage of the total number of launched tasks that should use the specified provider.
type: int
base:
description:
- How many tasks, at a minimum, should use the specified provider.
type: int
purge_capacity_providers:
version_added: 5.2.0
description:
- Toggle overwriting of existing capacity providers or strategy. This is needed for backwards compatibility.
- By default I(purge_capacity_providers=false). In a release after 2024-06-01 this will be changed to I(purge_capacity_providers=true).
required: false
type: bool
default: false
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
Expand All @@ -56,6 +91,21 @@
name: default
state: present

- name: Cluster creation with capacity providers and strategies.
community.aws.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
purge_capacity_providers: True

- name: Cluster deletion
community.aws.ecs_cluster:
name: default
Expand All @@ -75,6 +125,16 @@
description: how many services are active in this cluster
returned: 0 if a new cluster
type: int
capacityProviders:
version_added: 5.2.0
description: list of capacity providers used in this cluster
returned: always
type: list
defaultCapacityProviderStrategy:
version_added: 5.2.0
description: list of capacity provider strategies used in this cluster
returned: always
type: list
clusterArn:
description: the ARN of the cluster just created
type: str
Expand Down Expand Up @@ -112,6 +172,8 @@
pass # Handled by AnsibleAWSModule

from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict


class EcsClusterManager:
Expand Down Expand Up @@ -145,8 +207,26 @@ def describe_cluster(self, cluster_name):
return c
raise Exception("Unknown problem describing cluster %s." % cluster_name)

def create_cluster(self, clusterName='default'):
response = self.ecs.create_cluster(clusterName=clusterName)
def create_cluster(self, cluster_name, capacity_providers, capacity_provider_strategy):
params = dict(clusterName=cluster_name)
if capacity_providers:
params['capacityProviders'] = snake_dict_to_camel_dict(capacity_providers)
if capacity_provider_strategy:
params['defaultCapacityProviderStrategy'] = snake_dict_to_camel_dict(capacity_provider_strategy)
response = self.ecs.create_cluster(**params)
return response['cluster']

def update_cluster(self, cluster_name, capacity_providers, capacity_provider_strategy):
params = dict(cluster=cluster_name)
if capacity_providers:
params['capacityProviders'] = snake_dict_to_camel_dict(capacity_providers)
else:
params['capacityProviders'] = []
if capacity_provider_strategy:
params['defaultCapacityProviderStrategy'] = snake_dict_to_camel_dict(capacity_provider_strategy)
else:
params['defaultCapacityProviderStrategy'] = []
response = self.ecs.put_cluster_capacity_providers(**params)
return response['cluster']

def delete_cluster(self, clusterName):
Expand All @@ -159,7 +239,17 @@ def main():
state=dict(required=True, choices=['present', 'absent', 'has_instances']),
name=dict(required=True, type='str'),
delay=dict(required=False, type='int', default=10),
repeat=dict(required=False, type='int', default=10)
repeat=dict(required=False, type='int', default=10),
purge_capacity_providers=dict(required=False, type='bool', default=False),
capacity_providers=dict(required=False, type='list', elements='str'),
capacity_provider_strategy=dict(required=False,
type='list',
elements='dict',
options=dict(capacity_provider=dict(type='str'),
weight=dict(type='int'),
base=dict(type='int', default=0)
)
),
)
required_together = [['state', 'name']]

Expand All @@ -177,12 +267,53 @@ def main():

results = dict(changed=False)
if module.params['state'] == 'present':
# Pull requested and existing capacity providers and strategies.
purge_capacity_providers = module.params['purge_capacity_providers']
requested_cp = module.params['capacity_providers']
requested_cps = module.params['capacity_provider_strategy']
if existing and 'status' in existing and existing['status'] == "ACTIVE":
results['cluster'] = existing
existing_cp = existing['capacityProviders']
existing_cps = existing['defaultCapacityProviderStrategy']

if requested_cp is None:
requested_cp = []

# Check if capacity provider strategy needs to trigger an update.
cps_update_needed = False
if requested_cps is not None:
for strategy in requested_cps:
if snake_dict_to_camel_dict(strategy) not in existing_cps:
cps_update_needed = True
for strategy in existing_cps:
if camel_dict_to_snake_dict(strategy) not in requested_cps:
cps_update_needed = True
elif requested_cps is None and existing_cps != []:
cps_update_needed = True

# Unless purge_capacity_providers is true, we will not be updating the providers or strategy.
if not purge_capacity_providers:
module.deprecate('After 2024-06-01 the default value of purge_capacity_providers will change from false to true.'
' To maintain the existing behaviour explicitly set purge_capacity_providers=true',
date='2024-06-01', collection_name='community.aws')
cps_update_needed = False
requested_cp = existing_cp
requested_cps = existing_cps

# If either the providers or strategy differ, update the cluster.
if requested_cp != existing_cp or cps_update_needed:
if not module.check_mode:
results['cluster'] = cluster_mgr.update_cluster(cluster_name=module.params['name'],
capacity_providers=requested_cp,
capacity_provider_strategy=requested_cps)
results['changed'] = True
else:
results['cluster'] = existing
else:
if not module.check_mode:
# doesn't exist. create it.
results['cluster'] = cluster_mgr.create_cluster(module.params['name'])
results['cluster'] = cluster_mgr.create_cluster(cluster_name=module.params['name'],
capacity_providers=requested_cp,
capacity_provider_strategy=requested_cps)
results['changed'] = True

# delete the cluster
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/targets/ecs_cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@
that:
- not ecs_cluster_again.changed

- name: add capacity providers and strategy
ecs_cluster:
name: "{{ ecs_cluster_name }}"
state: present
purge_capacity_providers: True
capacity_providers:
- FARGATE
- FARGATE_SPOT
capacity_provider_strategy:
- capacity_provider: FARGATE
base: 1
weight: 1
- capacity_provider: FARGATE_SPOT
weight: 100
register: ecs_cluster_update

- name: check that ecs_cluster was correctly updated
assert:
that:
- ecs_cluster_update.changed
- ecs_cluster_update.cluster is defined
- ecs_cluster_update.cluster.capacityProviders is defined
- "'FARGATE' in ecs_cluster_update.cluster.capacityProviders"

- name: create a VPC to work in
ec2_vpc_net:
cidr_block: 10.0.0.0/16
Expand Down
0