python icon indicating copy to clipboard operation
python copied to clipboard

WebSocketBadStatusException: Handshake status 400 Bad Request

Open EricCat opened this issue 7 years ago • 6 comments

**What happened **: Run pod_exec example Raise exception: websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

url https://35.197.67.77/api/v1/namespaces/default/pods/time-series-daily-fb7c655d9-dztlz/exec?command=%2Fbin%2Fsh&command=-c&command=echo+This+message+goes+to+stderr+%3E%262%3B+echo+This+message+goes+to+stdout&stderr=True&stdin=False&stdout=True&tty=False
headers {'Accept': '*/*', 'Content-Type': 'application/json', 'User-Agent': 'Swagger-Codegen/10.0.1/python', 'authorization': 'Bearer ya29.c.EluIB9pDg1h-JblzCfQnMY3jswAfyVEZnILrS0hI1GTfS9Cx-hUXFfp88dk_9nZbZy4NlCzNudL-vfumlh8thrBTUHqFb8zMArmBtKxRmmgy7kLV1xE3UASB9eO8'}
get_websocket_url wss://35.197.67.77/api/v1/namespaces/default/pods/time-series-daily-fb7c655d9-dztlz/exec?command=%2Fbin%2Fsh&command=-c&command=echo+This+message+goes+to+stderr+%3E%262%3B+echo+This+message+goes+to+stdout&stderr=True&stdin=False&stdout=True&tty=False
sslopt {'cert_reqs': <VerifyMode.CERT_REQUIRED: 2>, 'ca_certs': '/tmp/tmp6ezew065', 'check_hostname': False}

Traceback (most recent call last):
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 260, in websocket_call
    client = WSClient(configuration, get_websocket_url(url), headers)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 78, in __init__
    self.sock.connect(url, header=header)
  File "/usr/local/lib/python3.7/site-packages/websocket/_core.py", line 226, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python3.7/site-packages/websocket/_handshake.py", line 79, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python3.7/site-packages/websocket/_handshake.py", line 160, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 36, in <module>
    exec_pod_cmd(K8S_PODNAME, ['echo This message goes to stderr >&2; echo This message goes to stdout'])
  File "test.py", line 26, in exec_pod_cmd
    resp = stream(api.connect_get_namespaced_pod_exec, pod_managed_name, namespace, command=exec_command, stderr=True, stdin=False, stdout=True, tty=False,_request_timeout=30, _preload_content=False)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/stream.py", line 36, in stream
    return func(*args, **kwargs)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 835, in connect_get_namespaced_pod_exec
    (data) = self.connect_get_namespaced_pod_exec_with_http_info(name, namespace, **kwargs)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 935, in connect_get_namespaced_pod_exec_with_http_info
    collection_formats=collection_formats)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/api_client.py", line 334, in call_api
    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/api_client.py", line 168, in __call_api
    _request_timeout=_request_timeout)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/stream.py", line 31, in _intercept_request_call
    return ws_client.websocket_call(config, *args, **kwargs)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 268, in websocket_call
    raise ApiException(status=0, reason=str(e))
kubernetes.client.rest.ApiException: (0)
Reason: Handshake status 400 Bad Request

Environment:

  • Kubernetes version (v1.12.9-gke.7):
  • OS (Debian 9):
  • Python version (3.6, 3.7)
  • Python client version (10.0.1)

EricCat avatar Sep 19 '19 11:09 EricCat

I found it's due to Istio blocked the WebSocket that sent from kubernetes. so the issue will be closed

EricCat avatar Sep 23 '19 04:09 EricCat

@EricCat How did you get it to work with Istio ?

Anany-k avatar Mar 25 '20 13:03 Anany-k

@Anany-k No, there isn't a solution to get python CLI to work with Istio. I created a new namespace (k8s) just for running the python CLI pods. Otherwise, I have to use kubectl exec to call the pods in the Istio namespace

EricCat avatar Aug 05 '20 09:08 EricCat

There is a way to accomplish this if you the pod doesn't need the Istio sidecar. In that case you can add an annotation to the pod and the Python client exec will work: sidecar.istio.io/inject: "false"

tardis4500 avatar Mar 04 '21 20:03 tardis4500

I would suggest trying adding the container. I had a similar issue with kubectl attach that went away when I specified the container. In my case it looked like this:

resp = stream(
    api_instance.connect_get_namespaced_pod_attach,
    pod_name,
    namespace,
    container=container_name,
    stderr=True, stdin=True,
    stdout=True, tty=True,
    _preload_content=False
)

capitanFlint129 avatar May 10 '22 14:05 capitanFlint129

upgraded websocket-client from 0.7.0 to pip install websocket-client==0.15.0 It worked

zssvaidar avatar May 03 '23 03:05 zssvaidar