Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lexicons/gcp/src/composites/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,13 @@ export const compositeCatalog: CompositeEntry[] = [
"name": "location",
"type": "string",
"required": false,
"description": "GCP region."
"description": "GCP region (default: GCP.Region, resolved from GCP_REGION at build)."
},
{
"name": "projectId",
"type": "string",
"required": false,
"description": "GCP project the service runs in, as `projectRef.external` (default: GCP.ProjectId, resolved from GCP_PROJECT_ID at build)."
},
{
"name": "port",
Expand Down
17 changes: 12 additions & 5 deletions lexicons/gcp/src/composites/cloud-run-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import {
CloudRunService as CloudRunServiceResource,
IAMPolicyMember,
} from "../generated";
import { GCP } from "../pseudo";

export interface CloudRunServiceProps {
/** Service name. */
name: string;
/** Container image. */
image: string;
/** GCP region. */
/** GCP region (default: GCP.Region, resolved from GCP_REGION at build). */
location?: string;
/** GCP project the service runs in, as `projectRef.external` (default: GCP.ProjectId, resolved from GCP_PROJECT_ID at build). */
projectId?: string;
/** Port the container listens on (default: 8080). */
port?: number;
/** CPU limit (default: "1000m"). */
Expand Down Expand Up @@ -47,10 +50,12 @@ export interface CloudRunServiceProps {
*
* @example
* ```ts
* import { CloudRunService } from "@intentius/chant-lexicon-gcp";
* import { CloudRunServiceComposite } from "@intentius/chant-lexicon-gcp";
*
* const { service, publicIam } = CloudRunService({
* const { service, publicIam } = CloudRunServiceComposite({
* name: "my-api",
* projectId: "my-project",
* location: "us-central1",
* image: "gcr.io/my-project/my-api:latest",
* publicAccess: true,
* });
Expand All @@ -61,6 +66,7 @@ export const CloudRunServiceComposite = Composite((props: CloudRunServiceProps)
name,
image,
location,
projectId,
port = 8080,
cpuLimit = "1000m",
memoryLimit = "512Mi",
Expand Down Expand Up @@ -97,7 +103,8 @@ export const CloudRunServiceComposite = Composite((props: CloudRunServiceProps)
...(namespace && { namespace }),
labels: { ...commonLabels, "app.kubernetes.io/component": "service" },
},
...(location && { location }),
location: location ?? GCP.Region,
projectRef: { external: projectId ?? GCP.ProjectId },
template: {
maxInstanceRequestConcurrency,
scaling: {
Expand All @@ -106,7 +113,7 @@ export const CloudRunServiceComposite = Composite((props: CloudRunServiceProps)
},
containers,
},
} as Record<string, unknown>, defs?.service));
}, defs?.service));

const result: Record<string, any> = { service };

Expand Down
17 changes: 17 additions & 0 deletions lexicons/gcp/src/composites/composites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ describe("CloudRunServiceComposite", () => {
expect(result.service).toBeDefined();
});

test("sets projectRef and location, which the RunService CRD requires", () => {
const result = CloudRunServiceComposite({
name: "api",
image: "gcr.io/p/api:1",
projectId: "proj",
location: "us-central1",
});
expect(p(result.service).projectRef).toEqual({ external: "proj" });
expect(p(result.service).location).toBe("us-central1");
});

test("defaults projectRef and location to the GCP pseudo-parameters", () => {
const result = CloudRunServiceComposite({ name: "api", image: "gcr.io/p/api:1" });
expect(JSON.parse(JSON.stringify(p(result.service).projectRef))).toEqual({ external: { Ref: "GCP::ProjectId" } });
expect(JSON.parse(JSON.stringify(p(result.service).location))).toEqual({ Ref: "GCP::Region" });
});

test("no public IAM by default", () => {
const result = CloudRunServiceComposite({ name: "api", image: "gcr.io/p/api:1" });
expect(result.publicIam).toBeUndefined();
Expand Down
Loading