Skip to content

Commit 93c733e

Browse files
committed
containers: update push to resolve the correct uri
Before it was possible to get incorrect/confusing uris for images pushed with containers push. Example: containers push registry.cloudflare.com/image:tag would become registry.cloudflare.com/<accountid>/registry.cloudflare.com/image:tag. This change modifies resolveImageName to behave like: image:tag -> prepend registry.cloudflare.com/accountid/ registry.cloudflare.com/image:tag -> registry.cloudlfare.com/accountid/image:tag registry.cloudflare.com/accountid/image:tag -> no change anyother-registry.com/anything -> no change
1 parent 0837a8d commit 93c733e

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed

.changeset/common-beers-push.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@cloudflare/containers-shared": minor
3+
"wrangler": patch
4+
---
5+
6+
Handle more cases for correctly resolving the full uri for an image when using containers push.

packages/containers-shared/src/images.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
isCloudflareRegistryLink,
55
} from "./knobs";
66
import { dockerLoginManagedRegistry } from "./login";
7+
import { getCloudflareRegistryWithAccountNamespace } from "./registry";
78
import {
89
checkExposedPorts,
910
cleanupDuplicateImageTags,
@@ -124,24 +125,29 @@ export async function prepareContainerImagesForDev(args: {
124125
/**
125126
* Resolve an image name to the full unambiguous name.
126127
*
127-
* For now, this only converts images stored in the managed registry to contain
128-
* the user's account ID in the path.
128+
* image:tag -> prepend registry.cloudflare.com/accountid/
129+
* registry.cloudflare.com/image:tag -> registry.cloudlfare.com/accountid/image:tag
130+
* registry.cloudflare.com/accountid/image:tag -> no change
131+
* anyother-registry.com/anything -> no change
129132
*/
130133
export function resolveImageName(accountId: string, image: string): string {
131134
let url: URL;
132135
try {
133136
url = new URL(`http://${image}`);
134137
} catch {
135-
return image;
138+
// not a valid url so assume it is in the format image:tag and pre-pend the registry
139+
return getCloudflareRegistryWithAccountNamespace(accountId, image);
136140
}
137-
141+
// hostname not the managed registry, passthrough
138142
if (url.hostname !== getCloudflareContainerRegistry()) {
139143
return image;
140144
}
141145

146+
// is managed registry and has the account id, passthrough
142147
if (url.pathname.startsWith(`/${accountId}`)) {
143148
return image;
144149
}
145150

151+
// is managed registry and doesn't have the account id,add it to the path
146152
return `${url.hostname}/${accountId}${url.pathname}`;
147153
}

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,66 @@ describe("containers push", () => {
7979
"Unsupported platform"
8080
);
8181
});
82+
83+
it("should tag image with the correct uri if given an <image>:<tag> argument", async () => {
84+
setIsTTY(false);
85+
setWranglerConfig({});
86+
vi.mocked(dockerImageInspect).mockResolvedValue("linux/amd64");
87+
expect(std.err).toMatchInlineSnapshot(`""`);
88+
89+
await runWrangler("containers push test-app:tag");
90+
91+
expect(runDockerCmd).toHaveBeenCalledTimes(2);
92+
expect(runDockerCmd).toHaveBeenNthCalledWith(1, "docker", [
93+
"tag",
94+
`test-app:tag`,
95+
`${getCloudflareContainerRegistry()}/some-account-id/test-app:tag`,
96+
]);
97+
expect(runDockerCmd).toHaveBeenNthCalledWith(2, "docker", [
98+
"push",
99+
`${getCloudflareContainerRegistry()}/some-account-id/test-app:tag`,
100+
]);
101+
});
102+
103+
it("should tag image with the correct uri if given an registry.cloudflare.com/<image>:<tag> argument", async () => {
104+
setIsTTY(false);
105+
setWranglerConfig({});
106+
vi.mocked(dockerImageInspect).mockResolvedValue("linux/amd64");
107+
expect(std.err).toMatchInlineSnapshot(`""`);
108+
109+
await runWrangler("containers push registry.cloudflare.com/test-app:tag");
110+
111+
expect(runDockerCmd).toHaveBeenCalledTimes(2);
112+
expect(runDockerCmd).toHaveBeenNthCalledWith(1, "docker", [
113+
"tag",
114+
`registry.cloudflare.com/test-app:tag`,
115+
`${getCloudflareContainerRegistry()}/some-account-id/test-app:tag`,
116+
]);
117+
expect(runDockerCmd).toHaveBeenNthCalledWith(2, "docker", [
118+
"push",
119+
`${getCloudflareContainerRegistry()}/some-account-id/test-app:tag`,
120+
]);
121+
});
122+
123+
it("should tag image with the correct uri if given an registry.cloudflare.com/some-account-id/<image>:<tag> argument", async () => {
124+
setIsTTY(false);
125+
setWranglerConfig({});
126+
vi.mocked(dockerImageInspect).mockResolvedValue("linux/amd64");
127+
expect(std.err).toMatchInlineSnapshot(`""`);
128+
129+
await runWrangler(
130+
"containers push registry.cloudflare.com/some-account-id/test-app:tag"
131+
);
132+
133+
expect(runDockerCmd).toHaveBeenCalledTimes(2);
134+
expect(runDockerCmd).toHaveBeenNthCalledWith(1, "docker", [
135+
"tag",
136+
`registry.cloudflare.com/some-account-id/test-app:tag`,
137+
`${getCloudflareContainerRegistry()}/some-account-id/test-app:tag`,
138+
]);
139+
expect(runDockerCmd).toHaveBeenNthCalledWith(2, "docker", [
140+
"push",
141+
`${getCloudflareContainerRegistry()}/some-account-id/test-app:tag`,
142+
]);
143+
});
82144
});

packages/wrangler/src/cloudchamber/build.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,9 @@ export async function pushCommand(
280280
) {
281281
try {
282282
await dockerLoginManagedRegistry(args.pathToDocker);
283+
283284
const accountId = await getAccountId(config);
284-
const newTag = getCloudflareRegistryWithAccountNamespace(
285-
accountId,
286-
args.TAG
287-
);
285+
const newTag = resolveImageName(accountId, args.TAG);
288286
const dockerPath = args.pathToDocker ?? getDockerPath();
289287
await checkImagePlatform(dockerPath, args.TAG);
290288
await runDockerCmd(dockerPath, ["tag", args.TAG, newTag]);

0 commit comments

Comments
 (0)