英文:
kubernetes python api read_namespaced_pod_log returns http 400 with pod log as well in exception
问题
我正在尝试使用Kubernetes Python库获取Pod日志。我正在Minikube上进行测试,我已将配置中的assert_hostname设置为False。
所使用的API是:read_namespaced_pod_log
以下是示例代码:
#!/usr/bin/python3
import requests
import json
from kubernetes import client, config
from kubernetes.client import configuration
from kubernetes.stream import stream
config.load_kube_config()
configuration.assert_hostname = False
k8sapi = client.CoreV1Api()
resp_obj = k8sapi.list_pod_for_all_namespaces(
watch=False,
_preload_content=False
)
pods_all = json.loads(resp_obj.data)
for pod in pods_all["items"]:
pod_name = pod["metadata"]["name"]
pod_ns = pod["metadata"]["namespace"]
pod_details = json.loads((k8sapi.read_namespaced_pod(name=pod_name, namespace=pod_ns, _preload_content=False)).data)
for container in pod_details["status"]["containerStatuses"]:
if "waiting" in container["state"]:
try:
pod_log = json.loads(k8sapi.read_namespaced_pod_log(name=pod_name, container=container, namespace=pod_ns, _preload_content=False).data)
#print(pod_log)
#print(pod_log)
except Exception as e:
#print("error_log: "+ str(e))
print("this is error")
#print(pod_log)
print("-------")
#print(pod_log)
响应中返回的异常错误信息如下:
Traceback (most recent call last):
File "/Users/gv/chatgpt_k8s/scripts/./chatgptk8s_monitor.py", line 26, in <module>
pod_log = json.loads(k8sapi.read_namespaced_pod_log(name=pod_name, container=container, namespace=pod_ns, _preload_content=False).data)
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api/core_v1_api.py", line 23747, in read_namespaced_pod_log
return self.read_namespaced_pod_log_with_http_info(name, namespace, **kwargs) # noqa: E501
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api/core_v1_api.py", line 23866, in read_namespaced_pod_log_with_http_info
return this.api_client.call_api(
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return this.__call_api(resource_path, method,
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py", line 180, in __call_api
response_data = this.request(
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py", line 373, in request
return this.rest_client.GET(url,
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/rest.py", line 241, in GET
return this.request("GET", url,
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/rest.py", line 235, in request
raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': '9ea1de46-a89a-4c34-9ad3-c944f3ce9960', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Sun, 04 Jun 2023 19:06:36 GMT', 'Content-Length': '418'})
HTTP response body: b'{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"container {\'name\': \'nginx\', \'state\': {\'waiting\': {\'reason\': \'ImagePullBackOff\', \'message\': \'Back-off pulling image \\\"nginx:laterror\\\"\'}}, \'lastState\': {}, \'ready\': False, \'restartCount\': 0, \'image\': \'nginx:laterror\', \'imageID\': \'\', \'started\': False} is not valid for pod nginx-deployment-cb99699bc-7jr98","reason":"BadRequest","code":400}\n'
英文:
I'm trying to fetch pod logs using Kubernetes python library. I'm testing on minikube, I have added configuration.assert_hostname = False
API used is: read_namespaced_pod_log
Below is an example code:
#!/usr/bin/python3
import requests
import json
from kubernetes import client, config
from kubernetes.client import configuration
from kubernetes.stream import stream
config.load_kube_config()
configuration.assert_hostname = False
k8sapi = client.CoreV1Api()
resp_obj = k8sapi.list_pod_for_all_namespaces(
watch=False,
_preload_content=False
)
pods_all = json.loads(resp_obj.data)
for pod in pods_all["items"]:
pod_name = pod["metadata"]["name"]
pod_ns = pod["metadata"]["namespace"]
pod_details = json.loads((k8sapi.read_namespaced_pod(name=pod_name, namespace=pod_ns, _preload_content=False)).data)
for container in pod_details["status"]["containerStatuses"]:
if "waiting" in container["state"]:
try:
pod_log = json.loads(k8sapi.read_namespaced_pod_log(name=pod_name, container=container, namespace=pod_ns, _preload_content=False).data)
#print(pod_log)
#print(pod_log)
except Exception as e:
#print("error_log: "+ str(e))
print("this is error")
#print(pod_log)
print("-------")
#print(pod_log)
Exception error returned in response:
Traceback (most recent call last):
File "/Users/gv/chatgpt_k8s/scripts/./chatgptk8s_monitor.py", line 26, in <module>
pod_log = json.loads(k8sapi.read_namespaced_pod_log(name=pod_name, container=container, namespace=pod_ns, _preload_content=False).data)
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api/core_v1_api.py", line 23747, in read_namespaced_pod_log
return self.read_namespaced_pod_log_with_http_info(name, namespace, **kwargs) # noqa: E501
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api/core_v1_api.py", line 23866, in read_namespaced_pod_log_with_http_info
return self.api_client.call_api(
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py", line 348, in call_api
return self.__call_api(resource_path, method,
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py", line 180, in __call_api
response_data = self.request(
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py", line 373, in request
return self.rest_client.GET(url,
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/rest.py", line 241, in GET
return self.request("GET", url,
File "/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/rest.py", line 235, in request
raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': '9ea1de46-a89a-4c34-9ad3-c944f3ce9960', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Sun, 04 Jun 2023 19:06:36 GMT', 'Content-Length': '418'})
HTTP response body: b'{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"container {\'name\': \'nginx\', \'state\': {\'waiting\': {\'reason\': \'ImagePullBackOff\', \'message\': \'Back-off pulling image \\"nginx:laterror\\"\'}}, \'lastState\': {}, \'ready\': False, \'restartCount\': 0, \'image\': \'nginx:laterror\', \'imageID\': \'\', \'started\': False} is not valid for pod nginx-deployment-cb99699bc-7jr98","reason":"BadRequest","code":400}\n'
答案1
得分: 0
在这个调用中:
pod_log = json.loads(
k8sapi.read_namespaced_pod_log(
name=pod_name, container=container, namespace=pod_ns, _preload_content=False
).data
)
container
参数的值应该是要获取日志的容器在 pod 中的名称,但在你的代码中,container
是由这个循环创建的一个结构化变量:
for container in pod_details["status"]["containerStatuses"]:
你可能想要:
pod_log = json.loads(
k8sapi.read_namespaced_pod_log(
name=pod_name, container=container['name'], namespace=pod_ns, _preload_content=False
).data
)
我的代码中有一个拼写错误,是因为你使用了 _preload_content=False
,而我通常不这样做。
你的 except Exception as e
语句将错误信息隐藏起来了;如果你将异常信息打印出来,这将会很明显。
我已经纠正了代码。你会发现你有一个新的错误,因为 read_namespaced_pod_log
方法返回的 .data
属性并不包含 JSON 数据;你可能会想要:
pod_log = k8sapi.read_namespaced_pod_log(
name=pod_name, container=container["name"], namespace=pod_ns, _preload_content=False
).data
英文:
In this call:
pod_log = json.loads(
k8sapi.read_namespaced_pod_log(
name=pod_name, container=container, namespace=pod_ns, _preload_content=False
).data
)
The value of the container
parameter should be the name of the container in the pod for which you want logs, but in your code container
is a structured variable created by this loop:
for container in pod_details["status"]["containerStatuses"]:
You probably want:
pod_log = json.loads(
k8sapi.read_namespaced_pod_log(
name=pod_name, container=container['name'], namespace=pod_ns, _preload_content=False
).data
)
There was a typo in my code, introduced because you're working with _preload_content=False
and I generally do not.
Your except Exception as e
is hiding error messages from you; this would have been obvious if you were to print out the exception message.
I've corrected the code. You'll find you have a new error, because the .data
attribute returned by the read_namespaced_pod_log
method does not contain JSON data; you'll probably want:
pod_log = k8sapi.read_namespaced_pod_log(
name=pod_name, container=container["name"], namespace=pod_ns, _preload_content=False
).data
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论