googleapis / google-cloud-python

Google Cloud Client Library for Python
https://googleapis.github.io/google-cloud-python/
Apache License 2.0
4.84k stars 1.53k forks source link

Get Docker Image size in GAR #11436

Closed ruodingt closed 1 year ago

ruodingt commented 1 year ago

Hi team,

I am trying to use python client to fetch metadata (like image size) of a docker images with arc.get_docker_image.

I am expecting the following code could work:

from google.cloud.artifactregistry import ArtifactRegistryClient, ListTagsRequest, ListPackagesRequest, Package, GetDockerImageRequest

src_img = f'{loc}-docker.pkg.dev/{proj}/{repo_src}/{pn}:{tag_name}' 

# src_img looks like `australia-southeast1-docker.pkg.dev/kkk-x-xxx-build-prod-xxxxxx/docker-registry-repository/abc-pipeline-flex:latest`
gir = GetDockerImageRequest(dict(name=src_img))
dkim = arc.get_docker_image(name=src_img)

However I got error:

google.api_core.exceptions.InvalidArgument: 400 Invalid resource field value in the request. [reason: "RESOURCE_PROJECT_INVALID"
domain: "googleapis.com"
metadata {
  key: "method"
  value: "google.devtools.artifactregistry.v1.ArtifactRegistry.GetDockerImage"
}
metadata {
  key: "service"
  value: "artifactregistry.googleapis.com"
}
]

Please give me some idea how to fetch the image size with program.

parthea commented 1 year ago

I was not able to re-create the error. See example below to list all repositories, all images and their corresponding size.

from google.cloud.artifactregistry import ArtifactRegistryClient, ListRepositoriesRequest

project = "<YOUR PROJECT>"
location = "<YOUR LOCATION>"
arc = ArtifactRegistryClient()

request = ListRepositoriesRequest(
    parent=f"projects/{project}/locations/{location}",
)

# Make the request to list artifact registry repositories
paged_result = arc.list_repositories(request=request)

for repository in paged_result:
    # Get docker images for each repository
    images = arc.list_docker_images(parent=repository.name)

    for image in images:
        # For each image, get the size
        image_details = arc.get_docker_image(name=image.name)
        print(f"name: {image.name} size: {image_details.image_size_bytes}")
parthea commented 1 year ago

I'm going to close this issue but please feel free to open a new issue if you have additional questions.

glasser commented 11 months ago

I ran into this same error with the NodeJS client. The issue is that this API requires you to pass in name in the form projects/kkk-x-xxx-build-prod-xxxxxx/locations/australia-southeast1/repositories/docker-registry-repository/dockerImages/abc-pipeline-flex@sha256:2dd6956aa4f462badb7aa445f73092b19e55e2be2c03bee4ec230700963b3f55 rather than the form you use the repository with Docker.

In addition, as far as I can tell, it only lets you pass in the sha, not a tag. If you want to look up by tag you have to use the completely separate get_tag API, like arc.get_tag(name='projects/kkk-x-xxx-build-prod-xxxxxx/locations/australia-southeast1/repositories/docker-registry-repository/packages/abc-pipeline-flex/tags/latest'), which will give you back and object with a "version" that you then need extract the SHA from in order to pass to get_docker_image in the format above.

StevenACoffman commented 9 months ago

@glasser Thanks! I used your comment above and managed to stumble through this awkward situation using the Go SDK. I eventually added to our internal documentation:

Most people expect to supply a Docker Registry URL like: docker://us-central1-docker.pkg.dev/gcp-project/myrepo/myapp:mytag@sha256:2dd6956aa4f462badb7aa445f73092b19e55e2be2c03bee4ec230700963b3f55

Instead, we have to construct a valid Name in a different format for each of GetTagRequest, ListDockerImagesRequest and GetDockerImageRequest operations. GetTag response returns a Tag. ListDockerImages and GetDockerImage responses are composed of DockerImage objects.

CMorton737 commented 4 months ago
  • projects/gcp-project/locations/us-central1/repositories/myrepo/packages/myapp@sha256:2dd6956aa4f462badb7aa445f73092b19e55e2be2c03bee4ec230700963b3f55

I've found that GetDockerImage RPC API actually requires the name format to be projects/gcp-project/locations/us-central1/repositories/myrepo/dockerImages/myapp@sha256:2dd6956aa4f462badb7aa445f73092b19e55e2be2c03bee4ec230700963b3f55