Kubectl get file creation time in pod

5/25/2020

How can I get time created of file (epoch) inside pod using kubectl exec or Kubernetes API for python?

I need this time to calculate the time elapsed from now to file created time.

Any suggestions?

Thanks

-- samisaviv
kubernetes
python

1 Answer

5/25/2020

I assume you have a kubeconfig file under ~/.kube/config

from kubernetes import client, config
from kubernetes.client.rest import ApiException
from pprint import pprint

config.load_kube_config()
v1 = client.CoreV1Api()

namespace = 'default'

try:
    api_response = v1.list_namespaced_pod(namespace)
    pprint(api_response.items[0].metadata.creation_timestamp)
except ApiException as e:
    print("Exception when calling CoreV1Api->list_namespaced_pod: %s\n" % e)

It returns tzutc, which you will need to parse.

$ python3 get_pods.py
datetime.datetime(2020, 5, 25, 16, 2, 39, tzinfo=tzutc())
-- suren
Source: StackOverflow