From 6f5d21598fde5dfdb8c7774bd49200af2d74bc31 Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 18:59:11 +0100 Subject: [PATCH 1/8] Adding 'replicas_max_per_node' placement parameter --- plugins/modules/docker_swarm_service.py | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/plugins/modules/docker_swarm_service.py b/plugins/modules/docker_swarm_service.py index f8a4bd00e..7d237e3f9 100644 --- a/plugins/modules/docker_swarm_service.py +++ b/plugins/modules/docker_swarm_service.py @@ -344,6 +344,12 @@ - Requires API version >= 1.27. type: list elements: dict + replicas_max_per_node: + description: + - Maximum number of tasks per node. + - Corresponds to the C(--replicas_max_per_node) option of C(docker service create). + - Requires API version >= 1.40 + type: int type: dict publish: description: @@ -840,6 +846,7 @@ constraints: - node.role == manager - engine.labels.operatingsystem == ubuntu 14.04 + replicas_max_per_node: 2 - name: Set configs community.docker.docker_swarm_service: @@ -1208,6 +1215,7 @@ def __init__(self, docker_api_version, docker_py_version): self.configs = None self.secrets = None self.constraints = None + self.replicas_max_per_node = None self.networks = None self.stop_grace_period = None self.stop_signal = None @@ -1257,6 +1265,7 @@ def get_facts(self): 'log_driver_options': self.log_driver_options, 'publish': self.publish, 'constraints': self.constraints, + 'replicas_max_per_node': self.replicas_max_per_node, 'placement_preferences': self.placement_preferences, 'labels': self.labels, 'container_labels': self.container_labels, @@ -1472,9 +1481,15 @@ def get_placement_from_ansible_params(params): ) preferences = placement.get('preferences') + replicas_max_per_node = get_value( + 'replicas_max_per_node', + placement + ) + return { 'constraints': constraints, 'placement_preferences': preferences, + 'replicas_max_per_node': replicas_max_per_node, } @classmethod @@ -1683,6 +1698,8 @@ def compare(self, os): differences.add('args', parameter=self.args, active=os.args) if has_list_changed(self.constraints, os.constraints): differences.add('constraints', parameter=self.constraints, active=os.constraints) + if has_list_changed(self.replicas_max_per_node, os.replicas_max_per_node): + differences.add('replicas_max_per_node', parameter=self.replicas_max_per_node, active=os.replicas_max_per_node) if has_list_changed(self.placement_preferences, os.placement_preferences, sort_lists=False): differences.add('placement_preferences', parameter=self.placement_preferences, active=os.placement_preferences) if has_list_changed(self.groups, os.groups): @@ -1923,6 +1940,8 @@ def build_placement(self): placement_args = {} if self.constraints is not None: placement_args['constraints'] = self.constraints + if self.replicas_max_per_node is not None: + placement_args['maxreplicas'] = self.replicas_max_per_node if self.placement_preferences is not None: placement_args['preferences'] = [ {key.title(): {'SpreadDescriptor': value}} @@ -2164,6 +2183,7 @@ def get_service(self, name): placement = task_template_data.get('Placement') if placement: ds.constraints = placement.get('Constraints') + ds.replicas_max_per_node = placement.get('MaxReplicas') placement_preferences = [] for preference in placement.get('Preferences', []): placement_preferences.append( @@ -2603,6 +2623,7 @@ def main(): placement=dict(type='dict', options=dict( constraints=dict(type='list', elements='str'), preferences=dict(type='list', elements='dict'), + replicas_max_per_node=dict(type='int'), )), tty=dict(type='bool'), dns=dict(type='list', elements='str'), @@ -2748,6 +2769,14 @@ def main(): ) is not None, usage_msg='set placement.constraints' ), + placement_config_replicas_max_per_node=dict( + docker_py_version='4.4.3', + docker_api_version='1.40', + detect_usage=lambda c: (c.module.params['placement'] or {}).get( + 'replicas_max_per_node' + ) is not None, + usage_msg='set placement.replicas_max_per_node' + ), mounts_tmpfs=dict( docker_py_version='2.6.0', detect_usage=_detect_mount_tmpfs_usage, From 6b3d2a6b2d94841505246bd6cf6ac2c161f66d16 Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 19:09:12 +0100 Subject: [PATCH 2/8] Adding 'replicas_max_per_node' placement parameter --- plugins/modules/docker_swarm_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/docker_swarm_service.py b/plugins/modules/docker_swarm_service.py index 7d237e3f9..52efa5aa5 100644 --- a/plugins/modules/docker_swarm_service.py +++ b/plugins/modules/docker_swarm_service.py @@ -1698,7 +1698,7 @@ def compare(self, os): differences.add('args', parameter=self.args, active=os.args) if has_list_changed(self.constraints, os.constraints): differences.add('constraints', parameter=self.constraints, active=os.constraints) - if has_list_changed(self.replicas_max_per_node, os.replicas_max_per_node): + if self.replicas_max_per_node is not None and self.replicas_max_per_node != os.replicas_max_per_node: differences.add('replicas_max_per_node', parameter=self.replicas_max_per_node, active=os.replicas_max_per_node) if has_list_changed(self.placement_preferences, os.placement_preferences, sort_lists=False): differences.add('placement_preferences', parameter=self.placement_preferences, active=os.placement_preferences) From 9c2b04e0bac6b0d1faffd1c200a568a3076ae130 Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 19:14:24 +0100 Subject: [PATCH 3/8] Adding 'replicas_max_per_node' integration tests --- .../tasks/tests/placement.yml | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/integration/targets/docker_swarm_service/tasks/tests/placement.yml b/tests/integration/targets/docker_swarm_service/tasks/tests/placement.yml index 71873b206..cc4290dee 100644 --- a/tests/integration/targets/docker_swarm_service/tasks/tests/placement.yml +++ b/tests/integration/targets/docker_swarm_service/tasks/tests/placement.yml @@ -200,3 +200,59 @@ - constraints_1 is failed - "'Minimum version required' in constraints_1.msg" when: docker_api_version is version('1.27', '<') or docker_py_version is version('2.4.0', '<') + +#################################################################### +## placement.replicas_max_per_node ##################################################### +#################################################################### + +- name: placement.replicas_max_per_node + docker_swarm_service: + name: "{{ service_name }}" + image: "{{ docker_test_image_alpine }}" + resolve_image: no + command: '/bin/sh -v -c "sleep 10m"' + placement: + replicas_max_per_node: 1 + register: replicas_max_per_node_1 + ignore_errors: yes + +- name: placement.replicas_max_per_node (idempotency) + docker_swarm_service: + name: "{{ service_name }}" + image: "{{ docker_test_image_alpine }}" + resolve_image: no + command: '/bin/sh -v -c "sleep 10m"' + placement: + replicas_max_per_node: 1 + register: replicas_max_per_node_2 + ignore_errors: yes + +- name: placement.replicas_max_per_node (change) + docker_swarm_service: + name: "{{ service_name }}" + image: "{{ docker_test_image_alpine }}" + resolve_image: no + command: '/bin/sh -v -c "sleep 10m"' + placement: + replicas_max_per_node: 2 + register: replicas_max_per_node_3 + ignore_errors: yes + + +- name: cleanup + docker_swarm_service: + name: "{{ service_name }}" + state: absent + diff: no + +- assert: + that: + - replicas_max_per_node_1 is changed + - replicas_max_per_node_2 is not changed + - replicas_max_per_node_3 is changed + when: docker_api_version is version('1.40', '>=') and docker_py_version is version('4.4.3', '>=') +- assert: + that: + - replicas_max_per_node_1 is failed + - "'Minimum version required' in replicas_max_per_node_1.msg" + when: docker_api_version is version('1.40', '<') or docker_py_version is version('4.4.3', '<') From ce597dad2ea1016f0eb7c20303aef634cc85bbec Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 19:33:43 +0100 Subject: [PATCH 4/8] Adding 'replicas_max_per_node' feature --- plugins/modules/docker_swarm_service.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/docker_swarm_service.py b/plugins/modules/docker_swarm_service.py index 52efa5aa5..3c37765a1 100644 --- a/plugins/modules/docker_swarm_service.py +++ b/plugins/modules/docker_swarm_service.py @@ -13,6 +13,7 @@ - "Dario Zanzico (@dariko)" - "Jason Witkowski (@jwitko)" - "Hannes Ljungberg (@hannseman)" + - "Piotr Wojciechowski (@wojciechowskipiotr)" short_description: docker swarm service description: - Manages docker services via a swarm manager node. From a6a892b72efe57c3f3b2a8578196c5bd3d089183 Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 19:59:36 +0100 Subject: [PATCH 5/8] 'replicas_max_per_node' documentation update --- plugins/modules/docker_swarm_service.py | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/modules/docker_swarm_service.py b/plugins/modules/docker_swarm_service.py index 3c37765a1..7f123776b 100644 --- a/plugins/modules/docker_swarm_service.py +++ b/plugins/modules/docker_swarm_service.py @@ -351,6 +351,7 @@ - Corresponds to the C(--replicas_max_per_node) option of C(docker service create). - Requires API version >= 1.40 type: int + version_added: 1.3.0 type: dict publish: description: From 8f209e4070f82c4d0f9c0b876a952b5fa4d0ad17 Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 20:37:54 +0100 Subject: [PATCH 6/8] 'replicas_max_per_node' unit test fix --- tests/integration/targets/docker_swarm_service/vars/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/targets/docker_swarm_service/vars/main.yml b/tests/integration/targets/docker_swarm_service/vars/main.yml index 8ec7ffebd..0f21e6456 100644 --- a/tests/integration/targets/docker_swarm_service/vars/main.yml +++ b/tests/integration/targets/docker_swarm_service/vars/main.yml @@ -35,6 +35,7 @@ service_expected_output: - {mode: null, protocol: udp, published_port: 60001, target_port: 60001} read_only: null replicas: null + replicas_max_per_node: null reserve_cpu: null reserve_memory: null restart_policy: null From e2728ecac059757ee0900efe1d71a53fe5195c82 Mon Sep 17 00:00:00 2001 From: WojciechowskiPiotr Date: Tue, 23 Feb 2021 21:48:12 +0100 Subject: [PATCH 7/8] Documentation and changelog updates --- changelogs/fragments/92-replicas-max-per-node.yml | 2 ++ plugins/modules/docker_swarm_service.py | 1 + 2 files changed, 3 insertions(+) create mode 100644 changelogs/fragments/92-replicas-max-per-node.yml diff --git a/changelogs/fragments/92-replicas-max-per-node.yml b/changelogs/fragments/92-replicas-max-per-node.yml new file mode 100644 index 000000000..dd6df55a8 --- /dev/null +++ b/changelogs/fragments/92-replicas-max-per-node.yml @@ -0,0 +1,2 @@ +minor_changes: + - "docker_swarm_service - Adding support for maximum number of tasks per node (replicas_max_per_node) when running swarm service in replicated mode. Introduced in API 1.40" diff --git a/plugins/modules/docker_swarm_service.py b/plugins/modules/docker_swarm_service.py index 7f123776b..4d42a1e18 100644 --- a/plugins/modules/docker_swarm_service.py +++ b/plugins/modules/docker_swarm_service.py @@ -726,6 +726,7 @@ "publish": null, "read_only": null, "replicas": 1, + "replicas_max_per_node": 1, "reserve_cpu": 0.25, "reserve_memory": 20971520, "restart_policy": "on-failure", From c39e3f95f9ff0b65bdfe4ffba49b63b666bc6460 Mon Sep 17 00:00:00 2001 From: Piotr Wojciechowski <23406016+WojciechowskiPiotr@users.noreply.github.com> Date: Wed, 24 Feb 2021 09:39:33 +0100 Subject: [PATCH 8/8] Update changelogs/fragments/92-replicas-max-per-node.yml Co-authored-by: Felix Fontein --- changelogs/fragments/92-replicas-max-per-node.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/92-replicas-max-per-node.yml b/changelogs/fragments/92-replicas-max-per-node.yml index dd6df55a8..99d4473d5 100644 --- a/changelogs/fragments/92-replicas-max-per-node.yml +++ b/changelogs/fragments/92-replicas-max-per-node.yml @@ -1,2 +1,2 @@ minor_changes: - - "docker_swarm_service - Adding support for maximum number of tasks per node (replicas_max_per_node) when running swarm service in replicated mode. Introduced in API 1.40" + - "docker_swarm_service - adding support for maximum number of tasks per node (``replicas_max_per_node``) when running swarm service in replicated mode. Introduced in API 1.40 (https://github.com/ansible-collections/community.docker/issues/7, https://github.com/ansible-collections/community.docker/pull/92)."