4

i have a code to get the number of Pods it give the output but it loads too many unwanted data in (ret_pod), is there a better way to do it?

from  kubernetes import client , config 

config.load_kube_config()
v1= client.CoreV1Api()
ret_pod = v1.list_pod_for_all_namespaces(watch=False)
print(len(ret_pod.items))

which gives me the output of

kubectl get po -A -o json 

and then finds the length. but i just want to do the output of

kubectl get po -A
  • 1
    What's wrong with the way you're doing it? Looks fine to me.... – Z4-tier Jul 05 '20 at 19:43
  • if i have 1000 pods it will load data of all 1000 pods i only want the count of these pods @Z4-tier – Pradeep Padmanaban C Jul 06 '20 at 03:41
  • 1
    There is no API for just a count. You could maybe use a field selector but overall what you have is correct. The API is kept very simple. – coderanger Jul 06 '20 at 08:47
  • 1
    @coderanger, as to `field_selector`, it will only allow to filter / narrow down the result e.g. to pods, having that specific field, containing some specific value i.e. this example `ret_pod = v1.list_pod_for_all_namespaces(field_selector='metadata.name=python-client',watch=False)` will retrieve only a pod, which name is `python-client` but it will retrieve its entire json manifest anyway. So the example shown above is probably the most effective way of performing such task. – mario Jan 20 '21 at 14:49

1 Answers1

4

In kubectl way:

kubectl get po -A --no-headers | wc -l
RammusXu
  • 1,180
  • 1
  • 7
  • 21