Skip to content

Commit

Permalink
Improve idempotency of podman_container (#40)
Browse files Browse the repository at this point in the history
Consider pod container differences, they change utc, network, ipc according to their pods.
Add different default for cpu_shares on podman 1.8.* versions
Add test for containers idempotency in pods
Partially solves #21 and #31
  • Loading branch information
sshnaidm committed May 11, 2020
1 parent b421c19 commit 376f8aa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
15 changes: 12 additions & 3 deletions plugins/modules/podman_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,10 @@ def __init__(self, module, podman_version):

def default_dict(self):
# make here any changes to self.defaults related to podman version
# https://github.com/containers/libpod/pull/5669
if (LooseVersion(self.version) >= LooseVersion('1.8.0')
and LooseVersion(self.version) < LooseVersion('1.9.0')):
self.defaults['cpu_shares'] = 1024
return self.defaults


Expand Down Expand Up @@ -1534,6 +1538,8 @@ def diffparam_image(self):
def diffparam_ipc(self):
before = self.info['hostconfig']['ipcmode']
after = self.params['ipc']
if self.params['pod'] and not after:
after = before
return self._diff_update_and_compare('ipc', before, after)

def diffparam_label(self):
Expand Down Expand Up @@ -1583,6 +1589,8 @@ def diffparam_memory_reservation(self):
def diffparam_network(self):
before = [self.info['hostconfig']['networkmode']]
after = self.params['network']
if self.params['pod'] and not self.module.params['network']:
after = before
return self._diff_update_and_compare('network', before, after)

def diffparam_no_hosts(self):
Expand Down Expand Up @@ -1639,6 +1647,8 @@ def diffparam_user(self):
def diffparam_uts(self):
before = self.info['hostconfig']['utsmode']
after = self.params['uts']
if self.params['pod'] and not after:
after = before
return self._diff_update_and_compare('uts', before, after)

def diffparam_volume(self):
Expand Down Expand Up @@ -1666,9 +1676,8 @@ def diffparam_volumes_from(self):

def diffparam_workdir(self):
before = self.info['config']['workingdir']
if self.params['workdir'] is not None:
after = self.params['workdir']
else:
after = self.params['workdir']
if after is None:
after = before
return self._diff_update_and_compare('workdir', before, after)

Expand Down
48 changes: 48 additions & 0 deletions tests/integration/targets/podman_container/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,51 @@
that:
- "'podman rm -f testidem' in remove.podman_actions"

# - name: Create a pod
# shell: podman pod create --name testidempod

- name: Check basic idempotency of pod container
containers.podman.podman_container:
name: testidem-pod
image: docker.io/alpine
state: present
command: sleep 20m
pod: "new:testidempod"

- name: Check basic idempotency of pod container - run it again
containers.podman.podman_container:
name: testidem-pod
image: alpine:latest
state: present
command: sleep 20m
pod: testidempod
register: idem

- name: Check that nothing was changed in pod containers
assert:
that:
- not idem.changed

- name: Run changed pod container (with tty enabled)
containers.podman.podman_container:
name: testidem-pod
image: alpine
state: present
command: sleep 20m
tty: true
pod: testidempod
register: idem1

- name: Check that container is recreated when changed
assert:
that:
- idem1 is changed

- name: Remove container
containers.podman.podman_container:
name: testidem-pod
state: absent

always:
- name: Delete all container leftovers from tests
containers.podman.podman_container:
Expand All @@ -385,3 +430,6 @@
- "alpine:3.7"
- "container"
- "container2"

- name: Remove pod
shell: podman pod rm -f testidempod

0 comments on commit 376f8aa

Please sign in to comment.