运行此工具可确定您的Google Cloud 项目、文件夹或组织是否有 Container Registry 使用情况。
准备工作
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
所需的角色
如需获得查看 Google Cloud 项目、文件夹或组织中的容器注册表使用情况所需的权限,请让管理员为您授予以下 IAM 角色:
-
Google Cloud 项目、文件夹或组织的 Cloud Asset Viewer (
roles/cloudasset.viewer
) -
Google Cloud 项目、文件夹或组织的 Storage Object Viewer (
roles/storage.objectViewer
)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
查看 Container Registry 用量
您可以运行范围限定为单个 Google Cloud 项目、文件夹或组织的工具。
项目
运行以下命令可查找Google Cloud 项目中的所有 Container Registry 用量。
gcloud container images list-gcr-usage \
--project=PROJECT
其中,PROJECT 是您的 Google Cloud 项目 ID。如需了解如何查找项目 ID,请参阅标识项目。
该工具会返回指定项目的托管位置列表及其使用状态。使用状态定义如下:
ACTIVE
:过去 30 天内有 Container Registry 使用情况。主机位置和项目不会被重定向。INACTIVE
:过去 30 天内未使用过 Container Registry。宿主位置和项目不会被重定向。REDIRECTED
:项目已重定向到 Artifact Registry,但仍有 Container Registry Cloud Storage 存储分区。在 Container Registry 停用后,此项目将继续运行,您无需采取进一步行动。您可以删除 Container Registry Cloud Storage 存储分区,以降低费用。REDIRECTION_INCOMPLETE
:请求会重定向到 Artifact Registry,但数据仍会从 Container Registry 复制。LEGACY
:Container Registry 用量未知。这种状态是由将容器映像元数据文件存储在 Cloud Storage 存储分区中的旧版 Container Registry 项目造成的。如需详细了解旧版 Container Registry 项目,请参阅容器映像元数据存储变更。
如果该工具遇到错误(例如缺少查看 Cloud Storage 存储分区或 Container Registry 项目的权限),您会看到类似于以下内容的错误消息:
repository: us.gcr.io/my-project
usage: |-
response: {'status': 401}
Operation on project 'no-gcr-permission' failed. Caller does not have permission 'storage.objects.list'. To configure permissions, follow instructions at: https://cloud.google.com/container-registry/docs/access-control: None
文件夹
运行以下命令可在Google Cloud 文件夹中查找任何 Container Registry 用量。
gcloud container images list-gcr-usage \
--folder=FOLDER
其中 FOLDER 是您的 Google Cloud 文件夹 ID。如需了解如何列出组织中的文件夹,请参阅列出文件夹。
该工具会返回以下使用状态列表:
ACTIVE
:过去 30 天内有 Container Registry 使用情况。主机位置和项目不会被重定向。INACTIVE
:过去 30 天内未使用过 Container Registry。宿主位置和项目不会被重定向。REDIRECTED
:项目已重定向到 Artifact Registry,但仍有 Container Registry Cloud Storage 存储分区。在 Container Registry 停用后,此项目将继续运行,您无需采取进一步行动。您可以删除 Container Registry Cloud Storage 存储分区,以降低费用。REDIRECTION_INCOMPLETE
:请求会重定向到 Artifact Registry,但数据仍会从 Container Registry 复制。LEGACY
:Container Registry 用量未知。这种状态是由将容器映像元数据文件存储在 Cloud Storage 存储分区中的旧版 Container Registry 项目造成的。如需详细了解旧版 Container Registry 项目,请参阅容器映像元数据存储变更。
如果该工具遇到错误(例如缺少查看 Cloud Storage 存储分区或 Container Registry 项目的权限),您会看到类似于以下内容的错误消息:
repository: us.gcr.io/my-project
usage: |-
response: {'status': 401}
Operation on project 'no-gcr-permission' failed. Caller does not have permission 'storage.objects.list'. To configure permissions, follow instructions at: https://cloud.google.com/container-registry/docs/access-control: None
组织
运行以下命令可查找Google Cloud 组织中的所有 Container Registry 使用情况。
gcloud container images list-gcr-usage \
--organization=ORGANIZATION
其中 ORGANIZATION 是您的 Google Cloud 组织 ID。如需了解如何查找组织 ID,请参阅获取组织资源 ID。
该工具会返回以下使用状态列表:
ACTIVE
:过去 30 天内有 Container Registry 使用情况。主机位置和项目不会被重定向。INACTIVE
:过去 30 天内未使用过 Container Registry。宿主位置和项目不会被重定向。REDIRECTED
:项目已重定向到 Artifact Registry,但仍有 Container Registry Cloud Storage 存储分区。在 Container Registry 停用后,此项目将继续运行,您无需采取进一步行动。您可以删除 Container Registry Cloud Storage 存储分区,以降低费用。REDIRECTION_INCOMPLETE
:请求会重定向到 Artifact Registry,但数据仍会从 Container Registry 复制。LEGACY
:Container Registry 用量未知。这种状态是由将容器映像元数据文件存储在 Cloud Storage 存储分区中的旧版 Container Registry 项目造成的。如需详细了解旧版 Container Registry 项目,请参阅容器映像元数据存储变更。
如果该工具遇到错误(例如缺少查看 Cloud Storage 存储分区或 Container Registry 项目的权限),您会看到类似于以下内容的错误消息:
repository: us.gcr.io/my-project
usage: |-
response: {'status': 401}
Operation on project 'no-gcr-permission' failed. Caller does not have permission 'storage.objects.list'. To configure permissions, follow instructions at: https://cloud.google.com/container-registry/docs/access-control: None
过滤结果
如需控制列出哪些资源项,请传递 --filter=EXPRESSION
标志。如果给定项的表达式求值结果为 true,则系统会列出该项。
如需列出贵组织中的所有有效 Container Registry 使用情况,您可以按有效使用情况进行过滤:
gcloud container images list-gcr-usage \
--organization=ORGANIZATION \
--filter="usage=ACTIVE"
如需查找尚未重定向的所有项目,您可以按 usage!=REDIRECTED
进行过滤:
gcloud container images list-gcr-usage \
--organization=ORGANIZATION \
--filter="usage!=REDIRECTED"
如需详细了解过滤条件表达式,请运行 gcloud topic filters
或阅读参考文档。