如果您查看对象上可用的方法,例如:
>>> import kubernetes.config
>>> client = kubernetes.config.new_client_from_config()
>>> core = kubernetes.client.CoreV1Api(client)
>>> res = core.read_namespace('kube-system')
>>> dir(res)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api_version', '_kind', '_metadata', '_spec', '_status', 'api_version', 'attribute_map', 'discriminator', 'kind', 'local_vars_configuration', 'metadata', 'openapi_types', 'spec', 'status', 'to_dict', 'to_str']
...你会看到有一个to_dict 方法。将对象返回为
一个字典,然后您可以将其序列化为 YAML 或 JSON 或
随便:
>>> import yaml
>>> print(yaml.safe_dump(res.to_dict()))
api_version: v1
kind: Namespace
metadata:
[...]