Get Kubernetes POD restart count with Python client

It takes some time to search right keywords.

#!/usr/bin/python3

import sys

from kubernetes import client, config
from kubernetes.client.api import core_v1_api
from kubernetes.client import configuration

def main():
    config.load_kube_config("/home/cylee/.kube/config")
    configuration.assert_hostname = False

    core_v1 = core_v1_api.CoreV1Api()

    namespace_names = list()
    namespaces = core_v1.list_namespace()

    for namespace in namespaces.items:
        namespace_names.append(namespace.metadata.name)

    print(namespace_names)

    for namespace_name in namespace_names:
        pods = core_v1.list_namespaced_pod(namespace_name)
        for pod in pods.items:
            print("{}, restart {}".format(pod.metadata.name,
                pod.status.container_statuses[0].restart_count))


    return 0

if __name__ == "__main__":
    sys.exit(main())

Reference

POD status

Container status