Skip to content

Commit

Permalink
Add option to specify kube file content in module
Browse files Browse the repository at this point in the history
Fix #463
Signed-off-by: Sagi Shnaidman <[email protected]>
  • Loading branch information
sshnaidm committed Oct 11, 2024
1 parent d4977ea commit c512d79
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 9 deletions.
43 changes: 34 additions & 9 deletions plugins/modules/podman_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
description:
- Path to file with YAML configuration for a Pod.
type: path
required: True
kube_file_content:
description:
- Content of the kube file.
type: str
annotation:
description:
- Add an annotation to the container or pod.
Expand Down Expand Up @@ -268,10 +271,16 @@ def __init__(self, module, executable):
}.items():
if self.module.params[param] is not None:
self.command += ["%s=%s" % (arg, self.module.params[param])]
self.command += [self.module.params['kube_file']]
if self.module.params['kube_file']:
self.command += [self.module.params['kube_file']]
elif self.module.params['kube_file_content']:
self.command += ['-']

def _command_run(self, cmd):
rc, out, err = self.module.run_command(cmd)
if self.module.params['kube_file_content']:
rc, out, err = self.module.run_command(cmd, data=self.module.params['kube_file_content'])
else:
rc, out, err = self.module.run_command(cmd)
self.actions.append(" ".join(cmd))
if self.module.params['debug']:
self.module.log('PODMAN-PLAY-KUBE command: %s' % " ".join(cmd))
Expand All @@ -287,13 +296,23 @@ def tear_down_pods(self):
'''
changed = False
kube_file = self.module.params['kube_file']

rc, out, err = self._command_run([self.executable, "kube", "play", "--down", kube_file])
kube_file_content = self.module.params['kube_file_content']
if kube_file:
rc, out, err = self._command_run([self.executable, "kube", "play", "--down", kube_file])
elif kube_file_content:
rc, out, err = self._command_run([self.executable, "kube", "play", "--down", "-"])
if rc != 0 and "no such pod" in err:
changed = False
return changed, out, err
if rc != 0:
self.module.fail_json(msg="Failed to delete Pod with %s" % (kube_file))
else:
changed = True
self.module.fail_json(msg="Failed to delete Pod with %s: %s %s" % (
kube_file if kube_file else "YAML content", out, err))

# hack to check if no resources are deleted
for line in out.splitlines():
if line and not line.endswith(':'):
changed = True
break
return changed, out, err

def discover_pods(self):
Expand Down Expand Up @@ -384,7 +403,8 @@ def main():
argument_spec=dict(
annotation=dict(type='dict', aliases=['annotations']),
executable=dict(type='str', default='podman'),
kube_file=dict(type='path', required=True),
kube_file=dict(type='path'),
kube_file_content=dict(type='str'),
authfile=dict(type='path'),
build=dict(type='bool'),
cert_dir=dict(type='path'),
Expand Down Expand Up @@ -419,11 +439,16 @@ def main():
required_if=[
('state', 'quadlet', ['quadlet_filename']),
],
required_one_of=[
('kube_file', 'kube_file_content'),
],
)

executable = module.get_bin_path(
module.params['executable'], required=True)
manage = PodmanKubeManagement(module, executable)
changed = False
out = err = ''
if module.params['state'] == 'absent':
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']))
Expand Down
180 changes: 180 additions & 0 deletions tests/integration/targets/podman_play/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,18 @@
state: absent
register: remove_pod

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

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

- name: Get deleted pod info
containers.podman.podman_pod_info:
Expand All @@ -130,6 +138,177 @@
that:
- nonexist.pods == []

- name: Run with file content
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
debug: true
state: started
kube_file_content: |
kind: ConfigMap
metadata:
name: new
data:
FOO1: bar1
---
apiVersion: v1
kind: Pod
metadata:
name: contentpod
spec:
containers:
- command:
- top
name: container-42
image: alpine
envFrom:
- configMapRef:
name: new
optional: false
---
apiVersion: v1
kind: ConfigMap
metadata:
name: newest
data:
FOO3: bar3
register: playc1

- name: Get info about pods
containers.podman.podman_pod_info:
executable: "{{ test_executable | default('podman') }}"
register: infopods

- name: Check if the pod was created
assert:
that:
- "'contentpod' in (infopods.pods | map(attribute='Name') | list)"

- name: Run with file content - again
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
debug: true
state: started
kube_file_content: |
kind: ConfigMap
metadata:
name: new
data:
FOO1: bar1
---
apiVersion: v1
kind: Pod
metadata:
name: contentpod
spec:
containers:
- command:
- top
name: container-42
image: alpine
envFrom:
- configMapRef:
name: new
optional: false
---
apiVersion: v1
kind: ConfigMap
metadata:
name: newest
data:
FOO3: bar3
register: playc2

- name: Check info
assert:
that:
- playc1 is changed
- playc2 is not changed

- name: Remove with file content
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
debug: true
state: absent
kube_file_content: |
kind: ConfigMap
metadata:
name: new
data:
FOO1: bar1
---
apiVersion: v1
kind: Pod
metadata:
name: contentpod
spec:
containers:
- command:
- top
name: container-42
image: alpine
envFrom:
- configMapRef:
name: new
optional: false
---
apiVersion: v1
kind: ConfigMap
metadata:
name: newest
data:
FOO3: bar3
register: playc3

- name: Remove with file content - again
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
debug: true
state: absent
kube_file_content: |
kind: ConfigMap
metadata:
name: new
data:
FOO1: bar1
---
apiVersion: v1
kind: Pod
metadata:
name: contentpod
spec:
containers:
- command:
- top
name: container-42
image: alpine
envFrom:
- configMapRef:
name: new
optional: false
---
apiVersion: v1
kind: ConfigMap
metadata:
name: newest
data:
FOO3: bar3
register: playc4

- name: Check info
assert:
that:
- playc3 is changed
- playc4 is not changed

- name: Get info about pods
containers.podman.podman_pod_info:
executable: "{{ test_executable | default('podman') }}"
register: infopods2

- name: Check if the pod was created
assert:
that:
- "'contentpod' not in (infopods2.pods | map(attribute='Name') | list)"

- name: Create a Quadlet for kube with filename
containers.podman.podman_play:
Expand Down Expand Up @@ -294,6 +473,7 @@
- web-deploy-pod-0
- web-deploy-pod-1
- web-deploy-pod-2
- contentpod

- name: Test idempotency for root pods
include_tasks: root-play.yml
Expand Down

0 comments on commit c512d79

Please sign in to comment.