Kubectl 在 pod 中获取文件创建时间



如何使用kubectl execKubernetes API for python获取 pod 内文件(纪元(创建的时间?

我需要这段时间来计算从现在到文件创建时间所经过的时间。

有什么建议吗?

谢谢

我假设你在~/.kube/config下有一个kubeconfig文件

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: %sn" % e)

它返回 tzutc,您需要对其进行解析。

$ python3 get_pods.py
datetime.datetime(2020, 5, 25, 16, 2, 39, tzinfo=tzutc())