-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Upgrades functions::list command to utilize Cloud Run API #9425
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
base: master
Are you sure you want to change the base?
Changes from 18 commits
12b9257
0a30722
df189ee
66cf473
3af536c
f6ef8c0
d9fd843
84725cf
6a45457
9cedf12
481d02f
0362303
d4ed0f9
2de426f
7cb75d8
4d06024
3712d0c
d1986ac
8f1c06d
1cef2a6
0de7514
66e5d6f
c1f2e7b
bd8ea09
86ef3f6
9287590
896d3a1
a38b669
c253ae9
0779bff
a36e9d9
5acc3cf
812fda9
4ebc27b
369c402
963ff16
1ba7368
536c3d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Upgraded functions::list command to use cloud run api (#9425) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,72 @@ | ||
| import { Command } from "../command"; | ||
| import * as args from "../deploy/functions/args"; | ||
| import { needProjectId } from "../projectUtils"; | ||
| import { Options } from "../options"; | ||
| import { requirePermissions } from "../requirePermissions"; | ||
| import * as backend from "../deploy/functions/backend"; | ||
| import { logger } from "../logger"; | ||
| import * as Table from "cli-table3"; | ||
| import { Options } from "../options"; | ||
| import { FunctionsPlatform } from "../deploy/functions/backend"; | ||
|
|
||
| type PLATFORM_DISPLAY_NAME = "v1" | "v2" | "run"; | ||
| const PLATFORM_TO_DISPLAY_NAME: Record<FunctionsPlatform, PLATFORM_DISPLAY_NAME> = { | ||
| gcfv1: "v1", | ||
| gcfv2: "v2", | ||
| run: "run", | ||
| }; | ||
|
|
||
| export const command = new Command("functions:list") | ||
| .description("list all deployed functions in your Firebase project") | ||
| .before(requirePermissions, ["cloudfunctions.functions.list"]) | ||
| .before(requirePermissions, ["cloudfunctions.functions.list", "run.services.list"]) | ||
| .action(async (options: Options) => { | ||
| const projectId = needProjectId(options); | ||
| const context = { | ||
| projectId: needProjectId(options), | ||
| projectId, | ||
| } as args.Context; | ||
| const existing = await backend.existingBackend(context); | ||
| const endpointsList = backend.allEndpoints(existing).sort(backend.compareFunctions); | ||
|
|
||
| let v1Endpoints: backend.Endpoint[] = []; | ||
| try { | ||
| const existing = await backend.existingBackend(context); | ||
| v1Endpoints = backend.allEndpoints(existing); | ||
| } catch (err: any) { | ||
| logger.debug(`Failed to list v1 functions:`, err); | ||
| logger.warn( | ||
| `Failed to list v1 functions. Ensure you have the Cloud Functions API enabled and the necessary permissions.`, | ||
| ); | ||
| } | ||
|
|
||
| const endpointMap = new Map<string, backend.Endpoint>(); | ||
| for (const endpoint of v1Endpoints) { | ||
| const key = `${endpoint.region}/${endpoint.id}`; | ||
| endpointMap.set(key, endpoint); | ||
| } | ||
|
|
||
| const endpoints = Array.from(endpointMap.values()); | ||
| endpoints.sort(backend.compareFunctions); | ||
|
|
||
| if (endpoints.length === 0) { | ||
| logger.info(`No functions found in project ${projectId}.`); | ||
| return []; | ||
| } | ||
|
|
||
| const table = new Table({ | ||
| head: ["Function", "Version", "Trigger", "Location", "Memory", "Runtime"], | ||
| style: { head: ["yellow"] }, | ||
| }); | ||
| for (const endpoint of endpointsList) { | ||
| }) as any; | ||
|
|
||
| for (const endpoint of endpoints) { | ||
| const trigger = backend.endpointTriggerType(endpoint); | ||
| const availableMemoryMb = endpoint.availableMemoryMb || "---"; | ||
| const entry = [ | ||
| endpoint.id, | ||
| endpoint.platform === "gcfv2" ? "v2" : "v1", | ||
| PLATFORM_TO_DISPLAY_NAME[endpoint.platform] || "v1", | ||
brittanycho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| trigger, | ||
| endpoint.region, | ||
| availableMemoryMb, | ||
| endpoint.runtime, | ||
| ]; | ||
| table.push(entry); | ||
|
Check warning on line 68 in src/commands/functions-list.ts
|
||
| } | ||
| logger.info(table.toString()); | ||
|
Check warning on line 70 in src/commands/functions-list.ts
|
||
| return endpointsList; | ||
| return endpoints; | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import * as gcf from "../../gcp/cloudfunctions"; | ||
| import * as gcfV2 from "../../gcp/cloudfunctionsv2"; | ||
| import { listServices, endpointFromService } from "../../gcp/runv2"; | ||
brittanycho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import * as utils from "../../utils"; | ||
| import { Runtime } from "./runtimes/supported"; | ||
| import { FirebaseError } from "../../error"; | ||
|
|
@@ -551,14 +552,29 @@ | |
| existingBackend.endpoints[endpoint.region][endpoint.id] = endpoint; | ||
| } | ||
| unreachableRegions.gcfV2 = gcfV2Results.unreachable; | ||
| } catch (err: any) { | ||
| if (err.status === 404 && err.message?.toLowerCase().includes("method not found")) { | ||
| // customer has preview enabled without allowlist set | ||
| } else { | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const runServices = await listServices(ctx.projectId, "goog-managed-by=cloudfunctions"); | ||
| for (const service of runServices) { | ||
| const endpoint = endpointFromService(service); | ||
| existingBackend.endpoints[endpoint.region] = existingBackend.endpoints[endpoint.region] || {}; | ||
| existingBackend.endpoints[endpoint.region][endpoint.id] = endpoint; | ||
| } | ||
| } catch (err: any) { | ||
| utils.logLabeledWarning( | ||
| "functions", | ||
| `Failed to list Cloud Run services: ${err.message}. Ensure you have the Cloud Run Admin API enabled and the necessary permissions.`, | ||
| ); | ||
brittanycho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| unreachableRegions.run = ["unknown"]; // Indicate that Run services could not be listed | ||
|
||
| } | ||
|
|
||
| ctx.existingBackend = existingBackend; | ||
| ctx.unreachableRegions = unreachableRegions; | ||
| return ctx.existingBackend; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -200,6 +200,38 @@ export async function updateService(service: Omit<Service, ServiceOutputFields>) | |
| return svc; | ||
| } | ||
|
|
||
| export async function listServices(projectId: string, filter?: string): Promise<Service[]> { | ||
| const allServices: Service[] = []; | ||
| let pageToken: string | undefined = undefined; | ||
|
|
||
| do { | ||
| const queryParams: Record<string, string> = {}; | ||
| if (pageToken) { | ||
| queryParams["pageToken"] = pageToken; | ||
| } | ||
| if (filter) { | ||
| const parts = filter.split("="); | ||
| queryParams["filter"] = `labels."${parts[0]}"="${parts[1]}"`; | ||
| } | ||
brittanycho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const res = await client.get<{ services?: Service[]; nextPageToken?: string }>( | ||
| `/projects/${projectId}/locations/-/services`, | ||
| { queryParams }, | ||
| ); | ||
|
|
||
| if (res.status !== 200) { | ||
| throw new FirebaseError(`Failed to list services: ${res.status} ${JSON.stringify(res.body)}`); | ||
brittanycho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
brittanycho marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (res.body.services) { | ||
| allServices.push(...res.body.services); | ||
| } | ||
| pageToken = res.body.nextPageToken; | ||
| } while (pageToken); | ||
|
|
||
| return allServices; | ||
| } | ||
|
|
||
| // TODO: Replace with real version: | ||
| function functionNameToServiceName(id: string): string { | ||
| return id.toLowerCase().replace(/_/g, "-"); | ||
|
|
@@ -487,9 +519,14 @@ export function endpointFromService(service: Omit<Service, ServiceOutputFields>) | |
| service.annotations?.[FUNCTION_TARGET_ANNOTATION] || | ||
| service.annotations?.[FUNCTION_ID_ANNOTATION] || | ||
| id, | ||
|
|
||
| // TODO: trigger types. | ||
| httpsTrigger: {}, | ||
| ...(service.annotations?.[TRIGGER_TYPE_ANNOTATION] === "HTTP_TRIGGER" | ||
| ? { httpsTrigger: {} } | ||
| : { | ||
| eventTrigger: { | ||
| eventType: service.annotations?.[TRIGGER_TYPE_ANNOTATION] || "unknown", | ||
| retry: false, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is retry always false here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iiuc, I don't think the Cloud Run Service object stores info on retry policies so I don't think we'd be able to know the actual retry setting for a trigger? I didn't see any related fields in https://cloud.google.com/run/docs/reference/rest/v2/projects.locations.services#resource:-service |
||
| }, | ||
| }), | ||
| }; | ||
| proto.renameIfPresent(endpoint, service.template, "concurrency", "containerConcurrency"); | ||
| proto.renameIfPresent(endpoint, service.labels || {}, "codebase", CODEBASE_LABEL); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.