Skip to content

Commit ec92c97

Browse files
committed
Simplify; format
1 parent 3d43507 commit ec92c97

File tree

9 files changed

+315
-345
lines changed

9 files changed

+315
-345
lines changed

src/deploy/functions/prepare.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,11 @@ export async function prepare(
154154
let resource: string;
155155
if (endpoint.platform === "gcfv1") {
156156
resource = `projects/${endpoint.project}/locations/${endpoint.region}/functions/${endpoint.id}`;
157-
} else if (endpoint.platform === "gcfv2") {
157+
} else if (endpoint.platform === "gcfv2" || endpoint.platform === "run") {
158158
// N.B. If GCF starts allowing v1's allowable characters in IDs they're
159159
// going to need to have a transform to create a service ID (which has a
160160
// more restrictive character set). We'll need to reimplement that here.
161-
resource = `projects/${endpoint.project}/locations/${endpoint.region}/services/${endpoint.id}`;
162-
} else if (endpoint.platform === "run") {
161+
// BUG BUG BUG. This has happened and we need to fix it.
163162
resource = `projects/${endpoint.project}/locations/${endpoint.region}/services/${endpoint.id}`;
164163
} else {
165164
assertExhaustive(endpoint.platform);

src/deploy/functions/pricing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export function monthlyMinInstanceCost(endpoints: backend.Endpoint[]): number {
225225
v2CpuBill -= V2_FREE_TIER.vCpu * V2_RATES.vCpu[1];
226226
v2CpuBill = Math.max(v2CpuBill, 0);
227227

228-
let runMemoryBill =
228+
let runMemoryBill =
229229
usage["run"][1].ram * V2_RATES.memoryGb[1] + usage["run"][2].ram * V2_RATES.memoryGb[2];
230230
runMemoryBill -= V2_FREE_TIER.memoryGb * V2_RATES.memoryGb[1];
231231
runMemoryBill = Math.max(runMemoryBill, 0);

src/deploy/functions/release/fabricator.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ export class Fabricator {
184184
} else if (endpoint.platform === "gcfv2") {
185185
await this.createV2Function(endpoint, scraperV2);
186186
} else if (endpoint.platform === "run") {
187-
throw new FirebaseError("Creating new Cloud Run functions is not supported yet.", { exit: 1 })
187+
throw new FirebaseError("Creating new Cloud Run functions is not supported yet.", {
188+
exit: 1,
189+
});
188190
} else {
189191
assertExhaustive(endpoint.platform);
190192
}
@@ -209,7 +211,7 @@ export class Fabricator {
209211
} else if (update.endpoint.platform === "gcfv2") {
210212
await this.updateV2Function(update.endpoint, scraperV2);
211213
} else if (update.endpoint.platform === "run") {
212-
throw new FirebaseError("Updating Cloud Run functions is not supported yet.", { exit: 1 })
214+
throw new FirebaseError("Updating Cloud Run functions is not supported yet.", { exit: 1 });
213215
} else {
214216
assertExhaustive(update.endpoint.platform);
215217
}
@@ -631,7 +633,9 @@ export class Fabricator {
631633
// by the GCF API. This includes schedules, task queues, and blocking function triggers.
632634
async setTrigger(endpoint: backend.Endpoint): Promise<void> {
633635
if (endpoint.platform === "run") {
634-
throw new FirebaseError("Setting triggers for Cloud Run functions is not supported yet.", { exit: 1 });
636+
throw new FirebaseError("Setting triggers for Cloud Run functions is not supported yet.", {
637+
exit: 1,
638+
});
635639
}
636640
if (backend.isScheduleTriggered(endpoint)) {
637641
if (endpoint.platform === "gcfv1") {
@@ -651,7 +655,9 @@ export class Fabricator {
651655

652656
async deleteTrigger(endpoint: backend.Endpoint): Promise<void> {
653657
if (endpoint.platform === "run") {
654-
throw new FirebaseError("Deleting triggers for Cloud Run functions is not supported yet.", { exit: 1 });
658+
throw new FirebaseError("Deleting triggers for Cloud Run functions is not supported yet.", {
659+
exit: 1,
660+
});
655661
}
656662
if (backend.isScheduleTriggered(endpoint)) {
657663
if (endpoint.platform === "gcfv1") {

src/deploy/functions/runtimes/discovery/v1alpha1.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export type WireEndpoint = build.Triggered &
6767
serviceAccountEmail?: build.Field<string>;
6868
region?: build.ListField;
6969
entryPoint: string;
70-
// Run is an automatic upgrade from "v2"; we don't use it on the wire.
7170
platform?: build.FunctionsPlatform;
7271
secretEnvironmentVariables?: Array<ManifestSecretEnv> | null;
7372
};

src/experiments.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export const ALL_EXPERIMENTS = experiments({
5858
public: true,
5959
},
6060
runfunctions: {
61-
shortDescription: "Functions created using the V2 API target Cloud Run Functions (not production ready)",
61+
shortDescription:
62+
"Functions created using the V2 API target Cloud Run Functions (not production ready)",
6263
public: false,
6364
},
6465

src/gcp/k8s.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ describe("megabytes", () => {
2727
expect(k8s.mebibytes("1e9")).to.equal(1e9 / Bytes.MiB);
2828
expect(k8s.mebibytes("1.5E6")).to.equal((1.5 * 1e6) / Bytes.MiB);
2929
});
30-
});
30+
});

src/gcp/k8s.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Resource } from "../extensions/types";
2-
31
// AvailableMemory suffixes and their byte count.
42
type MemoryUnit = "" | "k" | "M" | "G" | "T" | "Ki" | "Mi" | "Gi" | "Ti";
53
const BYTES_PER_UNIT: Record<MemoryUnit, number> = {
@@ -37,20 +35,22 @@ export function mebibytes(memory: string): number {
3735
return bytes / (1 << 20);
3836
}
3937

40-
export interface EnvVarSource {
41-
secretKeyRef: {
42-
secret: string;
43-
version: "latest" | number;
44-
};
38+
export interface PlaintextEnvVar {
39+
name: string;
40+
value: string;
4541
}
4642

47-
export type EnvVar = {
43+
export interface SecretEnvVar {
4844
name: string;
49-
} & ({
50-
value: string;
51-
} | {
52-
valueSource: EnvVarSource;
53-
});
45+
valueSource: {
46+
secretKeyRef: {
47+
secret: string; // Secret name
48+
version?: string; // Optional version, defaults to latest
49+
};
50+
};
51+
}
52+
53+
export type EnvVar = PlaintextEnvVar | SecretEnvVar;
5454

5555
export type ResourceType = "cpu" | "memory" | "nvidia.com/gpu";
5656

@@ -63,7 +63,7 @@ export interface Container {
6363
workingDir?: string;
6464
resources: {
6565
limits: Record<ResourceType, string>;
66-
}
66+
};
6767
cpuIdle?: boolean;
6868
startupCpuBoost?: boolean;
6969
}

src/gcp/runv2.spec.ts

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai";
22

33
import * as runv2 from "./runv2";
44
import * as backend from "../deploy/functions/backend";
5-
import { latest, Runtime } from "../deploy/functions/runtimes/supported";
5+
import { latest } from "../deploy/functions/runtimes/supported";
66
import { CODEBASE_LABEL } from "../functions/constants";
77

88
describe("runv2", () => {
@@ -23,44 +23,7 @@ describe("runv2", () => {
2323
cpu: 1,
2424
};
2525

26-
const RUN_SERVICE_V2_OUTPUT_BASE: runv2.Service = {
27-
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
28-
generation: 1,
29-
labels: {
30-
[runv2.RUNTIME_LABEL]: latest("nodejs"),
31-
[runv2.CLIENT_NAME_LABEL]: "firebase-functions",
32-
},
33-
annotations: {
34-
[runv2.CLIENT_NAME_ANNOTATION]: "cli-firebase",
35-
[runv2.FUNCTION_TARGET_ANNOTATION]: FUNCTION_ID,
36-
[runv2.FUNCTION_ID_ANNOTATION]: FUNCTION_ID,
37-
[runv2.CPU_BOOST_ANNOTATION]: "true",
38-
},
39-
template: {
40-
containers: [
41-
{
42-
name: runv2.DEFAULT_FUNCTION_CONTAINER_NAME,
43-
image: IMAGE_URI,
44-
env: [],
45-
resources: {
46-
limits: {
47-
cpu: "1",
48-
memory: "256Mi",
49-
},
50-
startupCpuBoost: true,
51-
},
52-
},
53-
],
54-
containerConcurrency: backend.DEFAULT_CONCURRENCY, // Default for CPU >= 1
55-
},
56-
createTime: new Date().toISOString(),
57-
updateTime: new Date().toISOString(),
58-
creator: "[email protected]",
59-
lastModifier: "[email protected]",
60-
etag: "test-etag",
61-
};
62-
63-
const RUN_SERVICE_V2_INPUT_BASE: Omit<runv2.Service, runv2.ServiceOutputFields> = {
26+
const BASE_RUN_SERVICE: Omit<runv2.Service, runv2.ServiceOutputFields> = {
6427
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
6528
labels: {
6629
[runv2.RUNTIME_LABEL]: latest("nodejs"),
@@ -98,7 +61,7 @@ describe("runv2", () => {
9861
httpsTrigger: {},
9962
};
10063

101-
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(RUN_SERVICE_V2_INPUT_BASE);
64+
expect(runv2.serviceFromEndpoint(endpoint, IMAGE_URI)).to.deep.equal(BASE_RUN_SERVICE);
10265
});
10366

10467
it("should handle different codebase", () => {
@@ -108,10 +71,10 @@ describe("runv2", () => {
10871
httpsTrigger: {},
10972
};
11073
const expectedServiceInput: Omit<runv2.Service, runv2.ServiceOutputFields> = {
111-
...RUN_SERVICE_V2_INPUT_BASE,
74+
...BASE_RUN_SERVICE,
11275
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
11376
labels: {
114-
...RUN_SERVICE_V2_INPUT_BASE.labels,
77+
...BASE_RUN_SERVICE.labels,
11578
[CODEBASE_LABEL]: "my-codebase",
11679
},
11780
};
@@ -126,7 +89,7 @@ describe("runv2", () => {
12689
};
12790
const expectedServiceInput = JSON.parse(
12891
JSON.stringify({
129-
...RUN_SERVICE_V2_INPUT_BASE,
92+
...BASE_RUN_SERVICE,
13093
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
13194
}),
13295
);
@@ -145,7 +108,7 @@ describe("runv2", () => {
145108
};
146109
const expectedServiceInput = JSON.parse(
147110
JSON.stringify({
148-
...RUN_SERVICE_V2_INPUT_BASE,
111+
...BASE_RUN_SERVICE,
149112
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
150113
}),
151114
);
@@ -167,7 +130,7 @@ describe("runv2", () => {
167130
};
168131
const expectedServiceInput = JSON.parse(
169132
JSON.stringify({
170-
...RUN_SERVICE_V2_INPUT_BASE,
133+
...BASE_RUN_SERVICE,
171134
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
172135
}),
173136
);
@@ -185,7 +148,7 @@ describe("runv2", () => {
185148
};
186149
const expectedServiceInput = JSON.parse(
187150
JSON.stringify({
188-
...RUN_SERVICE_V2_INPUT_BASE,
151+
...BASE_RUN_SERVICE,
189152
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
190153
}),
191154
);
@@ -203,7 +166,7 @@ describe("runv2", () => {
203166
};
204167
const expectedServiceInput = JSON.parse(
205168
JSON.stringify({
206-
...RUN_SERVICE_V2_INPUT_BASE,
169+
...BASE_RUN_SERVICE,
207170
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${FUNCTION_ID.toLowerCase()}`,
208171
}),
209172
);
@@ -227,8 +190,8 @@ describe("runv2", () => {
227190

228191
describe("endpointFromService", () => {
229192
it("should copy a minimal service", () => {
230-
const service: runv2.Service = {
231-
...RUN_SERVICE_V2_OUTPUT_BASE,
193+
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
194+
...BASE_RUN_SERVICE,
232195
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
233196
labels: {
234197
[runv2.RUNTIME_LABEL]: latest("nodejs"),
@@ -274,8 +237,8 @@ describe("runv2", () => {
274237
});
275238

276239
it("should detect a service that's GCF managed", () => {
277-
const service: runv2.Service = {
278-
...RUN_SERVICE_V2_OUTPUT_BASE,
240+
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
241+
...BASE_RUN_SERVICE,
279242
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
280243
labels: {
281244
[runv2.RUNTIME_LABEL]: latest("nodejs"),
@@ -323,8 +286,8 @@ describe("runv2", () => {
323286
});
324287

325288
it("should derive id from FUNCTION_TARGET_ANNOTATION if FUNCTION_ID_ANNOTATION is missing", () => {
326-
const service: runv2.Service = {
327-
...RUN_SERVICE_V2_OUTPUT_BASE,
289+
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
290+
...BASE_RUN_SERVICE,
328291
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
329292
labels: {
330293
[runv2.RUNTIME_LABEL]: latest("nodejs"),
@@ -348,8 +311,8 @@ describe("runv2", () => {
348311
});
349312

350313
it("should derive id from service name part if FUNCTION_ID_ANNOTATION and FUNCTION_TARGET_ANNOTATION are missing", () => {
351-
const service: runv2.Service = {
352-
...RUN_SERVICE_V2_OUTPUT_BASE,
314+
const service: Omit<runv2.Service, runv2.ServiceOutputFields> = {
315+
...BASE_RUN_SERVICE,
353316
name: `projects/${PROJECT_ID}/locations/${LOCATION}/services/${SERVICE_ID}`,
354317
labels: {
355318
[runv2.RUNTIME_LABEL]: latest("nodejs"),
@@ -373,7 +336,7 @@ describe("runv2", () => {
373336
});
374337

375338
it("should copy env vars and secrets", () => {
376-
const service: runv2.Service = JSON.parse(JSON.stringify(RUN_SERVICE_V2_OUTPUT_BASE));
339+
const service: runv2.Service = JSON.parse(JSON.stringify(BASE_RUN_SERVICE));
377340
service.template.containers![0].env = [
378341
{ name: "FOO", value: "bar" },
379342
{
@@ -395,7 +358,7 @@ describe("runv2", () => {
395358
});
396359

397360
it("should copy concurrency, min/max instances", () => {
398-
const service: runv2.Service = JSON.parse(JSON.stringify(RUN_SERVICE_V2_OUTPUT_BASE));
361+
const service: runv2.Service = JSON.parse(JSON.stringify(BASE_RUN_SERVICE));
399362
service.template.containerConcurrency = 10;
400363
service.annotations![runv2.MIN_INSTANCES_ANNOTATION] = "2";
401364
service.annotations![runv2.MAX_INSTANCES_ANNOTATION] = "5";
@@ -447,4 +410,4 @@ describe("runv2", () => {
447410
expect(runv2.endpointFromService(service)).to.deep.equal(expectedEndpoint);
448411
});
449412
});
450-
});
413+
});

0 commit comments

Comments
 (0)