Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

k8s_exec: Return rc for the command executed #158

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/122_k8s_exec_rc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- k8s_exec - return RC for the command executed (https://github.com/ansible-collections/community.kubernetes/issues/122).
13 changes: 13 additions & 0 deletions molecule/default/tasks/exec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@
that:
- "'nameserver' in output.stdout"

- name: Check if rc is returned for the given command
k8s_exec:
namespace: "{{ exec_namespace }}"
pod: "{{ pod }}"
command: 'false'
register: command_status
ignore_errors: True

- name: Check last command status
assert:
that:
- command_status.return_code != 0

always:
- name: "Cleanup namespace"
k8s:
Expand Down
54 changes: 50 additions & 4 deletions plugins/modules/k8s_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
- "openshift == 0.4.3"
- "PyYAML >= 3.11"

notes:
- Return code C(return_code) for the command executed is added in output in version 0.11.1.
options:
proxy:
description:
- The URL of an HTTP proxy to use for the connection. Can also be specified via K8S_AUTH_PROXY environment variable.
- The URL of an HTTP proxy to use for the connection.
- Can also be specified via I(K8S_AUTH_PROXY) environment variable.
- Please note that this module does not pick up typical proxy settings from the environment (e.g. HTTP_PROXY).
type: str
namespace:
Expand All @@ -48,7 +51,8 @@
required: yes
container:
description:
- The name of the container in the pod to connect to. Defaults to only container if there is only one container in the pod.
- The name of the container in the pod to connect to.
- Defaults to only container if there is only one container in the pod.
type: str
required: no
command:
Expand All @@ -64,6 +68,19 @@
namespace: myproject
pod: zuul-scheduler
command: zuul-scheduler full-reconfigure

- name: Check RC status of command executed
community.kubernetes.k8s_exec:
namespace: myproject
pod: busybox-test
command: cmd_with_non_zero_exit_code
register: command_status
ignore_errors: True

- name: Check last command status
debug:
msg: "cmd failed"
when: command_status.return_code != 0
'''

RETURN = r'''
Expand All @@ -85,10 +102,23 @@
stderr_lines:
description: The command stderr
type: str
return_code:
description: The command status code
type: int
'''

import copy
import shlex
import traceback

try:
import yaml
IMP_YAML = True
except ImportError:
IMP_YAML_ERR = traceback.format_exc()
IMP_YAML = False

from ansible.module_utils.basic import missing_required_lib
from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule
from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC

Expand All @@ -113,6 +143,10 @@ def argspec(self):

def main():
module = KubernetesExecCommand()

if not IMP_YAML:
module.fail_json(msg=missing_required_lib("yaml"), exception=IMP_YAML_ERR)

# Load kubernetes.client.Configuration
module.get_api_client()
api = core_v1_api.CoreV1Api()
Expand All @@ -131,15 +165,27 @@ def main():
stdin=False,
tty=False,
_preload_content=False, **optional_kwargs)
stdout, stderr = [], []
stdout, stderr, rc = [], [], 0
while resp.is_open():
resp.update(timeout=1)
if resp.peek_stdout():
stdout.append(resp.read_stdout())
if resp.peek_stderr():
stderr.append(resp.read_stderr())
err = resp.read_channel(3)
err = yaml.safe_load(err)
if err['status'] == 'Success':
rc = 0
else:
rc = int(err['details']['causes'][0]['message'])

module.exit_json(
changed=True, stdout="".join(stdout), stderr="".join(stderr))
# Some command might change environment, but ultimately failing at end
changed=True,
stdout="".join(stdout),
stderr="".join(stderr),
return_code=rc
)


if __name__ == '__main__':
Expand Down