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

Add encoding to AR package names #3918

Merged
merged 4 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/deploy/functions/containerCleaner.ts
Expand Up @@ -130,7 +130,18 @@ export async function cleanupBuildImages(
// requests through a ThrottlerQueue.
export class ArtifactRegistryCleaner {
static packagePath(func: backend.TargetIds): string {
return `projects/${func.project}/locations/${func.region}/repositories/gcf-artifacts/packages/${func.id}`;
// GCFv1 names can include upper-case letters, but docker images cannot.
// to fix this, the artifact registry path for these images uses a custom encoding scheme.
// * Underscores are doubled
// * Dashes are doubled
// * A leading capital letter is replaced with <lower><dash><lower>
// * Other capital letters are replaced with <underscore><lower>
const encodedId = func.id
.replace(/_/g, "__")
.replace(/-/g, "--")
.replace(/^[A-Z]/, (first) => `${first.toLowerCase()}-${first.toLowerCase()}`)
.replace(/[A-Z]/g, (upper) => `_${upper.toLowerCase()}`);
return `projects/${func.project}/locations/${func.region}/repositories/gcf-artifacts/packages/${encodedId}`;
}

static POLLER_OPTIONS = {
Expand Down
17 changes: 17 additions & 0 deletions src/test/deploy/functions/containerCleaner.spec.ts
Expand Up @@ -240,6 +240,23 @@ describe("ArtifactRegistryCleaner", () => {
);
expect(poll.pollOperation).to.not.have.been.called;
});

it("encodeds to avoid upper-case letters", async () => {
const cleaner = new containerCleaner.ArtifactRegistryCleaner();
const func = {
id: "Strange-Casing_cases",
region: "region",
project: "project",
};

ar.deletePackage.returns(Promise.resolve({ name: "op", done: true }));

await cleaner.cleanupFunction(func);
expect(ar.deletePackage).to.have.been.calledWith(
"projects/project/locations/region/repositories/gcf-artifacts/packages/s-strange--_casing__cases"
);
expect(poll.pollOperation).to.not.have.been.called;
});
});

describe("ContainerRegistryCleaner", () => {
Expand Down