|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package images provides operations around docker images. |
| 16 | +package images |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "log/slog" |
| 22 | + "strings" |
| 23 | + |
| 24 | + artifactregistry "cloud.google.com/go/artifactregistry/apiv1" |
| 25 | + artifactregistrypb "cloud.google.com/go/artifactregistry/apiv1/artifactregistrypb" |
| 26 | +) |
| 27 | + |
| 28 | +// ArtifactRegistryClient is the implementation of ImageRegistryClient |
| 29 | +// to interact with Artifact Registry. |
| 30 | +type ArtifactRegistryClient struct { |
| 31 | + client *artifactregistry.Client |
| 32 | +} |
| 33 | + |
| 34 | +// containerImage is a data structure for parsing Docker image parameters. |
| 35 | +type containerImage struct { |
| 36 | + // Name is the short name of the docker image. |
| 37 | + Name string |
| 38 | + // Tag is the named tag of the docker image. |
| 39 | + Tag string |
| 40 | + // SHA is the SHA256 (e.g. `sha256:abcd1234`). |
| 41 | + SHA string |
| 42 | + // Location is the Artifact Registry location (e.g. `us-central1`). |
| 43 | + Location string |
| 44 | + // Project is the name of the GCP project that holds the AR repository. |
| 45 | + Project string |
| 46 | + // Repository is the name of the AR repository. |
| 47 | + Repository string |
| 48 | +} |
| 49 | + |
| 50 | +// BaseName returns the image name without a pinned SHA or tag. |
| 51 | +func (i *containerImage) BaseName() string { |
| 52 | + return fmt.Sprintf("%s-docker.pkg.dev/%s/%s/%s", i.Location, i.Project, i.Repository, i.Name) |
| 53 | +} |
| 54 | + |
| 55 | +// String returns the image name with pinned SHA or tag. |
| 56 | +func (i *containerImage) String() string { |
| 57 | + var b strings.Builder |
| 58 | + b.WriteString(i.BaseName()) |
| 59 | + if i.SHA != "" { |
| 60 | + b.WriteString("@") |
| 61 | + b.WriteString(i.SHA) |
| 62 | + } else if i.Tag != "" { |
| 63 | + b.WriteString(":") |
| 64 | + b.WriteString(i.Tag) |
| 65 | + } |
| 66 | + return b.String() |
| 67 | +} |
| 68 | + |
| 69 | +// NewArtifactRegistryClient creates a new ArtifactRegistryClient. |
| 70 | +func NewArtifactRegistryClient(ctx context.Context) (*ArtifactRegistryClient, error) { |
| 71 | + client, err := artifactregistry.NewClient(ctx) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return &ArtifactRegistryClient{ |
| 76 | + client: client, |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +// Close cleans up any open resources. |
| 81 | +func (c *ArtifactRegistryClient) Close() error { |
| 82 | + return c.client.Close() |
| 83 | +} |
| 84 | + |
| 85 | +// FindLatest returns the latest docker image given a current image. |
| 86 | +func (c *ArtifactRegistryClient) FindLatest(ctx context.Context, imageName string) (string, error) { |
| 87 | + image, err := parseImage(imageName) |
| 88 | + if err != nil { |
| 89 | + return "", err |
| 90 | + } |
| 91 | + |
| 92 | + if c.client == nil { |
| 93 | + return "", fmt.Errorf("no client configured") |
| 94 | + } |
| 95 | + |
| 96 | + it := c.client.ListVersions(ctx, &artifactregistrypb.ListVersionsRequest{ |
| 97 | + Parent: fmt.Sprintf("projects/%s/locations/%s/repositories/%s/packages/%s", image.Project, image.Location, image.Repository, image.Name), |
| 98 | + View: artifactregistrypb.VersionView_FULL, |
| 99 | + OrderBy: "create_time DESC", |
| 100 | + }) |
| 101 | + version, err := it.Next() |
| 102 | + if err != nil { |
| 103 | + return "", err |
| 104 | + } |
| 105 | + slog.Info("Found packages version", "version", version.GetName()) |
| 106 | + |
| 107 | + // latest SHA is found as the "subjectDigest" metadata field |
| 108 | + latestSha := "" |
| 109 | + for key, field := range version.GetMetadata().GetFields() { |
| 110 | + if key == "subjectDigest" { |
| 111 | + slog.Info("Found SHA", "sha", field.GetStringValue()) |
| 112 | + latestSha = field.GetStringValue() |
| 113 | + break |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if latestSha == "" { |
| 118 | + return "", fmt.Errorf("failed to find updated SHA for latest version: %s", version.GetName()) |
| 119 | + } |
| 120 | + |
| 121 | + newImage := &containerImage{ |
| 122 | + Name: image.Name, |
| 123 | + Location: image.Location, |
| 124 | + Repository: image.Repository, |
| 125 | + Project: image.Project, |
| 126 | + SHA: latestSha, |
| 127 | + } |
| 128 | + return newImage.String(), nil |
| 129 | +} |
| 130 | + |
| 131 | +func parseImage(pinnedImage string) (*containerImage, error) { |
| 132 | + parsedImage := &containerImage{} |
| 133 | + baseName := "" |
| 134 | + |
| 135 | + atParts := strings.Split(pinnedImage, "@") |
| 136 | + colonParts := strings.Split(pinnedImage, ":") |
| 137 | + if len(atParts) == 2 { |
| 138 | + baseName = atParts[0] |
| 139 | + parsedImage.SHA = atParts[1] |
| 140 | + } else if len(colonParts) == 2 { |
| 141 | + baseName = colonParts[0] |
| 142 | + parsedImage.Tag = colonParts[1] |
| 143 | + } |
| 144 | + |
| 145 | + if baseName == "" { |
| 146 | + slog.Info("image does not appear to be pinned") |
| 147 | + baseName = pinnedImage |
| 148 | + } |
| 149 | + |
| 150 | + parts := strings.Split(baseName, "/") |
| 151 | + if len(parts) < 4 { |
| 152 | + return nil, fmt.Errorf("unexpected image format %q, expected an AR formatted image", baseName) |
| 153 | + } |
| 154 | + |
| 155 | + host := parts[0] |
| 156 | + if strings.HasSuffix(host, "-docker.pkg.dev") { |
| 157 | + parsedImage.Location = strings.TrimSuffix(host, "-docker.pkg.dev") |
| 158 | + } else { |
| 159 | + return nil, fmt.Errorf("unexpected host format %q, expected AR formatted host with -docker.pkg.dev suffix", host) |
| 160 | + } |
| 161 | + |
| 162 | + parsedImage.Project = parts[1] |
| 163 | + parsedImage.Repository = parts[2] |
| 164 | + parsedImage.Name = parts[3] |
| 165 | + |
| 166 | + return parsedImage, nil |
| 167 | +} |
0 commit comments