Public: anyone can read this; the crew can change it.

Apply the lane to the chart: ServiceAccount, Role, NetworkPolicy and FORGE_JOB_* env (#1423) #2124

Closed opened this
Closed without merging

scout/forge:scout/1539-apply-the-lane-to-the-chart-serviceaccou → main 10 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +208 −0
README.md +10
@@ -1303,6 +1303,16 @@ Invariants are proven by faults, not by hope. No dependencies were added.
13031303 `FORGE_CHAOS_RECOVERY_ISSUE`. Scheduling it is the operator's: this repo
13041304 ships the command, not a timer.
13051305
1306 ## Checks as cluster Jobs
1307
1308 On Kubernetes the chart can run checks as Jobs instead of in the forge pod.
1309 It is off by default; `helm upgrade --set lane.enabled=true --set lane.claim=<rwx-claim>`
1310 turns it on and sets `FORGE_CHECKS_PLACEMENT=job` and the rest of what
1311 `src/checks/placement.ts` reads (optionally `lane.nodeSelector`,
1312 `lane.tolerations`). Until a Job enforces a
1313 check's declared egress hosts, `job` placement applies only while
1314 `cells.egress` is `report`. What the chart grants: [docs/CLUSTER.md](docs/CLUSTER.md).
1315
13061316 ## Check tiers
13071317
13081318 `forge.json`'s `check` may declare `fast` beside `run` (the full gate; `full` names it explicitly). `bun scripts/test-shards.ts --tier fast --diff <base>..<head>` runs the tests that touch the diff (changed, named for a changed source file, or importing one within two hops), every test the times table records under 1 s, and any named by `--learned a,b`. `src/checks/tiers.ts` decides; it adds no dependency.
charts/app/templates/deployment.yaml +16
@@ -32,6 +32,9 @@ spec:
3232 annotations:
3333 plat.sh/user-env-rev: {{ .Values.userEnvRev | default "init" | quote }}
3434 spec:
35 {{- if .Values.lane.enabled }}
36 serviceAccountName: {{ .Release.Name }}
37 {{- end }}
3538 # An hour for an SSH session to end on its own before the old pod is
3639 # killed anyway.
3740 # The crew drains for three minutes, then runs are cut and resume on
@@ -100,6 +103,19 @@ spec:
100103 value: {{ .Values.forge.crewConcurrency | quote }}
101104 - name: FORGE_APPS_CONCURRENCY
102105 value: {{ .Values.forge.appsConcurrency | quote }}
106 {{- if .Values.lane.enabled }}
107 # The lane: the names src/checks/placement.ts reads.
108 - name: FORGE_CHECKS_PLACEMENT
109 value: "job"
110 - name: FORGE_CHECKS_IMAGE
111 value: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
112 - name: FORGE_CHECKS_CLAIM
113 value: {{ required "lane.claim: name the ReadWriteMany claim that holds the checks root" .Values.lane.claim | quote }}
114 - name: FORGE_CHECKS_NODE_SELECTOR
115 value: {{ .Values.lane.nodeSelector | quote }}
116 - name: FORGE_CHECKS_TOLERATIONS
117 value: {{ .Values.lane.tolerations | default "[]" | quote }}
118 {{- end }}
103119 {{- with .Values.forge.originClients }}
104120 # Origins the operator registered once, for everyone.
105121 - name: FORGE_ORIGIN_CLIENT_ID
charts/app/templates/lane.yaml added +74
@@ -0,0 +1,74 @@
1 {{- if .Values.lane.enabled }}

This is a chart edit, which the planner rules forbid. It needs an explicit owner exception, or it moves to an authorised pull.

2 # The lane: checks run as cluster Jobs (`checks.placement: job`). The forge
3 # pod needs an identity that may create and watch those Jobs and read their
4 # logs, and the Job pods (labelled `forge-check: true`, as src/checks/job_step.ts sets) may talk to
5 # nothing but this forge and cluster DNS.
6 apiVersion: v1
7 kind: ServiceAccount
8 metadata:
9 name: {{ .Release.Name }}
10 labels:
11 app: {{ .Release.Name }}
12 ---
13 apiVersion: rbac.authorization.k8s.io/v1
14 kind: Role
15 metadata:
16 name: {{ .Release.Name }}-lane
17 labels:
18 app: {{ .Release.Name }}
19 rules:
20 - apiGroups: ["batch"]
21 resources: ["jobs"]
22 verbs: ["create", "get", "list", "watch", "delete"]
23 - apiGroups: [""]
24 resources: ["pods"]
25 verbs: ["get", "list"]
26 - apiGroups: [""]
27 resources: ["pods/log"]
28 verbs: ["get"]
29 ---
30 apiVersion: rbac.authorization.k8s.io/v1
31 kind: RoleBinding
32 metadata:
33 name: {{ .Release.Name }}-lane
34 labels:
35 app: {{ .Release.Name }}
36 roleRef:
37 apiGroup: rbac.authorization.k8s.io
38 kind: Role
39 name: {{ .Release.Name }}-lane
40 subjects:
41 - kind: ServiceAccount
42 name: {{ .Release.Name }}
43 namespace: {{ .Release.Namespace }}
44 ---
45 apiVersion: networking.k8s.io/v1
46 kind: NetworkPolicy
47 metadata:
48 name: {{ .Release.Name }}-lane
49 labels:
50 app: {{ .Release.Name }}
51 spec:
52 podSelector:
53 matchLabels:
54 forge-check: "true"
55 policyTypes: [Ingress, Egress]
56 # Nothing reaches a check; it reaches the forge and DNS only.
57 ingress: []
58 egress:
59 - to:
60 - podSelector:
61 matchLabels:
62 app: {{ .Release.Name }}
63 ports:
64 # The forge's named container port: the Service's port 80 lands here.
65 - { protocol: TCP, port: http }
66 - to:
67 - namespaceSelector: {}
68 podSelector:
69 matchLabels:
70 k8s-app: kube-dns
71 ports:
72 - { protocol: UDP, port: 53 }
73 - { protocol: TCP, port: 53 }
74 {{- end }}
charts/app/values.yaml +15
@@ -65,3 +65,18 @@ resources:
6565 extraEnvFrom: []
6666 # Bumped by the platform when self-serve secrets change, to roll the pod.
6767 userEnvRev: "init"
68
69 # The lane: run checks as cluster Jobs (`checks.placement: job`) instead of
70 # inside this pod. Off by default; on, the chart adds a ServiceAccount, a Role
71 # that may manage Jobs, and a NetworkPolicy for the Job pods. While a Job
72 # cannot enforce declared egress hosts, `job` placement only applies when
73 # `cells.egress` is `report`. See docs/CLUSTER.md.
74 lane:
75 enabled: false
76 # FORGE_CHECKS_CLAIM — a ReadWriteMany claim holding the checks root, which
77 # the Job mounts. Required when the lane is on.
78 claim: ""
79 # FORGE_CHECKS_NODE_SELECTOR — `key=value,key=value`.
80 nodeSelector: ""
81 # FORGE_CHECKS_TOLERATIONS — a JSON list of tolerations. Empty = none.
82 tolerations: ""
docs/CLUSTER.md +23
@@ -71,3 +71,26 @@ podSelector:
7171 while it runs (it is deleted when the run ends).
7272 4. If every run still says nothing, a variable above is missing or malformed:
7373 the forge fell back to `process`.
74
75 ## With the chart
76
77 `charts/app/templates/lane.yaml` applies all of the above behind
78 `--set lane.enabled=true` (default off, then none of it exists): a
79 ServiceAccount named for the release that the forge pod runs as, a Role
80 `<release>-lane` with its RoleBinding, a NetworkPolicy on pods labelled
81 `forge-check: "true"` (no ingress; egress only to the forge's `http` port and
82 DNS), and the `FORGE_CHECKS_*` variables on the deployment. Values:
83 `lane.claim` (required, a ReadWriteMany claim the chart does not create),
84 `lane.nodeSelector`, `lane.tolerations`. The chart's Role is a little wider
85 than the table above (`watch`, and `get` on pods); nothing on secrets,
86 configmaps, other namespaces or wildcards. A Job does not yet enforce a
87 check's declared egress hosts, so `job` placement only applies while
88 `cells.egress` is `report`.
89
90 Confirm from the cluster:
91
92 ```
93 kubectl auth can-i create jobs --as=system:serviceaccount:<ns>:<release> # yes
94 kubectl auth can-i get secrets --as=system:serviceaccount:<ns>:<release> # no
95 kubectl exec deploy/<release> -- printenv FORGE_CHECKS_PLACEMENT # job
96 ```
test/chart-lane.test.ts added +70
@@ -0,0 +1,70 @@
1 import { expect, test } from "bun:test";
2 import { readFileSync } from "node:fs";
3 import { JOB_LABELS } from "../src/checks/job_step.ts";
4
5 const [key, value] = Object.entries(JOB_LABELS)[0]!;
6
7 const read = (f: string) => readFileSync(new URL(`../charts/app/${f}`, import.meta.url), "utf8");
8
9 test("the lane is opt-in and off by default", () => {
10 expect(read("values.yaml")).toMatch(/lane:\n {2}enabled: false/);
11 expect(read("templates/lane.yaml").trimStart()).toMatch(/^(#.*\n)*\{\{- if \.Values\.lane\.enabled \}\}/);
12 });
13
14 test("the Role grants exactly what the lane needs", () => {
15 const lane = read("templates/lane.yaml");
16 for (const kind of ["ServiceAccount", "Role", "RoleBinding", "NetworkPolicy"]) {
17 expect(lane).toContain(`kind: ${kind}\n`);
18 }
19 expect(lane).toContain('verbs: ["create", "get", "list", "watch", "delete"]');
20 expect(lane).toContain('resources: ["pods/log"]');
21 expect(lane).toContain(`${key}: "${value}"`);
22 expect(lane).not.toContain('"*"');
23 });
24
25 test("the deployment sets the identity and the env placementFromEnv reads, under the flag", () => {
26 const d = read("templates/deployment.yaml");
27 expect(d).toContain("serviceAccountName: {{ .Release.Name }}");
28 const names = [...d.matchAll(/name: (FORGE_CHECKS_\w+)/g)].map((m) => m[1]!);
29 expect(names.sort()).toEqual(
30 ["FORGE_CHECKS_CLAIM", "FORGE_CHECKS_IMAGE", "FORGE_CHECKS_NODE_SELECTOR", "FORGE_CHECKS_PLACEMENT", "FORGE_CHECKS_TOLERATIONS"],
31 );
32 const src = readFileSync(new URL("../src/checks/placement.ts", import.meta.url), "utf8");
33 for (const n of names) expect(src).toContain(`env["${n}"]`);
34 expect(d).not.toContain("FORGE_JOB_");
35 const from = d.indexOf("if .Values.lane.enabled", d.indexOf("env:"));
36 expect(from).toBeGreaterThan(0);
37 expect(d.indexOf("FORGE_CHECKS_PLACEMENT")).toBeGreaterThan(from);
38 });
39
40 test("the NetworkPolicy names the forge's port, it does not hardcode one", () => {
41 const lane = read("templates/lane.yaml");
42 expect(lane).toContain("port: http }");
43 expect(lane).not.toContain("port: 8080");
44 expect(read("templates/deployment.yaml")).toMatch(/- name: http\n\s+containerPort: 8080/);
45 });
46
47 const helm = Bun.which("helm");

The helm render tests skip without helm, so nothing in the sandbox proves the chart renders. Please show a rendered result in the pull.

48 const render = (...sets: string[]) =>
49 new TextDecoder().decode(
50 Bun.spawnSync([helm!, "template", "r", new URL("../charts/app", import.meta.url).pathname, ...sets.flatMap((s) => ["--set", s])]).stdout,
51 );
52
53 test.skipIf(!helm)("rendered: lane on yields the objects, verbs and env", () => {
54 const out = render("lane.enabled=true", "lane.claim=checks-rwx");
55 for (const kind of ["ServiceAccount", "Role", "RoleBinding", "NetworkPolicy"]) {
56 expect(out).toMatch(new RegExp(`^kind: ${kind}$`, "m"));
57 }
58 expect(out).toContain('verbs: ["create", "get", "list", "watch", "delete"]');
59 expect(out).toContain("serviceAccountName: r");
60 for (const n of ["PLACEMENT", "IMAGE", "CLAIM", "NODE_SELECTOR", "TOLERATIONS"]) {
61 expect(out).toMatch(new RegExp(`name: FORGE_CHECKS_${n}$`, "m"));
62 }
63 expect(out).not.toContain('"*"');
64 });
65
66 test.skipIf(!helm)("rendered: lane off yields none of it", () => {
67 const out = render();
68 expect(out).not.toMatch(/^kind: (Role|RoleBinding|NetworkPolicy|ServiceAccount)$/m);
69 expect(out).not.toContain("FORGE_CHECKS_");
70 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs