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

Add 'ingress' option to docker_network module #999

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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,5 @@
minor_changes:
- Added 'ingress' option to docker_network module
- Containers reconnect to network only if they really exist.
- Enabled "force" option in Docker network container disconnect api call.
- Added waiting while container actually disconnect from Swarm network.
34 changes: 31 additions & 3 deletions plugins/modules/docker_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
- Enable IPv6 networking.
type: bool

ingress:
description:
- Enable Swarm routing-mesh.
type: bool

ipam_driver:
description:
- Specify an IPAM driver.
Expand Down Expand Up @@ -273,6 +278,7 @@

import re
import traceback
import time

from ansible.module_utils.common.text.converters import to_native

Expand Down Expand Up @@ -311,6 +317,7 @@ def __init__(self, client):
self.enable_ipv6 = None
self.scope = None
self.attachable = None
self.ingress = None

for key, value in client.module.params.items():
setattr(self, key, value)
Expand Down Expand Up @@ -507,6 +514,10 @@ def has_different_config(self, net):
differences.add('attachable',
parameter=self.parameters.attachable,
active=net.get('Attachable'))
if self.parameters.ingress is not None and self.parameters.ingress != net.get('Ingress', False):
differences.add('ingress',
parameter=self.parameters.ingress,
active=net.get('Ingress'))
if self.parameters.labels:
if not net.get('Labels'):
differences.add('labels',
Expand Down Expand Up @@ -543,6 +554,8 @@ def create_network(self):
data['Scope'] = self.parameters.scope
if self.parameters.attachable is not None:
data['Attachable'] = self.parameters.attachable
if self.parameters.ingress is not None:
data['Ingress'] = self.parameters.ingress
if self.parameters.labels is not None:
data["Labels"] = self.parameters.labels

Expand Down Expand Up @@ -579,6 +592,9 @@ def remove_network(self):
self.disconnect_all_containers()
if not self.check_mode:
self.client.delete_call('/networks/{0}', self.parameters.name)
if self.existing_network.get('Scope', 'local') == 'swarm':
while self.get_existing_network():
time.sleep(0.1)
self.results['actions'].append("Removed network %s" % (self.parameters.name,))
self.results['changed'] = True

Expand All @@ -587,9 +603,21 @@ def is_container_connected(self, container_name):
return False
return container_name in container_names_in_network(self.existing_network)

def is_container_exist(self, container_name):
try:
container = self.client.get_container(container_name)
return True if container else False

except DockerException as e:
self.client.fail('An unexpected Docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
self.client.fail(
'An unexpected requests error occurred when trying to talk to the Docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())

def connect_containers(self):
for name in self.parameters.connected:
if not self.is_container_connected(name):
if not self.is_container_connected(name) and self.is_container_exist(name):
if not self.check_mode:
data = {
"Container": name,
Expand Down Expand Up @@ -620,7 +648,7 @@ def disconnect_all_containers(self):

def disconnect_container(self, container_name):
if not self.check_mode:
data = {"Container": container_name}
data = {"Container": container_name, "Force": True}
self.client.post_json('/networks/{0}/disconnect', self.parameters.name, data=data)
self.results['actions'].append("Disconnected container %s" % (container_name,))
self.results['changed'] = True
Expand Down Expand Up @@ -684,6 +712,7 @@ def main():
debug=dict(type='bool', default=False),
scope=dict(type='str', choices=['local', 'global', 'swarm']),
attachable=dict(type='bool'),
ingress=dict(type='bool'),
)

option_minimal_versions = dict(
Expand All @@ -700,7 +729,6 @@ def main():
option_minimal_versions=option_minimal_versions,
)
sanitize_labels(client.module.params['labels'], 'labels', client)

try:
cm = DockerNetworkManager(client)
client.module.exit_json(**cm.results)
Expand Down
66 changes: 66 additions & 0 deletions tests/integration/targets/docker_network/tasks/tests/ingress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- name: Registering network name
set_fact:
nname_1: "{{ name_prefix ~ '-network-1' }}"
- name: Registering network name
set_fact:
dnetworks: "{{ dnetworks + [nname_1] }}"

####################################################################
## overlay #########################################################
####################################################################

- block:
# Ingress networks require swarm initialization before they'll work
- name: swarm
docker_swarm:
state: present
advertise_addr: "{{ ansible_default_ipv4.address | default('127.0.0.1') }}"

- name: cleanup default swarm ingress network
docker_network:
name: ingress
state: absent

- name: ingress
docker_network:
name: "{{ nname_1 }}"
driver: overlay
ingress: true
register: ingress_1

- name: ingress (idempotency)
docker_network:
name: "{{ nname_1 }}"
driver: overlay
ingress: true
register: ingress_2

- name: ingress (change)
docker_network:
name: "{{ nname_1 }}"
driver: overlay
ingress: false
register: ingress_3

- name: cleanup network
docker_network:
name: "{{ nname_1 }}"
state: absent
force: true

- assert:
that:
- ingress_1 is changed
- ingress_2 is not changed
- ingress_3 is changed

always:
- name: cleanup swarm
docker_swarm:
state: absent
force: true
Loading