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_test.go +139

@@ -0,0 +1,139 @@
1 package agent
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9 "time"
10 )
11
12 // fakePruneRuntime puts a docker on PATH that records how it was asked to prune
13 // and answers with a reclaimed total. FAKE_PRUNE_FAIL makes it refuse, which is
14 // the case the runner has to survive with one line rather than a failure.
15 func fakePruneRuntime(t *testing.T) string {
16 t.Helper()
17 dir := t.TempDir()
18 log := filepath.Join(dir, "prune.log")
19 script := `#!/bin/sh
20 printf '%s\n' "$*" >> "$FAKE_PRUNE_LOG"
21 [ "${FAKE_PRUNE_FAIL:-0}" = 0 ] || {
22 echo 'Error: the image store is not available' >&2
23 exit 1
24 }
25 echo 'Total reclaimed space: 1.5GB'
26 `
27 if err := os.WriteFile(filepath.Join(dir, "docker"), []byte(script), 0o755); err != nil {
28 t.Fatal(err)
29 }
30 t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
31 t.Setenv("FAKE_PRUNE_LOG", log)
32 return log
33 }
34
35 func pruneCalls(t *testing.T, log string) []string {
36 t.Helper()
37 data, err := os.ReadFile(log)
38 if os.IsNotExist(err) {
39 return nil
40 }
41 if err != nil {
42 t.Fatal(err)
43 }
44 return strings.Split(strings.TrimSpace(string(data)), "\n")
45 }
46
47 // The policy is the runtime's own age filter, and the scope depends on whether the
48 // runner could fetch an image again: a never-pull runner keeps every tag.
49 func TestImageTrimmerUsesTheRuntimesPruneForThePolicy(t *testing.T) {
50 log := fakePruneRuntime(t)
51
52 box, err := containerOptions(Options{ContainerRuntime: "docker", ContainerPull: "missing"}, t.TempDir())
53 if err != nil {
54 t.Fatal(err)
55 }
56 trimmer := newImageTrimmer(box, 24*time.Hour)
57 if !trimmer.enabled() {
58 t.Fatal("a configured age did not enable the trimmer")
59 }
60 trimmer.trim(context.Background(), box)
61 calls := pruneCalls(t, log)
62 if len(calls) != 1 || !strings.Contains(calls[0], "until=24h0m0s") || !strings.Contains(calls[0], "--all") {
63 t.Fatalf("prune calls = %q, want one aged, all-images prune", calls)
64 }
65
66 if err := os.Remove(log); err != nil {
67 t.Fatal(err)
68 }
69 box, err = containerOptions(Options{ContainerRuntime: "docker", ContainerPull: "never"}, t.TempDir())
70 if err != nil {
71 t.Fatal(err)
72 }
73 trimmer = newImageTrimmer(box, 24*time.Hour)
74 trimmer.trim(context.Background(), box)
75 calls = pruneCalls(t, log)
76 if len(calls) != 1 || strings.Contains(calls[0], "--all") {
77 t.Fatalf("never-pull prune calls = %q, want dangling layers only", calls)
78 }
79 }
80
81 // A pass that races a job resolving or starting an image gives way. The lease is
82 // the thing that must not lose; the next beat tries again.
83 func TestImageTrimmerGivesWayToAJob(t *testing.T) {
84 log := fakePruneRuntime(t)
85 box, err := containerOptions(Options{ContainerRuntime: "docker"}, t.TempDir())
86 if err != nil {
87 t.Fatal(err)
88 }
89 trimmer := newImageTrimmer(box, time.Hour)
90
91 release := box.images.hold()
92 trimmer.trim(context.Background(), box)
93 if calls := pruneCalls(t, log); len(calls) != 0 {
94 t.Fatalf("prune ran while a job held the image guard: %q", calls)
95 }
96 release()
97
98 trimmer.trim(context.Background(), box)
99 if calls := pruneCalls(t, log); len(calls) != 1 {
100 t.Fatalf("prune did not run after the job released the guard: %q", calls)
101 }
102 }
103
104 // A runtime that cannot prune is a line, not a retry every five minutes and not a
105 // runner that refuses to serve.
106 func TestImageTrimmerStopsAfterTheRuntimeRefuses(t *testing.T) {
107 log := fakePruneRuntime(t)
108 box, err := containerOptions(Options{ContainerRuntime: "docker"}, t.TempDir())
109 if err != nil {
110 t.Fatal(err)
111 }
112 t.Setenv("FAKE_PRUNE_FAIL", "1")
113 trimmer := newImageTrimmer(box, time.Hour)
114 trimmer.trim(context.Background(), box)
115 trimmer.trim(context.Background(), box)
116 if calls := pruneCalls(t, log); len(calls) != 1 {
117 t.Fatalf("a refusing runtime was asked %d times, want once: %q", len(calls), calls)
118 }
119 if !trimmer.stopped {
120 t.Fatal("the trimmer kept trying after the runtime refused")
121 }
122 }
123
124 // Zero is a policy ("keep every image"), not a very short age.
125 func TestImageTrimmerIsOffAtAgeZero(t *testing.T) {
126 log := fakePruneRuntime(t)
127 box, err := containerOptions(Options{ContainerRuntime: "docker"}, t.TempDir())
128 if err != nil {
129 t.Fatal(err)
130 }
131 trimmer := newImageTrimmer(box, 0)
132 if trimmer.enabled() {
133 t.Fatal("age 0 enabled the image trimmer")
134 }
135 trimmer.trim(context.Background(), box)
136 if calls := pruneCalls(t, log); len(calls) != 0 {
137 t.Fatalf("age 0 pruned images: %q", calls)
138 }
139 }