Skip to content

Commit 993850f

Browse files
authored
Make Hosting's Run rewrite for v2 functions use API responses (#5491)
* Make Hosting's Run rewrite for v2 functions use API responses * Hide log line in debug logs
1 parent 51ebb55 commit 993850f

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

src/deploy/functions/backend.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ export type Endpoint = TargetIds &
366366

367367
// Marked as true if a user specifically called this function or codebase with the --only flag.
368368
targetedByOnly?: boolean;
369+
370+
// Output only. For v2 functions, this is the run service ID.
371+
// This may eventually be different than id because GCF is going to start
372+
// doing name translations
373+
runServiceId?: string;
369374
};
370375

371376
export interface RequiredAPI {

src/deploy/hosting/convertConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export async function convertConfig(
213213
const apiRewrite: api.Rewrite = {
214214
...target,
215215
run: {
216-
serviceId: endpoint.id,
216+
serviceId: endpoint.runServiceId ?? endpoint.id,
217217
region: endpoint.region,
218218
},
219219
};

src/gcp/cloudfunctionsv2.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,5 +691,14 @@ export function endpointFromFunction(gcfFunction: CloudFunction): backend.Endpoi
691691
if (gcfFunction.labels?.[HASH_LABEL]) {
692692
endpoint.hash = gcfFunction.labels[HASH_LABEL];
693693
}
694+
const serviceName = gcfFunction.serviceConfig.service;
695+
if (!serviceName) {
696+
logger.debug(
697+
"Got a v2 function without a service name." +
698+
"Maybe we've migrated to using the v2 API everywhere and missed this code"
699+
);
700+
} else {
701+
endpoint.runServiceId = utils.last(serviceName.split("/"));
702+
}
694703
return endpoint;
695704
}

src/test/deploy/hosting/convertConfig.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import * as api from "../../../hosting/api";
99
import { FirebaseError } from "../../../error";
1010
import { Payload } from "../../../deploy/functions/args";
1111

12-
const FUNCTION_ID = "function";
12+
const FUNCTION_ID = "functionId";
13+
const SERVICE_ID = "function-id";
1314
const PROJECT_ID = "project";
1415
const REGION = "region";
1516

@@ -36,6 +37,9 @@ function endpoint(opts?: Partial<backend.Endpoint>): backend.Endpoint {
3637
) {
3738
ret.httpsTrigger = {};
3839
}
40+
if (opts?.platform === "gcfv2") {
41+
ret.runServiceId = opts?.id ?? SERVICE_ID;
42+
}
3943
return ret as backend.Endpoint;
4044
}
4145

@@ -178,7 +182,7 @@ describe("convertConfig", () => {
178182
name: "defaults to a us-central1 rewrite if one is avaiable, v2 edition",
179183
input: { rewrites: [{ glob: "/foo", function: { functionId: FUNCTION_ID } }] },
180184
want: {
181-
rewrites: [{ glob: "/foo", run: { region: "us-central1", serviceId: FUNCTION_ID } }],
185+
rewrites: [{ glob: "/foo", run: { region: "us-central1", serviceId: SERVICE_ID } }],
182186
},
183187
functionsPayload: {
184188
functions: {
@@ -192,6 +196,7 @@ describe("convertConfig", () => {
192196
region: "europe-west2",
193197
platform: "gcfv2",
194198
httpsTrigger: {},
199+
runServiceId: SERVICE_ID,
195200
},
196201
{
197202
id: FUNCTION_ID,
@@ -201,6 +206,7 @@ describe("convertConfig", () => {
201206
region: "us-central1",
202207
platform: "gcfv2",
203208
httpsTrigger: {},
209+
runServiceId: SERVICE_ID,
204210
}
205211
),
206212
haveBackend: backend.empty(),
@@ -236,7 +242,7 @@ describe("convertConfig", () => {
236242
{
237243
name: "rewrites referencing CF3v2 functions being deployed are changed to Cloud Run (during release)",
238244
input: { rewrites: [{ regex: "/foo$", function: { functionId: FUNCTION_ID } }] },
239-
want: { rewrites: [{ regex: "/foo$", run: { serviceId: FUNCTION_ID, region: REGION } }] },
245+
want: { rewrites: [{ regex: "/foo$", run: { serviceId: SERVICE_ID, region: REGION } }] },
240246
functionsPayload: {
241247
functions: {
242248
default: {
@@ -248,6 +254,7 @@ describe("convertConfig", () => {
248254
region: REGION,
249255
platform: "gcfv2",
250256
httpsTrigger: {},
257+
runServiceId: SERVICE_ID,
251258
}),
252259
haveBackend: backend.empty(),
253260
},
@@ -262,7 +269,7 @@ describe("convertConfig", () => {
262269
],
263270
},
264271
want: {
265-
rewrites: [{ regex: "/foo$", run: { serviceId: FUNCTION_ID, region: "us-central1" } }],
272+
rewrites: [{ regex: "/foo$", run: { serviceId: SERVICE_ID, region: "us-central1" } }],
266273
},
267274
existingBackend: backend.of(endpoint({ platform: "gcfv2", region: "us-central1" })),
268275
},
@@ -275,7 +282,7 @@ describe("convertConfig", () => {
275282
},
276283
existingBackend: backend.of(endpoint({ platform: "gcfv2", region: "us-central1" })),
277284
want: {
278-
rewrites: [{ regex: "/foo$", run: { serviceId: FUNCTION_ID, region: "us-central1" } }],
285+
rewrites: [{ regex: "/foo$", run: { serviceId: SERVICE_ID, region: "us-central1" } }],
279286
},
280287
},
281288
{

src/test/gcp/cloudfunctionsv2.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,23 @@ describe("cloudfunctionsv2", () => {
382382
});
383383
});
384384

385+
it("should copy run service IDs", () => {
386+
const fn: cloudfunctionsv2.CloudFunction = {
387+
...HAVE_CLOUD_FUNCTION_V2,
388+
serviceConfig: {
389+
...HAVE_CLOUD_FUNCTION_V2.serviceConfig,
390+
service: "projects/p/locations/l/services/service-id",
391+
},
392+
};
393+
expect(cloudfunctionsv2.endpointFromFunction(fn)).to.deep.equal({
394+
...ENDPOINT,
395+
httpsTrigger: {},
396+
platform: "gcfv2",
397+
uri: RUN_URI,
398+
runServiceId: "service-id",
399+
});
400+
});
401+
385402
it("should translate event triggers", () => {
386403
let want: backend.Endpoint = {
387404
...ENDPOINT,

0 commit comments

Comments
 (0)