feat(gitea_runner): bound the Docker image cache by count and disk usage, not only by age #319

Closed
opened 2026-11-23 17:00:51 +00:00 by Claude · 0 comments
Owner

Problem

The Docker prune of gitea_runner (tasks/prune.yml) removes only images older than gitea_runner_docker_prune_image_age, via docker image prune --all --filter until=<age>. An age limit does not cap disk use: the space the cache takes grows with how often new image versions are published. A runner whose job images get a new pinned version several times a day keeps every one of them for the whole retention window.

Image builds that run on the runner, such as kaniko compiling Go tools, need several GB of scratch space at the same time. When the cache has filled the disk, those builds fail partway through. Jobs can also fail at container setup. A shorter age only moves the limit; it still depends on how often images are released.

Proposal

Bound the cache by what is kept and by disk pressure, in the manner of kubelet image garbage collection. Decided in triage:

  1. Keep the newest N image IDs per repository. A POSIX sh script, roles/gitea_runner/files/gitea-runner-docker-prune-keep (deployed with copy: src:), groups images by repository, orders image IDs by Created, and keeps the newest N. Every tag of a kept image stays. Images used by any container (docker ps -a) are always kept, and dangling images are always removed. N counts image IDs, not tags, so one image with several tags uses one slot.
  2. Weekly service. gitea-runner-docker-prune.service keeps its age-based prunes unchanged and also runs the count prune when N > 0. An image is removed if either rule removes it.
  3. Pressure path. A separate service and timer (hourly by default) runs behind the same job gate (ExecCondition= gate), plus a second ExecCondition= that checks the use of the filesystem holding docker info --format '{{.DockerRootDir}}' against the threshold. Above the threshold, it runs the count prune and docker builder prune --force --keep-storage <size> once. There is no low watermark: if usage stays high, N or the disk size needs a human. The builder cap is in scope because job containers use the host's docker.sock by default, so a job's docker build fills the host's build cache.
  4. Turning it off. With the threshold at 0 and the prune enabled, the role stops the pressure timer and removes its units. tasks/remove.yml removes the new units and the script.

New inputs (all apply only when gitea_runner_docker_prune_enabled is true):

Input Type Default Meaning
gitea_runner_docker_prune_keep_per_repository int 0 newest N image IDs per repository; 0 = off
gitea_runner_docker_prune_disk_threshold_percent int 0 usage % of the Docker root's filesystem that triggers the pressure prune; 0 = off
gitea_runner_docker_prune_disk_check_schedule str hourly OnCalendar of the pressure timer
gitea_runner_docker_prune_builder_keep_storage str 10GB --keep-storage of the builder prune under pressure

validate.yml refuses a threshold outside 1–99, a threshold set while N is 0, and a schedule or size outside the character-set and unit checks the existing inputs use.

Acceptance criteria

  • The four inputs are in defaults/main.yml, argument_specs.yml, the README and validate.yml. With the defaults, behaviour is unchanged.
  • With N set, a runner holding M > N image IDs of one repository keeps exactly the newest N, plus any image a container uses. A multi-tag image counts once.
  • With the threshold set, the pressure prune runs when usage is above it and is skipped below it. The job gate still applies.
  • Setting the threshold back to 0 removes the pressure timer and its units. The remove path removes them and the script.
  • Molecule verifies the count script and the threshold condition against a stub docker that returns fixed image ls / ps / info output and records rmi calls. Cases: M > N, an image in use, a multi-tag image, and usage above and below the threshold. Molecule has no real Docker.
  • Changelog fragment (minor_changes).

Assisted-by: Claude Code:claude-opus-5-5

## Problem The Docker prune of `gitea_runner` (`tasks/prune.yml`) removes only images older than `gitea_runner_docker_prune_image_age`, via `docker image prune --all --filter until=<age>`. An age limit does not cap disk use: the space the cache takes grows with how often new image versions are published. A runner whose job images get a new pinned version several times a day keeps every one of them for the whole retention window. Image builds that run on the runner, such as kaniko compiling Go tools, need several GB of scratch space at the same time. When the cache has filled the disk, those builds fail partway through. Jobs can also fail at container setup. A shorter age only moves the limit; it still depends on how often images are released. ## Proposal Bound the cache by what is kept and by disk pressure, in the manner of kubelet image garbage collection. Decided in triage: 1. **Keep the newest N image IDs per repository.** A POSIX sh script, `roles/gitea_runner/files/gitea-runner-docker-prune-keep` (deployed with `copy: src:`), groups images by repository, orders image IDs by `Created`, and keeps the newest N. Every tag of a kept image stays. Images used by any container (`docker ps -a`) are always kept, and dangling images are always removed. N counts image IDs, not tags, so one image with several tags uses one slot. 2. **Weekly service.** `gitea-runner-docker-prune.service` keeps its age-based prunes unchanged and also runs the count prune when N > 0. An image is removed if either rule removes it. 3. **Pressure path.** A separate service and timer (hourly by default) runs behind the same job gate (`ExecCondition=` gate), plus a second `ExecCondition=` that checks the use of the filesystem holding `docker info --format '{{.DockerRootDir}}'` against the threshold. Above the threshold, it runs the count prune and `docker builder prune --force --keep-storage <size>` once. There is no low watermark: if usage stays high, N or the disk size needs a human. The builder cap is in scope because job containers use the host's `docker.sock` by default, so a job's `docker build` fills the host's build cache. 4. **Turning it off.** With the threshold at `0` and the prune enabled, the role stops the pressure timer and removes its units. `tasks/remove.yml` removes the new units and the script. New inputs (all apply only when `gitea_runner_docker_prune_enabled` is true): | Input | Type | Default | Meaning | |---|---|---|---| | `gitea_runner_docker_prune_keep_per_repository` | int | `0` | newest N image IDs per repository; `0` = off | | `gitea_runner_docker_prune_disk_threshold_percent` | int | `0` | usage % of the Docker root's filesystem that triggers the pressure prune; `0` = off | | `gitea_runner_docker_prune_disk_check_schedule` | str | `hourly` | `OnCalendar` of the pressure timer | | `gitea_runner_docker_prune_builder_keep_storage` | str | `10GB` | `--keep-storage` of the builder prune under pressure | `validate.yml` refuses a threshold outside 1–99, a threshold set while N is `0`, and a schedule or size outside the character-set and unit checks the existing inputs use. ## Acceptance criteria - [ ] The four inputs are in `defaults/main.yml`, `argument_specs.yml`, the README and `validate.yml`. With the defaults, behaviour is unchanged. - [ ] With N set, a runner holding M > N image IDs of one repository keeps exactly the newest N, plus any image a container uses. A multi-tag image counts once. - [ ] With the threshold set, the pressure prune runs when usage is above it and is skipped below it. The job gate still applies. - [ ] Setting the threshold back to `0` removes the pressure timer and its units. The remove path removes them and the script. - [ ] Molecule verifies the count script and the threshold condition against a stub `docker` that returns fixed `image ls` / `ps` / `info` output and records `rmi` calls. Cases: M > N, an image in use, a multi-tag image, and usage above and below the threshold. Molecule has no real Docker. - [ ] Changelog fragment (`minor_changes`). Assisted-by: Claude Code:claude-opus-5-5
Claude added the Kind/Enhancement
Priority
High
2
Status
Needs Triage
labels 2026-11-23 17:00:51 +00:00
Defame1297 was assigned by Claude 2026-11-23 17:55:58 +00:00
Claude added
Status
Ready for Agent
and removed
Status
Needs Triage
labels 2026-11-23 17:55:58 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#319