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/container/images_test.go +146

@@ -0,0 +1,146 @@
1 package container
2
3 import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9 "time"
10 )
11
12 // A prune is the runtime's own command, with the age and the scope in terms every
13 // supported runtime understands. This is the whole policy in one assertion.
14 func TestPruneImagesAsksTheRuntimeForAgeAndScope(t *testing.T) {
15 dir := t.TempDir()
16 log := filepath.Join(dir, "args")
17 fakeRuntime(t, "podman", `#!/bin/sh
18 printf '%s\n' "$@" >> "$FAKE_LOG"
19 echo "Total reclaimed space: 1.5GB"
20 `)
21 t.Setenv("FAKE_LOG", log)
22 cfg, err := Validate(Config{Runtime: "podman"})
23 if err != nil {
24 t.Fatal(err)
25 }
26
27 got, err := PruneImages(context.Background(), cfg, 24*time.Hour, true)
28 if err != nil {
29 t.Fatalf("PruneImages: %v", err)
30 }
31 if want := int64(1610612736); got != want { // 1.5 GiB as the runtime prints it
32 t.Fatalf("reclaimed = %d, want %d", got, want)
33 }
34 data, err := os.ReadFile(log)
35 if err != nil {
36 t.Fatal(err)
37 }
38 want := []string{"image", "prune", "--force", "--filter", "until=24h0m0s", "--all"}
39 if args := strings.Split(strings.TrimSpace(string(data)), "\n"); strings.Join(args, " ") != strings.Join(want, " ") {
40 t.Fatalf("prune arguments = %q, want %q", args, want)
41 }
42
43 // Dangling layers only is the offline-safe half of the policy: no --all.
44 if err := os.Remove(log); err != nil {
45 t.Fatal(err)
46 }
47 if _, err := PruneImages(context.Background(), cfg, time.Hour, false); err != nil {
48 t.Fatalf("PruneImages dangling: %v", err)
49 }
50 data, err = os.ReadFile(log)
51 if err != nil {
52 t.Fatal(err)
53 }
54 if strings.Contains(string(data), "--all") {
55 t.Fatalf("dangling-only prune asked to remove tagged images:\n%s", data)
56 }
57 }
58
59 // An age of zero is the operator saying "keep every image". It must not run a
60 // command, because a prune is not free and 0 is a policy, not a very short age.
61 func TestPruneImagesDoesNothingAtAgeZero(t *testing.T) {
62 dir := t.TempDir()
63 log := filepath.Join(dir, "args")
64 fakeRuntime(t, "docker", `#!/bin/sh
65 printf 'ran\n' >> "$FAKE_LOG"
66 `)
67 t.Setenv("FAKE_LOG", log)
68 cfg, err := Validate(Config{Runtime: "docker"})
69 if err != nil {
70 t.Fatal(err)
71 }
72 got, err := PruneImages(context.Background(), cfg, 0, true)
73 if err != nil || got != 0 {
74 t.Fatalf("PruneImages(0) = %d, %v; want 0, nil", got, err)
75 }
76 if _, err := os.Stat(log); !os.IsNotExist(err) {
77 t.Fatalf("prune ran with age 0: %v", err)
78 }
79 }
80
81 // A runtime that will not prune is an error for the caller to say once; it must
82 // carry the runtime's own last word so the line explains the state.
83 func TestPruneImagesReportsTheRuntimesReason(t *testing.T) {
84 fakeRuntime(t, "nerdctl", `#!/bin/sh
85 echo 'WARN[0000] something unrelated' >&2
86 echo 'FATA[0000] image prune is not supported by this runtime' >&2
87 exit 1
88 `)
89 cfg, err := Validate(Config{Runtime: "nerdctl"})
90 if err != nil {
91 t.Fatal(err)
92 }
93 _, err = PruneImages(context.Background(), cfg, time.Hour, true)
94 want := "nerdctl image prune: FATA[0000] image prune is not supported by this runtime"
95 if err == nil || err.Error() != want {
96 t.Fatalf("PruneImages = %v, want %q", err, want)
97 }
98 }
99
100 // The total is the runtime's accounting. Sizes in the wild use both bases and
101 // neither is ours to correct; the point is a log line that says roughly how much
102 // disk came back.
103 func TestReclaimedBytes(t *testing.T) {
104 t.Parallel()
105 cases := map[string]int64{
106 "Deleted Images:\nTotal reclaimed space: 1.2GB\n": int64(1288490188),
107 "Total reclaimed space: 512MiB": int64(512 << 20),
108 "noise\nTotal Reclaimed Space: 0B\n": 0,
109 "Total reclaimed space: 1,024MB\n": 1024 << 20,
110 "Total reclaimed space: 3.5 GB\n": int64(3758096384),
111 "Total reclaimed space: unreadable\n": 0,
112 "nothing was reclaimed and the line is absent\n": 0,
113 }
114 for out, want := range cases {
115 if got := reclaimedBytes(out); got != want {
116 t.Errorf("reclaimedBytes(%q) = %d, want %d", out, got, want)
117 }
118 }
119 }
120
121 func TestRuntimeBytes(t *testing.T) {
122 t.Parallel()
123 for text, want := range map[string]int64{
124 "0B": 0,
125 "1B": 1,
126 "1K": 1 << 10,
127 "2KiB": 2 << 10,
128 "3MB": 3 << 20,
129 "4MiB": 4 << 20,
130 "5GB": 5 << 30,
131 "6GiB": 6 << 30,
132 "7TB": 7 << 40,
133 "8TiB": 8 << 40,
134 "1.5GB": int64(1610612736),
135 } {
136 got, ok := runtimeBytes(text)
137 if !ok || got != want {
138 t.Errorf("runtimeBytes(%q) = %d, %v; want %d", text, got, ok, want)
139 }
140 }
141 for _, text := range []string{"", "GB", "1XB", "-1MB"} {
142 if got, ok := runtimeBytes(text); ok {
143 t.Errorf("runtimeBytes(%q) = %d, true; want false", text, got)
144 }
145 }
146 }