-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcloudfunctionsv2.ts
More file actions
766 lines (709 loc) · 25.9 KB
/
cloudfunctionsv2.ts
File metadata and controls
766 lines (709 loc) · 25.9 KB
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import { Client, ClientVerbOptions } from "../apiv2";
import { FirebaseError } from "../error";
import { functionsV2Origin } from "../api";
import { logger } from "../logger";
import { AUTH_BLOCKING_EVENTS } from "../functions/events/v1";
import { PUBSUB_PUBLISH_EVENT } from "../functions/events/v2";
import * as backend from "../deploy/functions/backend";
import * as supported from "../deploy/functions/runtimes/supported";
import * as proto from "./proto";
import * as utils from "../utils";
import * as projectConfig from "../functions/projectConfig";
import {
BLOCKING_EVENT_TO_LABEL_KEY,
BLOCKING_LABEL,
BLOCKING_LABEL_KEY_TO_EVENT,
CODEBASE_LABEL,
HASH_LABEL,
} from "../functions/constants";
import { RequireKeys } from "../metaprogramming";
import { captureRuntimeValidationError } from "./cloudfunctions";
import { mebibytes } from "./k8s";
export const API_VERSION = "v2";
// Defined by Cloud Run: https://cloud.google.com/run/docs/configuring/max-instances#setting
const DEFAULT_MAX_INSTANCE_COUNT = 100;
const client = new Client({
urlPrefix: functionsV2Origin(),
auth: true,
apiVersion: API_VERSION,
});
export type VpcConnectorEgressSettings = "PRIVATE_RANGES_ONLY" | "ALL_TRAFFIC";
export type IngressSettings = "ALLOW_ALL" | "ALLOW_INTERNAL_ONLY" | "ALLOW_INTERNAL_AND_GCLB";
export type FunctionState = "ACTIVE" | "FAILED" | "DEPLOYING" | "DELETING" | "UNKONWN";
// Values allowed for the operator field in EventFilter
export type EventFilterOperator = "match-path-pattern";
// Values allowed for the event trigger retry policy in case of a function's execution failure.
export type RetryPolicy =
| "RETRY_POLICY_UNSPECIFIED"
| "RETRY_POLICY_DO_NOT_RETRY"
| "RETRY_POLICY_RETRY";
/** Settings for building a container out of the customer source. */
export interface BuildConfig {
runtime?: supported.Runtime;
entryPoint: string;
source: Source;
sourceToken?: string;
environmentVariables: Record<string, string>;
serviceAccount?: string | null;
// Output only
build?: string;
workerPool?: string;
}
export interface StorageSource {
bucket: string;
object: string;
generation?: number;
}
export interface RepoSource {
projectId: string;
repoName: string;
// oneof revision
branchName: string;
tagName: string;
commitSha: string;
// end oneof revision
dir: string;
invertRegex: boolean;
}
export interface Source {
// oneof source
storageSource?: StorageSource;
repoSource?: RepoSource;
// end oneof source
}
export interface EventFilter {
attribute: string;
value: string;
operator?: EventFilterOperator;
}
/**
* Configurations for secret environment variables attached to a cloud functions resource.
*/
export interface SecretEnvVar {
/* Name of the environment variable. */
key: string;
/* Project identifier (or project number) of the project that contains the secret. */
projectId: string;
/* Name of the secret in secret manager. e.g. MY_SECRET, NOT projects/abc/secrets/MY_SECRET */
secret: string;
/* Version of the secret (version number or the string 'latest') */
version?: string;
}
/** The Cloud Run service that underlies a Cloud Function. */
export interface ServiceConfig {
// Output only
service?: string;
// Output only. All Cloud Run services are HTTP services. So all Cloud
// Functions will have a URI. This URI will be different from the
// cloudfunctions.net URLs.
uri?: string;
timeoutSeconds?: number | null;
availableMemory?: string | null;
availableCpu?: string | null;
environmentVariables?: Record<string, string> | null;
secretEnvironmentVariables?: SecretEnvVar[] | null;
maxInstanceCount?: number | null;
minInstanceCount?: number | null;
maxInstanceRequestConcurrency?: number | null;
vpcConnector?: string | null;
vpcConnectorEgressSettings?: VpcConnectorEgressSettings | null;
ingressSettings?: IngressSettings | null;
// The service account for default credentials. Defaults to the
// default compute account. This is different from the v1 default
// of the default GAE account.
serviceAccountEmail?: string | null;
}
export interface EventTrigger {
// Output only. The resource name of the underlying EventArc trigger.
trigger?: string;
// When unspecified will default to the region of the Cloud Function.
// single-region names must match the function name.
triggerRegion?: string;
eventType: string;
eventFilters?: EventFilter[];
pubsubTopic?: string;
// The service account that a trigger runs as. Must have the
// run.routes.invoke permission on the target service. Defaults
// to the defualt compute service account.
serviceAccountEmail?: string;
retryPolicy?: RetryPolicy;
// The name of the channel associated with the trigger in
// `projects/{project}/locations/{location}/channels/{channel}` format.
channel?: string;
}
interface CloudFunctionBase {
name: string;
description?: string;
buildConfig?: BuildConfig;
serviceConfig?: ServiceConfig;
eventTrigger?: EventTrigger;
labels?: Record<string, string> | null;
}
export type OutputCloudFunction = CloudFunctionBase & {
state: FunctionState;
updateTime: Date;
serviceConfig?: RequireKeys<ServiceConfig, "service" | "uri">;
url: string;
};
export type InputCloudFunction = CloudFunctionBase & {
buildConfig: BuildConfig;
// serviceConfig is required.
serviceConfig: ServiceConfig;
};
export interface OperationMetadata {
createTime: string;
endTime: string;
target: string;
verb: string;
statusDetail: string;
cancelRequested: boolean;
apiVersion: string;
}
export interface Operation {
name: string;
// Note: this field is always present, but not used in prod and is a PITA
// to add in tests.
metadata?: OperationMetadata;
done: boolean;
error?: { code: number; message: string; details: unknown };
response?: OutputCloudFunction;
}
// Private API interface for ListFunctionsResponse. listFunctions returns
// a CloudFunction[]
interface ListFunctionsResponse {
functions: OutputCloudFunction[];
unreachable: string[];
}
interface GenerateUploadUrlResponse {
uploadUrl: string;
storageSource: StorageSource;
}
/**
* Logs an error from a failed function deployment.
* @param func The function that was unsuccessfully deployed.
* @param type Type of deployment - create, update, or delete.
* @param err The error returned from the operation.
*/
function functionsOpLogReject(func: InputCloudFunction, type: string, err: any): void {
// Sniff for runtime validation errors and log a more user-friendly warning.
if (err?.message?.includes("Runtime validation errors")) {
const capturedMessage = captureRuntimeValidationError(err.message);
utils.logLabeledWarning("functions", capturedMessage + " for function " + func.name);
}
if (err?.message?.includes("maxScale may not exceed")) {
const maxInstances = func.serviceConfig.maxInstanceCount || DEFAULT_MAX_INSTANCE_COUNT;
utils.logLabeledWarning(
"functions",
`Your current project quotas don't allow for the current max instances setting of ${maxInstances}. ` +
"Either reduce this function's maximum instances, or request a quota increase on the underlying Cloud Run service " +
"at https://cloud.google.com/run/quotas.",
);
const suggestedFix = func.buildConfig.runtime?.startsWith("python")
? "firebase_functions.options.set_global_options(max_instances=10)"
: "setGlobalOptions({maxInstances: 10})";
utils.logLabeledWarning(
"functions",
`You can adjust the max instances value in your function's runtime options:\n\t${suggestedFix}`,
);
} else {
utils.logLabeledWarning("functions", `${err?.message}`);
if (err?.context?.response?.statusCode === 429) {
utils.logLabeledWarning(
"functions",
`Got "Quota Exceeded" error while trying to ${type} ${func.name}. Waiting to retry...`,
);
} else if (
err?.message?.includes(
"If you recently started to use Eventarc, it may take a few minutes before all necessary permissions are propagated to the Service Agent",
)
) {
utils.logLabeledWarning(
"functions",
`Since this is your first time using 2nd gen functions, we need a little bit longer to finish setting everything up. Retry the deployment in a few minutes.`,
);
}
utils.logLabeledWarning(
"functions",
` failed to ${type} function ${func.name}`,
);
}
throw new FirebaseError(`Failed to ${type} function ${func.name}`, {
original: err,
status: err?.context?.response?.statusCode,
context: { function: func.name },
});
}
/**
* Creates an upload URL and pre-provisions a StorageSource.
*/
export async function generateUploadUrl(
projectId: string,
location: string,
): Promise<GenerateUploadUrlResponse> {
try {
const res = await client.post<never, GenerateUploadUrlResponse>(
`projects/${projectId}/locations/${location}/functions:generateUploadUrl`,
);
return res.body;
} catch (err: any) {
logger.info(
"\n\nThere was an issue deploying your functions. Verify that your project has a Google App Engine instance setup at https://console.cloud.google.com/appengine and try again. If this issue persists, please contact support.",
);
throw err;
}
}
/**
* Creates a new Cloud Function.
*/
export async function createFunction(cloudFunction: InputCloudFunction): Promise<Operation> {
// the API is a POST to the collection that owns the function name.
const components = cloudFunction.name.split("/");
const functionId = components.splice(-1, 1)[0];
cloudFunction.buildConfig.environmentVariables = {
...cloudFunction.buildConfig.environmentVariables,
// Disable GCF from automatically running npm run build script
// https://cloud.google.com/functions/docs/release-notes
GOOGLE_NODE_RUN_SCRIPTS: "",
};
cloudFunction.serviceConfig.environmentVariables = {
...cloudFunction.serviceConfig.environmentVariables,
FUNCTION_TARGET: cloudFunction.buildConfig.entryPoint.replaceAll("-", "."),
// Enable logging execution id by default for better debugging
LOG_EXECUTION_ID: "true",
};
try {
const res = await client.post<typeof cloudFunction, Operation>(
components.join("/"),
cloudFunction,
{ queryParams: { functionId } },
);
return res.body;
} catch (err: any) {
throw functionsOpLogReject(cloudFunction, "create", err);
}
}
/**
* Gets the definition of a Cloud Function
*/
export async function getFunction(
projectId: string,
location: string,
functionId: string,
): Promise<OutputCloudFunction> {
const name = `projects/${projectId}/locations/${location}/functions/${functionId}`;
const res = await client.get<OutputCloudFunction>(name);
return res.body;
}
/**
* List all functions in all regions
* Customers should generally use backend.existingBackend and backend.checkAvailability.
*/
export async function listAllFunctions(projectId: string): Promise<ListFunctionsResponse> {
return await listFunctionsInternal(projectId, /* region=*/ "-");
}
async function listFunctionsInternal(
projectId: string,
region: string,
): Promise<ListFunctionsResponse> {
type Response = ListFunctionsResponse & { nextPageToken?: string };
const functions: OutputCloudFunction[] = [];
const unreacahble = new Set<string>();
let pageToken = "";
while (true) {
const url = `projects/${projectId}/locations/${region}/functions`;
// V2 API returns both V1 and V2 Functions. Add filter condition to return only V2 functions.
const opts: ClientVerbOptions = { queryParams: { filter: `environment="GEN_2"` } };
if (pageToken !== "") {
opts.queryParams = { ...opts.queryParams, pageToken };
}
const res = await client.get<Response>(url, opts);
functions.push(...(res.body.functions || []));
for (const region of res.body.unreachable || []) {
unreacahble.add(region);
}
if (!res.body.nextPageToken) {
return {
functions,
unreachable: Array.from(unreacahble),
};
}
pageToken = res.body.nextPageToken;
}
}
/**
* Updates a Cloud Function.
* Customers can force a field to be deleted by setting that field to `undefined`
*/
export async function updateFunction(cloudFunction: InputCloudFunction): Promise<Operation> {
cloudFunction.buildConfig.environmentVariables = {
...cloudFunction.buildConfig.environmentVariables,
// Disable GCF from automatically running npm run build script
// https://cloud.google.com/functions/docs/release-notes
GOOGLE_NODE_RUN_SCRIPTS: "",
};
cloudFunction.serviceConfig.environmentVariables = {
...cloudFunction.serviceConfig.environmentVariables,
FUNCTION_TARGET: cloudFunction.buildConfig.entryPoint.replaceAll("-", "."),
// Enable logging execution id by default for better debugging
LOG_EXECUTION_ID: "true",
};
// Keys in labels and environmentVariables and secretEnvironmentVariables are user defined, so we don't recurse
// for field masks.
const fieldMasks = proto.fieldMasks(
cloudFunction,
/* doNotRecurseIn...=*/ "labels",
"serviceConfig.environmentVariables",
"serviceConfig.secretEnvironmentVariables",
"buildConfig.environmentVariables",
);
try {
const queryParams = {
updateMask: fieldMasks.join(","),
};
const res = await client.patch<typeof cloudFunction, Operation>(
cloudFunction.name,
cloudFunction,
{ queryParams },
);
return res.body;
} catch (err: any) {
throw functionsOpLogReject(cloudFunction, "update", err);
}
}
/**
* Deletes a Cloud Function.
* It is safe, but should be unnecessary, to delete a Cloud Function by just its name.
*/
export async function deleteFunction(cloudFunction: string): Promise<Operation> {
try {
const res = await client.delete<Operation>(cloudFunction);
return res.body;
} catch (err: any) {
throw functionsOpLogReject({ name: cloudFunction } as InputCloudFunction, "update", err);
}
}
/**
* Generate a v2 Cloud Function API object from a versionless Endpoint object.
*/
export function functionFromEndpoint(endpoint: backend.Endpoint): InputCloudFunction {
if (endpoint.platform !== "gcfv2") {
throw new FirebaseError(
"Trying to create a v2 CloudFunction with v1 API. This should never happen",
);
}
if (endpoint.runtime && !supported.isRuntime(endpoint.runtime)) {
throw new FirebaseError(
"Failed internal assertion. Trying to deploy a new function with a deprecated runtime." +
" This should never happen",
);
}
const gcfFunction: InputCloudFunction = {
name: backend.functionName(endpoint),
buildConfig: {
runtime: endpoint.runtime || undefined,
entryPoint: endpoint.entryPoint,
source: {
storageSource: endpoint.source?.storageSource,
},
// We don't use build environment variables,
environmentVariables: {},
},
serviceConfig: {},
};
proto.copyIfPresent(gcfFunction, endpoint, "labels");
proto.copyIfPresent(
gcfFunction.serviceConfig,
endpoint,
"environmentVariables",
"secretEnvironmentVariables",
"ingressSettings",
"timeoutSeconds",
);
if (Object.prototype.hasOwnProperty.call(endpoint, "serviceAccount")) {
const serviceAccount = endpoint.serviceAccount;
if (!serviceAccount) {
gcfFunction.buildConfig.serviceAccount = null;
gcfFunction.serviceConfig.serviceAccountEmail = null;
} else {
const email = proto.formatServiceAccount(serviceAccount, endpoint.project, true);
gcfFunction.buildConfig.serviceAccount = `projects/${endpoint.project}/serviceAccounts/${email}`;
gcfFunction.serviceConfig.serviceAccountEmail = email;
}
}
// Memory must be set because the default value of GCF gen 2 is Megabytes and
// we use mebibytes
const mem = endpoint.availableMemoryMb || backend.DEFAULT_MEMORY;
gcfFunction.serviceConfig.availableMemory = mem > 1024 ? `${mem / 1024}Gi` : `${mem}Mi`;
proto.renameIfPresent(gcfFunction.serviceConfig, endpoint, "minInstanceCount", "minInstances");
proto.renameIfPresent(gcfFunction.serviceConfig, endpoint, "maxInstanceCount", "maxInstances");
// N.B. only convert CPU and concurrency fields for 2nd gen functions, once we
// eventually use the v2 API to configure both 1st and 2nd gen functions)
proto.renameIfPresent(
gcfFunction.serviceConfig,
endpoint,
"maxInstanceRequestConcurrency",
"concurrency",
);
proto.convertIfPresent(gcfFunction.serviceConfig, endpoint, "availableCpu", "cpu", (cpu) => {
return String(cpu);
});
if (endpoint.vpc) {
proto.renameIfPresent(gcfFunction.serviceConfig, endpoint.vpc, "vpcConnector", "connector");
proto.renameIfPresent(
gcfFunction.serviceConfig,
endpoint.vpc,
"vpcConnectorEgressSettings",
"egressSettings",
);
} else if (endpoint.vpc === null) {
gcfFunction.serviceConfig.vpcConnector = null;
gcfFunction.serviceConfig.vpcConnectorEgressSettings = null;
}
if (backend.isEventTriggered(endpoint)) {
gcfFunction.eventTrigger = {
eventType: endpoint.eventTrigger.eventType,
retryPolicy: "RETRY_POLICY_UNSPECIFIED",
};
if (gcfFunction.serviceConfig.serviceAccountEmail) {
gcfFunction.eventTrigger.serviceAccountEmail = gcfFunction.serviceConfig.serviceAccountEmail;
}
if (gcfFunction.eventTrigger.eventType === PUBSUB_PUBLISH_EVENT) {
if (!endpoint.eventTrigger.eventFilters?.topic) {
throw new FirebaseError(
"Error: Pub/Sub event trigger is missing topic: " +
JSON.stringify(endpoint.eventTrigger, null, 2),
);
}
gcfFunction.eventTrigger.pubsubTopic = endpoint.eventTrigger.eventFilters.topic;
gcfFunction.eventTrigger.eventFilters = [];
for (const [attribute, value] of Object.entries(endpoint.eventTrigger.eventFilters)) {
if (attribute === "topic") continue;
gcfFunction.eventTrigger.eventFilters.push({ attribute, value });
}
} else {
gcfFunction.eventTrigger.eventFilters = [];
for (const [attribute, value] of Object.entries(endpoint.eventTrigger.eventFilters || {})) {
gcfFunction.eventTrigger.eventFilters.push({ attribute, value });
}
for (const [attribute, value] of Object.entries(
endpoint.eventTrigger.eventFilterPathPatterns || {},
)) {
gcfFunction.eventTrigger.eventFilters.push({
attribute,
value,
operator: "match-path-pattern",
});
}
}
proto.renameIfPresent(
gcfFunction.eventTrigger,
endpoint.eventTrigger,
"triggerRegion",
"region",
);
proto.copyIfPresent(gcfFunction.eventTrigger, endpoint.eventTrigger, "channel");
endpoint.eventTrigger.retry
? (gcfFunction.eventTrigger.retryPolicy = "RETRY_POLICY_RETRY")
: (gcfFunction.eventTrigger.retryPolicy = "RETRY_POLICY_DO_NOT_RETRY");
// By default, Functions Framework in GCFv2 opts to downcast incoming cloudevent messages to legacy formats.
// Since Firebase Functions SDK expects messages in cloudevent format, we set FUNCTION_SIGNATURE_TYPE to tell
// Functions Framework to disable downcast before passing the cloudevent message to function handler.
// See https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/README.md#configure-the-functions-
gcfFunction.serviceConfig.environmentVariables = {
...gcfFunction.serviceConfig.environmentVariables,
FUNCTION_SIGNATURE_TYPE: "cloudevent",
};
} else if (backend.isScheduleTriggered(endpoint)) {
// trigger type defaults to HTTPS.
gcfFunction.labels = { ...gcfFunction.labels, "deployment-scheduled": "true" };
} else if (backend.isTaskQueueTriggered(endpoint)) {
gcfFunction.labels = { ...gcfFunction.labels, "deployment-taskqueue": "true" };
} else if (backend.isCallableTriggered(endpoint)) {
gcfFunction.labels = { ...gcfFunction.labels, "deployment-callable": "true" };
if (endpoint.callableTrigger.genkitAction) {
gcfFunction.labels["genkit-action"] = "true";
}
} else if (backend.isDataConnectGraphqlTriggered(endpoint)) {
gcfFunction.labels = { ...gcfFunction.labels, "deployment-fdcgraphql": "true" };
} else if (backend.isBlockingTriggered(endpoint)) {
gcfFunction.labels = {
...gcfFunction.labels,
[BLOCKING_LABEL]:
BLOCKING_EVENT_TO_LABEL_KEY[
endpoint.blockingTrigger.eventType as (typeof AUTH_BLOCKING_EVENTS)[number]
],
};
}
const codebase = endpoint.codebase || projectConfig.DEFAULT_CODEBASE;
if (codebase !== projectConfig.DEFAULT_CODEBASE) {
gcfFunction.labels = {
...gcfFunction.labels,
[CODEBASE_LABEL]: codebase,
};
} else {
delete gcfFunction.labels?.[CODEBASE_LABEL];
}
if (endpoint.hash) {
gcfFunction.labels = {
...gcfFunction.labels,
[HASH_LABEL]: endpoint.hash,
};
}
return gcfFunction;
}
/**
* Generate a versionless Endpoint object from a v2 Cloud Function API object.
*/
export function endpointFromFunction(gcfFunction: OutputCloudFunction): backend.Endpoint {
const [, project, , region, , id] = gcfFunction.name.split("/");
let trigger: backend.Triggered;
if (gcfFunction.labels?.["deployment-scheduled"] === "true") {
trigger = {
scheduleTrigger: {},
};
} else if (gcfFunction.labels?.["deployment-taskqueue"] === "true") {
trigger = {
taskQueueTrigger: {},
};
} else if (gcfFunction.labels?.["deployment-callable"] === "true") {
trigger = {
callableTrigger: {},
};
} else if (gcfFunction.labels?.["deployment-fdcgraphql"] === "true") {
trigger = {
dataConnectGraphqlTrigger: {},
};
} else if (gcfFunction.labels?.[BLOCKING_LABEL]) {
trigger = {
blockingTrigger: {
eventType: BLOCKING_LABEL_KEY_TO_EVENT[gcfFunction.labels[BLOCKING_LABEL]],
},
};
} else if (gcfFunction.eventTrigger) {
const eventFilters: Record<string, string> = {};
const eventFilterPathPatterns: Record<string, string> = {};
if (
gcfFunction.eventTrigger.pubsubTopic &&
gcfFunction.eventTrigger.eventType === PUBSUB_PUBLISH_EVENT
) {
eventFilters.topic = gcfFunction.eventTrigger.pubsubTopic;
} else {
for (const eventFilter of gcfFunction.eventTrigger.eventFilters || []) {
if (eventFilter.operator === "match-path-pattern") {
eventFilterPathPatterns[eventFilter.attribute] = eventFilter.value;
} else {
eventFilters[eventFilter.attribute] = eventFilter.value;
}
}
}
trigger = {
eventTrigger: {
eventType: gcfFunction.eventTrigger.eventType,
retry: gcfFunction.eventTrigger.retryPolicy === "RETRY_POLICY_RETRY" ? true : false,
},
};
if (Object.keys(eventFilters).length) {
trigger.eventTrigger.eventFilters = eventFilters;
}
if (Object.keys(eventFilterPathPatterns).length) {
trigger.eventTrigger.eventFilterPathPatterns = eventFilterPathPatterns;
}
proto.copyIfPresent(trigger.eventTrigger, gcfFunction.eventTrigger, "channel");
proto.renameIfPresent(
trigger.eventTrigger,
gcfFunction.eventTrigger,
"region",
"triggerRegion",
);
} else {
trigger = { httpsTrigger: {} };
}
if (gcfFunction.buildConfig?.runtime && !supported.isRuntime(gcfFunction.buildConfig.runtime)) {
logger.debug("GCFv2 function has a deprecated runtime:", JSON.stringify(gcfFunction, null, 2));
}
const endpoint: backend.Endpoint = {
platform: "gcfv2",
id,
project,
region,
...trigger,
entryPoint: gcfFunction.buildConfig?.entryPoint || "",
runtime: gcfFunction.buildConfig?.runtime || undefined,
source: gcfFunction.buildConfig?.source,
};
if (gcfFunction.serviceConfig) {
proto.copyIfPresent(
endpoint,
gcfFunction.serviceConfig,
"ingressSettings",
"environmentVariables",
"secretEnvironmentVariables",
"timeoutSeconds",
"uri",
);
proto.renameIfPresent(
endpoint,
gcfFunction.serviceConfig,
"serviceAccount",
"serviceAccountEmail",
);
proto.convertIfPresent(
endpoint,
gcfFunction.serviceConfig,
"availableMemoryMb",
"availableMemory",
(prod) => {
if (prod === null) {
logger.debug("Prod should always return a valid memory amount");
return prod as never;
}
const mem = mebibytes(prod);
if (!backend.isValidMemoryOption(mem)) {
logger.debug("Converting a function to an endpoint with an invalid memory option", mem);
}
return mem as backend.MemoryOptions;
},
);
proto.convertIfPresent(endpoint, gcfFunction.serviceConfig, "cpu", "availableCpu", (cpu) => {
let cpuVal: number | null = Number(cpu);
if (Number.isNaN(cpuVal)) {
cpuVal = null;
}
return cpuVal;
});
proto.renameIfPresent(endpoint, gcfFunction.serviceConfig, "minInstances", "minInstanceCount");
proto.renameIfPresent(endpoint, gcfFunction.serviceConfig, "maxInstances", "maxInstanceCount");
proto.renameIfPresent(
endpoint,
gcfFunction.serviceConfig,
"concurrency",
"maxInstanceRequestConcurrency",
);
proto.copyIfPresent(endpoint, gcfFunction, "labels");
if (gcfFunction.serviceConfig.vpcConnector) {
endpoint.vpc = { connector: gcfFunction.serviceConfig.vpcConnector };
proto.renameIfPresent(
endpoint.vpc,
gcfFunction.serviceConfig,
"egressSettings",
"vpcConnectorEgressSettings",
);
}
const serviceName = gcfFunction.serviceConfig.service;
if (!serviceName) {
logger.debug(
"Got a v2 function without a service name." +
"Maybe we've migrated to using the v2 API everywhere and missed this code",
);
} else {
endpoint.runServiceId = utils.last(serviceName.split("/"));
}
}
proto.renameIfPresent(endpoint, gcfFunction, "uri", "url");
endpoint.codebase = gcfFunction.labels?.[CODEBASE_LABEL] || projectConfig.DEFAULT_CODEBASE;
if (gcfFunction.labels?.[HASH_LABEL]) {
endpoint.hash = gcfFunction.labels[HASH_LABEL];
}
proto.copyIfPresent(endpoint, gcfFunction, "state");
return endpoint;
}