Skip to content

Commit cf16deb

Browse files
authored
CC-6280: Correctly handle image names that contain a slash (#11007)
* CC-6280: Correctly handle image names that contain a slash * Fix formatting
1 parent 6643bd4 commit cf16deb

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

.changeset/five-worms-cut.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/containers-shared": patch
3+
---
4+
5+
Correctly handle image names that contain a slash

packages/containers-shared/src/images.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,31 @@ export async function prepareContainerImagesForDev(args: {
132132
* anyother-registry.com/anything -> no change
133133
*/
134134
export function resolveImageName(accountId: string, image: string): string {
135-
let url: URL;
135+
let url: URL | undefined;
136136
try {
137137
url = new URL(`http://${image}`);
138138
} catch {
139-
// not a valid url so assume it is in the format image:tag and pre-pend the registry
139+
// Invalid URL
140+
}
141+
142+
if (
143+
url === undefined ||
144+
(!url.host.match(/[:.]/) && url.hostname !== "localhost")
145+
) {
146+
// Not a valid URL so assume it is in the format image:tag and prepend the registry
140147
return getCloudflareRegistryWithAccountNamespace(accountId, image);
141148
}
142-
// hostname not the managed registry, passthrough
149+
143150
if (url.hostname !== getCloudflareContainerRegistry()) {
151+
// hostname not the managed registry, passthrough
144152
return image;
145153
}
146-
// is managed registry and has the account id, passthrough
154+
147155
if (url.pathname.startsWith(`/${accountId}`)) {
156+
// is managed registry and has the account id, passthrough
148157
return image;
149158
}
159+
150160
// check if already looks like it has an account id (32 char hex string)
151161
const accountIdPattern = /^\/([a-f0-9]{32})\//;
152162
const match = accountIdPattern.exec(url.pathname);

packages/wrangler/src/__tests__/containers/push.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ describe("containers push", () => {
100100
]);
101101
});
102102

103+
it("should tag image with the correct uri if given an <namespace>/<image>:<tag> argument", async () => {
104+
await runWrangler("containers push test-namespace/app:tag");
105+
106+
expect(runDockerCmd).toHaveBeenCalledTimes(2);
107+
expect(runDockerCmd).toHaveBeenNthCalledWith(1, "docker", [
108+
"tag",
109+
`test-namespace/app:tag`,
110+
`${getCloudflareContainerRegistry()}/some-account-id/test-namespace/app:tag`,
111+
]);
112+
expect(runDockerCmd).toHaveBeenNthCalledWith(2, "docker", [
113+
"push",
114+
`${getCloudflareContainerRegistry()}/some-account-id/test-namespace/app:tag`,
115+
]);
116+
});
117+
103118
it("should tag image with the correct uri if given an registry.cloudflare.com/<image>:<tag> argument", async () => {
104119
setIsTTY(false);
105120
setWranglerConfig({});

0 commit comments

Comments
 (0)