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.
Python kuberenetes client now has a complete example how to do exec
:
https://github.com/kubernetes-client/python/blob/master/examples/exec.py
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)
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
Kubernetes 1.4+ REST API likely will not work with a Python client on MacOS X if you are using system Python 2.7. This is because I believe it requires TLS 1.2 for secure connections and OpenSSL on MacOS X used by system Python 2.7, only supports up to TLS 1.1.
You will have to use either Python from HomeBrew, or PSF version of Python 3.6. If using PSF Python 3.6, ensure you do the extra post install step to install the certificate bundle.