Make docker_swarm_service option publish.published_port optional (#101)

* Change docker_swarm_service option publish.published_port from required to optional. If not specified random high port is assigned by docker.

* Update changelogs/fragments/101-make-service-published-port-optional.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
sgpinkus 2021-03-13 23:37:56 +11:00 committed by GitHub
parent b42b76fc45
commit 5b74313023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 4 deletions

View File

@ -0,0 +1,3 @@
minor_changes:
- docker_swarm_service - change ``publish.published_port`` option from mandatory
to optional. Docker will assign random high port if not specified (https://github.com/ansible-collections/community.docker/issues/99).

View File

@ -365,7 +365,7 @@ options:
description: description:
- The port to make externally available. - The port to make externally available.
type: int type: int
required: yes required: no
target_port: target_port:
description: description:
- The port inside the container to expose. - The port inside the container to expose.
@ -2066,9 +2066,10 @@ class DockerService(DockerBaseClass):
for port in self.publish: for port in self.publish:
port_spec = { port_spec = {
'Protocol': port['protocol'], 'Protocol': port['protocol'],
'PublishedPort': port['published_port'],
'TargetPort': port['target_port'] 'TargetPort': port['target_port']
} }
if port.get('published_port'):
port_spec['PublishedPort'] = port['published_port']
if port.get('mode'): if port.get('mode'):
port_spec['PublishMode'] = port['mode'] port_spec['PublishMode'] = port['mode']
ports.append(port_spec) ports.append(port_spec)
@ -2214,7 +2215,7 @@ class DockerServiceManager(object):
ds.publish.append({ ds.publish.append({
'protocol': port['Protocol'], 'protocol': port['Protocol'],
'mode': port.get('PublishMode', None), 'mode': port.get('PublishMode', None),
'published_port': int(port['PublishedPort']), 'published_port': port.get('PublishedPort', None),
'target_port': int(port['TargetPort']) 'target_port': int(port['TargetPort'])
}) })
@ -2618,7 +2619,7 @@ def main():
options=dict(type='dict'), options=dict(type='dict'),
)), )),
publish=dict(type='list', elements='dict', options=dict( publish=dict(type='list', elements='dict', options=dict(
published_port=dict(type='int', required=True), published_port=dict(type='int', required=False),
target_port=dict(type='int', required=True), target_port=dict(type='int', required=True),
protocol=dict(type='str', default='tcp', choices=['tcp', 'udp']), protocol=dict(type='str', default='tcp', choices=['tcp', 'udp']),
mode=dict(type='str', choices=['ingress', 'host']), mode=dict(type='str', choices=['ingress', 'host']),