mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-13 13:02:01 +00:00
k8s_exec: Select first container from the pod (#363)
k8s_exec: Select first container from the pod SUMMARY 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 akasurde@redhat.com ISSUE TYPE Bugfix Pull Request COMPONENT NAME changelogs/fragments/358-k8s_exec.yml plugins/modules/k8s_exec.py Reviewed-by: Mike Graves <mgraves@redhat.com> Reviewed-by: None <None>
This commit is contained in:
3
changelogs/fragments/358-k8s_exec.yml
Normal file
3
changelogs/fragments/358-k8s_exec.yml
Normal file
@@ -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).
|
||||||
@@ -13,6 +13,21 @@
|
|||||||
- name: sleeper
|
- name: sleeper
|
||||||
image: busybox
|
image: busybox
|
||||||
command: ["sleep", "infinity"]
|
command: ["sleep", "infinity"]
|
||||||
|
multi_container_pod_name: pod-2
|
||||||
|
multi_container_pod_definition:
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Pod
|
||||||
|
metadata:
|
||||||
|
name: "{{ multi_container_pod_name }}"
|
||||||
|
namespace: "{{ exec_namespace }}"
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: sleeper-1
|
||||||
|
image: busybox
|
||||||
|
command: ["sleep", "infinity"]
|
||||||
|
- name: sleeper-2
|
||||||
|
image: busybox
|
||||||
|
command: ["sleep", "infinity"]
|
||||||
|
|
||||||
block:
|
block:
|
||||||
- name: "Ensure that {{ exec_namespace }} namespace exists"
|
- name: "Ensure that {{ exec_namespace }} namespace exists"
|
||||||
@@ -57,6 +72,25 @@
|
|||||||
- command_status.rc != 0
|
- command_status.rc != 0
|
||||||
- command_status.return_code != 0
|
- command_status.return_code != 0
|
||||||
|
|
||||||
|
- name: Create a multi container pod
|
||||||
|
k8s:
|
||||||
|
definition: "{{ multi_container_pod_definition }}"
|
||||||
|
wait: yes
|
||||||
|
wait_sleep: 1
|
||||||
|
wait_timeout: 30
|
||||||
|
|
||||||
|
- name: Execute command on the first container of the pod
|
||||||
|
k8s_exec:
|
||||||
|
pod: "{{ multi_container_pod_name }}"
|
||||||
|
namespace: "{{ exec_namespace }}"
|
||||||
|
command: echo hello
|
||||||
|
register: output
|
||||||
|
|
||||||
|
- name: Assert k8s_exec output is correct
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- "'hello' in output.stdout"
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: "Cleanup namespace"
|
- name: "Cleanup namespace"
|
||||||
k8s:
|
k8s:
|
||||||
|
|||||||
@@ -40,27 +40,28 @@ options:
|
|||||||
description:
|
description:
|
||||||
- The URL of an HTTP proxy to use for the connection.
|
- The URL of an HTTP proxy to use for the connection.
|
||||||
- Can also be specified via I(K8S_AUTH_PROXY) environment variable.
|
- 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
|
type: str
|
||||||
namespace:
|
namespace:
|
||||||
description:
|
description:
|
||||||
- The pod namespace name
|
- The pod namespace name.
|
||||||
type: str
|
type: str
|
||||||
required: yes
|
required: yes
|
||||||
pod:
|
pod:
|
||||||
description:
|
description:
|
||||||
- The pod name
|
- The pod name.
|
||||||
type: str
|
type: str
|
||||||
required: yes
|
required: yes
|
||||||
container:
|
container:
|
||||||
description:
|
description:
|
||||||
- The name of the container in the pod to connect to.
|
- The name of the container in the pod to connect to.
|
||||||
- Defaults to only container if there is only one container in the pod.
|
- 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
|
type: str
|
||||||
required: no
|
required: no
|
||||||
command:
|
command:
|
||||||
description:
|
description:
|
||||||
- The command to execute
|
- The command to execute.
|
||||||
type: str
|
type: str
|
||||||
required: yes
|
required: yes
|
||||||
"""
|
"""
|
||||||
@@ -84,6 +85,13 @@ EXAMPLES = r"""
|
|||||||
debug:
|
debug:
|
||||||
msg: "cmd failed"
|
msg: "cmd failed"
|
||||||
when: command_status.rc != 0
|
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"""
|
RETURN = r"""
|
||||||
@@ -134,6 +142,7 @@ from ansible_collections.kubernetes.core.plugins.module_utils.common import (
|
|||||||
try:
|
try:
|
||||||
from kubernetes.client.apis import core_v1_api
|
from kubernetes.client.apis import core_v1_api
|
||||||
from kubernetes.stream import stream
|
from kubernetes.stream import stream
|
||||||
|
from kubernetes.client.rest import ApiException
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# ImportError are managed by the common module already.
|
# ImportError are managed by the common module already.
|
||||||
pass
|
pass
|
||||||
@@ -157,6 +166,19 @@ def execute_module(module, k8s_ansible_mixin):
|
|||||||
optional_kwargs = {}
|
optional_kwargs = {}
|
||||||
if module.params.get("container"):
|
if module.params.get("container"):
|
||||||
optional_kwargs["container"] = module.params["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:
|
try:
|
||||||
resp = stream(
|
resp = stream(
|
||||||
api.connect_get_namespaced_pod_exec,
|
api.connect_get_namespaced_pod_exec,
|
||||||
|
|||||||
Reference in New Issue
Block a user