示例:我想获取所有运行时间超过 3 天的命名空间。我已经借助此命令按标签和创建时间戳对命名空间进行了排序: kubectl get namespaces -l Provisioner=foo --sort-by=.metadata.creationTimestamp
最佳答案
如果您可以使用shell/bash
:
kubectl get ns -o jsonpath='{range .items[*]}{.metadata.name} {.metadata.creationTimestamp}{"\n"}{end}' | while read -r name timestamp; do
echo "$name" | awk -v current_time=$(date +%s) -v three_days_back=$(date +%s -d "3 day ago") -v ns_time=$(date --date="${timestamp}" +%s) '(current_time - ns_time) >(current_time - three_days_back){print $0}';
done
关于Kubernetes:如何获取运行超过 3 天的命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68891324/