vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Bound the runner's image storage by age
3455902aa415e013b0e5525e3fd2271a2b1a4e9a · Keenan Nemetz · 3d ago · parent 1a9b3838
Bound the runner's image storage by age The job cache and saved checkouts have caps, but image storage belonged to the runtime and grew two ways: a changed `.vole/images` definition left a permanent new digest tag behind, and a moved registry tag left the previous layers dangling. `--disk-reserve` could not reclaim either; it stops a job instead. Add `--image-max-age` (default 168h; 0 keeps every image), threaded through agent.Options. At startup and on the cache trimmer's beat the runner asks the runtime to prune unused images older than the age, using the filter docker, podman, and nerdctl all express. A job holds an image guard from resolution until its container is up, so a pass that races a lease gives way. A runner with `--container-pull never` keeps every tagged image and prunes dangling layers only, because it may have no network to fetch an evicted image again. A runtime that will not prune is reported once and then left alone: the runner keeps serving. The primitive lives in ci/container so `vole workflow run` can use it later; the policy and its beat live in ci/agent beside the two existing trimmers. closes: 01M8CR0RN88JF14PKAZ0D387ND
ci/container/images.go +116
| @@ -0,0 +1,116 @@ | |||
| 1 | package container | ||
| 2 | |||
| 3 | // Image storage is the one part of a runner's disk the runtime owns. Vole cannot | ||
| 4 | // walk it like a cache and cannot put a quota on it, so the only bound it can | ||
| 5 | // offer is the runtime's own prune, asked in terms the runtime understands: age. | ||
| 6 | // | ||
| 7 | // The policy is deliberately conservative. Pruning is an optimisation, never a | ||
| 8 | // reason to refuse to serve, so a runtime that will not answer is reported once | ||
| 9 | // by the caller and then left alone. An image a container is running from is | ||
| 10 | // never a candidate: every supported runtime refuses to remove one. | ||
| 11 | |||
| 12 | import ( | ||
| 13 | "context" | ||
| 14 | "fmt" | ||
| 15 | "os/exec" | ||
| 16 | "strconv" | ||
| 17 | "strings" | ||
| 18 | "time" | ||
| 19 | ) | ||
| 20 | |||
| 21 | // PruneTimeout bounds one prune pass. Removing an old image is local work, but a | ||
| 22 | // runtime under load can take a while to answer; a runner that gives up says so | ||
| 23 | // once rather than holding a slot on it. | ||
| 24 | const PruneTimeout = 10 * time.Minute | ||
| 25 | |||
| 26 | // PruneImages reclaims image storage older than age. | ||
| 27 | // | ||
| 28 | // all includes tagged images no container references. Without it only dangling | ||
| 29 | // layers go, which is what a runner that may have to start a job offline can | ||
| 30 | // afford: a tag may be the only copy of an image it still needs. | ||
| 31 | // | ||
| 32 | // The returned count is what the runtime says it reclaimed. It is zero when the | ||
| 33 | // runtime reports no total, which is a smaller truth than refusing the pass. | ||
| 34 | func PruneImages(ctx context.Context, cfg Config, age time.Duration, all bool) (int64, error) { | ||
| 35 | if age <= 0 { | ||
| 36 | return 0, nil | ||
| 37 | } | ||
| 38 | args := []string{"image", "prune", "--force", "--filter", "until=" + age.String()} | ||
| 39 | if all { | ||
| 40 | // --all is spelled out rather than -a: docker, podman, and nerdctl all | ||
| 41 | // accept it, and a runtime that grew a different short flag should fail | ||
| 42 | // loudly on the word rather than on a letter. | ||
| 43 | args = append(args, "--all") | ||
| 44 | } | ||
| 45 | ctx, cancel := context.WithTimeout(ctx, PruneTimeout) | ||
| 46 | defer cancel() | ||
| 47 | cmd := exec.CommandContext(ctx, cfg.Runtime, args...) | ||
| 48 | out, err := cmd.CombinedOutput() | ||
| 49 | if err != nil { | ||
| 50 | reason := lastLine(string(out)) | ||
| 51 | if ctx.Err() == context.DeadlineExceeded { | ||
| 52 | reason = fmt.Sprintf("%s image prune did not finish within %s", cfg.Runtime, PruneTimeout) | ||
| 53 | } | ||
| 54 | if reason == "" { | ||
| 55 | reason = err.Error() | ||
| 56 | } | ||
| 57 | return 0, fmt.Errorf("%s image prune: %s", cfg.Runtime, reason) | ||
| 58 | } | ||
| 59 | return reclaimedBytes(string(out)), nil | ||
| 60 | } | ||
| 61 | |||
| 62 | // reclaimedBytes reads the total the runtimes print at the end of a prune. The | ||
| 63 | // number is the runtime's own accounting, not a sum of image sizes: layers are | ||
| 64 | // shared, so adding sizes per image would claim space that was never freed. | ||
| 65 | func reclaimedBytes(out string) int64 { | ||
| 66 | const marker = "total reclaimed space:" | ||
| 67 | lines := strings.Split(out, "\n") | ||
| 68 | for i := len(lines) - 1; i >= 0; i-- { | ||
| 69 | line := strings.TrimSpace(lines[i]) | ||
| 70 | if len(line) < len(marker) || !strings.EqualFold(line[:len(marker)], marker) { | ||
| 71 | continue | ||
| 72 | } | ||
| 73 | if n, ok := runtimeBytes(strings.TrimSpace(line[len(marker):])); ok { | ||
| 74 | return n | ||
| 75 | } | ||
| 76 | } | ||
| 77 | return 0 | ||
| 78 | } | ||
| 79 | |||
| 80 | // runtimeBytes turns the human size a runtime prints ("1.2GB", "512MiB", "0B") | ||
| 81 | // into bytes. It is deliberately forgiving about the unit: the report is for the | ||
| 82 | // operator's log line, and getting it slightly wrong is better than a prune that | ||
| 83 | // did happen being reported as an error. | ||
| 84 | func runtimeBytes(text string) (int64, bool) { | ||
| 85 | text = strings.TrimSpace(text) | ||
| 86 | if text == "" { | ||
| 87 | return 0, false | ||
| 88 | } | ||
| 89 | i := 0 | ||
| 90 | for i < len(text) && (text[i] >= '0' && text[i] <= '9' || text[i] == '.' || text[i] == ',') { | ||
| 91 | i++ | ||
| 92 | } | ||
| 93 | number := strings.ReplaceAll(text[:i], ",", "") | ||
| 94 | unit := strings.ToUpper(strings.TrimSpace(text[i:])) | ||
| 95 | n, err := strconv.ParseFloat(number, 64) | ||
| 96 | if err != nil || n < 0 { | ||
| 97 | return 0, false | ||
| 98 | } | ||
| 99 | mult := float64(1) | ||
| 100 | switch unit { | ||
| 101 | case "", "B": | ||
| 102 | case "K", "KB", "KIB": | ||
| 103 | mult = 1 << 10 | ||
| 104 | case "M", "MB", "MIB": | ||
| 105 | mult = 1 << 20 | ||
| 106 | case "G", "GB", "GIB": | ||
| 107 | mult = 1 << 30 | ||
| 108 | case "T", "TB", "TIB": | ||
| 109 | mult = 1 << 40 | ||
| 110 | case "P", "PB", "PIB": | ||
| 111 | mult = 1 << 50 | ||
| 112 | default: | ||
| 113 | return 0, false | ||
| 114 | } | ||
| 115 | return int64(n * mult), true | ||
| 116 | } | ||