Skip to content

Commit

Permalink
Update the logic for deleting pods/containers in podman_play (#727)
Browse files Browse the repository at this point in the history
* Add test case for deleting pod with podman_play

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Update test case for deleting pod with podman_play

Signed-off-by: nishipy <goodisonev4@gmail.com>

* Add tear_down_pods() for Podman 3.4.0 or later

Signed-off-by: nishipy <goodisonev4@gmail.com>

---------

Signed-off-by: nishipy <goodisonev4@gmail.com>
  • Loading branch information
nishipy authored Mar 28, 2024
1 parent 44b2ce6 commit 6e04b20
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
34 changes: 30 additions & 4 deletions plugins/modules/podman_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
HAS_YAML = False

from ansible.module_utils.basic import AnsibleModule # noqa: F402
from ansible_collections.containers.podman.plugins.module_utils.podman.common import LooseVersion, get_podman_version


class PodmanKubeManagement:
Expand All @@ -196,6 +197,7 @@ def __init__(self, module, executable):
self.actions = []
self.executable = executable
self.command = [self.executable, 'play', 'kube']
self.version = get_podman_version(module)
creds = []
# pod_name = extract_pod_name(module.params['kube_file'])
if self.module.params['annotation']:
Expand Down Expand Up @@ -244,6 +246,22 @@ def _command_run(self, cmd):
self.module.log('PODMAN-PLAY-KUBE rc: %s' % rc)
return rc, out, err

def tear_down_pods(self):
'''
Tear down the pod and contaiers by using --down option in kube play
which is supported since Podman 3.4.0
'''
changed = False
kube_file = self.module.params['kube_file']

rc, out, err = self._command_run([self.executable, "kube", "play", "--down", kube_file])
if rc != 0:
self.module.fail_json(msg="Failed to delete Pod with %s" % (kube_file))
else:
changed = True

return changed, out, err

def discover_pods(self):
pod_name = ''
if self.module.params['kube_file']:
Expand Down Expand Up @@ -292,8 +310,12 @@ def remove_associated_pods(self, pods):
return changed, out_all, err_all

def pod_recreate(self):
pods = self.discover_pods()
self.remove_associated_pods(pods)
if self.version is not None and LooseVersion(self.version) >= LooseVersion('3.4.0'):
self.tear_down_pods()
else:
pods = self.discover_pods()
self.remove_associated_pods(pods)

# Create a pod
rc, out, err = self._command_run(self.command)
if rc != 0:
Expand Down Expand Up @@ -357,8 +379,12 @@ def main():
module.params['executable'], required=True)
manage = PodmanKubeManagement(module, executable)
if module.params['state'] == 'absent':
pods = manage.discover_pods()
changed, out, err = manage.remove_associated_pods(pods)
if manage.version is not None and LooseVersion(manage.version) > LooseVersion('3.4.0'):
manage.module.log(msg="version: %s, kube file %s" % (manage.version, manage.module.params['kube_file']))
changed, out, err = manage.tear_down_pods()
else:
pods = manage.discover_pods()
changed, out, err = manage.remove_associated_pods(pods)
else:
changed, out, err = manage.play()
results = {
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/targets/podman_play/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@
that:
- info1['pods'][0]['State'] == 'Running'

- name: Remove pods created by kube play
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
kube_file: /tmp/play3.yaml
state: absent
register: remove_pod

- name: Check if the pod was removed as expected
assert:
that:
- remove_pod is changed

- name: Get deleted pod info
containers.podman.podman_pod_info:
executable: "{{ test_executable | default('podman') }}"
name: web-deploy-pod
register: nonexist

- name: Check if the result is as expected
assert:
that:
- nonexist.pods == []

always:

- name: Delete all pods leftovers from tests
Expand Down

0 comments on commit 6e04b20

Please sign in to comment.