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.

Blame · ci/agent/images.go

Line attribution on main · 80 line(s)

3455902a
Keenan Nemetz
3d ago
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
de3eec16
Keenan Nemetz
2h ago
26 "rodent.software/vole/ci/execute"
3455902a
Keenan Nemetz
3d ago
27 "rodent.software/vole/internal/format"
28 )
29
30 // imageTrimmer applies the age policy on the cache trimmer's beat. stopped is set
de3eec16
Keenan Nemetz
2h ago
31 // after a backend refuses: one line explains the state, and later passes stay
3455902a
Keenan Nemetz
3d ago
32 // quiet instead of repeating it every five minutes.
de3eec16
Keenan Nemetz
2h ago
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.
3455902a
Keenan Nemetz
3d ago
37 type imageTrimmer struct {
de3eec16
Keenan Nemetz
2h ago
38 exec execute.Executor
3455902a
Keenan Nemetz
3d ago
39 age time.Duration
40 mu sync.Mutex
41 stopped bool
42 }
43
de3eec16
Keenan Nemetz
2h ago
44 func newImageTrimmer(exec execute.Executor, age time.Duration) *imageTrimmer {
45 return &imageTrimmer{exec: exec, age: age}
3455902a
Keenan Nemetz
3d ago
46 }
47
48 func (t *imageTrimmer) enabled() bool {
49 return t != nil && t.age > 0
50 }
51
de3eec16
Keenan Nemetz
2h ago
52 func (t *imageTrimmer) trim(ctx context.Context) {
3455902a
Keenan Nemetz
3d ago
53 t.mu.Lock()
54 defer t.mu.Unlock()
55 if t.stopped {
56 return
57 }
de3eec16
Keenan Nemetz
2h ago
58 removed, err := t.exec.Prune(ctx, t.age)
3455902a
Keenan Nemetz
3d ago
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
de3eec16
Keenan Nemetz
2h ago
69 func (t *imageTrimmer) loop(ctx context.Context) {
3455902a
Keenan Nemetz
3d ago
70 ticker := time.NewTicker(cacheTrimInterval)
71 defer ticker.Stop()
72 for {
73 select {
74 case <-ctx.Done():
75 return
76 case <-ticker.C:
de3eec16
Keenan Nemetz
2h ago
77 t.trim(ctx)
3455902a
Keenan Nemetz
3d ago
78 }
79 }
80 }