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
2 changes: 1 addition & 1 deletion docs/ROUTINE_TASKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Executed on workspace initialization (DevContainer startup) or on-demand.

#### 5. Roadmap Implement Job (Runner)
- **Frequency**: Continuous. The Service's row `implement` is due every 10 minutes ([k8s/README.md](../k8s/README.md), Roadmap service step 16).
- **Methodology**: The Service runs no step. In namespace `devops` it makes a runner Job from the suspended CronJob `devops-cli-runner` for the first Staged or Planned item (a Planned item has its plan on the default branch because a person merged the staging pull request; the job makes its branch and its draft pull request), and makes no second Job for the repository while one runs. The runner has the dev tools, 2 CPU, 4 Gi and 7200 s; its pod has no Kubernetes token, and its shell gets none of the variables of the job. The Service's ServiceAccount has a Role that creates and reads Jobs, lists pods and reads pod logs in that namespace, and nothing else ([k8s/README.md](../k8s/README.md), Runner Jobs). A later round comments the state and the end of the log on the item. A Job that leaves its item In Progress, or Ready with no stop, stops the item (`implement.stopped` is `job`) until a person comments `implement: retry`. A person merges the pull request.
- **Methodology**: The Service runs no step. In namespace `devops` it makes a runner Job from the suspended CronJob `devops-cli-runner` for the first Staged or Planned item (a Planned item has its plan on the default branch because a person merged the staging pull request; the job makes its branch and its draft pull request), and makes no second Job for the repository while one runs. The runner has the dev tools, 2 CPU, 4 Gi of memory (8 Gi at most) and 7200 s; its pod has no Kubernetes token, and its shell gets none of the variables of the job, except the uv paths and the worker bound of the test runner. The Service's ServiceAccount has a Role that creates and reads Jobs, lists pods and reads pod logs in that namespace, and nothing else ([k8s/README.md](../k8s/README.md), Runner Jobs). A later round comments the state and the end of the log on the item. A Job that leaves its item In Progress, or Ready with no stop, stops the item (`implement.stopped` is `job`) until a person comments `implement: retry`. A person merges the pull request.
- **Commands**:
```bash
kubectl -n devops get jobs -l implement-repo
Expand Down
4 changes: 2 additions & 2 deletions k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,11 @@ kubectl -n devops logs -f job/<name>
The implement job runs tests, `ruff`, `mypy` and OpenSpec, which the Service image does not hold. Its steps run in a runner Job (#2667). `devops k8s run-job --template runner --repo <owner/name> -- roadmap implement --item <number> --confirm` makes one by hand. `--repo` is required with the runner template: it sets `service.github_app.token_repository` on the Job, and the job runs no step unless that setting equals the repository it acts on. The Service makes one for each Staged or Planned item in the row `implement` (Roadmap service, step 16).

- **Image.** Stage `runner` of the `Dockerfile` is the `builder` stage with git and the dev dependencies of `uv.lock`. The release publishes it as `ghcr.io/dan-petty/devops-cli/runner`, next to the Service image, and Image Updater keeps it on the digest of `latest` (Service Image Updates). Each Job runs `uv run` in its own worktree, with the uv cache on the home volume.
- **Template.** Suspended CronJob `devops-cli-runner` has 2 CPU, 4 Gi of memory, an `emptyDir` home of 4 Gi, an `emptyDir` `/tmp` of 1 Gi, `ephemeral-storage` of 2 Gi requested and 6 Gi at most, `activeDeadlineSeconds: 7200` and `backoffLimit: 0`. It sets `DEVOPS_CLI_ROADMAP_IMPLEMENT_IN_RUNNER=true`, which no other pod sets. It uses ServiceAccount `devops-cli`, which mounts no token, and the Secret `devops-cli` through `envFrom`. A Job is made from the template and changes only the args, the name, the labels and the environment value `DEVOPS_CLI_SERVICE_GITHUB_APP_TOKEN_REPOSITORY`.
- **Template.** Suspended CronJob `devops-cli-runner` has 2 CPU, 4 Gi of memory requested and 8 Gi at most, an `emptyDir` home of 4 Gi, an `emptyDir` `/tmp` of 1 Gi, `ephemeral-storage` of 2 Gi requested and 6 Gi at most, `activeDeadlineSeconds: 7200` and `backoffLimit: 0`. It sets `DEVOPS_CLI_ROADMAP_IMPLEMENT_IN_RUNNER=true`, which no other pod sets, and `PYTEST_XDIST_AUTO_NUM_WORKERS=2`, the CPU limit, because `pytest-xdist` counts the CPUs of the node and not of the pod (about 280 MB for each worker). It uses ServiceAccount `devops-cli`, which mounts no token, and the Secret `devops-cli` through `envFrom`. A Job is made from the template and changes only the args, the name, the labels and the environment value `DEVOPS_CLI_SERVICE_GITHUB_APP_TOKEN_REPOSITORY`.
- **Role.** ServiceAccount `roadmap-service` is the account of the Service pod. Role `roadmap-service-jobs` in namespace `devops` allows `create`, `get` and `list` on `jobs`, `get` on CronJob `devops-cli-runner`, `list` on `pods` and `get` on `pods/log`, and nothing else. Kubernetes cannot limit `create` and `list` to a name, so those two cover the namespace. A holder of the Role can make any pod that Pod Security `restricted` admits in namespace `devops`, and such a pod can take Secret `devops-cli`. The Service pod holds that Secret already, so the Role gives no new secret. That holds only while namespace `devops` holds no Secret but `devops-cli`, so the admission policy below pins the Jobs of the Service. The Service pod mounts a projected token that expires after one hour; the client reads it again as the kubelet renews it. A Job pod mounts no token.
- **Admission policy.** ValidatingAdmissionPolicy `roadmap-service-jobs` and its binding (`k8s/devops/roadmap-service/admissionpolicy.yaml`, #2696) refuse a `CREATE` or an `UPDATE` of a Job in namespace `devops` unless the Job has: one container and no init container, with no `command`; the image `ghcr.io/dan-petty/devops-cli/runner`, alone or followed by `:` or `@`; ServiceAccount `devops-cli` with `automountServiceAccountToken: false`; only Secret `devops-cli` in `envFrom` and no `valueFrom` in `env`; only the volumes `tmp`, `home` and `config` of the template; no `hostNetwork`, `hostPID` or `hostIPC`; no `priorityClassName`, `nodeName`, `imagePullSecrets` or `manualSelector`; the pod label `app.kubernetes.io/name: devops-cli-job`, because the NetworkPolicies select pods by that label and a Job pod with the label of the Service would take its egress to the API server; `completions` and `parallelism` of 1 or not set; `backoffLimit: 0`; and an `activeDeadlineSeconds` of 7200 at most. The policy applies only to the user `system:serviceaccount:devops:roadmap-service`. It never evaluates a Job of another identity, such as the CLI user (`devops k8s run-job`), Argo CD or the CronJob controller. It runs in the API server, so no webhook can be down. `failurePolicy` is `Fail`: if an expression errors, the Job of the Service is refused, the create raises an error, and the next round tries again. Argo CD syncs the policy with the other manifests of the stack, and AppProject `homelab` already admits the two cluster-scoped kinds. The test `tests/test_k8s_job_admission_policy.py` runs the expressions against the Job that CronJob `devops-cli-runner` makes, so a change of the template that the policy refuses fails in CI. To check the live policy, make a Job as the Service with `kubectl --as=system:serviceaccount:devops:roadmap-service create --dry-run=server -f <job>`. These are not pinned: the tag or digest of the image, the `args`, the plain `env` values, the resources, `nodeSelector`, `tolerations`, `hostUsers`, the security context (Pod Security `restricted` checks the pod) and the number of Jobs. A person with an admin role can delete the policy, and Argo CD restores it.
- **Network.** Policy `roadmap-service-egress` admits the Service pod, and no other pod, to TCP 6443, with the cloud metadata address excluded. A Job pod keeps the egress of `devops-default-perimeter`: DNS, the gateway, the cache, the OTLP collector and public HTTPS. No policy admits it to the API server.
- **Guarantees.** The shell of a step has none of the variables of the job. A step cannot read the environment or the memory of the job process, because the job clears its dumpable flag. The installation token of the job names one repository (`service.github_app.token_repository`), and the job runs no step unless that setting equals its repository and the process acts as the GitHub App. A Job ends after 7200 s at most and is not retried. The token is in the environment of a `git` or `gh` child of the job while that child runs, and a process of the same user can read it through `/proc`. A pod cannot hide it: the default seccomp profile refuses the user namespace that a sandbox for the shell needs. So the job guarantees an order: it adopts the orphans of its children, and when a step ends it kills every process the step left, also a daemon in a session of its own, before it runs any `git` or `gh`. A process that survives stops the item. These properties are not guaranteed: public HTTPS is open, so a step can send the clone to a public host; and a step runs as the same user as the job, so it can change the files that the job reads later, such as the clone, `.git/config` and the hooks of the worktree. A hook runs inside a `git` child of the job, with the token in its environment. The token limits the harm to one repository, to the permissions of ADR 0015 and to one hour. A real sandbox for the shell is planned (see Person steps). A person merges every pull request.
- **Guarantees.** The shell of a step has none of the variables of the job, except the uv paths and the worker bound of the test runner. A step cannot read the environment or the memory of the job process, because the job clears its dumpable flag. The installation token of the job names one repository (`service.github_app.token_repository`), and the job runs no step unless that setting equals its repository and the process acts as the GitHub App. A Job ends after 7200 s at most and is not retried. The token is in the environment of a `git` or `gh` child of the job while that child runs, and a process of the same user can read it through `/proc`. A pod cannot hide it: the default seccomp profile refuses the user namespace that a sandbox for the shell needs. So the job guarantees an order: it adopts the orphans of its children, and when a step ends it kills every process the step left, also a daemon in a session of its own, before it runs any `git` or `gh`. A process that survives stops the item. These properties are not guaranteed: public HTTPS is open, so a step can send the clone to a public host; and a step runs as the same user as the job, so it can change the files that the job reads later, such as the clone, `.git/config` and the hooks of the worktree. A hook runs inside a `git` child of the job, with the token in its environment. The token limits the harm to one repository, to the permissions of ADR 0015 and to one hour. A real sandbox for the shell is planned (see Person steps). A person merges every pull request.
- **Person steps.** After the first release that publishes the runner image, a person makes the GHCR package `runner` public, as the package `service` is. The Job takes the whole Secret `devops-cli`. A narrower Secret for the runner is the better end state, and a person makes secrets. A follow-up is filed: a real sandbox for the shell of a step ([#2695](https://github.com/dan-petty/devops-cli/issues/2695)). After the first sync of the admission policy, a person checks once that the Service's Job passes it, with the `kubectl --as` command of the policy above.

#### Webhook route
Expand Down
14 changes: 12 additions & 2 deletions k8s/devops/cronjob-runner.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ spec:
# jobs.
- name: OTEL_SERVICE_NAME
value: devops-cli-job
# `pytest-xdist` starts one worker for each CPU of the node, not of the pod
# (`-n logical`, 280 MB each). On a node with 48 CPUs that is about 13 GB in a pod
# with 2 CPUs (#2806). The value equals the CPU limit; a test checks it. The shell
# of a step takes it from here.
- name: PYTEST_XDIST_AUTO_NUM_WORKERS
value: "2"
volumeMounts:
- name: tmp
mountPath: /tmp
Expand All @@ -77,14 +83,18 @@ spec:
readOnly: true
resources:
# The storage limit covers both volumes below (4 Gi and 1 Gi) and the log, so a
# step cannot fill the disk of the node.
# step cannot fill the disk of the node. The memory request is the normal peak of
# a Job (1.24 GB at most over the 10 Jobs that did not run out of memory). The
# memory limit is a burst class (#2806): twice the 4.13 GB that the killed Job had
# reached, and below the 15 Gi that the smallest node can allocate, so a pod that
# lands on it cannot starve the node. The CPU limit only slows a step, so it stays.
requests:
cpu: "2"
memory: 4Gi
ephemeral-storage: 2Gi
limits:
cpu: "2"
memory: 4Gi
memory: 8Gi
ephemeral-storage: 6Gi
volumes:
- name: tmp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-30
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Design

## Decisions

- **Raise the limit to 8 Gi and keep the request at 4 Gi.** The measured peak of normal Jobs is 1.24 GB at most, so 4 Gi is right for the request: the scheduler needs only that. The killed pod had reached 4.13 GB at its cap, and 8 Gi is twice that. A step can burst, and a kill needs a real runaway. 8 Gi fits the smallest node (15.5 Gi allocatable), which matters because the pod has no node selector. Pinning the pod to the large nodes would need a role label and a second change, and the worker bound already removes the suspected cause, so the change takes the lower limit.
- **Bound the test workers with the variable that xdist reads.** The cause is a worker count from the CPUs of the node. `PYTEST_XDIST_AUTO_NUM_WORKERS` is the documented setting of `pytest-xdist` for this, and `-n logical` honors it. Setting it to the CPU limit (2) cost no time in the measured run (23 s with 2 workers, 24 s with 16). The manifest holds the value, and a test checks that it equals the CPU limit, so the two cannot drift.
- **The isolated child keeps the variable too.** `devops ci` starts `pytest` through `run_subprocess`, and `build_subprocess_env` drops every variable that is not on its allowlist. So `PYTEST_XDIST_AUTO_NUM_WORKERS` joins `DEFAULT_ALLOWED_ENV_VARS`. It holds a number and matches no denied pattern.
- **The shell takes the variable from the process, like `UV_PYTHON`.** The shell of a step gets only `PATH`, `HOME` and the locale of the process, plus `shell_environment`. So a variable on the pod does not reach `pytest` unless `shell_environment` passes it. The function passes it only when the process has it, so a developer machine keeps its own count.
- **A separate line before the call, not a change of the line after it.** The line after the call has the outcome, which a killed call never reaches. The new line needs only the command, so it can come first. The line after the call stays as it is, so the count of refused programs and the other readers of the line do not change.
- **The command line shows a bounded form.** The first word must be a known program (the rule of the line after the call). Any other first word shows as `other`, because a first word can be a password or a token that the model read from a file. For a known program the line shows at most `CONST_ROADMAP_IMPLEMENT_LOG_COMMAND_WORDS` words. Each word is read to a fixed length first. An argument with a space is free text, and the line hides it. In `name=value`, the line keeps the name and hides the value unless it is a number, because a value has no form for a mask to find. `sanitize_command_args_for_display` masks the other words and the word after a flag such as `--password`. The text is then escaped with `unicode_escape` so that it is one line, and cut to `CONST_ROADMAP_IMPLEMENT_LOG_COMMAND_CHARS`. A word that the project masks keeps its name, so the line still shows the real flag.
- **Residual risk, accepted.** A secret as a bare word with no form, such as a password after `git commit -m` that has no space, or the word after a flag that the project does not know, stays in the line. The Job log is the only evidence of what the agent did, and a command with no argument cannot show a cause like `-n 48`. The issue asks for the arguments. The bound on words and characters and the allowlist of programs limit the harm.
- **CPU and storage limits stay.** See Feasibility in the proposal for the numbers.
- **The roadmap-service limits stay.** Its peak is 57 percent of its limit.

## Rejected alternatives

- **Raise the limit only.** The evidence shows 48 workers cost about 13 GB. A limit that fits the smallest node would still kill the same run, and a limit that covers 48 workers would leave a small node with no margin. It fixes the symptom of one run and not the cause.
- **Bound the workers only, keep 4 Gi.** The cause of this kill is not proven, as the log has no command. Another command, such as `mypy --strict` on a large file set, can also pass 4 Gi. The owner asked for a burst class, and the cost is a limit that the node can pay.
- **Remove the memory limit.** A pod with no limit has no cgroup cap, so a runaway command would make the kernel kill other pods on the node. The 8 Gi limit keeps a runaway inside its pod.
- **Detect the cgroup quota in the code and pick the worker count.** It is hand-rolled code for what a variable of the installed tool does.
- **Change `addopts` to `-n 2`.** It slows the full suite on a developer machine and in CI, where the CPUs are real.
- **Write the command in the line after the call.** A killed call never writes it, which is the case the issue is about.
- **Log the whole command.** A command can be long and can hold a secret. The words and characters are bounded.
Loading
Loading