Running `connect_get_namespaced_pod_exec` using kubernetes client corev1api gives bad request

1/5/2019

kubernetes client corev1api connect_get_namespaced_pod_exec fails to run for python.

I have checked the python version == 2.7 and pip freeze - ipaddress==1.0.22, urllib3==1.24.1 and websocket-client==0.54.0 are the versions which satisfy the requirement - as mentioned here: https://github.com/kubernetes-client/python/blob/master/README.md#hostname-doesnt-match followed the issue on this thread - https://github.com/kubernetes-client/python/issues/36 - not much help.

Tried usings stream as suggested here - https://github.com/kubernetes-client/python/blob/master/examples/exec.py

Ran:

api_response = stream(core_v1_api.connect_get_namespaced_pod_exec,
                      name, namespace,
                      command=exec_command,
                      stderr=True, stdin=False,
                      stdout=True, tty=False)

Got this error:

ApiException: (0) Reason: hostname '10.47.7.95' doesn't match either of '', 'cluster.local'

Without stream using directly the CoreV1Api -

Ran :

core_v1_api = client.CoreV1Api()
api_response = core_v1_api.connect_get_namespaced_pod_exec(name=name,namespace=namespace,command=exec_command,stderr=True, stdin=False,stdout=True, tty=False)

Got this error:

ApiException: (400) Reason: Bad Request HTTP response headers: HTTPHeaderDict({'Date': 'Sat, 05 Jan 2019 08:01:22 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}

-- Ultrablendz
api
kubernetes
python

1 Answer

1/5/2019

I wrote a simple program to check that:

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

# create an instance of the API class

config.load_kube_config()
api_instance = client.CoreV1Api()

exec_command = [
    '/bin/sh',
    '-c',
    'echo This is Prafull Ladha and it is test function']
resp = stream(api_instance.connect_get_namespaced_pod_exec, "nginx-deployment-76bf4969df-467z2", 'default',
              command=exec_command,
              stderr=True, stdin=False,
              stdout=True, tty=False)
print("Response: " + resp)

It is working perfectly fine for me.

I believe you're using minikube for development purpose. It is not able to recognise your hostname. You can make it work by disabling assert_hostname in your program like:

from kubernetes.client import configuration 
config.load_kube_config()                                                 
configuration.assert_hostname = False

This should resolve your issue.

-- Prafull Ladha
Source: StackOverflow