gcloud 基于时间过滤结果

gcloud filter result based on time

我正在尝试在 GCP 上列出机器图像并根据 CreatimeTimestamp 筛选出结果 - 一些 hours/days。 我正在尝试做这样的事情:

gcloud beta compute machine-images list --sort-by "creationTimestamp" --project=XXXXX --format="table(name,creationTimestamp.date('%Y-%m-%d-%T'))" --filter="CREATION_TIMESTAMP.date('%Y-%m-$d-%H:%M:%S')<'$(date -d '3 hours ago' "+%Y-%m-%d-%H:%M:%S")'"

列出 3 小时前的机器图像

Update

Thinking more about your question, I think there's probably an easier way than my answer (below). The documentation suggests that you can do a date compare directly. So, perhaps:

WHEN=$(date +%s --date='3 hours ago')
gcloud beta compute machine-images list \
--project=${PROJECT} \
--format="table(name,creationTimestamp)") \
--filter="creationTimestamp.date(\"+%s\")>${WHEN}"

gcloud 过滤器不包含 shell,因此您不能包含例如过滤器中 bash 的 date 命令。

我认为(!)方法是format输出你想要的结果然后过滤。

IMAGES=$(gcloud beta compute machine-images list \
--project=${PROJECT} \
--format="csv[no-heading](name,creationTimestamp.date())")

for IMAGE in ${IMAGES}
do
  IFS=, read NAME TIMESTAMP <<< ${IMAGE}
  CREATED=$(date +%s --date=${TIMESTAMP}
  WHEN=$(date +%s --date='3 hours ago')
  if [ ${CREATED} -gt ${WHEN} ]
  then
    printf "Image: %s (Creation: %s)\n" NAME TIMESTAMP
  fi
done