0

I am trying to use kubernetes python client to execute a script inside, say podA from another pod, say podB, using the connect_get_namespaced_pod_exec method. Which is throwing this error:

Traceback (most recent call last):
File "/route.py", line 256, in run_script
resp = stream(v1.connect_get_namespaced_pod_exec(pod_name, namespace, command=\['/bin/sh', '-c', './script_to_run.sh'\]))
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/api/core_v1_api.py", line 994, in connect_get_namespaced_pod_exec
return self.connect_get_namespaced_pod_exec_with_http_info(name, namespace, \*\*kwargs)  # noqa: E501
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/api/core_v1_api.py", line 1101, in connect_get_namespaced_pod_exec_with_http_info
return self.api_client.call_api(
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return self.\__call_api(resource_path, method,
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 180, in \__call_api
response_data = self.request(
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/api_client.py", line 373, in request
return self.rest_client.GET(url,
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/rest.py", line 239, in GET
return self.request("GET", url,
File "/usr/local/lib/python3.8/site-packages/kubernetes/client/rest.py", line 233, in request
raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Wed, 25 Jan 2023 13:14:30 GMT', 'Content-Length': '139'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}

Python code:

from kubernetes import client as client1, config

config.load_incluster_config()
v1=client1.CoreV1Api()
ret=v1.list_pod_for_all_namespaces(watch=False)
pod_name='nginx'
namespace='ethan'
resp = stream(v1.connect_get_namespaced_pod_exec(pod_name, namespace, command=\['/bin/sh', '-c', './script_to_run.sh'\]))
print(resp)

ClusterRole yaml:

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: clusterrole1
rules:
- apiGroups: [""]
  resources: ["namespaces", "pods", "services", "secrets","pods/exec"]
  verbs: ["create", "delete", "get", "list", "patch", "update", "watch"]
- apiGroups: ["extensions"]
  resources: ["deployments"]
  verbs: ["create", "delete", "get", "list", "patch", "update", "watch"]
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["create", "delete", "get", "update", "watch"]

The kubernetes Server version is : 1.16, and I was using the latest version of kubernetes python api. I tried downgrading that to 12.0.0, the issue still persisted. Why am I getting this error? What should I try to fix it?

0 Answers0