Skip to content

Commit

Permalink
docker: Allow publishing of ports with the same number but different …
Browse files Browse the repository at this point in the history
…protocol (ansible#38412)

* Don't deduplicate docker container ports with different protocols

* Test _parse_exposed_ports
  • Loading branch information
sharph authored and ryansb committed Jun 15, 2018
1 parent f21307d commit 3935cce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/ansible/modules/cloud/docker/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,8 @@ def _parse_exposed_ports(self, published_ports):
protocol = 'tcp'
port = int(publish_port)
for exposed_port in exposed:
if exposed_port[1] != protocol:
continue
if isinstance(exposed_port[0], string_types) and '-' in exposed_port[0]:
start_port, end_port = exposed_port[0].split('-')
if int(start_port) <= port <= int(end_port):
Expand Down Expand Up @@ -1229,7 +1231,7 @@ def has_different_configuration(self, image):

# "ExposedPorts": null returns None type & causes AttributeError - PR #5517
if config.get('ExposedPorts') is not None:
expected_exposed = [re.sub(r'/.+$', '', p) for p in config.get('ExposedPorts', dict()).keys()]
expected_exposed = [self._normalize_port(p) for p in config.get('ExposedPorts', dict()).keys()]
else:
expected_exposed = []

Expand Down Expand Up @@ -1644,10 +1646,10 @@ def _get_expected_exposed(self, image):
self.log('_get_expected_exposed')
image_ports = []
if image:
image_ports = [re.sub(r'/.+$', '', p) for p in (image['ContainerConfig'].get('ExposedPorts') or {}).keys()]
image_ports = [self._normalize_port(p) for p in (image['ContainerConfig'].get('ExposedPorts') or {}).keys()]
param_ports = []
if self.parameters.ports:
param_ports = [str(p[0]) for p in self.parameters.ports]
param_ports = [str(p[0]) + '/' + p[1] for p in self.parameters.ports]
result = list(set(image_ports + param_ports))
self.log(result, pretty_print=True)
return result
Expand Down Expand Up @@ -1688,6 +1690,11 @@ def _convert_simple_dict_to_list(self, param_name, join_with=':'):
results.append("%s%s%s" % (key, join_with, value))
return results

def _normalize_port(self, port):
if '/' not in port:
return port + '/tcp'
return port


class ContainerManager(DockerBaseClass):
'''
Expand Down
Empty file.
19 changes: 19 additions & 0 deletions test/units/modules/cloud/docker/test_docker_container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from ansible.modules.cloud.docker.docker_container import TaskParameters


class TestTaskParameters(unittest.TestCase):
"""Unit tests for TaskParameters."""

def test_parse_exposed_ports_tcp_udp(self):
"""
Ensure _parse_exposed_ports does not cancel ports with the same
number but different protocol.
"""
task_params = TaskParameters.__new__(TaskParameters)
task_params.exposed_ports = None
result = task_params._parse_exposed_ports([80, '443', '443/udp'])
self.assertTrue((80, 'tcp') in result)
self.assertTrue((443, 'tcp') in result)
self.assertTrue((443, 'udp') in result)

0 comments on commit 3935cce

Please sign in to comment.