Skip to content

Commit

Permalink
fix diffparam_volume and add diffparam_mount and diffparam_tmpfs
Browse files Browse the repository at this point in the history
This is a fix for bug containers#355.

This compares the arguments podman recieved for the currently existing
container with the (effective) arguments to come.

This approach was taken over parsing Mounts from inspect because:

1. This line from https://github.com/containers/podman/blob/e084f0ee1e1ded564110e84eefca9f18b5669adf/libpod/container_inspect.go#L224
   regarding inspect's Mount value:

   "Only includes user-specified mounts. Only includes bind mounts and named volumes, not tmpfs volumes."

   Thus an incomplete solution would result.

2. The code required to parse so that it could be compared with the
   stuff to come was getting silly-level complex.

3. Anonymous volumes were impossible to decipher as both Name and
   Source are filled in with the podman-generated values.

Thus we compare the arguments podman create was run with to make the
existing container with the closest values to those arguments in the
new config.

This resulted in simpler code that takes care of the issue of anonymous
volumes.

The downside is that if someone moves, say, a tmpfs from mount to tmpfs
(or vice versa) that would reult in exactly the same result this will
be considered a different config. This can (possibly) be fixed if and
when it becomes an actual issue.

Signed-off-by: Andrew <[email protected]>
  • Loading branch information
Andrew authored and sshnaidm committed Apr 1, 2023
1 parent 1a7dc95 commit 7315a41
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions plugins/module_utils/podman/podman_container_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,19 @@ def diffparam_mac_address(self):
after = before
return self._diff_update_and_compare('mac_address', before, after)

def diffparam_mount(self):
before = []
if self.info['config'].get('createcommand'):
cr_com = self.info['config']['createcommand']
for i, v in enumerate(cr_com):
if v == '--mount':
before.append(cr_com[i + 1])
after = self.params.get('mount')
if not after:
after = []
before, after = sorted(list(set(before))), sorted(list(set(after)))
return self._diff_update_and_compare('mount', before, after)

def diffparam_network(self):
net_mode_before = self.info['hostconfig']['networkmode']
net_mode_after = ''
Expand Down Expand Up @@ -1192,6 +1205,21 @@ def diffparam_timezone(self):
after = self.params['timezone']
return self._diff_update_and_compare('timezone', before, after)

def diffparam_tmpfs(self):
before = []
if self.info['config'].get('createcommand'):
cr_com = self.info['config']['createcommand']
for i, v in enumerate(cr_com):
if v == '--tmpfs':
before.append(cr_com[i + 1])
after = []
tmpfs = self.params.get('tmpfs')
if tmpfs:
for k, v in tmpfs.items():
after.append('{}:{}'.format(k, v))
before, after = sorted(list(set(before))), sorted(list(set(after)))
return self._diff_update_and_compare('tmpfs', before, after)

def diffparam_tty(self):
before = self.info['config']['tty']
after = self.params['tty']
Expand Down Expand Up @@ -1237,37 +1265,15 @@ def diffparam_uts(self):
return self._diff_update_and_compare('uts', before, after)

def diffparam_volume(self):
def clean_volume(x):
'''Remove trailing and double slashes from volumes.'''
if not x.rstrip("/"):
return "/"
return x.replace("//", "/").rstrip("/")

before = self.info['mounts']
before_local_vols = []
if before:
volumes = []
local_vols = []
for m in before:
if m['type'] != 'volume':
volumes.append(
[
clean_volume(m['source']),
clean_volume(m['destination'])
])
elif m['type'] == 'volume':
local_vols.append(
[m['name'], clean_volume(m['destination'])])
before = [":".join(v) for v in volumes]
before_local_vols = [":".join(v) for v in local_vols]
if self.params['volume'] is not None:
after = [":".join(
[clean_volume(i) for i in v.split(":")[:2]]
) for v in self.params['volume']]
else:
before = []
if self.info['config'].get('createcommand'):
cr_com = self.info['config']['createcommand']
for i, v in enumerate(cr_com):
if v == '--volume':
before.append(cr_com[i + 1])
after = self.params.get('volume')
if not after:
after = []
if before_local_vols:
after = list(set(after).difference(before_local_vols))
before, after = sorted(list(set(before))), sorted(list(set(after)))
return self._diff_update_and_compare('volume', before, after)

Expand Down

0 comments on commit 7315a41

Please sign in to comment.