Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Container Registry images left after Functions deployment #3439

Merged
merged 10 commits into from
Jun 21, 2021
Prev Previous commit
Next Next commit
Simplify caching
  • Loading branch information
inlined committed Jun 1, 2021
commit ee75643a443b187f49e5359e560ac4fdab093810
79 changes: 16 additions & 63 deletions src/deploy/functions/containerCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,89 +119,42 @@ export interface Stat {
tags: gcr.Tag[];
}

export interface Node {
// If we haven't actually done an LS on this exact location
sparse: boolean;

// For directories
children: Record<string, Node>;

// For images
digests: gcr.Digest[];
tags: gcr.Tag[];
}

export class ContainerRegistryHelper {
readonly client: gcr.Client;
readonly cache: Node;
readonly cache: Record<string, Stat> = {};

constructor(subdomain: string) {
this.client = new gcr.Client(subdomain);
this.cache = {
sparse: true,
children: {},
digests: [],
tags: [],
};
}

private async getNode(path: string): Promise<Node> {
const parts = path.split("/");
let cwd = this.cache;
for (const part of parts) {
if (!cwd.children[part]) {
cwd.children[part] = {
sparse: true,
children: {},
digests: [],
tags: [],
};
}
cwd = cwd.children[part];
}
if (cwd.sparse) {
async ls(path: string): Promise<Stat> {
if (!this.cache[path]) {
const raw = await this.client.listTags(path);
cwd.sparse = false;
cwd.tags = raw.tags;
cwd.digests = Object.keys(raw.manifest);
cwd.children = {};
for (const child of raw.child) {
cwd.children[child] = {
sparse: true,
children: {},
digests: [],
tags: [],
};
}
this.cache[path] = {
tags: raw.tags,
digests: Object.keys(raw.manifest),
children: raw.child,
};
}
return cwd;
}

async ls(path: string): Promise<Stat> {
const node = await this.getNode(path);
return {
children: Object.keys(node.children),
digests: node.digests,
tags: node.tags,
};
return this.cache[path];
}

async rm(path: string): Promise<void> {
const node = await this.getNode(path);
const stat = await this.ls(path);
const deleteChildren: Promise<void>[] = [];
const recursive = Object.keys(node.children).map((child) => this.rm(`${path}/${child}`));
const recursive = stat.children.map((child) => this.rm(`${path}/${child}`));
// Let children ("directories") be cleaned up in parallel while we clean
// up the "files" in this location.

const deleteTags = node.tags.map((tag) => this.client.deleteTag(path, tag));
const deleteTags = stat.tags.map((tag) => this.client.deleteTag(path, tag));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to delete all tags before starting to delete the images? If not, we could combine these await Promise.all()'s and do them in parallel

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also a little concerned about how this behaves if just one call fails - for example, if the first Promise.all rejects, this errors and we never try to deleteImages... but we already made our recursive call, so that has started but will get 'cut off' at some arbitrary point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Tags pin an image and must be removed before an image can be removed. I've added comments to clarify this.
  2. Agreed. I didn't feel super warm and fuzzy about these errors. I've made rm recurse as far as it can irrespective of errors and throw a random error it encounters. Rather than investing in a multi-error type to aggregate FirebaseErrors, I'm just debug logging the error for future reference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me - this is definitely a tricky one to handle and it seems likely that if one call fails, the rest are high likelihood to as well. I feel better about this strategy tho.

await Promise.all(deleteTags);
node.tags = [];
stat.tags = [];

const deleteImages = node.digests.map((digest) => this.client.deleteImage(path, digest));
const deleteImages = stat.digests.map((digest) => this.client.deleteImage(path, digest));
await Promise.all(deleteImages);
node.digests = [];
stat.digests = [];

await Promise.all(recursive);
node.children = {};
stat.children = [];
}
}
5 changes: 5 additions & 0 deletions src/test/deploy/functions/containerCleaner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ describe("ContainerRegistryHelper", () => {
tags: [],
children: [],
});
await expect(helper.ls("foo/bar/baz")).to.eventually.deep.equal({
digests: [],
tags: [],
children: [],
});
});
});

Expand Down