Skip to content

Commit

Permalink
Add support for kube yaml files with multi-documents (#724)
Browse files Browse the repository at this point in the history
* Add support for multi doc kube yaml

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

* Update to skip yamllint

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

* Update ignores for sanity tests

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

* Update discover_pods()

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

* Update test for podman_play

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

---------

Signed-off-by: nishipy <goodisonev4@gmail.com>
  • Loading branch information
nishipy authored Mar 23, 2024
1 parent 890ae35 commit 44b2ce6
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 8 deletions.
12 changes: 5 additions & 7 deletions plugins/modules/podman_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,10 @@ def discover_pods(self):
if self.module.params['kube_file']:
if HAS_YAML:
with open(self.module.params['kube_file']) as f:
pod = yaml.safe_load(f)
if 'metadata' in pod:
pod_name = pod['metadata'].get('name')
else:
self.module.fail_json(
"No metadata in Kube file!\n%s" % pod)
pods = list(yaml.safe_load_all(f))
for pod in pods:
if 'metadata' in pod and pod['kind'] in ['Deployment', 'Pod']:
pod_name = pod['metadata'].get('name')
else:
with open(self.module.params['kube_file']) as text:
# the following formats are matched for a kube name:
Expand All @@ -266,7 +264,7 @@ def discover_pods(self):
if re_pod:
pod_name = re_pod.group(1)
if not pod_name:
self.module.fail_json("Deployment doesn't have a name!")
self.module.fail_json("This Kube file doesn't have Pod or Deployment!")
# Find all pods
all_pods = ''
# In case of one pod or replicasets
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/targets/podman_play/tasks/files/multi-yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
kind: ConfigMap
metadata:
name: foo
data:
FOO: bar
---
apiVersion: v1
kind: Pod
metadata:
name: foobar
spec:
containers:
- command:
- top
name: container-1
image: alpine
envFrom:
- configMapRef:
name: foo
optional: false
---
apiVersion: v1
kind: ConfigMap
metadata:
name: foo2
data:
FOO2: bar2
10 changes: 9 additions & 1 deletion tests/integration/targets/podman_play/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,12 @@
size: 10mb
userns: host
kube_dir: /tmp
kube_file: play-pod.yaml
kube_file: play-pod.yaml

- name: Test play kube with multi doc yaml
include_tasks: play-multi-yaml.yml
vars:
ansible_python_interpreter: "/usr/bin/python"
kube_dir: /tmp
kube_file: multi-yaml.yml
target_container: foobar-container-1
40 changes: 40 additions & 0 deletions tests/integration/targets/podman_play/tasks/play-multi-yaml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
- name: Test kube play wih multi doc yaml
block:
- name: Copy kube file
copy:
src: "{{ kube_file }}"
dest: "{{ kube_dir }}/{{ kube_file }}"
remote_src: false

- name: Create Pod with multi doc yaml
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
kube_file: "{{ kube_dir }}/{{ kube_file }}"
state: started
recreate: true
register: play_pod

- name: Get pod info
containers.podman.podman_container_info:
executable: "{{ test_executable | default('podman') }}"
name: "{{ target_container }}"
register: info

- name: Check if an expected container is running
assert:
that:
- info.containers.0.State.Running == true
- '"FOO=bar" in info.containers.0.Config.Env'

- name: Cleanup pod
containers.podman.podman_play:
executable: "{{ test_executable | default('podman') }}"
kube_file: "{{ kube_dir }}/{{ kube_file }}"
state: absent
register: remove_pod

- name: Check if the pod was removed as expected
assert:
that:
- remove_pod is changed
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.10.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.11.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.12.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.13.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.14.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.15.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.16.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.17.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.18.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip
1 change: 1 addition & 0 deletions tests/sanity/ignore-2.9.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tests/integration/targets/connection_buildah/runme.sh shellcheck:SC2086
tests/integration/targets/connection_podman/runme.sh shellcheck:SC2086
tests/integration/targets/podman_play/tasks/files/multi-yaml.yml yamllint!skip

0 comments on commit 44b2ce6

Please sign in to comment.