Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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/agent/images.go +120

@@ -0,0 +1,120 @@
1 package agent
2
3 // Bounding the runtime's image storage.
4 //
5 // Everything else a runner writes is a directory Vole can measure and trim. Image
6 // storage belongs to the runtime, so the runner cannot count it, quota it, or
7 // choose individual layers. What it can do is ask the runtime to prune in the
8 // terms the runtime owns: unused, and older than an age. Two tag families explain
9 // the growth. A changed .vole/images definition builds a new digest tag and leaves
10 // the old one behind, and a moved registry tag leaves the previous image's layers
11 // dangling.
12 //
13 // The pass is conservative in three ways. It runs through the runtime's own prune,
14 // so an image a container holds is never removed. It gives way to a job that is
15 // resolving or starting an image, because the lease is the thing that must not
16 // lose. And a runtime that will not prune is said once and then left alone: image
17 // storage is worth reclaiming, not worth refusing to serve for.
18
19 import (
20 "context"
21 "fmt"
22 "os"
23 "sync"
24 "time"
25
26 "rodent.software/vole/ci/container"
27 "rodent.software/vole/internal/format"
28 )
29
30 // imageUse is the boundary between a job acquiring an image and a prune pass.
31 // A job holds it from before the image is resolved (which may build it) until the
32 // job container is up; a prune takes it exclusively, so a tag cannot disappear
33 // between the resolve and the start.
34 type imageUse struct {
35 mu sync.RWMutex
36 }
37
38 // hold marks an image as in use and returns the release. A nil guard means the
39 // caller never asked for one (a hand-built runnerBox in a test), so holding and
40 // releasing are no-ops rather than a panic.
41 func (u *imageUse) hold() func() {
42 if u == nil {
43 return func() {}
44 }
45 u.mu.RLock()
46 return u.mu.RUnlock
47 }
48
49 // exclusive takes the guard for a prune pass. It does not wait: a job that is
50 // building or starting an image may hold it for minutes, and a trimmer that
51 // blocked a pass would be a goroutine waiting on a build rather than a runner
52 // doing work. A skipped pass costs nothing; the next beat tries again.
53 func (u *imageUse) exclusive() (func(), bool) {
54 if u == nil {
55 return func() {}, true
56 }
57 if !u.mu.TryLock() {
58 return nil, false
59 }
60 return u.mu.Unlock, true
61 }
62
63 // imageTrimmer applies the age policy on the cache trimmer's beat. stopped is set
64 // after a runtime refuses: one line explains the state, and later passes stay
65 // quiet instead of repeating it every five minutes.
66 type imageTrimmer struct {
67 age time.Duration
68 all bool
69 mu sync.Mutex
70 stopped bool
71 }
72
73 func newImageTrimmer(box runnerBox, age time.Duration) *imageTrimmer {
74 return &imageTrimmer{
75 age: age,
76 // --container-pull never means this runner may have no network to fetch an
77 // image again, so every tagged image is kept. Dangling layers are still
78 // unreachable by any tag or container and are safe to reclaim.
79 all: box.Pull != "never",
80 }
81 }
82
83 func (t *imageTrimmer) enabled() bool {
84 return t != nil && t.age > 0
85 }
86
87 func (t *imageTrimmer) trim(ctx context.Context, box runnerBox) {
88 t.mu.Lock()
89 defer t.mu.Unlock()
90 if t.stopped {
91 return
92 }
93 release, ok := box.images.exclusive()
94 if !ok {
95 return // a job is resolving or starting an image; the next pass tries again
96 }
97 defer release()
98 removed, err := container.PruneImages(ctx, box.Config, t.age, t.all)
99 if err != nil {
100 fmt.Fprintf(os.Stderr, "vole runner: not pruning images: %v\n", err)
101 t.stopped = true
102 return
103 }
104 if removed > 0 {
105 fmt.Fprintf(os.Stderr, "vole runner: trimmed %s from image storage\n", format.Bytes(removed))
106 }
107 }
108
109 func (t *imageTrimmer) loop(ctx context.Context, box runnerBox) {
110 ticker := time.NewTicker(cacheTrimInterval)
111 defer ticker.Stop()
112 for {
113 select {
114 case <-ctx.Done():
115 return
116 case <-ticker.C:
117 t.trim(ctx, box)
118 }
119 }
120 }