Skip to content
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

docker_swarm_service: fix crash during idempotence check if published_port is not specified #136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "docker_swarm_services - avoid crash during idempotence check if ``published_port`` is not specified (https://github.com/ansible-collections/community.docker/issues/107, https://github.com/ansible-collections/community.docker/pull/136)."
6 changes: 4 additions & 2 deletions plugins/modules/docker_swarm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,6 @@

import shlex
import time
import operator
import traceback

from distutils.version import LooseVersion
Expand Down Expand Up @@ -1792,7 +1791,10 @@ def has_publish_changed(self, old_publish):
old_publish = old_publish or []
if len(self.publish) != len(old_publish):
return True
publish_sorter = operator.itemgetter('published_port', 'target_port', 'protocol')

def publish_sorter(item):
return (item.get('published_port') or 0, item.get('target_port') or 0, item.get('protocol') or '')

publish = sorted(self.publish, key=publish_sorter)
old_publish = sorted(old_publish, key=publish_sorter)
for publish_item, old_publish_item in zip(publish, old_publish):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,40 @@
name: "{{ service_name }}"
register: publish_8_info

- name: publish (without published_port)
docker_swarm_service:
name: "{{ service_name }}"
image: "{{ docker_test_image_alpine }}"
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
publish:
- protocol: udp
target_port: 60001
mode: host
register: publish_9
ignore_errors: yes
- name: gather service info
docker_swarm_service_info:
name: "{{ service_name }}"
register: publish_9_info

- name: publish (without published_port, idempotence)
docker_swarm_service:
name: "{{ service_name }}"
image: "{{ docker_test_image_alpine }}"
resolve_image: no
command: '/bin/sh -v -c "sleep 10m"'
publish:
- protocol: udp
target_port: 60001
mode: host
register: publish_10
ignore_errors: yes
- name: gather service info
docker_swarm_service_info:
name: "{{ service_name }}"
register: publish_10_info

- name: cleanup
docker_swarm_service:
name: "{{ service_name }}"
Expand All @@ -1543,6 +1577,8 @@
- publish_7 is not changed
- publish_8 is changed
- (publish_8_info.service.Endpoint.Ports | length) == 2
- publish_9 is changed
- publish_10 is not changed
when: docker_api_version is version('1.25', '>=') and docker_py_version is version('3.0.0', '>=')
- assert:
that:
Expand Down