打印的python的dict或list对象用FEHelper查看

利用Python的kubernetes库,可以查询出来某个namespace里下的pod,但是返回的结果集过于复杂,想找到其中的pod IP, 需要费点时间。

1
2
3
all_pods = client.CoreV1Api().list_namespaced_pod(
CURRENT_RUNNING_NAMESPACE
).to_dict()["items"]

all_pods直接打的结果不是json,里面的None和datetime.datetime是无法被FEHelper识别的。
json直接dump的时候会报错误:TypeError: Object of type datetime is not JSON serializable。
这个时候只需要简单的加个参数就可以了。

1
json.dumps(all_pods, sort_keys=True, default=str)

打印出来的结果,拷贝到FEHelper然后去掉头尾的引号就可以识别了, 顺利找出pod IP的路径,拼出来想要的结果。

1
[s['status']['pod_ip'] for s in all_pods]
Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能需要科学上网方式。