feat(gitea_runner): bound the Docker image cache by count and disk usage #328

Merged
Defame1297 merged 4 commits from feat/319-docker-prune-count-threshold into main 2026-11-23 19:06:42 +00:00
Owner

What and why

Closes #319

An age-based prune does not cap disk use. The cache grows with how often new image versions are published, so builds on the runner can fail for lack of space. This adds two bounds, both off by default:

  • Count prune (gitea_runner_docker_prune_keep_per_repository): a POSIX sh script, files/gitea-runner-docker-prune-keep, keeps the newest N image IDs per repository, ordered by Created.
    • A multi-tag image counts once, and an ID kept by any of its repositories stays.
    • Images used by any container (ps --all) are never removed. Dangling images are removed.
    • If any listing fails, nothing is removed.
    • The weekly service runs it alongside the existing age prunes, which are unchanged.
  • Pressure prune (gitea_runner_docker_prune_disk_threshold_percent, _disk_check_schedule, _builder_keep_storage): a separate gitea-runner-docker-prune-pressure.service and .timer, hourly by default.
    • It sits behind the same job gate, plus a second ExecCondition=, files/gitea-runner-docker-prune-disk-check, which passes only when the filesystem holding DockerRootDir is above the threshold.
    • When it fires, it runs the count prune and docker builder prune --keep-storage once.
    • Setting the threshold back to 0 removes the timer, both units and the script.
  • Validation: validate.yml refuses a threshold outside 1–99, a threshold without a count, and a bad schedule or size, each with its own message.
  • Removal: remove.yml removes everything new.
  • Changelog: a minor_changes fragment.

Verified

  • pre-commit run --all-files: all hooks pass.
  • ansible-test sanity: passes. It ran on the pre-push hook for this push.
  • The keep script and the disk check, run locally against a stub docker with the verify fixtures, give the expected rmi set and exit codes.
    • Keep script cases: M > N, an old image in use, a multi-tag image, an ID in two repositories, a dangling image, an in-use dangling image, each listing failing, and N=0.
    • Disk check cases: above, at, and below the threshold; docker info failing; a missing root.
  • tasks/validate.yml, run on its own: defaults pass, and each refused input fails with its own message.
  • --keep-storage is a current, non-deprecated flag of docker builder prune (checked in docker/cli cli/command/builder/prune.go).
  • Two review passes, Standards and Spec against #319, found nothing blocking. Their findings are addressed in the follow-up commits.
  • Molecule default (adds stub-docker cases for both scripts, threshold 0 → off → on, and the remove path): runs in CI on this PR.

Not verified

  • Molecule: not run locally, because there is no container runtime. The new verify plays and the real systemd behaviour of the pressure units are first exercised by CI on this PR.
  • Docker's containerd image store (the default on fresh Docker 29 installs): not checked on a real host. The keep script assumes image ls --no-trunc {{.ID}} equals container inspect {{.Image}}. Worth one dry run with rmi replaced by echo on such a runner.
  • docker builder prune --keep-storage without --all (as #319 specifies): it may only consider part of the build cache, so I can't say whether the cap fully holds against heavy docker build cache. docker system df -v on a runner would show it.
  • Race window: there are a few seconds between the listing and rmi, the same window the weekly prune already documents. A container started in that window keeps its image, but a multi-tag image can lose its tags and be re-pulled.

  • I confirm third-party material is recorded in NOTICE and REUSE.toml, or there is none.

Assisted-by: Claude Code

## What and why Closes #319 An age-based prune does not cap disk use. The cache grows with how often new image versions are published, so builds on the runner can fail for lack of space. This adds two bounds, both off by default: - **Count prune** (`gitea_runner_docker_prune_keep_per_repository`): a POSIX sh script, `files/gitea-runner-docker-prune-keep`, keeps the newest N image IDs per repository, ordered by `Created`. - A multi-tag image counts once, and an ID kept by any of its repositories stays. - Images used by any container (`ps --all`) are never removed. Dangling images are removed. - If any listing fails, nothing is removed. - The weekly service runs it alongside the existing age prunes, which are unchanged. - **Pressure prune** (`gitea_runner_docker_prune_disk_threshold_percent`, `_disk_check_schedule`, `_builder_keep_storage`): a separate `gitea-runner-docker-prune-pressure.service` and `.timer`, hourly by default. - It sits behind the same job gate, plus a second `ExecCondition=`, `files/gitea-runner-docker-prune-disk-check`, which passes only when the filesystem holding `DockerRootDir` is above the threshold. - When it fires, it runs the count prune and `docker builder prune --keep-storage` once. - Setting the threshold back to `0` removes the timer, both units and the script. - **Validation:** `validate.yml` refuses a threshold outside 1–99, a threshold without a count, and a bad schedule or size, each with its own message. - **Removal:** `remove.yml` removes everything new. - **Changelog:** a `minor_changes` fragment. ## Verified - `pre-commit run --all-files`: all hooks pass. - `ansible-test sanity`: passes. It ran on the pre-push hook for this push. - The keep script and the disk check, run locally against a stub `docker` with the verify fixtures, give the expected `rmi` set and exit codes. - Keep script cases: M > N, an old image in use, a multi-tag image, an ID in two repositories, a dangling image, an in-use dangling image, each listing failing, and N=0. - Disk check cases: above, at, and below the threshold; `docker info` failing; a missing root. - `tasks/validate.yml`, run on its own: defaults pass, and each refused input fails with its own message. - `--keep-storage` is a current, non-deprecated flag of `docker builder prune` (checked in docker/cli `cli/command/builder/prune.go`). - Two review passes, Standards and Spec against #319, found nothing blocking. Their findings are addressed in the follow-up commits. - Molecule `default` (adds stub-docker cases for both scripts, threshold 0 → off → on, and the remove path): runs in CI on this PR. ## Not verified - **Molecule:** not run locally, because there is no container runtime. The new verify plays and the real systemd behaviour of the pressure units are first exercised by CI on this PR. - **Docker's containerd image store** (the default on fresh Docker 29 installs): not checked on a real host. The keep script assumes `image ls --no-trunc` `{{.ID}}` equals `container inspect` `{{.Image}}`. Worth one dry run with `rmi` replaced by `echo` on such a runner. - **`docker builder prune --keep-storage` without `--all`** (as #319 specifies): it may only consider part of the build cache, so I can't say whether the cap fully holds against heavy `docker build` cache. `docker system df -v` on a runner would show it. - **Race window:** there are a few seconds between the listing and `rmi`, the same window the weekly prune already documents. A container started in that window keeps its image, but a multi-tag image can lose its tags and be re-pulled. --- - [ ] I confirm third-party material is recorded in `NOTICE` and `REUSE.toml`, or there is none. Assisted-by: Claude Code
Defame1297 was assigned by Claude 2026-11-23 18:39:15 +00:00
Claude added 3 commits 2026-11-23 18:39:15 +00:00
An age-based prune does not cap disk use: the cache grows with how often
new image versions are published, and builds on the runner then fail for
lack of space. Keep the newest N image IDs per repository in the weekly
prune, and add an hourly pressure prune that runs the count prune and a
capped builder prune when the Docker root's filesystem passes a
threshold, behind the same job gate. Both are off by default.

Refs #319

Assisted-by: Claude Code
Give each new input its own assert message so every refused-input case
matches only its own input, and cover a negative count and a bad disk
check schedule. Fire the pressure prune only above the threshold, as
the inputs describe it. Name the disk check script apart from the
pressure service, prefix the new registers as internal, and keep the
new comments short.

Refs #319

Assisted-by: Claude Code
test(gitea_runner): pin the disk check and refused-input messages
Build / build (pull_request) Canceled after 0s
Lint / lint (pull_request) Canceled after 0s
Molecule / molecule (pull_request) Canceled after 0s
ee4cb160ea
The weekly schedule and image age cases matched text the disk check
schedule message also holds; match each assert's own message. Add a
case at exactly the root's use so the strict comparison is pinned, and
check the below message so a df failure cannot pass for it.

Refs #319

Assisted-by: Claude Code
Defame1297 added 1 commit 2026-11-23 18:41:13 +00:00
Merge branch 'main' into feat/319-docker-prune-count-threshold
Build / build (pull_request) Successful in 16s
Lint / lint (pull_request) Successful in 1m48s
Molecule / molecule (pull_request) Successful in 2m32s
35f7e772a5
Defame1297 approved these changes 2026-11-23 18:43:14 +00:00
Defame1297 merged commit bca13f4139 into main 2026-11-23 19:06:42 +00:00
Defame1297 deleted branch feat/319-docker-prune-count-threshold 2026-11-23 19:06:42 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#328