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.
|
3455902a
Keenan Nemetz
3d ago
|
1 | package container |
| 2 | ||
| 3 | // Image storage is the one part of a runner's disk the runtime owns. Vole cannot | |
| 4 | // walk it like a cache and cannot put a quota on it, so the only bound it can | |
| 5 | // offer is the runtime's own prune, asked in terms the runtime understands: age. | |
| 6 | // | |
| 7 | // The policy is deliberately conservative. Pruning is an optimisation, never a | |
| 8 | // reason to refuse to serve, so a runtime that will not answer is reported once | |
| 9 | // by the caller and then left alone. An image a container is running from is | |
| 10 | // never a candidate: every supported runtime refuses to remove one. | |
| 11 | ||
| 12 | import ( | |
| 13 | "context" | |
| 14 | "fmt" | |
| 15 | "os/exec" | |
| 16 | "strconv" | |
| 17 | "strings" | |
|
de3eec16
Keenan Nemetz
2h ago
|
18 | "sync" |
|
3455902a
Keenan Nemetz
3d ago
|
19 | "time" |
| 20 | ) | |
| 21 | ||
| 22 | // PruneTimeout bounds one prune pass. Removing an old image is local work, but a | |
| 23 | // runtime under load can take a while to answer; a runner that gives up says so | |
| 24 | // once rather than holding a slot on it. | |
| 25 | const PruneTimeout = 10 * time.Minute | |
| 26 | ||
| 27 | // PruneImages reclaims image storage older than age. | |
| 28 | // | |
| 29 | // all includes tagged images no container references. Without it only dangling | |
| 30 | // layers go, which is what a runner that may have to start a job offline can | |
| 31 | // afford: a tag may be the only copy of an image it still needs. | |
| 32 | // | |
| 33 | // The returned count is what the runtime says it reclaimed. It is zero when the | |
| 34 | // runtime reports no total, which is a smaller truth than refusing the pass. | |
| 35 | func PruneImages(ctx context.Context, cfg Config, age time.Duration, all bool) (int64, error) { | |
| 36 | if age <= 0 { | |
| 37 | return 0, nil | |
| 38 | } | |
| 39 | args := []string{"image", "prune", "--force", "--filter", "until=" + age.String()} | |
| 40 | if all { | |
| 41 | // --all is spelled out rather than -a: docker, podman, and nerdctl all | |
| 42 | // accept it, and a runtime that grew a different short flag should fail | |
| 43 | // loudly on the word rather than on a letter. | |
| 44 | args = append(args, "--all") | |
| 45 | } | |
| 46 | ctx, cancel := context.WithTimeout(ctx, PruneTimeout) | |
| 47 | defer cancel() | |
| 48 | cmd := exec.CommandContext(ctx, cfg.Runtime, args...) | |
| 49 | out, err := cmd.CombinedOutput() | |
| 50 | if err != nil { | |
| 51 | reason := lastLine(string(out)) | |
| 52 | if ctx.Err() == context.DeadlineExceeded { | |
| 53 | reason = fmt.Sprintf("%s image prune did not finish within %s", cfg.Runtime, PruneTimeout) | |
| 54 | } | |
| 55 | if reason == "" { | |
| 56 | reason = err.Error() | |
| 57 | } | |
| 58 | return 0, fmt.Errorf("%s image prune: %s", cfg.Runtime, reason) | |
| 59 | } | |
| 60 | return reclaimedBytes(string(out)), nil | |
| 61 | } | |
| 62 | ||
| 63 | // reclaimedBytes reads the total the runtimes print at the end of a prune. The | |
| 64 | // number is the runtime's own accounting, not a sum of image sizes: layers are | |
| 65 | // shared, so adding sizes per image would claim space that was never freed. | |
| 66 | func reclaimedBytes(out string) int64 { | |
| 67 | const marker = "total reclaimed space:" | |
| 68 | lines := strings.Split(out, "\n") | |
| 69 | for i := len(lines) - 1; i >= 0; i-- { | |
| 70 | line := strings.TrimSpace(lines[i]) | |
| 71 | if len(line) < len(marker) || !strings.EqualFold(line[:len(marker)], marker) { | |
| 72 | continue | |
| 73 | } | |
| 74 | if n, ok := runtimeBytes(strings.TrimSpace(line[len(marker):])); ok { | |
| 75 | return n | |
| 76 | } | |
| 77 | } | |
| 78 | return 0 | |
| 79 | } | |
| 80 | ||
| 81 | // runtimeBytes turns the human size a runtime prints ("1.2GB", "512MiB", "0B") | |
| 82 | // into bytes. It is deliberately forgiving about the unit: the report is for the | |
| 83 | // operator's log line, and getting it slightly wrong is better than a prune that | |
| 84 | // did happen being reported as an error. | |
| 85 | func runtimeBytes(text string) (int64, bool) { | |
| 86 | text = strings.TrimSpace(text) | |
| 87 | if text == "" { | |
| 88 | return 0, false | |
| 89 | } | |
| 90 | i := 0 | |
| 91 | for i < len(text) && (text[i] >= '0' && text[i] <= '9' || text[i] == '.' || text[i] == ',') { | |
| 92 | i++ | |
| 93 | } | |
| 94 | number := strings.ReplaceAll(text[:i], ",", "") | |
| 95 | unit := strings.ToUpper(strings.TrimSpace(text[i:])) | |
| 96 | n, err := strconv.ParseFloat(number, 64) | |
| 97 | if err != nil || n < 0 { | |
| 98 | return 0, false | |
| 99 | } | |
| 100 | mult := float64(1) | |
| 101 | switch unit { | |
| 102 | case "", "B": | |
| 103 | case "K", "KB", "KIB": | |
| 104 | mult = 1 << 10 | |
| 105 | case "M", "MB", "MIB": | |
| 106 | mult = 1 << 20 | |
| 107 | case "G", "GB", "GIB": | |
| 108 | mult = 1 << 30 | |
| 109 | case "T", "TB", "TIB": | |
| 110 | mult = 1 << 40 | |
| 111 | case "P", "PB", "PIB": | |
| 112 | mult = 1 << 50 | |
| 113 | default: | |
| 114 | return 0, false | |
| 115 | } | |
| 116 | return int64(n * mult), true | |
| 117 | } | |
|
de3eec16
Keenan Nemetz
2h ago
|
118 | |
| 119 | // imageUse is the boundary between a job acquiring an image and a prune pass. | |
| 120 | // A job holds it from before the image is prepared (which may build it) until the | |
| 121 | // job's environment is up; a prune takes it exclusively, so an image cannot | |
| 122 | // disappear between the prepare and the start. | |
| 123 | // | |
| 124 | // It lives beside PruneImages rather than in the caller because both sides of the | |
| 125 | // boundary are the container backend's: a second backend bounds its own storage its | |
| 126 | // own way, and the runner only asks. | |
| 127 | type imageUse struct { | |
| 128 | mu sync.RWMutex | |
| 129 | } | |
| 130 | ||
| 131 | // hold marks an image as in use and returns the release. A nil guard means the | |
| 132 | // caller never asked for one (a Backend built by hand in a test), so holding and | |
| 133 | // releasing are no-ops rather than a panic. | |
| 134 | func (u *imageUse) hold() func() { | |
| 135 | if u == nil { | |
| 136 | return func() {} | |
| 137 | } | |
| 138 | u.mu.RLock() | |
| 139 | return u.mu.RUnlock | |
| 140 | } | |
| 141 | ||
| 142 | // exclusive takes the guard for a prune pass. It does not wait: a job that is | |
| 143 | // building or starting an image may hold it for minutes, and a trimmer that | |
| 144 | // blocked a pass would be a goroutine waiting on a build rather than a runner | |
| 145 | // doing work. A skipped pass costs nothing; the next beat tries again. | |
| 146 | func (u *imageUse) exclusive() (func(), bool) { | |
| 147 | if u == nil { | |
| 148 | return func() {}, true | |
| 149 | } | |
| 150 | if !u.mu.TryLock() { | |
| 151 | return nil, false | |
| 152 | } | |
| 153 | return u.mu.Unlock, true | |
| 154 | } |