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

[2.8] [docker_network] Fix idempotency when using aux_addresses in ipam_config #56972

Merged
merged 2 commits into from
May 28, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/docker_network_aux_addresses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- docker_network module - fix idempotency when using ``aux_addresses`` in ``ipam_config``.
16 changes: 15 additions & 1 deletion lib/ansible/modules/cloud/docker/docker_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,20 @@ def get_ip_version(cidr):
raise ValueError('"{0}" is not a valid CIDR'.format(cidr))


def normalize_ipam_config_key(key):
"""Normalizes IPAM config keys returned by Docker API to match Ansible keys

:param key: Docker API key
:type key: str
:return Ansible module key
:rtype str
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, we use sphinx's format for documenting parameters.

special_cases = {
'AuxiliaryAddresses': 'aux_addresses'
}
return special_cases.get(key, key.lower())


class DockerNetworkManager(object):

def __init__(self, client):
Expand Down Expand Up @@ -447,7 +461,7 @@ def has_different_config(self, net):
continue
camelkey = None
for net_key in net_config:
if key == net_key.lower():
if key == normalize_ipam_config_key(net_key):
camelkey = net_key
break
if not camelkey or net_config.get(camelkey) != value:
Expand Down
87 changes: 77 additions & 10 deletions test/integration/targets/docker_network/tasks/tests/ipam.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#################### network-ipam-0 deprecated ####################

- name: Create network with ipam_config and deprecated ipam_options
- name: Create network with ipam_config and deprecated ipam_options (conflicting)
docker_network:
name: "{{ nname_ipam_0 }}"
ipam_options:
Expand All @@ -28,32 +28,71 @@
- network is failed
- "network.msg == 'parameters are mutually exclusive: ipam_config|ipam_options'"

- name: Create network with deprecated custom IPAM config
- name: Create network with deprecated custom IPAM options
docker_network:
name: "{{ nname_ipam_0 }}"
ipam_options:
subnet: 172.3.29.0/24
gateway: 172.3.29.2
iprange: 172.3.29.0/26
aux_addresses:
host1: 172.3.29.3
host2: 172.3.29.4
register: network

- assert:
that:
- network is changed

- name: Change subnet of network with deprecated custom IPAM config
- name: Create network with deprecated custom IPAM options (idempotence)
docker_network:
name: "{{ nname_ipam_0 }}"
ipam_options:
subnet: 172.3.30.0/24
subnet: 172.3.29.0/24
gateway: 172.3.29.2
iprange: 172.3.29.0/26
aux_addresses:
host1: 172.3.29.3
host2: 172.3.29.4
register: network

- assert:
that:
- network is not changed

- name: Change of network created with deprecated custom IPAM options
docker_network:
name: "{{ nname_ipam_0 }}"
ipam_options:
subnet: 172.3.28.0/24
gateway: 172.3.28.2
iprange: 172.3.28.0/26
aux_addresses:
host1: 172.3.28.3
register: network
diff: yes

- assert:
that:
- network is changed
- network.diff.differences | length == 1
- network.diff.differences[0] == "ipam_config[0].subnet"
- network.diff.differences | length == 4
- '"ipam_config[0].subnet" in network.diff.differences'
- '"ipam_config[0].gateway" in network.diff.differences'
- '"ipam_config[0].iprange" in network.diff.differences'
- '"ipam_config[0].aux_addresses" in network.diff.differences'

- name: Remove gateway and iprange of network with deprecated custom IPAM options
docker_network:
name: "{{ nname_ipam_0 }}"
ipam_options:
subnet: 172.3.28.0/24
register: network

- assert:
that:
- network is not changed

- name: Cleanup network with ipam_config and deprecated ipam_options
- name: Cleanup network with deprecated custom IPAM options
docker_network:
name: "{{ nname_ipam_0 }}"
state: absent
Expand All @@ -76,7 +115,23 @@
that:
- network is changed

- name: Change subnet, gateway, iprange and auxiliary addresses of network with custom IPAM config
- name: Create network with custom IPAM config (idempotence)
docker_network:
name: "{{ nname_ipam_1 }}"
ipam_config:
- subnet: 172.3.27.0/24
gateway: 172.3.27.2
iprange: 172.3.27.0/26
aux_addresses:
host1: 172.3.27.3
host2: 172.3.27.4
register: network

- assert:
that:
- network is not changed

- name: Change of network created with custom IPAM config
docker_network:
name: "{{ nname_ipam_1 }}"
ipam_config:
Expand Down Expand Up @@ -128,6 +183,18 @@
that:
- network is changed

- name: Create network with IPv6 IPAM config (idempotence)
docker_network:
name: "{{ nname_ipam_2 }}"
enable_ipv6: yes
ipam_config:
- subnet: fdd1:ac8c:0557:7ce0::/64
register: network

- assert:
that:
- network is not changed

- name: Change subnet of network with IPv6 IPAM config
docker_network:
name: "{{ nname_ipam_2 }}"
Expand Down Expand Up @@ -178,7 +245,7 @@
that:
- network is changed

- name: Change subnet order of network with IPv6 and custom IPv4 IPAM config
- name: Change subnet order of network with IPv6 and custom IPv4 IPAM config (idempotence)
docker_network:
name: "{{ nname_ipam_3 }}"
enable_ipv6: yes
Expand All @@ -191,7 +258,7 @@
that:
- network is not changed

- name: Remove IPv6 from network with custom IPv4 and IPv6 IPAM config
- name: Remove IPv6 from network with custom IPv4 and IPv6 IPAM config (change)
docker_network:
name: "{{ nname_ipam_3 }}"
enable_ipv6: no
Expand Down