kubernetes python api read_namespaced_pod_log returns http 400 with pod log as well in exception

huangapple go评论80阅读模式
英文:

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[&quot;items&quot;]:
        pod_name  = pod[&quot;metadata&quot;][&quot;name&quot;]
        pod_ns = pod[&quot;metadata&quot;][&quot;namespace&quot;]
        pod_details = json.loads((k8sapi.read_namespaced_pod(name=pod_name, namespace=pod_ns, _preload_content=False)).data)
        for container in pod_details[&quot;status&quot;][&quot;containerStatuses&quot;]:
                if &quot;waiting&quot; in container[&quot;state&quot;]:
                        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(&quot;error_log: &quot;+ str(e))
                                print(&quot;this is error&quot;)
                        #print(pod_log)
        print(&quot;-------&quot;)
        #print(pod_log)

Exception error returned in response:

Traceback (most recent call last):
  File &quot;/Users/gv/chatgpt_k8s/scripts/./chatgptk8s_monitor.py&quot;, line 26, in &lt;module&gt;
    pod_log = json.loads(k8sapi.read_namespaced_pod_log(name=pod_name, container=container, namespace=pod_ns, _preload_content=False).data)
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api/core_v1_api.py&quot;, line 23747, in read_namespaced_pod_log
    return self.read_namespaced_pod_log_with_http_info(name, namespace, **kwargs)  # noqa: E501
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api/core_v1_api.py&quot;, line 23866, in read_namespaced_pod_log_with_http_info
    return self.api_client.call_api(
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py&quot;, line 348, in call_api
    return self.__call_api(resource_path, method,
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py&quot;, line 180, in __call_api
    response_data = self.request(
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/api_client.py&quot;, line 373, in request
    return self.rest_client.GET(url,
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/rest.py&quot;, line 241, in GET
    return self.request(&quot;GET&quot;, url,
  File &quot;/Users/gv/Library/Python/3.9/lib/python/site-packages/kubernetes/client/rest.py&quot;, line 235, in request
    raise ApiException(http_resp=r)
kubernetes.client.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({&#39;Audit-Id&#39;: &#39;9ea1de46-a89a-4c34-9ad3-c944f3ce9960&#39;, &#39;Cache-Control&#39;: &#39;no-cache, private&#39;, &#39;Content-Type&#39;: &#39;application/json&#39;, &#39;Date&#39;: &#39;Sun, 04 Jun 2023 19:06:36 GMT&#39;, &#39;Content-Length&#39;: &#39;418&#39;})
HTTP response body: b&#39;{&quot;kind&quot;:&quot;Status&quot;,&quot;apiVersion&quot;:&quot;v1&quot;,&quot;metadata&quot;:{},&quot;status&quot;:&quot;Failure&quot;,&quot;message&quot;:&quot;container {\&#39;name\&#39;: \&#39;nginx\&#39;, \&#39;state\&#39;: {\&#39;waiting\&#39;: {\&#39;reason\&#39;: \&#39;ImagePullBackOff\&#39;, \&#39;message\&#39;: \&#39;Back-off pulling image \\&quot;nginx:laterror\\&quot;\&#39;}}, \&#39;lastState\&#39;: {}, \&#39;ready\&#39;: False, \&#39;restartCount\&#39;: 0, \&#39;image\&#39;: \&#39;nginx:laterror\&#39;, \&#39;imageID\&#39;: \&#39;\&#39;, \&#39;started\&#39;: False} is not valid for pod nginx-deployment-cb99699bc-7jr98&quot;,&quot;reason&quot;:&quot;BadRequest&quot;,&quot;code&quot;:400}\n&#39;

答案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[&quot;status&quot;][&quot;containerStatuses&quot;]:

You probably want:

pod_log = json.loads(
    k8sapi.read_namespaced_pod_log(
        name=pod_name, container=container[&#39;name&#39;], 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[&quot;name&quot;], namespace=pod_ns, _preload_content=False
).data

huangapple
  • 本文由 发表于 2023年6月5日 03:19:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定