From db9cb0682ded5a6ef57cd02c2d5f059efd64d0be Mon Sep 17 00:00:00 2001 From: Mitsuru Nakakawaji Date: Fri, 16 Aug 2019 22:23:45 +0900 Subject: [PATCH] fix: docker_swarm_service does not publish both tcp and udp ports (#60616) * fix: docker_swarm_service does not publish both tcp and udp ports for same published port * fix the linting problems and add the changelog fragment. * add test * modify test to ensure result rather than return value (cherry picked from commit 064cd63f3d1c70019ca10a5eba75212ee9705c16) --- .../60616-support-multiple-port.yaml | 2 ++ .../cloud/docker/docker_swarm_service.py | 19 +++++++-------- .../tasks/tests/options.yml | 24 +++++++++++++++++++ 3 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/60616-support-multiple-port.yaml diff --git a/changelogs/fragments/60616-support-multiple-port.yaml b/changelogs/fragments/60616-support-multiple-port.yaml new file mode 100644 index 00000000000..7df343a36ba --- /dev/null +++ b/changelogs/fragments/60616-support-multiple-port.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_swarm_service - allow the same port to be published both with TCP and UDP." \ No newline at end of file diff --git a/lib/ansible/modules/cloud/docker/docker_swarm_service.py b/lib/ansible/modules/cloud/docker/docker_swarm_service.py index 722548acda9..920eddc3846 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm_service.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm_service.py @@ -2032,19 +2032,16 @@ class DockerService(DockerBaseClass): def build_endpoint_spec(self): endpoint_spec_args = {} if self.publish is not None: - ports = {} + ports = [] for port in self.publish: + port_spec = { + 'Protocol': port['protocol'], + 'PublishedPort': port['published_port'], + 'TargetPort': port['target_port'] + } if port.get('mode'): - ports[port['published_port']] = ( - port['target_port'], - port['protocol'], - port['mode'], - ) - else: - ports[port['published_port']] = ( - port['target_port'], - port['protocol'], - ) + port_spec['PublishMode'] = port['mode'] + ports.append(port_spec) endpoint_spec_args['ports'] = ports if self.endpoint_mode is not None: endpoint_spec_args['mode'] = self.endpoint_mode diff --git a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml index 5fc0a1e6f1a..4f08a462cec 100644 --- a/test/integration/targets/docker_swarm_service/tasks/tests/options.yml +++ b/test/integration/targets/docker_swarm_service/tasks/tests/options.yml @@ -1590,6 +1590,28 @@ register: publish_7 ignore_errors: yes +- name: publish (publishes the same port with both protocols) + docker_swarm_service: + name: "{{ service_name }}" + image: alpine:3.8 + resolve_image: no + command: '/bin/sh -v -c "sleep 10m"' + publish: + - protocol: udp + published_port: 60001 + target_port: 60001 + mode: host + - protocol: tcp + published_port: 60001 + target_port: 60001 + mode: host + register: publish_8 + ignore_errors: yes +- name: gather service info + docker_swarm_service_info: + name: "{{ service_name }}" + register: publish_8_info + - name: cleanup docker_swarm_service: name: "{{ service_name }}" @@ -1605,6 +1627,8 @@ - publish_5 is not changed - publish_6 is changed - publish_7 is not changed + - publish_8 is changed + - (publish_8_info.service.Endpoint.Ports | length) == 2 when: docker_api_version is version('1.25', '>=') and docker_py_version is version('3.0.0', '>=') - assert: that: