I know that this question is a little old, but I thought I would share what I found when trying to use python/kubernetes attach/exec for several debugging cases (since this isn't documented anywhere I can find).
As far as I can tell, it's all about making the keyword arguments match the actual container configuration as opposed to what you want the container to do.
When creating pods using kubectl run
, if you don't use -i --tty
flags (indicating interactive/TTY allocation), and then attempt to set either the tty
or stdin
flags to True
in your function, then you'll get a mysterious 500 error with no other debug info. If you need to use stdin
and tty
and you are using a configuration file (as opposed to run), then make sure you set the stdin
and tty
flags to true
in spec.containers
.
While running resp.readline_stdout()
, if you get a OverflowError: timestamp too large to convert to C _PyTime_t
, set the keyword argument timeout=<any integer>
. By default, the timeout variable defaults to None, which is an invalid value in that function.
If you run the attach/exec command and get an APIException and a status code of 0, the error Reason: hostname 'X.X.X.X' doesn't match either of...
, note that there appears to be an incompatibility with Python 2. Works in Python 3. Should be patched eventually.
I can confirm 404 code is thrown via an ApiException when the pod doesn't exist.
If you are getting a mysterious error saying upgrade request required
, note that you need to use the kubernetes.stream.stream
function to wrap the call to attach/exec. You can see this issue on GitHub and this example code to help you get past that part.
Here's my example: resp = kubernetes.stream.stream(k8s.connect_get_namespaced_pod_attach, name='alpine-python-2', namespace="default", stderr=True, stdin=True, stdout=True, tty=True, _preload_content=False)
Note that the _preload_content=False
is essential in the attach
command or else the call will block indefinitely.
I know that was probably more information than you wanted, but hopefully at least some of it will help you.