Skip to content

Commit

Permalink
k8s_exec: Return rc for the command executed
Browse files Browse the repository at this point in the history
k8s_exec now returns command return code in output.

Fixes: ansible-collections#122

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde committed Jul 10, 2020
1 parent 2e86b56 commit bf8c29d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
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.rc != 0

always:
- name: "Cleanup namespace"
k8s:
Expand Down
52 changes: 48 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 (rc) 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.rc != 0
'''

RETURN = r'''
Expand All @@ -85,10 +102,23 @@
stderr_lines:
description: The command stderr
type: str
rc:
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 @@ -112,6 +142,9 @@ def argspec(self):


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

module = KubernetesExecCommand()
# Load kubernetes.client.Configuration
module.get_api_client()
Expand All @@ -131,15 +164,26 @@ 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'])
changed = True if rc == 0 else False
module.exit_json(
changed=True, stdout="".join(stdout), stderr="".join(stderr))
changed=changed,
stdout="".join(stdout),
stderr="".join(stderr),
rc=rc
)


if __name__ == '__main__':
Expand Down

0 comments on commit bf8c29d

Please sign in to comment.