0

I want to achieve this command use kubernetes python client.

kubectl -n namespace get all -l key=value

I've seen several questions here List all resources in a namespace using the Kubernetes python client

but it's means

kubectl api-resources

not what I want.

I can't find an example case, so what is the right way to do this?

romlym
  • 561
  • 1
  • 7
  • 26
  • have you checked this [Stack link](https://stackoverflow.com/a/68196323) and this [solution by addii](https://stackoverflow.com/a/74120789/18878095) – Sai Chandra Gadde Apr 25 '23 at 06:41
  • Thank you but neither custom object nor @Aref Riant's suggestion nor dynamic client meets the need. They all need iteration, I'm looking for a way to get resources once. – romlym Apr 25 '23 at 07:43

1 Answers1

0

namespaced resources in Kubernetes api is structured like:

/api/v1/namespaces/{namespace}/pods
/api/v1/namespaces/{namespace}/secrets
/api/v1/namespaces/{namespace}/services
.
.
.

just for resources accessible by Core API

to have access to other resources like StatefulSets, you need to use Apps API

at the end of the day, you need to name all resources you need to retrieve,

from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
namespace = "default"

pods = v1.list_namespaced_pod(namespace)
services = v1.list_namespaced_service(namespace)
# And continue for all resources accessible by core api...


appv1 = client.AppsV1Api()
statefulsets = appv1.list_namespaced_stateful_set(namespace)
# And continue for all resources

You can use get_api_resources() to list all resource names possible, and then iterate over them like the code above, inform in the comments if you needed help.

Aref Riant
  • 582
  • 3
  • 14