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.
Put job execution behind an interface
de3eec162f93a697575c8bc756bd496fe4020376 · Keenan Nemetz · 2h ago · parent f784d769
Put job execution behind an interface Job execution was container-shaped all the way into the runner: ci/agent created the network, started the services and the job container, and held *container.Container and *container.Network values directly, and `vole workflow run` kept its own copy of the same orchestration. The only abstraction was Config.Runtime, a string naming one of three flag-compatible CLIs, which is enough for docker, podman and nerdctl and nothing else. ci/execute is that seam. An Executor prepares the job's image, starts its environment, runs one step while the caller keeps the lease alive, stops it, reclaims what a killed runner left behind, and bounds its own storage. A Sandbox is one job's environment: the job, the services beside it, and the network they share. Capabilities is what a backend can honour, so a job that asks for a network or for services is refused before it starts rather than quietly given less. ci/container implements it and takes over the orchestration that was in ci/agent and cmd/vole: the per-attempt network, the service containers and their log collection, the stale-container and stale-network reclamation, and the image build and prune coordination. The isolation flags are unchanged. This is the boundary a virtual-machine backend needs: a macOS guest behind Tart (#356), a Windows guest, or a Linux microVM disagree with a container about pids limits, user namespaces, tmpfs, capabilities and mounts, so they cannot be expressed as another runtime string. Nothing about the image or workflow schema changes here. closes: 01M8Z2M9PVKPNH923S3VJ5FR5E
ci/agent/images_test.go +41−112
| @@ -2,138 +2,67 @@ package agent | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "context" | |
| 5 | "os" | ||
| 6 | "path/filepath" | ||
| 7 | "strings" | ||
| 5 | "errors" | ||
| 6 | "io" | ||
| 8 | 7 | "testing" | |
| 9 | 8 | "time" | |
| 9 | |||
| 10 | "rodent.software/vole/ci/execute" | ||
| 10 | 11 | ) | |
| 11 | 12 | ||
| 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 | ||
| 13 | // stubExecutor is an executor whose only interesting behaviour is its prune, which | ||
| 14 | // is what the runner's image trimmer drives. The trimming policy belongs to the | ||
| 15 | // backend (ci/container tests it there); what is left here is the beat and the | ||
| 16 | // once-only complaint. | ||
| 17 | type stubExecutor struct { | ||
| 18 | calls int | ||
| 19 | err error | ||
| 20 | present bool | ||
| 33 | 21 | } | |
| 34 | 22 | ||
| 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") | ||
| 23 | func (s *stubExecutor) Capabilities() execute.Capabilities { | ||
| 24 | return execute.Capabilities{Backend: "stub"} | ||
| 45 | 25 | } | |
| 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 | } | ||
| 26 | func (s *stubExecutor) Probe(context.Context) error { return nil } | ||
| 27 | func (s *stubExecutor) Prepare(context.Context, execute.Image, io.Writer, execute.Runner) (string, func(), error) { | ||
| 28 | return "", nil, errors.New("not used") | ||
| 79 | 29 | } | |
| 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) | ||
| 30 | func (s *stubExecutor) Start(context.Context, execute.Spec) (execute.Sandbox, error) { | ||
| 31 | return execute.Nothing(), nil | ||
| 32 | } | ||
| 33 | func (s *stubExecutor) Reconcile(context.Context) error { return nil } | ||
| 34 | func (s *stubExecutor) Prune(_ context.Context, age time.Duration) (int64, error) { | ||
| 35 | s.calls++ | ||
| 36 | if age <= 0 { | ||
| 37 | return 0, nil | ||
| 101 | 38 | } | |
| 39 | return 0, s.err | ||
| 102 | 40 | } | |
| 103 | 41 | ||
| 104 | // A runtime that cannot prune is a line, not a retry every five minutes and not a | ||
| 42 | // A backend that cannot prune is a line, not a retry every five minutes and not a | ||
| 105 | 43 | // 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) | ||
| 44 | func TestImageTrimmerStopsAfterTheExecutorRefuses(t *testing.T) { | ||
| 45 | exec := &stubExecutor{err: errors.New("the image store is not available")} | ||
| 46 | trimmer := newImageTrimmer(exec, time.Hour) | ||
| 47 | trimmer.trim(context.Background()) | ||
| 48 | trimmer.trim(context.Background()) | ||
| 49 | if exec.calls != 1 { | ||
| 50 | t.Fatalf("a refusing executor was asked %d times, want once", exec.calls) | ||
| 118 | 51 | } | |
| 119 | 52 | if !trimmer.stopped { | |
| 120 | t.Fatal("the trimmer kept trying after the runtime refused") | ||
| 53 | t.Fatal("the trimmer kept trying after the executor refused") | ||
| 121 | 54 | } | |
| 122 | 55 | } | |
| 123 | 56 | ||
| 124 | // Zero is a policy ("keep every image"), not a very short age. | ||
| 57 | // Zero is a policy ("keep every image"), not a very short age: the pass is never | ||
| 58 | // scheduled at all. | ||
| 125 | 59 | 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) | ||
| 60 | exec := &stubExecutor{} | ||
| 61 | trimmer := newImageTrimmer(exec, 0) | ||
| 132 | 62 | if trimmer.enabled() { | |
| 133 | 63 | t.Fatal("age 0 enabled the image trimmer") | |
| 134 | 64 | } | |
| 135 | trimmer.trim(context.Background(), box) | ||
| 136 | if calls := pruneCalls(t, log); len(calls) != 0 { | ||
| 137 | t.Fatalf("age 0 pruned images: %q", calls) | ||
| 65 | if trimmer.age != 0 { | ||
| 66 | t.Fatalf("age = %s", trimmer.age) | ||
| 138 | 67 | } | |
| 139 | 68 | } | |