From 7afd4ceba022d51dc682cb648a0aa7c9b15cfefc Mon Sep 17 00:00:00 2001 From: karcadia Date: Sat, 28 May 2022 13:11:13 -0500 Subject: [PATCH] Capacity Provider Strategy for ECS Services #1137 Add requested feature for capacity provider strategy. Capacity Provider Strategy is mutually exclusive with launch_type and a given service cannot be changed from one to the other. Possibly still needs: Camel <=> Snake casing for the subelements of capacity_provider_strategy. Catch if provided list has more than 6 elements and fail cleanly. --- plugins/modules/ecs_service.py | 74 ++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 8 deletions(-) diff --git a/plugins/modules/ecs_service.py b/plugins/modules/ecs_service.py index 66f20b63d81..089b14991e9 100644 --- a/plugins/modules/ecs_service.py +++ b/plugins/modules/ecs_service.py @@ -158,6 +158,25 @@ required: false choices: ["EC2", "FARGATE"] type: str + capacity_provider_strategy: + description: + - The capacity provider strategy to use with your service. + required: false + type: list + elements: dict + suboptions: + capacityProvider: + 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 platform_version: type: str description: @@ -249,6 +268,18 @@ placement_strategy: - type: binpack field: memory + +# With capacity_provider_strategy (added in version 4.0) +- community.aws.ecs_service: + state: present + name: test-service + cluster: test-cluster + task_definition: test-task-definition + desired_count: 1 + capacity_provider_strategy: + - capacityProvider: test-capacity-provider-1 + weight: 1 + base: 0 ''' RETURN = r''' @@ -257,6 +288,23 @@ returned: when creating a service type: complex contains: + capacityProviderStrategy: + description: The capacity provider strategy to use with your service. + returned: always + type: complex + contains: + base: + description: How many tasks, at a minimum, should use the specified provider. + returned: always + type: int + capacityProvider: + description: Name of capacity provider. + returned: always + type: str + weight: + description: The relative percentage of the total number of launched tasks that should use the specified provider. + returned: always + type: int clusterArn: description: The Amazon Resource Name (ARN) of the of the cluster that hosts the service. returned: always @@ -574,7 +622,7 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan desired_count, client_token, role, deployment_configuration, placement_constraints, placement_strategy, health_check_grace_period_seconds, network_configuration, service_registries, launch_type, platform_version, - scheduling_strategy): + scheduling_strategy, capacity_provider_strategy): params = dict( cluster=cluster_name, @@ -600,7 +648,8 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan # desired count is not required if scheduling strategy is daemon if desired_count is not None: params['desiredCount'] = desired_count - + if capacity_provider_strategy: + params['capacityProviderStrategy'] = capacity_provider_strategy if scheduling_strategy: params['schedulingStrategy'] = scheduling_strategy response = self.ecs.create_service(**params) @@ -608,7 +657,7 @@ def create_service(self, service_name, cluster_name, task_definition, load_balan def update_service(self, service_name, cluster_name, task_definition, desired_count, deployment_configuration, network_configuration, - health_check_grace_period_seconds, force_new_deployment): + health_check_grace_period_seconds, force_new_deployment, capacity_provider_strategy): params = dict( cluster=cluster_name, service=service_name, @@ -696,14 +745,16 @@ def main(): launch_type=dict(required=False, choices=['EC2', 'FARGATE']), platform_version=dict(required=False, type='str'), service_registries=dict(required=False, type='list', default=[], elements='dict'), - scheduling_strategy=dict(required=False, choices=['DAEMON', 'REPLICA']) + scheduling_strategy=dict(required=False, choices=['DAEMON', 'REPLICA']), + capacity_provider_strategy=dict(required=False, type='list', default=[], elements='dict') ) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True, required_if=[('state', 'present', ['task_definition']), ('launch_type', 'FARGATE', ['network_configuration'])], - required_together=[['load_balancers', 'role']]) + required_together=[['load_balancers', 'role']], + mutually_exclusive=[['launch_type', 'capacity_provider_strategy']]) if module.params['state'] == 'present' and module.params['scheduling_strategy'] == 'REPLICA': if module.params['desired_count'] is None: @@ -768,7 +819,12 @@ def main(): if module.params['service_registries']: if (existing['serviceRegistries'] or []) != serviceRegistries: module.fail_json(msg="It is not possible to update the service registries of an existing service") - + if module.params['capacity_provider_strategy']: + if 'launchType' in existing.keys(): + module.fail_json(msg="It is not possible to change an existing service from launch_type to capacity_provider_strategy.") + if module.params['launch_type']: + if 'capacityProviderStrategy' in existing.keys(): + module.fail_json(msg="It is not possible to change an existing service from capacity_provider_strategy to launch_type.") if (existing['loadBalancers'] or []) != loadBalancers: module.fail_json(msg="It is not possible to update the load balancers of an existing service") @@ -780,7 +836,8 @@ def main(): deploymentConfiguration, network_configuration, module.params['health_check_grace_period_seconds'], - module.params['force_new_deployment']) + module.params['force_new_deployment'], + module.params['capacity_provider_strategy']) else: try: @@ -799,7 +856,8 @@ def main(): serviceRegistries, module.params['launch_type'], module.params['platform_version'], - module.params['scheduling_strategy'] + module.params['scheduling_strategy'], + module.params['capacity_provider_strategy'] ) except botocore.exceptions.ClientError as e: module.fail_json_aws(e, msg="Couldn't create service")