runtime

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanOldLogs

func CleanOldLogs(logDir string, maxAge time.Duration, log *slog.Logger)

CleanOldLogs removes job log files older than maxAge from the log directory. Called on startup and periodically to prevent unbounded disk usage.

func ImportImagesTo

func ImportImagesTo(ctx context.Context, c *client.Client, paths []string, snapshotter string, log *slog.Logger)

ImportImagesTo imports a list of OCI image tarballs into the given containerd client. Used to import deferred Linux images into a VM's containerd after the VM is ready.

Types

type Config

type Config struct {
	Client       *client.Client
	RunnerDir    string // host path to extracted runner binary
	RunnerMount  string // container path to mount runner at
	DefaultImage string // override default container image (auto-detected if empty)
	ImagesDir    string // directory containing pre-downloaded OCI image tarballs to import on startup
	LogDir       string // directory for per-job container logs
	DataDir      string // ephemerd data directory (used for dind socket paths)
	// ContainerDataDir is the path containerd/runc see for the DataDir.
	// On Linux this matches DataDir. On Darwin the host DataDir is shared
	// into the Linux VM via virtio-fs at a different path (e.g.
	// /mnt/ephemerd), and any bind-mount sources that reference the
	// DataDir must be rewritten to that VM-side path. When empty, falls
	// back to DataDir.
	ContainerDataDir string
	DindEnabled      bool // mount a fake Docker socket into each container
	// DindAllowPrivileged is forwarded to each per-job dind.Server.
	// When false, requests carrying HostConfig.Privileged=true or
	// HostConfig.CapAdd are rejected with HTTP 403. See
	// config.DindConfig.AllowPrivileged for the threat model.
	DindAllowPrivileged bool
	CacheProxyEnv       []string // extra env vars from cache proxies (e.g., GOPROXY=...)
	// Rlimits sets POSIX resource limits on each runner container's OCI
	// process. Zero values fall back to the containerd default (1024).
	// Applies on Linux only; ignored on Windows (HCS uses a different model).
	Rlimits config.RuntimeRlimits
	Network *networking.Manager
	// WindowsMemoryBytes is the memory limit for Hyper-V isolated Windows
	// runner containers. Zero leaves the OCI spec field unset, which gives
	// the HCS default (~1 GB) — too small for MSVC builds. Caller should
	// pass config.WindowsRunnerToml.MemoryBytes() which defaults to 4 GB.
	WindowsMemoryBytes uint64
	// WindowsCPUs is the virtual CPU count for Hyper-V isolated Windows
	// runner containers. Zero leaves the OCI spec field unset. Caller
	// should pass config.WindowsRunnerToml.CPUCount() which defaults to 2.
	WindowsCPUs uint64
	// BuildKit is the shared embedded BuildKit solver handed to each per-job
	// dind.Server for `docker build` support. Optional; nil means `docker build`
	// falls back to the platform default (buildah on Linux, 501 elsewhere).
	BuildKit *buildkit.Server
	// OnTaskStarted is invoked synchronously by Create after the container
	// task has successfully Start()ed. Nil means no hook. Used to wire
	// per-container resource samplers into the metrics endpoint; see
	// docs/arch/container-metrics.md.
	OnTaskStarted func(env *RunnerEnv)
	// OnTaskDestroy is invoked synchronously by Destroy before the
	// container is torn down. Symmetric with OnTaskStarted.
	OnTaskDestroy func(env *RunnerEnv)
	Log           *slog.Logger
}

Config for the container runtime.

type CreateConfig

type CreateConfig struct {
	ID    string // unique job identifier (container name, dind socket path)
	Image string // OCI image reference (empty = use default)

	// Provider is the forge provider name (e.g. "github", "gitea") that
	// queued the job. Together with Repo it's used to scope dind's
	// per-repo image cache. Empty disables caching for this job.
	Provider string

	// Repo is the forge-native repo path (e.g. "owner/repo"). Together
	// with Provider it's used to scope dind's per-repo image cache.
	// Empty disables caching for this job.
	Repo string

	// JITConfig is the base64-encoded JIT config for GitHub runners.
	// Passed as "--jitconfig <value>" to the runner entrypoint.
	// Mutually exclusive with Entrypoint.
	JITConfig string

	// Env holds extra environment variables injected into the container.
	// Used by Gitea/Forgejo to pass instance URL, runner token, etc.
	Env map[string]string

	// Entrypoint overrides the container's process args.
	// When set, used instead of the default "--jitconfig" entrypoint.
	// When nil and JITConfig is set, uses the GitHub "--jitconfig" mode.
	// When nil and JITConfig is empty, uses the image's default CMD.
	Entrypoint []string
}

CreateConfig holds parameters for creating a runner environment.

type RunnerEnv

type RunnerEnv struct {
	ID        string
	Provider  string       // forge provider that queued the job (e.g. "github")
	Repo      string       // forge-native repo path (e.g. "owner/repo")
	Netns     string       // network namespace path (Linux only)
	RunnerDir string       // per-job runner copy, cleaned up on destroy
	Dind      *dind.Server // per-job fake Docker daemon (nil if disabled)
	Container client.Container
	Task      client.Task
}

RunnerEnv represents a running runner environment.

type Runtime

type Runtime struct {
	// contains filtered or unexported fields
}

Runtime manages container lifecycle for runner environments.

func New

func New(cfg Config) (*Runtime, error)

New creates a container runtime manager.

func (*Runtime) CleanOrphans

func (r *Runtime) CleanOrphans(ctx context.Context) error

CleanOrphans removes any leftover containers and snapshots from a previous ephemerd run. This should be called on startup before the scheduler starts accepting jobs.

func (*Runtime) Client

func (r *Runtime) Client() *client.Client

Client returns the underlying containerd client. Used by the in-VM debug-exec HTTP server so the Windows host can poke into running containers (kindest/node, buildkit) without leaving the VM.

func (*Runtime) Create

func (r *Runtime) Create(ctx context.Context, cfg CreateConfig) (*RunnerEnv, error)

Create provisions an ephemeral runner environment.

func (*Runtime) Destroy

func (r *Runtime) Destroy(ctx context.Context, env *RunnerEnv) error

Destroy tears down a runner environment completely.

func (*Runtime) ImportImages

func (r *Runtime) ImportImages(ctx context.Context) (deferred []string, err error)

ImportImages loads pre-downloaded OCI image tarballs from the images directory. Each tarball is inspected for its target OS. Images matching the host OS are imported into the host containerd and unpacked immediately. Images targeting a different OS (e.g. Linux images on a Windows host) are returned as deferred paths — the caller should import them into the appropriate VM's containerd after the VM is ready using ImportImagesTo.

On Linux, all images are imported directly (no deferral).

func (*Runtime) LogDir

func (r *Runtime) LogDir() string

LogDir returns the configured per-job log directory (empty if logs go to stdio).

func (*Runtime) PullImage

func (r *Runtime) PullImage(ctx context.Context, ref string) error

PullImage ensures the runner image is available locally. Serialized with a mutex to avoid concurrent pulls contending on the content store (which produces noisy lock errors).

func (*Runtime) SetTaskHooks

func (r *Runtime) SetTaskHooks(onStarted, onDestroy func(*RunnerEnv))

SetTaskHooks installs (or replaces) the OnTaskStarted / OnTaskDestroy callbacks. Used by main.go to wire metrics-sampler registration after both the runtime and the dispatch server have been constructed — constructor-order makes a plain Config-time hook awkward because the dispatch server depends on the runtime.

func (*Runtime) Wait

func (r *Runtime) Wait(ctx context.Context, env *RunnerEnv) (uint32, error)

Wait blocks until the runner environment's task exits. Returns the exit status code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL