Skip to content

Commit

Permalink
Add encoding to AR package names (firebase#3918)
Browse files Browse the repository at this point in the history
* Add encoding to AR package names

* Run formatter

* Add changelog
  • Loading branch information
inlined authored and devpeerapong committed Dec 14, 2021
1 parent f030d6a commit 6e01c46
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1 +1,2 @@
- Corrects a bug where containers in Artifact Registry would not be deleted if a function has an upper case character in its name
- Fixes issue where providing the `--project` flag during `init` would not be recognized with a default project already set. (#3870)
13 changes: 12 additions & 1 deletion src/deploy/functions/containerCleaner.ts
Expand Up @@ -102,7 +102,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 @@ -241,6 +241,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

0 comments on commit 6e01c46

Please sign in to comment.