|
| 1 | +// This code is very aggressive about running requests in parallel and does not use |
| 2 | +// a task queue, because the quota limits for GCR.io are absurdly high. At the time |
| 3 | +// of writing, we can make 50K requests per 10m. |
| 4 | +// https://cloud.google.com/container-registry/quotas |
| 5 | + |
| 6 | +import * as clc from "cli-color"; |
| 7 | + |
| 8 | +import { containerRegistryDomain } from "../../api"; |
| 9 | +import { logger } from "../../logger"; |
| 10 | +import * as docker from "../../gcp/docker"; |
| 11 | +import * as backend from "./backend"; |
| 12 | +import * as utils from "../../utils"; |
| 13 | + |
| 14 | +// A flattening of container_registry_hosts and |
| 15 | +// region_multiregion_map from regionconfig.borg |
| 16 | +const SUBDOMAIN_MAPPING: Record<string, string> = { |
| 17 | + "us-west2": "us", |
| 18 | + "us-west3": "us", |
| 19 | + "us-west4": "us", |
| 20 | + "us-central1": "us", |
| 21 | + "us-central2": "us", |
| 22 | + "us-east1": "us", |
| 23 | + "us-east4": "us", |
| 24 | + "northamerica-northeast1": "us", |
| 25 | + "southamerica-east1": "us", |
| 26 | + "europe-west1": "eu", |
| 27 | + "europe-west2": "eu", |
| 28 | + "europe-west3": "eu", |
| 29 | + "europe-west5": "eu", |
| 30 | + "europe-west6": "eu", |
| 31 | + "europe-central2": "eu", |
| 32 | + "asia-east1": "asia", |
| 33 | + "asia-east2": "asia", |
| 34 | + "asia-northeast1": "asia", |
| 35 | + "asia-northeast2": "asia", |
| 36 | + "asia-northeast3": "asia", |
| 37 | + "asia-south1": "asia", |
| 38 | + "asia-southeast2": "asia", |
| 39 | + "australia-southeast1": "asia", |
| 40 | +}; |
| 41 | + |
| 42 | +export async function cleanupBuildImages(functions: backend.FunctionSpec[]): Promise<void> { |
| 43 | + utils.logBullet(clc.bold.cyan("functions: ") + "cleaning up build files..."); |
| 44 | + const gcrCleaner = new ContainerRegistryCleaner(); |
| 45 | + const failedDomains: Set<string> = new Set(); |
| 46 | + await Promise.all( |
| 47 | + functions.map((func) => |
| 48 | + (async () => { |
| 49 | + try { |
| 50 | + await gcrCleaner.cleanupFunction(func); |
| 51 | + } catch (err) { |
| 52 | + const path = `${func.project}/${SUBDOMAIN_MAPPING[func.region]}/gcf`; |
| 53 | + failedDomains.add(`https://console.cloud.google.com/gcr/images/${path}`); |
| 54 | + } |
| 55 | + })() |
| 56 | + ) |
| 57 | + ); |
| 58 | + if (failedDomains.size) { |
| 59 | + let message = |
| 60 | + "Unhandled error cleaning up build images. This could result in a small monthly bill if not corrected. "; |
| 61 | + message += |
| 62 | + "You can attempt to delete these images by redeploying or you can delete them manually at"; |
| 63 | + if (failedDomains.size == 1) { |
| 64 | + message += " " + failedDomains.values().next().value; |
| 65 | + } else { |
| 66 | + message += [...failedDomains].map((domain) => "\n\t" + domain).join(""); |
| 67 | + } |
| 68 | + utils.logLabeledWarning("functions", message); |
| 69 | + } |
| 70 | + |
| 71 | + // TODO: clean up Artifact Registry images as well. |
| 72 | +} |
| 73 | + |
| 74 | +export class ContainerRegistryCleaner { |
| 75 | + readonly helpers: Record<string, DockerHelper> = {}; |
| 76 | + |
| 77 | + private helper(location: string): DockerHelper { |
| 78 | + const subdomain = SUBDOMAIN_MAPPING[location] || "us"; |
| 79 | + if (!this.helpers[subdomain]) { |
| 80 | + const origin = `https://${subdomain}.${containerRegistryDomain}`; |
| 81 | + this.helpers[subdomain] = new DockerHelper(origin); |
| 82 | + } |
| 83 | + return this.helpers[subdomain]; |
| 84 | + } |
| 85 | + |
| 86 | + // GCFv1 has the directory structure: |
| 87 | + // gcf/ |
| 88 | + // +- <region>/ |
| 89 | + // +- <uuid> |
| 90 | + // +- <hash> (tags: <FuncName>_version-<#>) |
| 91 | + // +- cache/ (Only present in first deploy of region) |
| 92 | + // | +- <hash> (tags: latest) |
| 93 | + // +- worker/ (Only present in first deploy of region) |
| 94 | + // +- <hash> (tags: latest) |
| 95 | + // |
| 96 | + // We'll parallel search for the valid <uuid> and their children |
| 97 | + // until we find one with the right tag for the function name. |
| 98 | + // The underlying Helper's caching should make this expensive for |
| 99 | + // the first function and free for the next functions in the same |
| 100 | + // region. |
| 101 | + async cleanupFunction(func: backend.FunctionSpec): Promise<void> { |
| 102 | + const helper = this.helper(func.region); |
| 103 | + const uuids = (await helper.ls(`${func.project}/gcf/${func.region}`)).children; |
| 104 | + |
| 105 | + const uuidTags: Record<string, string[]> = {}; |
| 106 | + const loadUuidTags: Promise<void>[] = []; |
| 107 | + for (const uuid of uuids) { |
| 108 | + loadUuidTags.push( |
| 109 | + (async () => { |
| 110 | + const path = `${func.project}/gcf/${func.region}/${uuid}`; |
| 111 | + const tags = (await helper.ls(path)).tags; |
| 112 | + uuidTags[path] = tags; |
| 113 | + })() |
| 114 | + ); |
| 115 | + } |
| 116 | + await Promise.all(loadUuidTags); |
| 117 | + |
| 118 | + const extractFunction = /^(.*)_version-\d+$/; |
| 119 | + const entry = Object.entries(uuidTags).find(([, tags]) => { |
| 120 | + return tags.find((tag) => extractFunction.exec(tag)?.[1] === func.id); |
| 121 | + }); |
| 122 | + |
| 123 | + if (!entry) { |
| 124 | + logger.debug("Could not find image for function", backend.functionName(func)); |
| 125 | + return; |
| 126 | + } |
| 127 | + await helper.rm(entry[0]); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export interface Stat { |
| 132 | + children: string[]; |
| 133 | + digests: docker.Digest[]; |
| 134 | + tags: docker.Tag[]; |
| 135 | +} |
| 136 | + |
| 137 | +export class DockerHelper { |
| 138 | + readonly client: docker.Client; |
| 139 | + readonly cache: Record<string, Stat> = {}; |
| 140 | + |
| 141 | + constructor(origin: string) { |
| 142 | + this.client = new docker.Client(origin); |
| 143 | + } |
| 144 | + |
| 145 | + async ls(path: string): Promise<Stat> { |
| 146 | + if (!this.cache[path]) { |
| 147 | + const raw = await this.client.listTags(path); |
| 148 | + this.cache[path] = { |
| 149 | + tags: raw.tags, |
| 150 | + digests: Object.keys(raw.manifest), |
| 151 | + children: raw.child, |
| 152 | + }; |
| 153 | + } |
| 154 | + return this.cache[path]; |
| 155 | + } |
| 156 | + |
| 157 | + // While we can't guarantee all promises will succeed, we can do our darndest |
| 158 | + // to expunge as much as possible before throwing. |
| 159 | + async rm(path: string): Promise<void> { |
| 160 | + let toThrowLater: any = undefined; |
| 161 | + const stat = await this.ls(path); |
| 162 | + const recursive = stat.children.map((child) => |
| 163 | + (async () => { |
| 164 | + try { |
| 165 | + await this.rm(`${path}/${child}`); |
| 166 | + stat.children.splice(stat.children.indexOf(child), 1); |
| 167 | + } catch (err) { |
| 168 | + toThrowLater = err; |
| 169 | + } |
| 170 | + })() |
| 171 | + ); |
| 172 | + // Unlike a filesystem, we can delete a "directory" while its children are still being |
| 173 | + // deleted. Run these in parallel to improve performance and just wait for the result |
| 174 | + // before the function's end. |
| 175 | + |
| 176 | + // An image cannot be deleted until its tags have been removed. Do this in two phases. |
| 177 | + const deleteTags = stat.tags.map((tag) => |
| 178 | + (async () => { |
| 179 | + try { |
| 180 | + await this.client.deleteTag(path, tag); |
| 181 | + stat.tags.splice(stat.tags.indexOf(tag), 1); |
| 182 | + } catch (err) { |
| 183 | + logger.debug("Got error trying to remove docker tag:", err); |
| 184 | + toThrowLater = err; |
| 185 | + } |
| 186 | + })() |
| 187 | + ); |
| 188 | + await Promise.all(deleteTags); |
| 189 | + |
| 190 | + const deleteImages = stat.digests.map((digest) => |
| 191 | + (async () => { |
| 192 | + try { |
| 193 | + await this.client.deleteImage(path, digest); |
| 194 | + stat.digests.splice(stat.digests.indexOf(digest), 1); |
| 195 | + } catch (err) { |
| 196 | + logger.debug("Got error trying to remove docker image:", err); |
| 197 | + toThrowLater = err; |
| 198 | + } |
| 199 | + })() |
| 200 | + ); |
| 201 | + await Promise.all(deleteImages); |
| 202 | + |
| 203 | + await Promise.all(recursive); |
| 204 | + |
| 205 | + if (toThrowLater) { |
| 206 | + throw toThrowLater; |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments