Skip to content

pod_exec() yields inconsistent results #2328

Description

@plup

What happened (please include outputs or screenshots):

I'm trying to implement this code sample: https://github.com/kubernetes-client/python/blob/master/examples/pod_exec.py#L69-L82

But running multiple times the same command returns different results.

What you expected to happen:

I'm expecting all command execution to return the same result

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

Create a test.py

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

config.load_kube_config()
r = stream(client.CoreV1Api().connect_get_namespaced_pod_exec, name='mypod', namespace='default',
           container='mgmt', command=['false'], stderr=True, stdin=False, stdout=True, tty=False,
           _preload_content=False)

r.run_forever()
print(vars(r))
print(r.returncode)

Run it multiple times:

$ for i in {1..10}; do python test.py; sleep 2; done
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x103325840>, 'sock': <websocket._core.WebSocket object at 0x103364d70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x105c89840>, 'sock': <websocket._core.WebSocket object at 0x105cc8d70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x104ead840>, 'sock': <websocket._core.WebSocket object at 0x104eecd70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x103115840>, 'sock': <websocket._core.WebSocket object at 0x103154d70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x1079cd840>, 'sock': <websocket._core.WebSocket object at 0x107a0cd70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"command terminated with non-zero exit code: command terminated with exit code 1","reason":"NonZeroExitCode","details":{"causes":[{"reason":"ExitCode","message":"1"}]}}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x105019840>, 'sock': <websocket._core.WebSocket object at 0x105058d70>, '_returncode': None}
1
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x10742d840>, 'sock': <websocket._core.WebSocket object at 0x107468d70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x105dcd840>, 'sock': <websocket._core.WebSocket object at 0x105e08d70>, '_returncode': None}
0
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"command terminated with non-zero exit code: command terminated with exit code 1","reason":"NonZeroExitCode","details":{"causes":[{"reason":"ExitCode","message":"1"}]}}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x105ae5840>, 'sock': <websocket._core.WebSocket object at 0x105b24d70>, '_returncode': None}
1
{'_connected': False, '_channels': {3: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Success"}\n'}, 'binary': False, 'newline': '\n', '_all': <_io.StringIO object at 0x1076a5840>, 'sock': <websocket._core.WebSocket object at 0x1076e4d70>, '_returncode': None}
0

All those runs should return a NonZeroExit status.

Anything else we need to know?:

Environment:

  • Kubernetes version (kubectl version): Client Version: v1.28.13
    Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
    Server Version: v1.29.11
  • OS (e.g., MacOS 10.13.6): MacOS Sequoia 15.2
  • Python version (python --version): Python 3.13.1
  • Python client version (pip list | grep kubernetes): kubernetes 31.0.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.kind/bugCategorizes issue or PR as related to a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions