Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set environment variable necessary to be a custom events source #5078

Merged
merged 4 commits into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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