kubernetes-client / python

Official Python client library for kubernetes
http://kubernetes.io/
Apache License 2.0
6.76k stars 3.27k forks source link

Executing command in pod fails when _request_timeout is passed in tuple format #2292

Open tusharrobin opened 2 weeks ago

tusharrobin commented 2 weeks ago

What happened (please include outputs or screenshots):

Executing command in pod fails if _request_timeout is passed as tuple. The exact error is Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: (0) Reason: '<' not supported between instances of 'float' and 'tuple'

What you expected to happen: Expected to run the command successfully.

How to reproduce it (as minimally and precisely as possible):

`from kubernetes import client, config from kubernetes.stream import stream

def run_command_in_pod(namespace, pod_name, container_name, command, timeout): config.load_kube_config() c = client.Configuration() c.debug = True api_instance = client.CoreV1Api()

exec_command = [
    '/bin/sh',
    '-c',
    command
]

try:
    response = stream(api_instance.connect_get_namespaced_pod_exec,
                      pod_name,
                      namespace,
                      container=container_name,
                      command=exec_command,
                      stderr=True, stdin=False,
                      stdout=True, tty=False,
                      _request_timeout=timeout)
    print("Command output: ", response)
except client.exceptions.ApiException as e:
    print("Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: %s\n" % e)

if name == 'main': namespace = 'test' pod_name = 'test' container_name = 'test' command = 'date' timeout = (5, 10)

run_command_in_pod(namespace, pod_name, container_name, command, timeout)`

Anything else we need to know?:

Environment:

roycaihw commented 2 weeks ago

/assign @yliaog

yliaog commented 2 weeks ago

timeout should be a float, not a tuple as the error suggests

Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: (0) Reason: '<' not supported between instances of 'float' and 'tuple'

tusharrobin commented 2 weeks ago

Hi @yliaog, Thanks for looking into this.

The documentation for the API mentions :param _request_timeout: timeout setting for this request. If one number provided, it will be total request timeout. It can also be a pair (tuple) of (connection, read) timeouts.

Reference: https://raw.githubusercontent.com/kubernetes-client/python/refs/heads/master/kubernetes/client/api/core_v1_api.py

If there is another way to set connect_timeout, let me know. Thanks again !

yliaog commented 2 weeks ago

I see. please checkout https://github.com/kubernetes-client/python/blob/master/examples/watch/timeout-settings.md

tusharrobin commented 2 weeks ago

In case if the pod is on a node which is network partitioned and if you execute command in a pod running on that node, it will take 30 seconds to timeout even if you set "_request_timeout" to be a lesser value.

If you use curl with "connect_timeout" to a smaller value, it does timeout after that duration so we need to set connect_timeout for the "connect_get_namespaced_pod_exec" api so that it can timeout during network partition.

The only option I see in kubernetes client is to set the _request_timeout to a tuple value which allows to set "connect_timeout" which is not working. Can you please check if there is an issue ?