Public: anyone can read this; the crew can change it.
Apply the lane to the chart: ServiceAccount, Role, NetworkPolicy and FORGE_JOB_* env (#1423) #1543
scout/forge:scout/1539-apply-the-lane-to-the-chart-serviceaccou → main 6 commits fork
7 files
changed +245
−0
README.md +9
| @@ -1085,6 +1085,15 @@ Invariants are proven by faults, not by hope. No dependencies were added. | ||
| 1085 | 1085 | `FORGE_CHAOS_RECOVERY_ISSUE`. Scheduling it is the operator's: this repo |
| 1086 | 1086 | ships the command, not a timer. |
| 1087 | 1087 | |
| 1088 | ## Checks as cluster Jobs | |
| 1089 | ||
| 1090 | On Kubernetes the chart can run checks as Jobs instead of in the forge pod. | |
| 1091 | It is off by default; turn it on with `helm upgrade --set lane.enabled=true` | |
| 1092 | (optionally `lane.nodeSelector`, `lane.tolerations`, `lane.pullSecrets`, | |
| 1093 | `lane.forgeUrl`) and set `checks.placement: job`. Until a Job enforces a | |
| 1094 | check's declared egress hosts, `job` placement applies only while | |
| 1095 | `cells.egress` is `report`. What the chart grants: [](docs/CLUSTER.md). | |
| 1096 | ||
| 1088 | 1097 | ## Check tiers |
| 1089 | 1098 | |
| 1090 | 1099 | `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/_helpers.tpl +11
| @@ -27,3 +27,14 @@ it) without any per-env values. | ||
| 27 | 27 | {{- define "forge.host" -}} |
| 28 | 28 | {{- regexReplaceAll "^https://" (include "forge.url" .) "" -}} |
| 29 | 29 | {{- end -}} |
| 30 | ||
| 31 | {{/* The pull secrets a lane Job uses: lane.pullSecrets, else imagePullSecrets' names. */}} | |
| 32 | {{- define "forge.lanePullSecrets" -}} | |
| 33 | {{- if .Values.lane.pullSecrets -}} | |
| 34 | {{- .Values.lane.pullSecrets -}} | |
| 35 | {{- else -}} | |
| 36 | {{- $names := list -}} | |
| 37 | {{- range .Values.imagePullSecrets -}}{{- $names = append $names .name -}}{{- end -}} | |
| 38 | {{- join "," $names -}} | |
| 39 | {{- end -}} | |
| 40 | {{- end -}} |
charts/app/templates/deployment.yaml +21
| @@ -32,6 +32,9 @@ spec: | ||
| 32 | 32 | annotations: |
| 33 | 33 | plat.sh/user-env-rev: {{ .Values.userEnvRev | default "init" | quote }} |
| 34 | 34 | spec: |
| 35 | {{- if .Values.lane.enabled }} | |
| 36 | serviceAccountName: {{ .Release.Name }} | |
| 37 | {{- end }} | |
| 35 | 38 | # An hour for an SSH session to end on its own before the old pod is |
| 36 | 39 | # killed anyway. |
| 37 | 40 | # The crew drains for three minutes, then runs are cut and resume on |
| @@ -100,6 +103,24 @@ spec: | ||
| 100 | 103 | value: {{ .Values.forge.crewConcurrency | quote }} |
| 101 | 104 | - name: FORGE_APPS_CONCURRENCY |
| 102 | 105 | value: {{ .Values.forge.appsConcurrency | quote }} |
| 106 | {{- if .Values.lane.enabled }} | |
| 107 | # The lane: what a check Job runs, where it reaches the forge, | |
| 108 | # and where the cluster may put it. | |
| 109 | - name: FORGE_JOB_IMAGE | |
| 110 | value: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" | |
| 111 | - name: FORGE_JOB_FORGE_URL | |
| 112 | value: {{ .Values.lane.forgeUrl | default (printf "http://%s.%s.svc:%v" .Release.Name .Release.Namespace .Values.service.port) | quote }} | |
| 113 | - name: FORGE_JOB_NODE_SELECTOR | |
| 114 | value: {{ .Values.lane.nodeSelector | quote }} | |
| 115 | - name: FORGE_JOB_TOLERATIONS | |
| 116 | value: {{ .Values.lane.tolerations | quote }} | |
| 117 | - name: FORGE_JOB_PULL_SECRETS | |
| 118 | value: {{ include "forge.lanePullSecrets" . | quote }} | |
| 119 | - name: NODE_NAME | |
| 120 | valueFrom: | |
| 121 | fieldRef: | |
| 122 | fieldPath: spec.nodeName | |
| 123 | {{- end }} | |
| 103 | 124 | {{- with .Values.forge.originClients }} |
| 104 | 125 | # Origins the operator registered once, for everyone. |
| 105 | 126 | - name: FORGE_ORIGIN_CLIENT_ID |
charts/app/templates/lane.yaml added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | {{- if .Values.lane.enabled }} | |
| 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.plat.sh/lane: check) 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.plat.sh/lane: check | |
| 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 +18
| @@ -65,3 +65,21 @@ resources: | ||
| 65 | 65 | extraEnvFrom: [] |
| 66 | 66 | # Bumped by the platform when self-serve secrets change, to roll the pod. |
| 67 | 67 | 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_JOB_FORGE_URL — how a Job reaches the forge. Empty = this release's | |
| 77 | # in-cluster Service. | |
| 78 | forgeUrl: "" | |
| 79 | # FORGE_JOB_NODE_SELECTOR — `key=value,key=value`. | |
| 80 | nodeSelector: "" | |
| 81 | # FORGE_JOB_TOLERATIONS — as the forge's setting reads them. | |
| 82 | tolerations: "" | |
| 83 | # FORGE_JOB_PULL_SECRETS — comma-separated secret names. Empty = the names | |
| 84 | # in imagePullSecrets. | |
| 85 | pullSecrets: "" |
docs/CLUSTER.md added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | # The lane on a cluster | |
| 2 | ||
| 3 | With `checks.placement: job` a check runs as a Kubernetes Job on any node the | |
| 4 | selector admits, not inside the forge pod. The chart grants this only when | |
| 5 | asked: `--set lane.enabled=true`. | |
| 6 | ||
| 7 | ## What the cluster grants | |
| 8 | ||
| 9 | `charts/app/templates/lane.yaml`, all behind `lane.enabled`: | |
| 10 | ||
| 11 | - a **ServiceAccount** named for the release, which the forge pod runs as; | |
| 12 | - a **Role** `<release>-lane`: `jobs` create/get/list/watch/delete, `pods` | |
| 13 | get/list, `pods/log` get; and its **RoleBinding**; | |
| 14 | - a **NetworkPolicy** for pods labelled `forge.plat.sh/lane: check`: no | |
| 15 | ingress; egress only to the forge (`app: <release>`, the container port named `http`, which the Service port maps to) and DNS. | |
| 16 | ||
| 17 | The deployment then sets `serviceAccountName`, `FORGE_JOB_IMAGE` (the forge's | |
| 18 | own image), `FORGE_JOB_FORGE_URL`, `FORGE_JOB_NODE_SELECTOR`, | |
| 19 | `FORGE_JOB_TOLERATIONS`, `FORGE_JOB_PULL_SECRETS` and `NODE_NAME` | |
| 20 | (`spec.nodeName`). Values: `lane.forgeUrl`, `lane.nodeSelector`, | |
| 21 | `lane.tolerations`, `lane.pullSecrets`. | |
| 22 | ||
| 23 | ## Egress | |
| 24 | ||
| 25 | A Job does not yet enforce a check's declared egress hosts, so `job` placement | |
| 26 | only applies while `cells.egress` is `report`. | |
| 27 | ||
| 28 | ## Why each verb | |
| 29 | ||
| 30 | - `jobs` create/get/list/watch: start a check and follow it to completion. | |
| 31 | - `jobs` delete: clean up finished Jobs so they do not accumulate. | |
| 32 | - `pods` get/list: find the pod a Job started, to read its logs. | |
| 33 | - `pods/log` get: read that pod's output. | |
| 34 | ||
| 35 | Nothing is granted on secrets, configmaps, other namespaces or any wildcard. | |
| 36 | ||
| 37 | ## Confirming the lane is on | |
| 38 | ||
| 39 | The lane has no page of its own; confirm it from the cluster: | |
| 40 | ||
| 41 | ||
| 42 | kubectl auth can-i create jobs --as=system:serviceaccount:<ns>:<release> # yes | |
| 43 | kubectl auth can-i get secrets --as=system:serviceaccount:<ns>:<release> # no | |
| 44 | kubectl get networkpolicy,role,rolebinding -n <ns> | grep lane | |
| 45 | kubectl exec deploy/<release> -- printenv FORGE_JOB_IMAGE | |
| 46 | ||
| 47 | ||
| 48 | With `lane.enabled=false` (the default) none of these objects exist. |
test/chart-lane.test.ts added +64
| @@ -0,0 +1,64 @@ | ||
| 1 | import { expect, test } from "bun:test"; | |
| 2 | import { readFileSync } from "node:fs"; | |
| 3 | ||
| 4 | const read = (f: string) => readFileSync(new URL(`../charts/app/${f}`, import.meta.url), "utf8"); | |
| 5 | ||
| 6 | test("the lane is opt-in and off by default", () => { | |
| 7 | expect(read("values.yaml")).toMatch(/lane:\n {2}enabled: false/); | |
| 8 | expect(read("templates/lane.yaml").trimStart()).toMatch(/^(#.*\n)*\{\{- if \.Values\.lane\.enabled \}\}/); | |
| 9 | }); | |
| 10 | ||
| 11 | test("the Role grants exactly what the lane needs", () => { | |
| 12 | const lane = read("templates/lane.yaml"); | |
| 13 | for (const kind of ["ServiceAccount", "Role", "RoleBinding", "NetworkPolicy"]) { | |
| 14 | expect(lane).toContain(`kind: ${kind}\n`); | |
| 15 | } | |
| 16 | expect(lane).toContain('verbs: ["create", "get", "list", "watch", "delete"]'); | |
| 17 | expect(lane).toContain('resources: ["pods/log"]'); | |
| 18 | expect(lane).toContain("forge.plat.sh/lane: check"); | |
| 19 | expect(lane).not.toContain('"*"'); | |
| 20 | }); | |
| 21 | ||
| 22 | test("the deployment sets the identity and every FORGE_JOB_ variable under the flag", () => { | |
| 23 | const d = read("templates/deployment.yaml"); | |
| 24 | expect(d).toContain("serviceAccountName: {{ .Release.Name }}"); | |
| 25 | for (const n of ["IMAGE", "FORGE_URL", "NODE_SELECTOR", "TOLERATIONS", "PULL_SECRETS"]) { | |
| 26 | expect(d).toContain(`name: FORGE_JOB_${n}`); | |
| 27 | } | |
| 28 | expect(d).toContain("fieldPath: spec.nodeName"); | |
| 29 | const from = d.indexOf("if .Values.lane.enabled", d.indexOf("env:")); | |
| 30 | expect(from).toBeGreaterThan(0); | |
| 31 | expect(d.indexOf("FORGE_JOB_IMAGE")).toBeGreaterThan(from); | |
| 32 | }); | |
| 33 | ||
| 34 | test("the NetworkPolicy names the forge's port, it does not hardcode one", () => { | |
| 35 | const lane = read("templates/lane.yaml"); | |
| 36 | expect(lane).toContain("port: http }"); | |
| 37 | expect(lane).not.toContain("port: 8080"); | |
| 38 | expect(read("templates/deployment.yaml")).toMatch(/- name: http\n\s+containerPort: 8080/); | |
| 39 | }); | |
| 40 | ||
| 41 | const helm = Bun.which("helm"); | |
| 42 | const render = (...sets: string[]) => | |
| 43 | new TextDecoder().decode( | |
| 44 | Bun.spawnSync([helm!, "template", "r", new URL("../charts/app", import.meta.url).pathname, ...sets.flatMap((s) => ["--set", s])]).stdout, | |
| 45 | ); | |
| 46 | ||
| 47 | test.skipIf(!helm)("rendered: lane on yields the objects, verbs and env", () => { | |
| 48 | const out = render("lane.enabled=true"); | |
| 49 | for (const kind of ["ServiceAccount", "Role", "RoleBinding", "NetworkPolicy"]) { | |
| 50 | expect(out).toMatch(new RegExp(`^kind: ${kind}$`, "m")); | |
| 51 | } | |
| 52 | expect(out).toContain('verbs: ["create", "get", "list", "watch", "delete"]'); | |
| 53 | expect(out).toContain("serviceAccountName: r"); | |
| 54 | for (const n of ["IMAGE", "FORGE_URL", "NODE_SELECTOR", "TOLERATIONS", "PULL_SECRETS"]) { | |
| 55 | expect(out).toMatch(new RegExp(`name: FORGE_JOB_${n}$`, "m")); | |
| 56 | } | |
| 57 | expect(out).not.toContain('"*"'); | |
| 58 | }); | |
| 59 | ||
| 60 | test.skipIf(!helm)("rendered: lane off yields none of it", () => { | |
| 61 | const out = render(); | |
| 62 | expect(out).not.toMatch(/^kind: (Role|RoleBinding|NetworkPolicy|ServiceAccount)$/m); | |
| 63 | expect(out).not.toContain("FORGE_JOB_"); | |
| 64 | }); |
new above
offline