Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: kubernetes exec failing with upgrade required message

Scenario:

I'm trying to run a basic ls command using kubernetes package via cli.connect_post_namespaced_pod_exec() however I get a stacktrace that I do not know how to debug. Yes I have tried searching around but I'm not really sure what the problem is as I'm using documentation example from here

OS:

macOS Sierra 10.12.2

Code:

#!/usr/local/bin/python2.7

import logging

from pprint import pprint
from kubernetes import client, config

FORMAT = "[%(filename)s:%(lineno)s - %(funcName)s() ] %(message)s"
level = logging.DEBUG
logging.basicConfig(format=FORMAT, level=level)

def main():
    path_to_config = "/Users/acabrer/.kube/config"
    config.load_kube_config(config_file=path_to_config)
    ns = "default"
    pod = "nginx"
    cmd = "ls"
    cli = cli = client.CoreV1Api()
    response = cli.connect_post_namespaced_pod_exec(pod, ns, stderr=True, stdin=True, stdout=True, command=cmd)
    pprint(response)

if __name__ == '__main__':
    main()

Stack Trace:

Traceback (most recent call last):
  File "/Users/acabrer/kube.py", line 16, in <module>
    main()
  File "/Users/acabrer/kube.py", line 12, in main
    response = cli.connect_post_namespaced_pod_exec(pod, ns, stderr=True, stdin=True, stdout=True)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 3605, in connect_post_namespaced_pod_exec
    (data) = self.connect_post_namespaced_pod_exec_with_http_info(name, namespace, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 3715, in connect_post_namespaced_pod_exec_with_http_info
    collection_formats=collection_formats)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 328, in call_api
    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 152, in __call_api
    _request_timeout=_request_timeout)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 373, in request
    body=body)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 257, in POST
    body=body)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 213, in request
    raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Date': 'Sat, 21 Jan 2017 00:55:28 GMT', 'Content-Length': '139', 'Content-Type': 'application/json'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}

Any input would be much appreaciated.

Edit 1:

abrahams-mbp:.kube acabrer$ curl --help |grep TLSv
 -1, --tlsv1         Use >= TLSv1 (SSL)
     --tlsv1.0       Use TLSv1.0 (SSL)
     --tlsv1.1       Use TLSv1.1 (SSL)
     --tlsv1.2       Use TLSv1.2 (SSL)
abrahams-mbp:.kube acabrer$ python2.7 -c "import ssl; print ssl.OPENSSL_VERSION_INFO"
(1, 0, 2, 10, 15)

Edit 2:

abrahams-mbp:.kube acabrer$ curl --tlsv1.2 https://x.x.x.x -k
Unauthorized
abrahams-mbp:.kube acabrer$ curl --tlsv1.1 https://x.x.x.x -k
curl: (35) Unknown SSL protocol error in connection to x.x.x.x:-9836

Edit 3: I put some print statements to see the full on request information in api_client.py and this is what I see.

Note: I removed the ip-address to my endpoint for security.

bash-3.2# vim /usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py
bash-3.2# /Users/acabrer/kube.py
################
POST
https://x.x.x.x/api/v1/namespaces/default/pods/nginx/exec
[('stdin', True), ('command', 'ls'), ('stderr', True), ('stdout', True)]
{'Content-Type': 'application/json', 'Accept': '*/*', 'User-Agent': 'Swagger-Codegen/1.0.0-alpha/python'}
[]
None
################

Thanks,

-Abe.

like image 608
Abraham Avatar asked Nov 15 '25 21:11

Abraham


2 Answers

I have encountered with the same problem.The solution to this is using kubernetes.stream .You need to import the package and change just one line code of code as follows:

from kubernetes.stream import stream
#response = cli.connect_post_namespaced_pod_exec(pod, ns, stderr=True, stdin=True,stdout=True, command=cmd)
response = stream(cli.connect_post_namespaced_pod_exec,pod, ns, stderr=True,stdin=True, stdout=True, command=cmd)
like image 72
Roshan Patil Avatar answered Nov 17 '25 12:11

Roshan Patil


To answer my own question here, this is a bug in actual python binding referenced here: https://github.com/kubernetes-incubator/client-python/issues/58

like image 44
Abraham Avatar answered Nov 17 '25 11:11

Abraham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!