1

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?

guettli
  • 25,042
  • 81
  • 346
  • 663

1 Answers1

1

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)

If you only want to get the description, you can use curl like this with Bearer Auth: 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

Or with TSL Auth:

 curl -s $APISERVER/openapi/v2 --cert client.crt --key client.key --cacert ca.crt

After that, you can use the tools to work with the openAPI description: https://openapi.tools

For example, upload json to https://mrin9.github.io/OpenAPI-Viewer and enjoy

qqNade
  • 1,942
  • 8
  • 10