Skip to content

Commit

Permalink
Set environment variable necessary to be a custom events source (#5078)
Browse files Browse the repository at this point in the history
* Set environment variable necessary to be a custom events source

* Set label in emulator
  • Loading branch information
inlined committed Oct 28, 2022
1 parent 74c1b19 commit 818ea6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/deploy/functions/prepare.ts
Expand Up @@ -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<string, unknown>): 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.
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/emulator/functionsEmulatorShared.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 818ea6c

Please sign in to comment.