Skip to content

Commit fd9dff8

Browse files
authored
wrangler deploy: Add containers configuration to PUT script (#8477)
Right now, container configuration only works on the PUT script path, but this will be fixed in further API changes.
1 parent 14680b9 commit fd9dff8

File tree

13 files changed

+172
-80
lines changed

13 files changed

+172
-80
lines changed

.changeset/common-paws-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
wrangler deploy includes container configuration when uploading the script

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function writeAppConfiguration(...app: ContainerApp[]) {
2222
"./wrangler.toml",
2323
TOML.stringify({
2424
name: "my-container",
25-
containers: { app },
25+
containers: app,
2626
}),
2727

2828
"utf-8"
@@ -103,6 +103,7 @@ describe("cloudchamber apply", () => {
103103
writeAppConfiguration({
104104
name: "my-container-app",
105105
instances: 3,
106+
class_name: "DurableObjectClass",
106107
configuration: {
107108
image: "./Dockerfile",
108109
},
@@ -122,15 +123,15 @@ describe("cloudchamber apply", () => {
122123
123124
├ NEW my-container-app
124125
125-
│ [[containers.app]]
126+
│ [[containers]]
126127
│ name = \\"my-container-app\\"
127128
│ instances = 3
128129
│ scheduling_policy = \\"regional\\"
129130
130-
│ [containers.app.configuration]
131+
│ [containers.configuration]
131132
│ image = \\"./Dockerfile\\"
132133
133-
│ [containers.app.constraints]
134+
│ [containers.constraints]
134135
│ tier = 2
135136
136137
├ Do you want to apply these changes?
@@ -150,6 +151,7 @@ describe("cloudchamber apply", () => {
150151
setIsTTY(false);
151152
writeAppConfiguration({
152153
name: "my-container-app",
154+
class_name: "DurableObjectClass",
153155
instances: 4,
154156
configuration: {
155157
image: "./Dockerfile",
@@ -184,12 +186,12 @@ describe("cloudchamber apply", () => {
184186
185187
├ EDIT my-container-app
186188
187-
│ [[containers.app]]
189+
│ [[containers]]
188190
│ - instances = 3
189191
│ + instances = 4
190192
│ name = \\"my-container-app\\"
191193
192-
│ [containers.app.constraints]
194+
│ [containers.constraints]
193195
│ - tier = 3
194196
│ + tier = 2
195197
@@ -216,13 +218,15 @@ describe("cloudchamber apply", () => {
216218
{
217219
name: "my-container-app",
218220
instances: 4,
221+
class_name: "DurableObjectClass",
219222
configuration: {
220223
image: "./Dockerfile",
221224
},
222225
},
223226
{
224227
name: "my-container-app-2",
225228
instances: 1,
229+
class_name: "DurableObjectClass2",
226230
configuration: {
227231
image: "other-app/Dockerfile",
228232
},
@@ -256,22 +260,22 @@ describe("cloudchamber apply", () => {
256260
257261
├ EDIT my-container-app
258262
259-
│ [[containers.app]]
263+
│ [[containers]]
260264
│ - instances = 3
261265
│ + instances = 4
262266
│ name = \\"my-container-app\\"
263267
264268
├ NEW my-container-app-2
265269
266-
│ [[containers.app]]
270+
│ [[containers]]
267271
│ name = \\"my-container-app-2\\"
268272
│ instances = 1
269273
│ scheduling_policy = \\"regional\\"
270274
271-
│ [containers.app.configuration]
275+
│ [containers.configuration]
272276
│ image = \\"other-app/Dockerfile\\"
273277
274-
│ [containers.app.constraints]
278+
│ [containers.constraints]
275279
│ tier = 1
276280
277281
├ Do you want to apply these changes?
@@ -296,6 +300,7 @@ describe("cloudchamber apply", () => {
296300
writeAppConfiguration({
297301
name: "my-container-app",
298302
instances: 4,
303+
class_name: "DurableObjectClass",
299304
configuration: {
300305
image: "./Dockerfile",
301306
labels: [
@@ -380,24 +385,24 @@ describe("cloudchamber apply", () => {
380385
381386
├ EDIT my-container-app
382387
383-
│ [[containers.app]]
388+
│ [[containers]]
384389
│ - instances = 3
385390
│ + instances = 4
386391
│ name = \\"my-container-app\\"
387392
388-
│ [[containers.app.configuration.labels]]
393+
│ [[containers.configuration.labels]]
389394
│ + name = \\"name-1\\"
390395
│ + value = \\"value-1\\"
391396
392-
│ + [[containers.app.configuration.labels]]
397+
│ + [[containers.configuration.labels]]
393398
│ name = \\"name-2\\"
394399
395-
│ [[containers.app.configuration.secrets]]
400+
│ [[containers.configuration.secrets]]
396401
│ - name = \\"MY_SECRET_1\\"
397402
│ - secret = \\"SECRET_NAME_1\\"
398403
│ - type = \\"env\\"
399404
400-
│ - [[containers.app.configuration.secrets]]
405+
│ - [[containers.configuration.secrets]]
401406
│ name = \\"MY_SECRET_2\\"
402407
403408
├ Do you want to apply these changes?
@@ -417,6 +422,7 @@ describe("cloudchamber apply", () => {
417422
test("can apply an application, and there is no changes", async () => {
418423
setIsTTY(false);
419424
writeAppConfiguration({
425+
class_name: "DurableObjectClass",
420426
name: "my-container-app",
421427
instances: 3,
422428
configuration: {
@@ -516,6 +522,7 @@ describe("cloudchamber apply", () => {
516522
const app = {
517523
name: "my-container-app",
518524
instances: 3,
525+
class_name: "DurableObjectClass",
519526
configuration: {
520527
image: "./Dockerfile",
521528
labels: [
@@ -554,6 +561,7 @@ describe("cloudchamber apply", () => {
554561
name: "my-container-app",
555562
instances: 3,
556563
created_at: new Date().toString(),
564+
class_name: "DurableObjectClass",
557565
account_id: "1",
558566
scheduling_policy: SchedulingPolicy.REGIONAL,
559567
configuration: {

packages/wrangler/src/__tests__/config/configuration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe("normalizeAndValidateConfig()", () => {
7878
upstream_protocol: "http",
7979
host: undefined,
8080
},
81-
containers: { app: [] },
81+
containers: undefined,
8282
cloudchamber: {},
8383
durable_objects: {
8484
bindings: [],

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8552,6 +8552,66 @@ addEventListener('fetch', event => {});`
85528552
expect(std.warn).toMatchInlineSnapshot(`""`);
85538553
});
85548554

8555+
it("should support durable object bindings to SQLite classes with containers", async () => {
8556+
writeWranglerConfig({
8557+
durable_objects: {
8558+
bindings: [
8559+
{
8560+
name: "EXAMPLE_DO_BINDING",
8561+
class_name: "ExampleDurableObject",
8562+
},
8563+
],
8564+
},
8565+
containers: [
8566+
{
8567+
name: "my-container",
8568+
instances: 10,
8569+
class_name: "ExampleDurableObject",
8570+
configuration: {
8571+
image: "docker.io/hello:world",
8572+
},
8573+
},
8574+
],
8575+
migrations: [
8576+
{ tag: "v1", new_sqlite_classes: ["ExampleDurableObject"] },
8577+
],
8578+
});
8579+
fs.writeFileSync(
8580+
"index.js",
8581+
`export class ExampleDurableObject {}; export default{};`
8582+
);
8583+
mockSubDomainRequest();
8584+
mockLegacyScriptData({
8585+
scripts: [{ id: "test-name", migration_tag: "v1" }],
8586+
});
8587+
mockUploadWorkerRequest({
8588+
expectedBindings: [
8589+
{
8590+
class_name: "ExampleDurableObject",
8591+
name: "EXAMPLE_DO_BINDING",
8592+
type: "durable_object_namespace",
8593+
},
8594+
],
8595+
useOldUploadApi: true,
8596+
expectedContainers: [{ class_name: "ExampleDurableObject" }],
8597+
});
8598+
8599+
await runWrangler("deploy index.js");
8600+
expect(std.out).toMatchInlineSnapshot(`
8601+
"Total Upload: xx KiB / gzip: xx KiB
8602+
Worker Startup Time: 100 ms
8603+
Your worker has access to the following bindings:
8604+
- Durable Objects:
8605+
- EXAMPLE_DO_BINDING: ExampleDurableObject
8606+
Uploaded test-name (TIMINGS)
8607+
Deployed test-name triggers (TIMINGS)
8608+
https://test-name.test-sub-domain.workers.dev
8609+
Current Version ID: Galaxy-Class"
8610+
`);
8611+
expect(std.err).toMatchInlineSnapshot(`""`);
8612+
expect(std.warn).toMatchInlineSnapshot(`""`);
8613+
});
8614+
85558615
it("should support durable objects and D1", async () => {
85568616
writeWranglerConfig({
85578617
main: "index.js",

packages/wrangler/src/__tests__/helpers/mock-upload-worker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function mockUploadWorkerRequest(
3939
useOldUploadApi?: boolean;
4040
expectedObservability?: CfWorkerInit["observability"];
4141
expectedSettingsPatch?: Partial<NonVersionedScriptSettings>;
42+
expectedContainers?: { class_name: string }[];
4243
} = {}
4344
) {
4445
const expectedScriptName = (options.expectedScriptName ??= "test-name");
@@ -117,6 +118,10 @@ export function mockUploadWorkerRequest(
117118
if ("expectedObservability" in options) {
118119
expect(metadata.observability).toEqual(expectedObservability);
119120
}
121+
if ("expectedContainers" in options) {
122+
expect(metadata.containers).toEqual(expectedContainers);
123+
}
124+
120125
if (expectedUnsafeMetaData !== undefined) {
121126
Object.keys(expectedUnsafeMetaData).forEach((key) => {
122127
expect(metadata[key]).toEqual(expectedUnsafeMetaData[key]);
@@ -172,6 +177,7 @@ export function mockUploadWorkerRequest(
172177
expectedUnsafeMetaData,
173178
expectedCapnpSchema,
174179
expectedLimits,
180+
expectedContainers,
175181
keepVars,
176182
keepSecrets,
177183
expectedDispatchNamespace,

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const bindingsConfigMock: Omit<
129129
],
130130
},
131131
workflows: [],
132-
containers: { app: [] },
132+
containers: undefined,
133133
r2_buckets: [
134134
{
135135
binding: "R2_BUCKET_BINDING",

packages/wrangler/src/cloudchamber/apply.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function containerAppToCreateApplication(
7878
): CreateApplicationRequest {
7979
const configuration =
8080
containerApp.configuration as UserDeploymentConfiguration;
81-
return {
81+
const app = {
8282
...containerApp,
8383
configuration,
8484
scheduling_policy:
@@ -95,6 +95,12 @@ function containerAppToCreateApplication(
9595
),
9696
},
9797
};
98+
99+
// delete the fields that should not be sent to API
100+
delete (app as Record<string, unknown>)["class_name"];
101+
delete (app as Record<string, unknown>)["image"];
102+
103+
return app;
98104
}
99105

100106
function isNumber(c: string | number) {
@@ -279,24 +285,24 @@ export async function applyCommand(
279285
"deploy changes to your application"
280286
);
281287

282-
if (config.containers.app.length === 0) {
288+
config.containers ??= [];
289+
290+
if (config.containers.length === 0) {
283291
endSection(
284292
"You don't have any container applications defined in your wrangler.toml",
285293
"You can set the following configuration in your wrangler.toml"
286294
);
287295
const configuration = {
288-
configuration: {
289-
image: "docker.io/cloudflare/hello-world:1.0",
290-
},
296+
image: "docker.io/cloudflare/hello-world:1.0",
291297
instances: 2,
292298
name: config.name ?? "my-containers-application",
293299
};
294300
const endConfig: JsonMap =
295301
args.env !== undefined
296302
? {
297-
env: { [args.env]: { containers: { app: [configuration] } } },
303+
env: { [args.env]: { containers: [configuration] } },
298304
}
299-
: { containers: { app: [configuration] } };
305+
: { containers: [configuration] };
300306
formatConfigSnippet(endConfig, config.configPath)
301307
.split("\n")
302308
.forEach((el) => {
@@ -331,11 +337,12 @@ export async function applyCommand(
331337

332338
log(dim("Container application changes\n"));
333339

334-
for (const appConfigNoDefaults of config.containers.app) {
340+
for (const appConfigNoDefaults of config.containers) {
335341
const appConfig = containerAppToCreateApplication(
336342
appConfigNoDefaults,
337343
args.skipDefaults
338344
);
345+
339346
const application = applicationByNames[appConfig.name];
340347
if (application !== undefined && application !== null) {
341348
// we need to sort the objects (by key) because the diff algorithm works with
@@ -345,18 +352,16 @@ export async function applyCommand(
345352
);
346353

347354
const prev = formatConfigSnippet(
348-
{ containers: { app: [prevApp as ContainerApp] } },
355+
{ containers: [prevApp as ContainerApp] },
349356
config.configPath
350357
);
351358
const now = formatConfigSnippet(
352359
{
353-
containers: {
354-
app: [
355-
sortObjectRecursive<CreateApplicationRequest>(
356-
appConfig
357-
) as ContainerApp,
358-
],
359-
},
360+
containers: [
361+
sortObjectRecursive<CreateApplicationRequest>(
362+
appConfig
363+
) as ContainerApp,
364+
],
360365
},
361366
config.configPath
362367
);
@@ -458,7 +463,7 @@ export async function applyCommand(
458463
updateStatus(bold.underline(green.underline("NEW")) + ` ${appConfig.name}`);
459464

460465
const s = formatConfigSnippet(
461-
{ containers: { app: [appConfig as ContainerApp] } },
466+
{ containers: [appConfig as ContainerApp] },
462467
config.configPath
463468
);
464469

@@ -469,10 +474,12 @@ export async function applyCommand(
469474
printLine(el, " ");
470475
});
471476

477+
const configToPush = { ...appConfig };
478+
472479
// add to the actions array to create the app later
473480
actions.push({
474481
action: "create",
475-
application: appConfig,
482+
application: configToPush,
476483
});
477484
}
478485

0 commit comments

Comments
 (0)