Skip to content

Commit

Permalink
Add log_opt and annotaion options to podman_play module (#668)
Browse files Browse the repository at this point in the history
* Add tests for options of podman_play

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

* Add log_opt and annotaion options for podman_play

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

* Fix pep8 errors

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

* Update tests in play-with-options.yml

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

* Update podman_play.py

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

* Update test

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

---------

Signed-off-by: nishipy <goodisonev4@gmail.com>
  • Loading branch information
nishipy authored Nov 26, 2023
1 parent 22e0c93 commit b7e8711
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
51 changes: 51 additions & 0 deletions plugins/modules/podman_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
- Path to file with YAML configuration for a Pod.
type: path
required: True
annotation:
description:
- Add an annotation to the container or pod.
type: dict
aliases:
- annotations
authfile:
description:
- Path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json,
Expand Down Expand Up @@ -78,6 +84,28 @@
description:
- Set logging driver for all created containers.
type: str
log_opt:
description:
- Logging driver specific options. Set custom logging configuration.
type: dict
aliases:
- log_options
suboptions:
path:
description:
- specify a path to the log file (e.g. /var/log/container/mycontainer.json).
type: str
required: false
max_size:
description:
- Specify a max size of the log file (e.g 10mb).
type: str
required: false
tag:
description:
- specify a custom log tag for the container. This option is currently supported only by the journald log driver in Podman.
type: str
required: false
log_level:
description:
- Set logging level for podman calls. Log messages above specified level
Expand Down Expand Up @@ -138,6 +166,18 @@
kube_file: ~/kube.yaml
state: started
- name: Recreate pod from a kube file with options
containers.podman.podman_play:
kube_file: ~/kube.yaml
state: started
recreate: true
annotations:
greeting: hello
greet_to: world
userns: host
log_opt:
path: /tmp/my-container.log
max_size: 10mb
'''
import re # noqa: F402
try:
Expand All @@ -158,6 +198,9 @@ def __init__(self, module, executable):
self.command = [self.executable, 'play', 'kube']
creds = []
# pod_name = extract_pod_name(module.params['kube_file'])
if self.module.params['annotation']:
for k, v in self.module.params['annotation'].items():
self.command.extend(['--annotation', '"{k}={v}"'.format(k=k, v=v)])
if self.module.params['username']:
creds += [self.module.params['username']]
if self.module.params['password']:
Expand All @@ -170,6 +213,9 @@ def __init__(self, module, executable):
if self.module.params['configmap']:
configmaps = ",".join(self.module.params['configmap'])
self.command.extend(['--configmap=%s' % configmaps])
if self.module.params['log_opt']:
for k, v in self.module.params['log_opt'].items():
self.command.extend(['--log-opt', '{k}={v}'.format(k=k.replace('_', '-'), v=v)])
start = self.module.params['state'] == 'started'
self.command.extend(['--start=%s' % str(start).lower()])
for arg, param in {
Expand Down Expand Up @@ -276,6 +322,7 @@ def play(self):
def main():
module = AnsibleModule(
argument_spec=dict(
annotation=dict(type='dict', aliases=['annotations']),
executable=dict(type='str', default='podman'),
kube_file=dict(type='path', required=True),
authfile=dict(type='path'),
Expand All @@ -287,6 +334,10 @@ def main():
username=dict(type='str'),
password=dict(type='str', no_log=True),
log_driver=dict(type='str'),
log_opt=dict(type='dict', aliases=['log_options'], options=dict(
path=dict(type='str'),
max_size=dict(type='str'),
tag=dict(type='str'))),
network=dict(type='list', elements='str'),
state=dict(
type='str',
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/targets/podman_play/tasks/files/play-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: web-deploy
labels:
app: webapp
spec:
containers:
- name: alpinex
image: alpine
command: ['sleep', '1d']
- name: alpiney
image: alpine
command: ['sleep', '1d']
14 changes: 13 additions & 1 deletion tests/integration/targets/podman_play/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,16 @@
include_tasks: play-with-build.yml
vars:
ansible_python_interpreter: "/usr/bin/python"


- name: Test play kube options
include_tasks: play-with-options.yml
vars:
ansible_python_interpreter: "/usr/bin/python"
target_pod: web-deploy
target_container: web-deploy-alpinex
log_opt:
path: /tmp/mycontainer.json
size: 10mb
userns: host
kube_dir: /tmp
kube_file: play-pod.yaml
52 changes: 52 additions & 0 deletions tests/integration/targets/podman_play/tasks/play-with-options.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
- name: Test play pod with options
block:
- name: Copy kube file
copy:
src: "{{ kube_file }}"
dest: "{{ kube_dir }}/{{ kube_file }}"
remote_src: false

- name: Create Pod with many options
containers.podman.podman_play:
kube_file: "{{ kube_dir }}/{{ kube_file }}"
state: started
recreate: true
annotation:
greeting: hello
greet_to: world
userns: "{{ userns }}"
log_opt:
path: "{{ log_opt.path }}"
max_size: "{{ log_opt.size }}"
register: play_pod

- name: Get pod info
containers.podman.podman_pod_info:
name: "{{ target_pod }}"
register: play_pod_info

- name: Check userns is set in Pod
assert:
that:
- play_pod_info.pods.0.InfraConfig.userns == userns

- name: Get container info
containers.podman.podman_container_info:
name: "{{ target_container }}"
register: play_container_info

- name: Check annotations and log options are set in Pod
assert:
that:
- play_container_info.containers.0.Config.Annotations["greeting"] == "hello"
- play_container_info.containers.0.Config.Annotations["greet_to"] == "world"
- play_container_info.containers.0.HostConfig.LogConfig["Path"] == log_opt.path
- play_container_info.containers.0.HostConfig.LogConfig["Size"] | lower == log_opt.size

always:

- name: Cleanup pods
containers.podman.podman_play:
kube_file: "{{ kube_dir }}/{{ kube_file }}"
state: absent

0 comments on commit b7e8711

Please sign in to comment.