Client Usage

In the previous section, a Go application that lists all the pods was developed. The same functionality as the previous application is performed in Python in this section. With the clean code and readability philosophy of Python, the same functionality is handled in around ten lines of code, as follows:

from kubernetes import client, config
import time
config.load_incluster_config()
v1 = client.CoreV1Api()
while True:
ret = v1.list_pod_for_all_namespaces(watch=False)
print('There are {:d} pods in the cluster:'.format(len(ret.items)))
for i in ret.items:
print('{:s}/{:s}'.format((i.metadata.namespace, i.metadata.name))
time.sleep(10)

These are the critical points to mention about the preceding code snippet:

  • In line 3, the in-cluster configuration, and in line 5, the client for the corev1 API are created.
  • Starting in line 8, an infinite loop starts with a sleep of 10 seconds at each iteration.
  • In line 9, all pods are requested from the v1 client and the response is parsed and written to the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.232.239