Google cloud platform 删除谷歌云注册表上未标记的图像

Google cloud platform 删除谷歌云注册表上未标记的图像,google-cloud-platform,google-container-registry,Google Cloud Platform,Google Container Registry,当我们使用相同的图像名称和版本(标签)重复推入gcr.io时,有大量未标记的图像。 有没有一种简单的方法可以从一个项目中删除所有未标记的图像,或者至少删除一个图像,以避免产生存储成本?gcloud容器图像列表标记gcr.io/project-id/repository--format=json--limit=unlimited将为您在repo中的图像提供一个易于使用的json信息块(例如带有相关标签的摘要) 为了仅列举所有缺少标记的摘要: gcloud容器图像列表标记gcr.io/project

当我们使用相同的图像名称和版本(标签)重复推入
gcr.io
时,有大量未标记的图像。

有没有一种简单的方法可以从一个项目中删除所有未标记的图像,或者至少删除一个图像,以避免产生存储成本?

gcloud容器图像列表标记gcr.io/project-id/repository--format=json--limit=unlimited
将为您在repo中的图像提供一个易于使用的json信息块(例如带有相关标签的摘要)

为了仅列举所有缺少标记的摘要:


gcloud容器图像列表标记gcr.io/project-id/repository--filter='-tags:'--format='get(digest)'--limit=unlimited

您可以通过以下方式对其进行迭代和删除:
gcloud容器映像删除--quiet gcr.io/project-id/repository@DIGEST

使用awk和xargs将它们粘合在一起时非常方便


gcloud容器图像列表标记gcr.io/${PROJECT_ID}/${IMAGE}--filter='-tags:'--format='get(digest)'--limit=unlimited | awk'{print“gcr.io/${PROJECT_ID}/${IMAGE}@'$1}“| xargs gcloud container images delete--quiet

我的用例是从特定项目中删除所有未标记的图像。我编写了一个简单的脚本来实现这一点:

delete_untagged() {
    echo "   |-Deleting untagged images for $1"
    while read digest; do
       gcloud container images delete $1@$digest --quiet 2>&1 | sed 's/^/        /'
    done < <(gcloud container images list-tags $1 --filter='-tags:*' --format='get(digest)' --limit=unlimited)
}

delete_for_each_repo() {
    echo "|-Will delete all untagged images in $1"
    while read repo; do
        delete_untagged $repo
    done < <(gcloud container images list --repository $1 --format="value(name)")
}

delete_for_each_repo gcr.io/<project-id>/<repository>
delete_untaged(){
echo“|-为$1删除未标记的图像”
一边读文摘,一边做
gcloud容器映像删除$1@$digest--quiet 2>&1|sed's/^/'
完成<有文件记录,但需要注意的一点是

摘要的格式必须为“
sha256:

因此,首先捕获未标记图像的形式
sha256:

$ DIGEST=`gcloud container images list-tags gcr.io/[PROJECT-ID]/[IMAGE] \
--filter='-tags:*' --format='get(digest)'`
$ echo $DIGEST
sha256:7c077a9ca45aea7134d8436a3071aceb5fa62758cc86eadec63f02692b7875f7
然后使用变量将其删除

$ gcloud container images delete --quiet gcr.io/[PROJECT-ID]/[IMAGE]@$DIGEST
Digests:
- gcr.io/[PROJECT-ID]/[IMAGE]@sha256:7c077a9ca45a......
Deleted [gcr.io/[PROJECT-ID]/[IMAGE]@sha256:7c077a9ca45a......].

前一个班轮不适合我。我现在正在使用这个班轮

  • 删除给定项目ID和图像的所有未标记图像
  • 使用创建令牌的Xargs-I(在本例中称为{arg})

Powershell由@Benos发布的一行程序版本

gcloud container images list-tags gcr.io/myprojectname/myimagename --filter='-tags:*' --format='get(tags)' --limit=unlimited | ForEach-Object { gcloud container images delete "gcr.io/myprojectname/myimagename:$PSItem" --quiet } 

删除
--filter='-tags:'
删除某个图像的所有标记(这是我试图完成的)

更正了Powershell命令

gcloud container images list-tags gcr.io/myprojectname/myimagename --filter='-tags:*' --format='get(digest)' --limit=unlimited | ForEach-Object { gcloud container images delete "gcr.io/myprojectname/myimagename@$PSItem" --quiet }

必须更改为@才能使其工作:gcr.io/myprojectname/myimagename@$PSItem
gcloud container images list-tags gcr.io/myprojectname/myimagename --filter='-tags:*' --format='get(digest)' --limit=unlimited | ForEach-Object { gcloud container images delete "gcr.io/myprojectname/myimagename@$PSItem" --quiet }