-
Notifications
You must be signed in to change notification settings - Fork 200
/
options.ts
373 lines (336 loc) · 9.39 KB
/
options.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
// The MIT License (MIT)
//
// Copyright (c) 2021 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import {
convertIfPresent,
copyIfPresent,
durationFromSeconds,
serviceAccountFromShorthand,
} from '../common/encoding';
import * as logger from '../logger';
import { ManifestEndpoint } from '../runtime/manifest';
import { TriggerAnnotation } from './core';
import { declaredParams } from './params';
import { ParamSpec } from './params/types';
import { HttpsOptions } from './providers/https';
/**
* List of all regions supported by Cloud Functions v2
*/
export const SUPPORTED_REGIONS = [
'asia-northeast1',
'europe-north1',
'europe-west1',
'europe-west4',
'us-central1',
'us-east1',
'us-west1',
] as const;
/**
* A region known to be supported by CloudFunctions v2
*/
export type SupportedRegion = typeof SUPPORTED_REGIONS[number];
/**
* Cloud Functions v2 min timeout value.
*/
export const MIN_TIMEOUT_SECONDS = 1;
/**
* Cloud Functions v2 max timeout value for event handlers.
*/
export const MAX_EVENT_TIMEOUT_SECONDS = 540;
/**
* Cloud Functions v2 max timeout for HTTPS functions.
*/
export const MAX_HTTPS_TIMEOUT_SECONDS = 36_000;
/**
* Maximum number of requests to serve on a single instance.
*/
export const MAX_CONCURRENCY = 1_000;
/**
* List of available memory options supported by Cloud Functions.
*/
export const SUPPORTED_MEMORY_OPTIONS = [
'128MiB',
'256MiB',
'512MiB',
'1GiB',
'2GiB',
'4GiB',
'8GiB',
'16GiB',
'32GiB',
] as const;
const MemoryOptionToMB: Record<MemoryOption, number> = {
'128MiB': 128,
'256MiB': 256,
'512MiB': 512,
'1GiB': 1024,
'2GiB': 2048,
'4GiB': 4096,
'8GiB': 8192,
'16GiB': 16384,
'32GiB': 32768,
};
/**
* A supported memory option.
*/
export type MemoryOption = typeof SUPPORTED_MEMORY_OPTIONS[number];
/**
* List of available options for VpcConnectorEgressSettings.
*/
export const SUPPORTED_VPC_EGRESS_SETTINGS = [
'PRIVATE_RANGES_ONLY',
'ALL_TRAFFIC',
] as const;
/**
* A valid VPC Egress setting.
*/
export type VpcEgressSetting = typeof SUPPORTED_VPC_EGRESS_SETTINGS[number];
/**
* List of available options for IngressSettings.
*/
export const SUPPORTED_INGRESS_SETTINGS = [
'ALLOW_ALL',
'ALLOW_INTERNAL_ONLY',
'ALLOW_INTERNAL_AND_GCLB',
] as const;
export type IngressSetting = typeof SUPPORTED_INGRESS_SETTINGS[number];
/**
* GlobalOptions are options that can be set across an entire project.
* These options are common to HTTPS and Event handling functions.
*/
export interface GlobalOptions {
/**
* Region where functions should be deployed.
* HTTP functions can override and specify more than one region.
*/
region?: SupportedRegion | string;
/**
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: MemoryOption | null;
/**
* Timeout for the function in sections, possible values are 0 to 540.
* HTTPS functions can specify a higher timeout.
* A value of null restores the default of 60s
*/
timeoutSeconds?: number | null;
/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
/**
* Number of requests a function can serve at once.
* Can only be applied to functions running on Cloud Functions v2.
* A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
*/
concurrency?: number | null;
/**
* Fractional number of CPUs to allocate to a function.
* Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
* This is different from the defaults when using the gcloud utility and is different from
* the fixed amount assigned in Google Cloud Functions generation 1.
* To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
* to the value "gcf_gen1"
*/
cpu?: number | 'gcf_gen1';
/**
* Connect cloud function to specified VPC connector.
* A value of null removes the VPC connector
*/
vpcConnector?: string | null;
/**
* Egress settings for VPC connector.
* A value of null turns off VPC connector egress settings
*/
vpcConnectorEgressSettings?: VpcEgressSetting | null;
/**
* Specific service account for the function to run as.
* A value of null restores the default service account.
*/
serviceAccount?: string | null;
/**
* Ingress settings which control where this function can be called from.
* A value of null turns off ingress settings.
*/
ingressSettings?: IngressSetting | null;
/**
* User labels to set on the function.
*/
labels?: Record<string, string>;
/**
* Invoker to set access control on https functions.
*/
invoker?: 'public' | 'private' | string | string[];
/*
* Secrets to bind to a functions.
*/
secrets?: string[];
}
let globalOptions: GlobalOptions | undefined;
/**
* Sets default options for all functions written using the v2 SDK.
* @param options Options to set as default
*/
export function setGlobalOptions(options: GlobalOptions) {
if (globalOptions) {
logger.warn('Calling setGlobalOptions twice leads to undefined behavior');
}
globalOptions = options;
}
/**
* Get the currently set default options.
* Used only for trigger generation.
* @internal
*/
export function getGlobalOptions(): GlobalOptions {
return globalOptions || {};
}
/**
* Options that can be set on an individual event-handling Cloud Function.
*/
export interface EventHandlerOptions extends GlobalOptions {
retry?: boolean;
}
/**
* Apply GlobalOptions to trigger definitions.
* @internal
*/
export function optionsToTriggerAnnotations(
opts: GlobalOptions | EventHandlerOptions | HttpsOptions
): TriggerAnnotation {
const annotation: TriggerAnnotation = {};
copyIfPresent(
annotation,
opts,
'concurrency',
'minInstances',
'maxInstances',
'ingressSettings',
'labels',
'vpcConnector',
'vpcConnectorEgressSettings',
'secrets'
);
convertIfPresent(
annotation,
opts,
'availableMemoryMb',
'memory',
(mem: MemoryOption) => {
return MemoryOptionToMB[mem];
}
);
convertIfPresent(annotation, opts, 'regions', 'region', (region) => {
if (typeof region === 'string') {
return [region];
}
return region;
});
convertIfPresent(
annotation,
opts,
'serviceAccountEmail',
'serviceAccount',
serviceAccountFromShorthand
);
convertIfPresent(
annotation,
opts,
'timeout',
'timeoutSeconds',
durationFromSeconds
);
convertIfPresent(
annotation,
(opts as any) as EventHandlerOptions,
'failurePolicy',
'retry',
(retry: boolean) => {
return retry ? { retry: true } : null;
}
);
return annotation;
}
/**
* Apply GlobalOptions to endpoint manifest.
* @internal
*/
export function optionsToEndpoint(
opts: GlobalOptions | EventHandlerOptions | HttpsOptions
): ManifestEndpoint {
const endpoint: ManifestEndpoint = {};
copyIfPresent(
endpoint,
opts,
'concurrency',
'minInstances',
'maxInstances',
'ingressSettings',
'labels',
'timeoutSeconds',
'cpu'
);
convertIfPresent(endpoint, opts, 'serviceAccountEmail', 'serviceAccount');
if (opts.vpcConnector) {
const vpc: ManifestEndpoint['vpc'] = { connector: opts.vpcConnector };
convertIfPresent(vpc, opts, 'egressSettings', 'vpcConnectorEgressSettings');
endpoint.vpc = vpc;
}
convertIfPresent(endpoint, opts, 'availableMemoryMb', 'memory', (mem) => {
return MemoryOptionToMB[mem];
});
convertIfPresent(endpoint, opts, 'region', 'region', (region) => {
if (typeof region === 'string') {
return [region];
}
return region;
});
convertIfPresent(
endpoint,
opts,
'secretEnvironmentVariables',
'secrets',
(secrets) => secrets.map((secret) => ({ key: secret }))
);
return endpoint;
}
/**
* @hidden
*/
export function __getSpec(): {
globalOptions: GlobalOptions;
params: ParamSpec[];
} {
return {
globalOptions: getGlobalOptions(),
params: declaredParams.map((p) => p.toSpec()),
};
}