Skip to content

Commit f568c9a

Browse files
committed
fix up loop in build command and config handling in ensureDiskSize
1 parent d98b4a6 commit f568c9a

File tree

3 files changed

+28
-81
lines changed

3 files changed

+28
-81
lines changed

packages/wrangler/src/__tests__/cloudchamber/build.test.ts

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
runDockerCmd,
88
} from "@cloudflare/containers-shared";
99
import { ensureDiskLimits } from "../../cloudchamber/build";
10-
import { resolveAppDiskSize } from "../../cloudchamber/common";
11-
import { type ContainerApp } from "../../config/environment";
1210
import { UserError } from "../../errors";
1311
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
1412
import { runInTempDir } from "../helpers/run-in-tmp";
@@ -17,12 +15,7 @@ import { mockAccountV4 as mockAccount } from "./utils";
1715
import type { CompleteAccountCustomer } from "@cloudflare/containers-shared";
1816

1917
const MiB = 1024 * 1024;
20-
const defaultConfiguration: ContainerApp = {
21-
name: "abc",
22-
class_name: "",
23-
instances: 0,
24-
image: "",
25-
};
18+
2619
vi.mock("@cloudflare/containers-shared", async (importOriginal) => {
2720
const actual = await importOriginal();
2821
return Object.assign({}, actual, {
@@ -255,13 +248,7 @@ describe("buildAndMaybePush", () => {
255248
ensureDiskLimits({
256249
requiredSize: 333 * MiB, // 333MiB
257250
account: accountBase,
258-
containerApp: {
259-
...defaultConfiguration,
260-
configuration: {
261-
image: "",
262-
disk: { size: "3GB" }, // This exceeds the account limit of 2GB
263-
},
264-
},
251+
configDiskSize: 3000 * MiB, // This exceeds the account limit of 2GB
265252
})
266253
).rejects.toThrow("Exceeded account limits");
267254
});
@@ -271,7 +258,7 @@ describe("buildAndMaybePush", () => {
271258
ensureDiskLimits({
272259
requiredSize: 3000 * MiB, // 3GiB
273260
account: accountBase,
274-
containerApp: undefined,
261+
configDiskSize: undefined,
275262
})
276263
).rejects.toThrow("Image too large");
277264
});
@@ -280,32 +267,10 @@ describe("buildAndMaybePush", () => {
280267
const result = await ensureDiskLimits({
281268
requiredSize: 256 * MiB, // 256MiB
282269
account: accountBase,
283-
containerApp: undefined,
270+
configDiskSize: undefined,
284271
});
285272

286273
expect(result).toEqual(undefined);
287274
});
288275
});
289-
290-
describe("resolveAppDiskSize", () => {
291-
it("should return parsed app disk size", () => {
292-
const result = resolveAppDiskSize({
293-
...defaultConfiguration,
294-
configuration: { image: "", disk: { size: "500MB" } },
295-
});
296-
expect(result).toBeCloseTo(500 * 1000 * 1000, -5);
297-
});
298-
299-
it("should return default size when disk size not set", () => {
300-
const result = resolveAppDiskSize({
301-
...defaultConfiguration,
302-
configuration: { image: "" },
303-
});
304-
expect(result).toBeCloseTo(2 * 1000 * 1000 * 1000, -5);
305-
});
306-
307-
it("should return undefined if app is not passed", () => {
308-
expect(resolveAppDiskSize(undefined)).toBeUndefined();
309-
});
310-
});
311276
});

packages/wrangler/src/cloudchamber/build.ts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import {
1818
import { UserError } from "../errors";
1919
import { logger } from "../logger";
2020
import { getAccountId } from "../user";
21-
import { resolveAppDiskSize } from "./common";
2221
import { loadAccount } from "./locations";
2322
import type { Config } from "../config";
24-
import type { ContainerApp } from "../config/environment";
2523
import type {
2624
CommonYargsArgv,
2725
StrictYargsOptionsToInterface,
@@ -80,7 +78,7 @@ export async function buildAndMaybePush(
8078
args: BuildArgs,
8179
pathToDocker: string,
8280
push: boolean,
83-
containerConfig?: ContainerApp
81+
configDiskSize: number | undefined
8482
): Promise<{ image: string; pushed: boolean }> {
8583
try {
8684
const imageTag = `${getCloudflareContainerRegistry()}/${args.tag}`;
@@ -122,7 +120,7 @@ export async function buildAndMaybePush(
122120
await ensureDiskLimits({
123121
requiredSize,
124122
account,
125-
containerApp: containerConfig,
123+
configDiskSize,
126124
});
127125

128126
await dockerLoginManagedRegistry(pathToDocker);
@@ -214,32 +212,31 @@ export async function buildAndMaybePush(
214212
}
215213

216214
export async function buildCommand(
217-
args: StrictYargsOptionsToInterface<typeof buildYargs>,
218-
config: Config
215+
args: StrictYargsOptionsToInterface<typeof buildYargs>
219216
) {
220217
// TODO: merge args with Wrangler config if available
221218
if (existsSync(args.PATH) && !isDir(args.PATH)) {
222219
throw new UserError(
223220
`${args.PATH} is not a directory. Please specify a valid directory path.`
224221
);
225222
}
226-
// if containers are not defined, the build should still work.
227-
const containers = config.containers ?? [undefined];
223+
228224
const pathToDockerfile = join(args.PATH, "Dockerfile");
229-
for (const container of containers) {
230-
await buildAndMaybePush(
231-
{
232-
tag: args.tag,
233-
pathToDockerfile,
234-
buildContext: args.PATH,
235-
platform: args.platform,
236-
// no option to add env vars at build time...?
237-
},
238-
getDockerPath() ?? args.pathToDocker,
239-
args.push,
240-
container
241-
);
242-
}
225+
226+
await buildAndMaybePush(
227+
{
228+
tag: args.tag,
229+
pathToDockerfile,
230+
buildContext: args.PATH,
231+
platform: args.platform,
232+
// no option to add env vars at build time...?
233+
},
234+
getDockerPath() ?? args.pathToDocker,
235+
args.push,
236+
// this means we wont be able to read the disk size from the config, but that option is deprecated anyway at least for containers.
237+
// and this never actually worked for cloudchamber as this command was previously reading it from config.containers not config.cloudchamber
238+
undefined
239+
);
243240
}
244241

245242
export async function pushCommand(
@@ -269,23 +266,22 @@ export async function pushCommand(
269266
export async function ensureDiskLimits(options: {
270267
requiredSize: number;
271268
account: CompleteAccountCustomer;
272-
containerApp: ContainerApp | undefined;
269+
configDiskSize: number | undefined;
273270
}): Promise<void> {
274271
const MB = 1000 * 1000;
275272
const MiB = 1024 * 1024;
276-
const appDiskSize = resolveAppDiskSize(options.containerApp);
277273
const accountDiskSize =
278274
(options.account.limits.disk_mb_per_deployment ?? 2000) * MB;
279275
// if appDiskSize is defined and configured to be more than the accountDiskSize, error
280-
if (appDiskSize && appDiskSize > accountDiskSize) {
276+
if (options.configDiskSize && options.configDiskSize > accountDiskSize) {
281277
throw new UserError(
282-
`Exceeded account limits: Your container is configured to use a disk size of ${appDiskSize / MB} MB. However, that exceeds the account limit of ${accountDiskSize / MB}`
278+
`Exceeded account limits: Your container is configured to use a disk size of ${options.configDiskSize / MB} MB. However, that exceeds the account limit of ${accountDiskSize / MB}`
283279
);
284280
}
285-
const maxAllowedImageSizeBytes = appDiskSize ?? accountDiskSize;
281+
const maxAllowedImageSizeBytes = options.configDiskSize ?? accountDiskSize;
286282

287283
logger.debug(
288-
`Disk size limits when building the container: appDiskSize:${appDiskSize}, accountDiskSize:${accountDiskSize}, maxAllowedImageSizeBytes=${maxAllowedImageSizeBytes}(${maxAllowedImageSizeBytes / MB} MB), requiredSized=${options.requiredSize}(${Math.ceil(options.requiredSize / MiB)}MiB)`
284+
`Disk size limits when building the container: appDiskSize:${options.configDiskSize}, accountDiskSize:${accountDiskSize}, maxAllowedImageSizeBytes=${maxAllowedImageSizeBytes}(${maxAllowedImageSizeBytes / MB} MB), requiredSized=${options.requiredSize}(${Math.ceil(options.requiredSize / MiB)}MiB)`
289285
);
290286
if (maxAllowedImageSizeBytes < options.requiredSize) {
291287
throw new UserError(

packages/wrangler/src/cloudchamber/common.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,20 +614,6 @@ export function resolveMemory(
614614
return undefined;
615615
}
616616

617-
// Return the amount of disk size in (MB) for an application, falls back to the account limits if the app config doesn't exist
618-
// sometimes the user wants to just build a container here, we should allow checking those based on the account limits if
619-
// app.configuration is not set
620-
// ordering: app.configuration.disk.size -> account.limits.disk_mb_per_deployment -> default fallback to 2GB in bytes
621-
export function resolveAppDiskSize(
622-
app: ContainerApp | undefined
623-
): number | undefined {
624-
if (app === undefined) {
625-
return undefined;
626-
}
627-
const disk = app.configuration?.disk?.size ?? "2GB";
628-
return Math.round(parseByteSize(disk));
629-
}
630-
631617
// Checks that instance type is one of 'dev', 'basic', or 'standard' and that it is not being set alongside memory or vcpu.
632618
// Returns the instance type to use if correctly set.
633619
export function checkInstanceType(

0 commit comments

Comments
 (0)