Skip to content

Commit

Permalink
k8s_exec: Select first container from the pod
Browse files Browse the repository at this point in the history
kubectl command select first container from the pod in order
to execute commands on. We replicate the same behavior in k8s_exec
module.

Fixes: #358

Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde committed Feb 3, 2022
1 parent b54e9ef commit bd0c8c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/358-k8s_exec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- k8s_exec - select first container from the pod if none specified (https://github.com/ansible-collections/kubernetes.core/issues/358).
30 changes: 26 additions & 4 deletions plugins/modules/k8s_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,28 @@
description:
- 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).
- Please note that this module does not pick up typical proxy settings from the environment (for example, HTTP_PROXY).
type: str
namespace:
description:
- The pod namespace name
- The pod namespace name.
type: str
required: yes
pod:
description:
- The pod name
- The pod name.
type: str
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.
- If not specified, will choose the first container from the given pod as kubectl cmdline does.
type: str
required: no
command:
description:
- The command to execute
- The command to execute.
type: str
required: yes
"""
Expand All @@ -84,6 +85,13 @@
debug:
msg: "cmd failed"
when: command_status.rc != 0
- name: Specify a container name to execute the command on
kubernetes.core.k8s_exec:
namespace: myproject
pod: busybox-test
container: manager
command: echo "hello"
"""

RETURN = r"""
Expand Down Expand Up @@ -134,6 +142,7 @@
try:
from kubernetes.client.apis import core_v1_api
from kubernetes.stream import stream
from kubernetes.client.rest import ApiException
except ImportError:
# ImportError are managed by the common module already.
pass
Expand All @@ -157,6 +166,19 @@ def execute_module(module, k8s_ansible_mixin):
optional_kwargs = {}
if module.params.get("container"):
optional_kwargs["container"] = module.params["container"]
else:
# default to the first container available on pod
resp = None
try:
resp = api.read_namespaced_pod(
name=module.params["pod"], namespace=module.params["namespace"]
)
except ApiException:
pass

if resp and len(resp.spec.containers) >= 1:
optional_kwargs["container"] = resp.spec.containers[0].name

try:
resp = stream(
api.connect_get_namespaced_pod_exec,
Expand Down

0 comments on commit bd0c8c2

Please sign in to comment.