Skip to content

Commit

Permalink
Add route, dns and ipam_driver to podman_network (#761)
Browse files Browse the repository at this point in the history
Signed-off-by: Sagi Shnaidman <[email protected]>
  • Loading branch information
sshnaidm authored May 27, 2024
1 parent 47fc4cc commit 2887be7
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
60 changes: 60 additions & 0 deletions plugins/modules/podman_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
description:
- disable dns plugin (default "false")
type: bool
dns:
description:
- Set network-scoped DNS resolver/nameserver for containers in this network.
If not set, the host servers from /etc/resolv.conf is used.
type: list
elements: str
driver:
description:
- Driver to manage the network (default "bridge")
Expand Down Expand Up @@ -61,11 +67,26 @@
description:
- Allocate container IP from range
type: str
ipam_driver:
description:
- Set the ipam driver (IP Address Management Driver) for the network.
When unset podman chooses an ipam driver automatically based on the network driver
type: str
choices:
- host-local
- dhcp
- none
ipv6:
description:
- Enable IPv6 (Dual Stack) networking. You must pass a IPv6 subnet.
The subnet option must be used with the ipv6 option.
type: bool
route:
description:
- A static route in the format <destination in CIDR notation>,<gateway>,<route metric (optional)>.
This route will be added to every container in this network.
type: list
elements: str
subnet:
description:
- Subnet in CIDR format
Expand Down Expand Up @@ -297,6 +318,11 @@ def check_version(self, param, minv=None, maxv=None):
def addparam_gateway(self, c):
return c + ['--gateway', self.params['gateway']]

def addparam_dns(self, c):
for dns in self.params['dns']:
c += ['--dns', dns]
return c

def addparam_driver(self, c):
return c + ['--driver', self.params['driver']]

Expand Down Expand Up @@ -326,6 +352,14 @@ def addparam_opt(self, c):
for k in opt])]
return c

def addparam_route(self, c):
for route in self.params['route']:
c += ['--route', route]
return c

def addparam_ipam_driver(self, c):
return c + ['--ipam-driver=%s' % self.params['ipam_driver']]

def addparam_disable_dns(self, c):
return c + ['--disable-dns=%s' % self.params['disable_dns']]

Expand Down Expand Up @@ -385,6 +419,11 @@ def diffparam_disable_dns(self):
before = after = self.params['disable_dns']
return self._diff_update_and_compare('disable_dns', before, after)

def diffparam_dns(self):
before = self.info.get('network_dns_servers', [])
after = self.params['dns'] or []
return self._diff_update_and_compare('dns', sorted(before), sorted(after))

def diffparam_driver(self):
# Currently only bridge is supported
before = after = 'bridge'
Expand Down Expand Up @@ -429,6 +468,23 @@ def diffparam_ip_range(self):
before = after = ''
return self._diff_update_and_compare('ip_range', before, after)

def diffparam_ipam_driver(self):
before = self.info.get("ipam_options", {}).get("driver", "")
after = self.params['ipam_driver']
if not after:
after = before
return self._diff_update_and_compare('ipam_driver', before, after)

def diffparam_route(self):
routes = self.info.get('routes', [])
if routes:
before = [",".join([
r['destination'], r['gateway'], str(r.get('metric', ''))]).rstrip(",") for r in routes]
else:
before = []
after = self.params['route'] or []
return self._diff_update_and_compare('route', sorted(before), sorted(after))

def diffparam_subnet(self):
# Disable idempotency of subnet for v4, subnets are added automatically
# TODO(sshnaidm): check if it's still the issue in v5
Expand Down Expand Up @@ -694,12 +750,15 @@ def main():
choices=['present', 'absent', 'quadlet']),
name=dict(type='str', required=True),
disable_dns=dict(type='bool', required=False),
dns=dict(type='list', elements='str', required=False),
driver=dict(type='str', required=False),
force=dict(type='bool', default=False),
gateway=dict(type='str', required=False),
interface_name=dict(type='str', required=False),
internal=dict(type='bool', required=False),
ip_range=dict(type='str', required=False),
ipam_driver=dict(type='str', required=False,
choices=['host-local', 'dhcp', 'none']),
ipv6=dict(type='bool', required=False),
subnet=dict(type='str', required=False),
macvlan=dict(type='str', required=False),
Expand All @@ -715,6 +774,7 @@ def main():
executable=dict(type='str', required=False, default='podman'),
debug=dict(type='bool', default=False),
recreate=dict(type='bool', default=False),
route=dict(type='list', elements='str', required=False),
quadlet_dir=dict(type='path', required=False),
quadlet_filename=dict(type='str', required=False),
quadlet_options=dict(type='list', elements='str', required=False),
Expand Down
148 changes: 148 additions & 0 deletions tests/integration/targets/podman_network/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,154 @@
that:
- info17 is not changed

- name: Create network with DNS
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
dns:
- 1.1.1.1
- 2.2.2.2
register: resultn2

- name: Check output - Create network with DNS
assert:
that:
- resultn2 is changed

- name: Create network with DNS again
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
dns:
- 1.1.1.1
- 2.2.2.2
register: resultn3

- name: Check output - Create network with DNS again
assert:
that:
- resultn3 is not changed

- name: Create network with DNS - different
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
dns:
- 1.1.1.1
register: resultn4

- name: Check output - Create network with DNS - different
assert:
that:
- resultn4 is changed

- name: Create network with routes
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
route:
- 1.1.1.1/32,2.2.2.2
- 3.3.3.0/24,4.4.4.4,240
- 7.7.0.0/16,9.9.9.9,100
register: resultn5

- name: Check output - Create network with routes
assert:
that:
- resultn5 is changed

- name: Create network with routes again
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
route:
- 1.1.1.1/32,2.2.2.2
- 3.3.3.0/24,4.4.4.4,240
- 7.7.0.0/16,9.9.9.9,100
register: resultn6

- name: Check output - Create network with routes again
assert:
that:
- resultn6 is not changed

- name: Create network with routes - different
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
route:
- 1.1.1.1/32,2.2.2.2
- 3.3.3.0/24,4.4.4.4,280
register: resultn7

- name: Check output - Create network with routes - different
assert:
that:
- resultn7 is changed

- name: Create network with IPAM driver none
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
ipam_driver: none
register: resultn8

- name: Check output - Create network with IPAM driver none
assert:
that:
- resultn8 is changed

- name: Create network with IPAM driver none again
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
ipam_driver: none
register: resultn9

- name: Check output - Create network with IPAM driver none again
assert:
that:
- resultn9 is not changed

- name: Create network without IPAM driver
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
register: resultn10

- name: Check output - Create network without IPAM driver
assert:
that:
- resultn10 is not changed

- name: Create network with IPAM driver host-local
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: present
ipam_driver: "host-local"
register: resultn11

- name: Check output - Create network with IPAM driver host-local
assert:
that:
- resultn11 is changed

- name: Delete network
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
name: "{{ network_name }}"
state: absent

- name: Create a Quadlet for network with filename
containers.podman.podman_network:
executable: "{{ test_executable | default('podman') }}"
Expand Down

0 comments on commit 2887be7

Please sign in to comment.