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;