From 818ea6c82cde6fccdb277eccaab8f7396811fac7 Mon Sep 17 00:00:00 2001 From: Thomas Bouldin Date: Fri, 28 Oct 2022 13:10:27 -0700 Subject: [PATCH] Set environment variable necessary to be a custom events source (#5078) * Set environment variable necessary to be a custom events source * Set label in emulator --- src/deploy/functions/prepare.ts | 16 +++++++++++++++- src/emulator/functionsEmulatorShared.ts | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/deploy/functions/prepare.ts b/src/deploy/functions/prepare.ts index 60801d85158..ad8d373e367 100644 --- a/src/deploy/functions/prepare.ts +++ b/src/deploy/functions/prepare.ts @@ -31,7 +31,9 @@ import { AUTH_BLOCKING_EVENTS } from "../../functions/events/v1"; import { generateServiceIdentity } from "../../gcp/serviceusage"; import { applyBackendHashToBackends } from "./cache/applyHash"; import { allEndpoints, Backend } from "./backend"; +import { assertExhaustive } from "../../functional"; +export const EVENTARC_SOURCE_ENV = "EVENTARC_CLOUD_EVENT_SOURCE"; function hasUserConfig(config: Record): boolean { // "firebase" key is always going to exist in runtime config. // If any other key exists, we can assume that user is using runtime config. @@ -138,7 +140,19 @@ export async function prepare( } for (const endpoint of backend.allEndpoints(wantBackend)) { - endpoint.environmentVariables = wantBackend.environmentVariables; + endpoint.environmentVariables = wantBackend.environmentVariables || {}; + let resource: string; + if (endpoint.platform === "gcfv1") { + resource = `projects/${endpoint.project}/locations/${endpoint.region}/functions/${endpoint.id}`; + } else if (endpoint.platform === "gcfv2") { + // N.B. If GCF starts allowing v1's allowable characters in IDs they're + // going to need to have a transform to create a service ID (which has a + // more restrictive cahracter set). We'll need to reimplement that here. + resource = `projects/${endpoint.project}/locations/${endpoint.region}/services/${endpoint.id}`; + } else { + assertExhaustive(endpoint.platform); + } + endpoint.environmentVariables[EVENTARC_SOURCE_ENV] = resource; endpoint.codebase = codebase; } wantBackends[codebase] = wantBackend; diff --git a/src/emulator/functionsEmulatorShared.ts b/src/emulator/functionsEmulatorShared.ts index 77155871e4f..d6414a09d45 100644 --- a/src/emulator/functionsEmulatorShared.ts +++ b/src/emulator/functionsEmulatorShared.ts @@ -25,6 +25,13 @@ const V2_EVENTS = [ ...events.v2.DATABASE_EVENTS, ]; +/** + * Label for eventarc event sources. + * TODO: Consider DRYing from functions/prepare.ts + * A nice place would be to put it in functionsv2.ts once we get rid of functions.ts + */ +export const EVENTARC_SOURCE_ENV = "EVENTARC_CLOUD_EVENT_SOURCE"; + export type SignatureType = "http" | "event" | "cloudevent"; export interface ParsedTriggerDefinition { @@ -171,6 +178,19 @@ export function emulatedFunctionsFromEndpoints( }; def.availableMemoryMb = endpoint.availableMemoryMb || 256; def.labels = endpoint.labels || {}; + if (endpoint.platform === "gcfv1") { + def.labels[EVENTARC_SOURCE_ENV] = + "cloudfunctions-emulated.googleapis.com" + + `/projects/${endpoint.project || "project"}/locations/${endpoint.region}/functions/${ + endpoint.id + }`; + } else if (endpoint.platform === "gcfv2") { + def.labels[EVENTARC_SOURCE_ENV] = + "run-emulated.googleapis.com" + + `/projects/${endpoint.project || "project"}/locations/${endpoint.region}/services/${ + endpoint.id + }`; + } def.timeoutSeconds = endpoint.timeoutSeconds || 60; def.secretEnvironmentVariables = endpoint.secretEnvironmentVariables || []; def.platform = endpoint.platform;