通过 Python 获取资源文档(如 kubectl explain)
Get docs for Resources (like kubectl explain) via Python
我想写一个类似于kubectl explain
的小程序。
我使用 python 客户端。
使用 kubectl explain pods -v=8
我可以看到调用了哪些 API。
URL是/openapi/v2
我试过这个:
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
from kubernetes.client import ApiClient
config.load_kube_config()
print(ApiClient().call_api('/openapi/v2', method='GET'))
但是结果是空的:
(None, 200, HTTPHeaderDict({'Accept-Ranges': 'bytes', 'Audit-Id': '5f025f01-cab9-4816-8579-751b47604275', 'Cache-Control': 'no-cache, private', 'Content-Length': '3315308', 'Content-Type': 'text/plain; charset=utf-8', 'Etag': '"194A5412D92C8239FAA388BD61A2729940609093EE00703602A983C97E2D7FD9FFA0E25F481A2659782EC80339F6A25CD9FD414B8D652409E1B521BB4F53E5DB"', 'Last-Modified': 'Thu, 31 Mar 2022 17:51:05 GMT', 'Vary': 'Accept-Encoding, Accept', 'X-Kubernetes-Pf-Flowschema-Uid': 'f70aa7db-e8d7-4690-becf-40ac57d88c1f', 'X-Kubernetes-Pf-Prioritylevel-Uid': '5c900157-e070-46c3-b774-a77dfa6128bc', 'Date': 'Sat, 02 Apr 2022 21:29:56 GMT'}))
如何获得 kubectl explain
通过 Python 显示的精美文档?
您已经在获取数据,只是在处理它时出现了一些错误:)
要关闭 post-processing,您需要将 _preload_content=False 参数传递给 call_api
然后代码看起来像这样:
import json
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
from kubernetes.client import ApiClient
config.load_kube_config()
apiClient = ApiClient()
answer = apiClient.call_api('/openapi/v2', method='GET', _preload_content=False)
data = json.loads(answer[0].data)
print(data)
如果你只想获取描述,你可以像这样使用带有 Bearer Auth 的 curl:
https://blog.ronnyvdb.net/2019/08/07/howto-curl-the-kubernetes-api-server
curl -s $APISERVER/openapi/v2 --header "Authorization: Bearer $TOKEN" --cacert ca.crt
或使用 TSL 验证:
curl -s $APISERVER/openapi/v2 --cert client.crt --key client.key --cacert ca.crt
之后,您可以使用工具来处理 openAPI 描述:https://openapi.tools
例如上传json到https://mrin9.github.io/OpenAPI-Viewer并享受
我想写一个类似于kubectl explain
的小程序。
我使用 python 客户端。
使用 kubectl explain pods -v=8
我可以看到调用了哪些 API。
URL是/openapi/v2
我试过这个:
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
from kubernetes.client import ApiClient
config.load_kube_config()
print(ApiClient().call_api('/openapi/v2', method='GET'))
但是结果是空的:
(None, 200, HTTPHeaderDict({'Accept-Ranges': 'bytes', 'Audit-Id': '5f025f01-cab9-4816-8579-751b47604275', 'Cache-Control': 'no-cache, private', 'Content-Length': '3315308', 'Content-Type': 'text/plain; charset=utf-8', 'Etag': '"194A5412D92C8239FAA388BD61A2729940609093EE00703602A983C97E2D7FD9FFA0E25F481A2659782EC80339F6A25CD9FD414B8D652409E1B521BB4F53E5DB"', 'Last-Modified': 'Thu, 31 Mar 2022 17:51:05 GMT', 'Vary': 'Accept-Encoding, Accept', 'X-Kubernetes-Pf-Flowschema-Uid': 'f70aa7db-e8d7-4690-becf-40ac57d88c1f', 'X-Kubernetes-Pf-Prioritylevel-Uid': '5c900157-e070-46c3-b774-a77dfa6128bc', 'Date': 'Sat, 02 Apr 2022 21:29:56 GMT'}))
如何获得 kubectl explain
通过 Python 显示的精美文档?
您已经在获取数据,只是在处理它时出现了一些错误:)
要关闭 post-processing,您需要将 _preload_content=False 参数传递给 call_api
然后代码看起来像这样:
import json
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
from kubernetes.client import ApiClient
config.load_kube_config()
apiClient = ApiClient()
answer = apiClient.call_api('/openapi/v2', method='GET', _preload_content=False)
data = json.loads(answer[0].data)
print(data)
如果你只想获取描述,你可以像这样使用带有 Bearer Auth 的 curl: https://blog.ronnyvdb.net/2019/08/07/howto-curl-the-kubernetes-api-server
curl -s $APISERVER/openapi/v2 --header "Authorization: Bearer $TOKEN" --cacert ca.crt
或使用 TSL 验证:
curl -s $APISERVER/openapi/v2 --cert client.crt --key client.key --cacert ca.crt
之后,您可以使用工具来处理 openAPI 描述:https://openapi.tools
例如上传json到https://mrin9.github.io/OpenAPI-Viewer并享受