-
Notifications
You must be signed in to change notification settings - Fork 903
/
fabricator.ts
544 lines (490 loc) · 19.9 KB
/
fabricator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
import * as clc from "cli-color";
import { Executor } from "./executor";
import { FirebaseError } from "../../../error";
import { SourceTokenScraper } from "./sourceTokenScraper";
import { Timer } from "./timer";
import { assertExhaustive } from "../../../functional";
import { getHumanFriendlyRuntimeName } from "../runtimes";
import { functionsOrigin, functionsV2Origin } from "../../../api";
import { logger } from "../../../logger";
import * as backend from "../backend";
import * as cloudtasks from "../../../gcp/cloudtasks";
import * as deploymentTool from "../../../deploymentTool";
import * as gcf from "../../../gcp/cloudfunctions";
import * as gcfV2 from "../../../gcp/cloudfunctionsv2";
import * as helper from "../functionsDeployHelper";
import * as planner from "./planner";
import * as poller from "../../../operation-poller";
import * as pubsub from "../../../gcp/pubsub";
import * as reporter from "./reporter";
import * as run from "../../../gcp/run";
import * as scheduler from "../../../gcp/cloudscheduler";
import * as utils from "../../../utils";
// TODO: Tune this for better performance.
const gcfV1PollerOptions = {
apiOrigin: functionsOrigin,
apiVersion: gcf.API_VERSION,
masterTimeout: 25 * 60 * 1_000, // 25 minutes is the maximum build time for a function
maxBackoff: 10_000,
};
const gcfV2PollerOptions = {
apiOrigin: functionsV2Origin,
apiVersion: gcfV2.API_VERSION,
masterTimeout: 25 * 60 * 1_000, // 25 minutes is the maximum build time for a function
maxBackoff: 10_000,
};
const DEFAULT_GCFV2_CONCURRENCY = 80;
export interface FabricatorArgs {
executor: Executor;
functionExecutor: Executor;
appEngineLocation: string;
// Required if creating or updating any GCFv1 functions
sourceUrl?: string;
// Required if creating or updating any GCFv2 functions
storage?: Record<string, gcfV2.StorageSource>;
}
const rethrowAs = <T>(endpoint: backend.Endpoint, op: reporter.OperationType) => (
err: unknown
): T => {
throw new reporter.DeploymentError(endpoint, op, err);
};
/** Fabricators make a customer's backend match a spec by applying a plan. */
export class Fabricator {
executor: Executor;
functionExecutor: Executor;
sourceUrl: string | undefined;
storage: Record<string, gcfV2.StorageSource> | undefined;
appEngineLocation: string;
constructor(args: FabricatorArgs) {
this.executor = args.executor;
this.functionExecutor = args.functionExecutor;
this.sourceUrl = args.sourceUrl;
this.storage = args.storage;
this.appEngineLocation = args.appEngineLocation;
}
async applyPlan(plan: planner.DeploymentPlan): Promise<reporter.Summary> {
const timer = new Timer();
const summary: reporter.Summary = {
totalTime: 0,
results: [],
};
const deployRegions = Object.values(plan).map(
async (changes): Promise<void> => {
const results = await this.applyRegionalChanges(changes);
summary.results.push(...results);
return;
}
);
const promiseResults = await utils.allSettled(deployRegions);
const errs = promiseResults
.filter((r) => r.status === "rejected")
.map((r) => (r as utils.PromiseRejectedResult).reason);
if (errs.length) {
logger.debug(
"Fabricator.applyRegionalChanges returned an unhandled exception. This should never happen",
JSON.stringify(errs, null, 2)
);
}
summary.totalTime = timer.stop();
return summary;
}
async applyRegionalChanges(
changes: planner.RegionalChanges
): Promise<Array<reporter.DeployResult>> {
const deployResults: reporter.DeployResult[] = [];
const handle = async (
op: reporter.OperationType,
endpoint: backend.Endpoint,
fn: () => Promise<void>
): Promise<void> => {
const timer = new Timer();
const result: Partial<reporter.DeployResult> = { endpoint };
try {
await fn();
this.logOpSuccess(op, endpoint);
} catch (err) {
result.error = err as Error;
}
result.durationMs = timer.stop();
deployResults.push(result as reporter.DeployResult);
};
const upserts: Array<Promise<void>> = [];
const scraper = new SourceTokenScraper();
for (const endpoint of changes.endpointsToCreate) {
this.logOpStart("creating", endpoint);
upserts.push(handle("create", endpoint, () => this.createEndpoint(endpoint, scraper)));
}
for (const update of changes.endpointsToUpdate) {
this.logOpStart("updating", update.endpoint);
upserts.push(handle("update", update.endpoint, () => this.updateEndpoint(update, scraper)));
}
await utils.allSettled(upserts);
// Note: every promise is generated by handle which records error in results.
// We've used hasErrors as a cheater here instead of viewing the results of allSettled
if (deployResults.find((r) => r.error)) {
for (const endpoint of changes.endpointsToDelete) {
deployResults.push({
endpoint,
durationMs: 0,
error: new reporter.AbortedDeploymentError(endpoint),
});
}
return deployResults;
}
const deletes: Array<Promise<void>> = [];
for (const endpoint of changes.endpointsToDelete) {
this.logOpStart("deleting", endpoint);
deletes.push(handle("delete", endpoint, () => this.deleteEndpoint(endpoint)));
}
await utils.allSettled(deletes);
return deployResults;
}
async createEndpoint(endpoint: backend.Endpoint, scraper: SourceTokenScraper): Promise<void> {
endpoint.labels = { ...endpoint.labels, ...deploymentTool.labels() };
if (endpoint.platform === "gcfv1") {
await this.createV1Function(endpoint, scraper);
} else if (endpoint.platform === "gcfv2") {
await this.createV2Function(endpoint);
} else {
assertExhaustive(endpoint.platform);
}
await this.setTrigger(endpoint);
}
async updateEndpoint(update: planner.EndpointUpdate, scraper: SourceTokenScraper): Promise<void> {
update.endpoint.labels = { ...update.endpoint.labels, ...deploymentTool.labels() };
if (update.deleteAndRecreate) {
await this.deleteEndpoint(update.deleteAndRecreate);
await this.createEndpoint(update.endpoint, scraper);
return;
}
if (update.endpoint.platform === "gcfv1") {
await this.updateV1Function(update.endpoint, scraper);
} else if (update.endpoint.platform === "gcfv2") {
await this.updateV2Function(update.endpoint);
} else {
assertExhaustive(update.endpoint.platform);
}
await this.setTrigger(update.endpoint);
}
async deleteEndpoint(endpoint: backend.Endpoint): Promise<void> {
await this.deleteTrigger(endpoint);
if (endpoint.platform === "gcfv1") {
await this.deleteV1Function(endpoint);
} else {
await this.deleteV2Function(endpoint);
}
}
async createV1Function(endpoint: backend.Endpoint, scraper: SourceTokenScraper): Promise<void> {
if (!this.sourceUrl) {
logger.debug("Precondition failed. Cannot create a GCF function without sourceUrl");
throw new Error("Precondition failed");
}
const apiFunction = gcf.functionFromEndpoint(endpoint, this.sourceUrl);
apiFunction.sourceToken = await scraper.tokenPromise();
const resultFunction = await this.functionExecutor
.run(async () => {
const op: { name: string } = await gcf.createFunction(apiFunction);
return poller.pollOperation<gcf.CloudFunction>({
...gcfV1PollerOptions,
pollerName: `create-${endpoint.region}-${endpoint.id}`,
operationResourceName: op.name,
onPoll: scraper.poller,
});
})
.catch(rethrowAs(endpoint, "create"));
endpoint.uri = resultFunction?.httpsTrigger?.url;
if (backend.isHttpsTriggered(endpoint)) {
const invoker = endpoint.httpsTrigger.invoker || ["public"];
if (!invoker.includes("private")) {
await this.executor
.run(async () => {
await gcf.setInvokerCreate(endpoint.project, backend.functionName(endpoint), invoker);
})
.catch(rethrowAs(endpoint, "set invoker"));
}
} else if (backend.isTaskQueueTriggered(endpoint)) {
// Like HTTPS triggers, taskQueueTriggers have an invoker, but unlike HTTPS they don't default
// public.
const invoker = endpoint.taskQueueTrigger.invoker;
if (invoker && !invoker.includes("private")) {
await this.executor
.run(async () => {
await gcf.setInvokerCreate(endpoint.project, backend.functionName(endpoint), invoker);
})
.catch(rethrowAs(endpoint, "set invoker"));
}
}
}
async createV2Function(endpoint: backend.Endpoint): Promise<void> {
if (!this.storage) {
logger.debug("Precondition failed. Cannot create a GCFv2 function without storage");
throw new Error("Precondition failed");
}
const apiFunction = gcfV2.functionFromEndpoint(endpoint, this.storage[endpoint.region]);
// N.B. As of GCFv2 private preview GCF no longer creates Pub/Sub topics
// for Pub/Sub event handlers. This may change, at which point this code
// could be deleted.
const topic = apiFunction.eventTrigger?.pubsubTopic;
if (topic) {
await this.executor
.run(async () => {
try {
await pubsub.createTopic({ name: topic });
} catch (err) {
// Pub/Sub uses HTTP 409 (CONFLICT) with a status message of
// ALREADY_EXISTS if the topic already exists.
if (err.status === 409) {
return;
}
throw new FirebaseError("Unexpected error creating Pub/Sub topic", {
original: err as Error,
});
}
})
.catch(rethrowAs(endpoint, "create topic"));
}
const resultFunction = (await this.functionExecutor
.run(async () => {
const op: { name: string } = await gcfV2.createFunction(apiFunction);
return await poller.pollOperation<gcfV2.CloudFunction>({
...gcfV2PollerOptions,
pollerName: `create-${endpoint.region}-${endpoint.id}`,
operationResourceName: op.name,
});
})
.catch(rethrowAs(endpoint, "create"))) as gcfV2.CloudFunction;
endpoint.uri = resultFunction.serviceConfig.uri;
const serviceName = resultFunction.serviceConfig.service!;
if (backend.isHttpsTriggered(endpoint)) {
const invoker = endpoint.httpsTrigger.invoker || ["public"];
if (!invoker.includes("private")) {
await this.executor
.run(() => run.setInvokerCreate(endpoint.project, serviceName, invoker))
.catch(rethrowAs(endpoint, "set invoker"));
}
} else if (backend.isTaskQueueTriggered(endpoint)) {
// Like HTTPS triggers, taskQueueTriggers have an invoker, but unlike HTTPS they don't default
// public.
const invoker = endpoint.taskQueueTrigger.invoker;
if (invoker && !invoker.includes("private")) {
await this.executor
.run(async () => {
await gcf.setInvokerCreate(endpoint.project, backend.functionName(endpoint), invoker);
})
.catch(rethrowAs(endpoint, "set invoker"));
}
}
await this.setConcurrency(
endpoint,
serviceName,
endpoint.concurrency || DEFAULT_GCFV2_CONCURRENCY
);
}
async updateV1Function(endpoint: backend.Endpoint, scraper: SourceTokenScraper): Promise<void> {
if (!this.sourceUrl) {
logger.debug("Precondition failed. Cannot update a GCF function without sourceUrl");
throw new Error("Precondition failed");
}
const apiFunction = gcf.functionFromEndpoint(endpoint, this.sourceUrl);
apiFunction.sourceToken = await scraper.tokenPromise();
const resultFunction = await this.functionExecutor
.run(async () => {
const op: { name: string } = await gcf.updateFunction(apiFunction);
return await poller.pollOperation<gcf.CloudFunction>({
...gcfV1PollerOptions,
pollerName: `update-${endpoint.region}-${endpoint.id}`,
operationResourceName: op.name,
onPoll: scraper.poller,
});
})
.catch(rethrowAs(endpoint, "update"));
endpoint.uri = resultFunction?.httpsTrigger?.url;
let invoker: string[] | undefined;
if (backend.isHttpsTriggered(endpoint)) {
invoker = endpoint.httpsTrigger.invoker;
} else if (backend.isTaskQueueTriggered(endpoint)) {
invoker = endpoint.taskQueueTrigger.invoker;
}
if (invoker) {
await this.executor
.run(() => gcf.setInvokerUpdate(endpoint.project, backend.functionName(endpoint), invoker!))
.catch(rethrowAs(endpoint, "set invoker"));
}
}
async updateV2Function(endpoint: backend.Endpoint): Promise<void> {
if (!this.storage) {
logger.debug("Precondition failed. Cannot update a GCFv2 function without storage");
throw new Error("Precondition failed");
}
const apiFunction = gcfV2.functionFromEndpoint(endpoint, this.storage[endpoint.region]);
// N.B. As of GCFv2 private preview the API chokes on any update call that
// includes the pub/sub topic even if that topic is unchanged.
// We know that the user hasn't changed the topic between deploys because
// of checkForInvalidChangeOfTrigger().
if (apiFunction.eventTrigger?.pubsubTopic) {
delete apiFunction.eventTrigger.pubsubTopic;
}
const resultFunction = await this.functionExecutor
.run(async () => {
const op: { name: string } = await gcfV2.updateFunction(apiFunction);
return await poller.pollOperation<gcfV2.CloudFunction>({
...gcfV2PollerOptions,
pollerName: `update-${endpoint.region}-${endpoint.id}`,
operationResourceName: op.name,
});
})
.catch(rethrowAs(endpoint, "update"));
endpoint.uri = resultFunction.serviceConfig.uri;
const serviceName = resultFunction.serviceConfig.service!;
let invoker: string[] | undefined;
if (backend.isHttpsTriggered(endpoint)) {
invoker = endpoint.httpsTrigger.invoker;
} else if (backend.isTaskQueueTriggered(endpoint)) {
invoker = endpoint.taskQueueTrigger.invoker;
}
if (invoker) {
await this.executor
.run(() => run.setInvokerUpdate(endpoint.project, serviceName, invoker!))
.catch(rethrowAs(endpoint, "set invoker"));
}
if (endpoint.concurrency) {
await this.setConcurrency(endpoint, serviceName, endpoint.concurrency);
}
}
async deleteV1Function(endpoint: backend.Endpoint): Promise<void> {
const fnName = backend.functionName(endpoint);
await this.functionExecutor
.run(async () => {
const op: { name: string } = await gcf.deleteFunction(fnName);
const pollerOptions = {
...gcfV1PollerOptions,
pollerName: `delete-${endpoint.region}-${endpoint.id}`,
operationResourceName: op.name,
};
await poller.pollOperation<void>(pollerOptions);
})
.catch(rethrowAs(endpoint, "delete"));
}
async deleteV2Function(endpoint: backend.Endpoint): Promise<void> {
const fnName = backend.functionName(endpoint);
await this.functionExecutor
.run(async () => {
const op: { name: string } = await gcfV2.deleteFunction(fnName);
const pollerOptions = {
...gcfV2PollerOptions,
pollerName: `delete-${endpoint.region}-${endpoint.id}`,
operationResourceName: op.name,
};
await poller.pollOperation<void>(pollerOptions);
})
.catch(rethrowAs(endpoint, "delete"));
}
async setConcurrency(
endpoint: backend.Endpoint,
serviceName: string,
concurrency: number
): Promise<void> {
await this.functionExecutor
.run(async () => {
const service = await run.getService(serviceName);
if (service.spec.template.spec.containerConcurrency === concurrency) {
logger.debug("Skipping setConcurrency on", serviceName, " because it already matches");
return;
}
delete service.status;
delete (service.spec.template.metadata as any).name;
service.spec.template.spec.containerConcurrency = concurrency;
await run.replaceService(serviceName, service);
})
.catch(rethrowAs(endpoint, "set concurrency"));
}
// Set/Delete trigger is responsible for wiring up a function with any trigger not owned
// by the GCF API. This includes schedules, task queues, and blocking function triggers.
async setTrigger(endpoint: backend.Endpoint): Promise<void> {
if (backend.isScheduleTriggered(endpoint)) {
if (endpoint.platform === "gcfv1") {
await this.upsertScheduleV1(endpoint);
return;
} else if (endpoint.platform === "gcfv2") {
await this.upsertScheduleV2(endpoint);
return;
}
assertExhaustive(endpoint.platform);
} else if (backend.isTaskQueueTriggered(endpoint)) {
await this.upsertTaskQueue(endpoint);
}
}
async deleteTrigger(endpoint: backend.Endpoint): Promise<void> {
if (backend.isScheduleTriggered(endpoint)) {
if (endpoint.platform === "gcfv1") {
await this.deleteScheduleV1(endpoint);
return;
} else if (endpoint.platform === "gcfv2") {
await this.deleteScheduleV2(endpoint);
return;
}
assertExhaustive(endpoint.platform);
} else if (backend.isTaskQueueTriggered(endpoint)) {
await this.disableTaskQueue(endpoint);
}
}
async upsertScheduleV1(endpoint: backend.Endpoint & backend.ScheduleTriggered): Promise<void> {
// The Pub/Sub topic is already created
const job = scheduler.jobFromEndpoint(endpoint, this.appEngineLocation);
await this.executor
.run(() => scheduler.createOrReplaceJob(job))
.catch(rethrowAs(endpoint, "upsert schedule"));
}
upsertScheduleV2(endpoint: backend.Endpoint & backend.ScheduleTriggered): Promise<void> {
return Promise.reject(
new reporter.DeploymentError(endpoint, "upsert schedule", new Error("Not implemented"))
);
}
async upsertTaskQueue(endpoint: backend.Endpoint & backend.TaskQueueTriggered): Promise<void> {
const queue = cloudtasks.queueFromEndpoint(endpoint);
await this.executor
.run(() => cloudtasks.upsertQueue(queue))
.catch(rethrowAs(endpoint, "upsert task queue"));
// Note: should we split setTrigger into createTrigger and updateTrigger so we can avoid a
// getIamPolicy on create?
if (endpoint.taskQueueTrigger.invoker) {
await this.executor
.run(() => cloudtasks.setEnqueuer(queue.name, endpoint.taskQueueTrigger.invoker!))
.catch(rethrowAs(endpoint, "set invoker"));
}
}
async deleteScheduleV1(endpoint: backend.Endpoint & backend.ScheduleTriggered): Promise<void> {
const job = scheduler.jobFromEndpoint(endpoint, this.appEngineLocation);
await this.executor
.run(() => scheduler.deleteJob(job.name))
.catch(rethrowAs(endpoint, "delete schedule"));
await this.executor
.run(() => pubsub.deleteTopic(job.pubsubTarget!.topicName))
.catch(rethrowAs(endpoint, "delete topic"));
}
deleteScheduleV2(endpoint: backend.Endpoint & backend.ScheduleTriggered): Promise<void> {
return Promise.reject(
new reporter.DeploymentError(endpoint, "delete schedule", new Error("Not implemented"))
);
}
async disableTaskQueue(endpoint: backend.Endpoint & backend.TaskQueueTriggered): Promise<void> {
const update = {
name: cloudtasks.queueNameForEndpoint(endpoint),
state: "DISABLED" as cloudtasks.State,
};
await this.executor
.run(() => cloudtasks.updateQueue(update))
.catch(rethrowAs(endpoint, "disable task queue"));
}
logOpStart(op: string, endpoint: backend.Endpoint): void {
const runtime = getHumanFriendlyRuntimeName(endpoint.runtime);
const label = helper.getFunctionLabel(endpoint);
utils.logBullet(
`${clc.bold.cyan("functions:")} ${op} ${runtime} function ${clc.bold(label)}...`
);
}
logOpSuccess(op: string, endpoint: backend.Endpoint): void {
const label = helper.getFunctionLabel(endpoint);
utils.logSuccess(`${clc.bold.green(`functions[${label}]`)} Successful ${op} operation.`);
}
}