Home > database >  Get docs for Resources (like kubectl explain) via Python
Get docs for Resources (like kubectl explain) via Python

Time:04-06

I would like to write a small programm similar to kubectl explain.

I use the python client.

With kubectl explain pods -v=8 I see which APIs get called.

The URL is /openapi/v2

I tried this:

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'))

But the result is empty:

(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'}))

How can I get the nice docs which kubectl explain shows via Python?

CodePudding user response:

You're already getting the data, it's just that some error occurs while processing it :) To turn off post-processing, you need to pass the _preload_content=False argument to call_api
Then the code will look something like this:

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)
  • Related