kubernetes-client / kubernetes-client/python
pod_exec() yields inconsistent results
- Dominant language
- Python
- Stars
- 7.7k
- Forks
- 3.5k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 18
Description
**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': , '_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': , '_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': , '_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': , '_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': , '_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': , '_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': , '_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': , '_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': , '_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': , '_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
Contributor guide
Research direction
Start with examples/pod_exec.py, especially the stream call using connect_get_namespaced_pod_exec and run_forever(), then reproduce the command repeatedly against the stated Kubernetes and client versions. Trace how the stream reports the channel 3 status and returncode; done means a failing command consistently reports the NonZeroExit status rather than alternating between success and failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kubernetes, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100