Home > Software engineering >  Read a Kubernetes Deployment with specific label with python kubernetes client
Read a Kubernetes Deployment with specific label with python kubernetes client

Time:10-19

I am looking for the python Kubernetes client equivalent for

kubectl get deploy -n app -l team=blue

I'm aware of this function deployment = api.read_namespaced_deployment(name='foo', namespace='bar') but how can I have same thing with label?

CodePudding user response:

read_namespaced_deployment expects deployment name, which does not make sense of using label, you should use list_namespaced_deployment

you can use label_selector

:param str label_selector: A selector to restrict the list of returned objects by their labels. Defaults to everything.

from kubernetes import client, config
config.load_kube_config()
kube_client = client.AppsV1Api()
resp = kube_client.list_namespaced_deployment(namespace="app", label_selector="team=blue")
for deployment in resp.items:
    print(deployment)

to get all label

 kubectl get deployment --show-labels
  • Related