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.
| 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/execute" |
| 27 | "rodent.software/vole/internal/format" |
| 28 | ) |
| 29 | |
| 30 | // imageTrimmer applies the age policy on the cache trimmer's beat. stopped is set |
| 31 | // after a backend refuses: one line explains the state, and later passes stay |
| 32 | // quiet instead of repeating it every five minutes. |
| 33 | // |
| 34 | // The policy itself belongs to the executor: what "an image" is, how to measure the |
| 35 | // space it takes, and whether removing one is even possible are all properties of |
| 36 | // the backend. All this keeps is the beat and the once-only complaint. |
| 37 | type imageTrimmer struct { |
| 38 | exec execute.Executor |
| 39 | age time.Duration |
| 40 | mu sync.Mutex |
| 41 | stopped bool |
| 42 | } |
| 43 | |
| 44 | func newImageTrimmer(exec execute.Executor, age time.Duration) *imageTrimmer { |
| 45 | return &imageTrimmer{exec: exec, age: age} |
| 46 | } |
| 47 | |
| 48 | func (t *imageTrimmer) enabled() bool { |
| 49 | return t != nil && t.age > 0 |
| 50 | } |
| 51 | |
| 52 | func (t *imageTrimmer) trim(ctx context.Context) { |
| 53 | t.mu.Lock() |
| 54 | defer t.mu.Unlock() |
| 55 | if t.stopped { |
| 56 | return |
| 57 | } |
| 58 | removed, err := t.exec.Prune(ctx, t.age) |
| 59 | if err != nil { |
| 60 | fmt.Fprintf(os.Stderr, "vole runner: not pruning images: %v\n", err) |
| 61 | t.stopped = true |
| 62 | return |
| 63 | } |
| 64 | if removed > 0 { |
| 65 | fmt.Fprintf(os.Stderr, "vole runner: trimmed %s from image storage\n", format.Bytes(removed)) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func (t *imageTrimmer) loop(ctx context.Context) { |
| 70 | ticker := time.NewTicker(cacheTrimInterval) |
| 71 | defer ticker.Stop() |
| 72 | for { |
| 73 | select { |
| 74 | case <-ctx.Done(): |
| 75 | return |
| 76 | case <-ticker.C: |
| 77 | t.trim(ctx) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |