With kubectl, I know i can run below command if I want to see specific resources YAML file
kubectl -n <some namespace> get <some resource> <some resource name> -o yaml
How would I get this same data using python's kubernetes-client ? Everything I've found so far only talks about creating a resource from a given yaml file.
In looking at docs, I noticed that each resource type generally has a get_api_resources() function which returns a V1ApiResourceList, where each item is a V1ApiResource. I was hoping there would be a way to get the resource's yaml-output by using a V1ApiResource object, but doesnt appear like that's the way to go about it.
Do you all have any suggestions ? Is this possible with kubernetes-client API ?
If you take a look at the methods available on an object, e.g.:
>>> 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']
...you'll see there is a to_dict
method. That returns the object as
a dictionary, which you can then serialize to YAML or JSON or
whatever:
>>> import yaml
>>> print(yaml.safe_dump(res.to_dict()))
api_version: v1
kind: Namespace
metadata:
[...]