mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-07-29 19:04:39 +00:00
Merge pull request #69 from fabianvf/properly-serialize-log
k8s_log no longer attempts to parse log as JSON
This commit is contained in:
@@ -79,6 +79,43 @@
|
|||||||
- "'hello world' in pod_log.log"
|
- "'hello world' in pod_log.log"
|
||||||
- item == 'hello world' or item == ''
|
- item == 'hello world' or item == ''
|
||||||
with_items: '{{ pod_log.log_lines }}'
|
with_items: '{{ pod_log.log_lines }}'
|
||||||
|
|
||||||
|
- name: Create a job that calculates 7
|
||||||
|
k8s:
|
||||||
|
state: present
|
||||||
|
wait: yes
|
||||||
|
wait_timeout: 120
|
||||||
|
wait_condition:
|
||||||
|
type: Complete
|
||||||
|
status: 'True'
|
||||||
|
definition:
|
||||||
|
apiVersion: batch/v1
|
||||||
|
kind: Job
|
||||||
|
metadata:
|
||||||
|
name: int-log
|
||||||
|
namespace: k8s-log
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: busybox
|
||||||
|
image: busybox
|
||||||
|
command: ["echo", "7"]
|
||||||
|
restartPolicy: Never
|
||||||
|
backoffLimit: 4
|
||||||
|
|
||||||
|
- name: retrieve logs from the job
|
||||||
|
k8s_log:
|
||||||
|
api_version: batch/v1
|
||||||
|
kind: Job
|
||||||
|
namespace: k8s-log
|
||||||
|
name: int-log
|
||||||
|
register: job_logs
|
||||||
|
|
||||||
|
- name: verify the log was successfully retrieved
|
||||||
|
assert:
|
||||||
|
that: job_logs.log_lines[0] == "7"
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: ensure that namespace is removed
|
- name: ensure that namespace is removed
|
||||||
k8s:
|
k8s:
|
||||||
|
|||||||
@@ -127,6 +127,9 @@ log_lines:
|
|||||||
|
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
from ansible.module_utils.six import PY2
|
||||||
|
|
||||||
from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule
|
from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule
|
||||||
from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC
|
from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC
|
||||||
|
|
||||||
@@ -182,11 +185,12 @@ class KubernetesLogModule(KubernetesAnsibleModule):
|
|||||||
if self.params.get('container'):
|
if self.params.get('container'):
|
||||||
kwargs['query_params'] = dict(container=self.params['container'])
|
kwargs['query_params'] = dict(container=self.params['container'])
|
||||||
|
|
||||||
log = resource.log.get(
|
log = serialize_log(resource.log.get(
|
||||||
name=name,
|
name=name,
|
||||||
namespace=self.params.get('namespace'),
|
namespace=self.params.get('namespace'),
|
||||||
|
serialize=False,
|
||||||
**kwargs
|
**kwargs
|
||||||
)
|
))
|
||||||
|
|
||||||
self.exit_json(changed=False, log=log, log_lines=log.split('\n'))
|
self.exit_json(changed=False, log=log, log_lines=log.split('\n'))
|
||||||
|
|
||||||
@@ -228,6 +232,12 @@ class KubernetesLogModule(KubernetesAnsibleModule):
|
|||||||
return selectors
|
return selectors
|
||||||
|
|
||||||
|
|
||||||
|
def serialize_log(response):
|
||||||
|
if PY2:
|
||||||
|
return response.data
|
||||||
|
return response.data.decode('utf8')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
KubernetesLogModule().execute_module()
|
KubernetesLogModule().execute_module()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user