exec

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package exec provides the driver that runs workloads as processes on the host.

It is the counterpart to the docker driver for work that has no image: a command, its arguments, and an environment. The command is started directly rather than through a shell, so nothing has to decide how to split it and no shell is involved unless the command names one.

A process outlives the server that started it. Takt releases it on shutdown and re-adopts it on the next start, which is what makes restarting the server a different thing from restarting the workloads it runs.

Index

Constants

View Source
const Name = "exec"

Name is how this driver identifies itself, and is what the server maps a workload's runtime onto when deciding which driver runs it.

Variables

View Source
var (
	// ErrNotConfinable is returned when the kernel does not offer the Landlock support
	// an exec workload is confined with.
	//
	// There is deliberately no degraded mode. A confinement that silently did nothing
	// on some hosts would be a guarantee nothing could reason about, and every mention
	// of it would have to be qualified.
	ErrNotConfinable = errors.New("kernel does not support confining exec workloads")
	// ErrNotConfined is returned when a workload could not be confined to the paths it
	// was given, and so was not started.
	ErrNotConfined = errors.New("workload could not be confined")
	// ErrGroupsInherited is returned when the server holds supplementary groups it
	// cannot keep from a workload.
	//
	// A workload runs as the server's user, and would otherwise hold every group that
	// user is in. The packaged install puts the user in the docker group, and a
	// workload holding it can run a privileged container. The ruleset does not cover
	// this: connecting to a socket is not an operation that names a path, so the
	// socket's own permissions are all that stand between a workload and the daemon.
	// Dropping the groups takes CAP_SETGID, which the unit grants, so a server without
	// it and with groups to drop refuses exec workloads rather than run them widened.
	ErrGroupsInherited = errors.New("exec workloads would inherit the server's supplementary groups")
)
View Source
var (
	// ErrNotExecWorkload is returned when the driver is given a workload whose
	// specification carries no exec block.
	ErrNotExecWorkload = errors.New("workload does not describe a command")
	// ErrInvalidWorkloadID is returned when a workload's identifier could not be used
	// as a directory beneath the driver's root.
	ErrInvalidWorkloadID = errors.New("workload identifier is not usable as a directory")
	// ErrInvalidMount is returned when a volume could not be placed inside the
	// workload's working directory.
	ErrInvalidMount = errors.New("volume cannot be mounted there")
	// ErrUnknownWorkload is returned when the driver has no record of a workload it
	// was asked about by name.
	ErrUnknownWorkload = errors.New("driver has no record of the workload")
	// ErrUnknownSignal is returned when the driver is asked to send a signal it does
	// not recognise.
	ErrUnknownSignal = errors.New("signal is not one this driver sends")
)
View Source
var (
	// ErrNoRunningInstance is returned when a probe is asked of an instance the
	// driver has nothing running for.
	ErrNoRunningInstance = errors.New("no running instance to probe")
	// ErrProbeFailed is returned when a probe ran and exited with a status other
	// than zero. The error carries the status and the tail of what the command
	// wrote.
	ErrProbeFailed = errors.New("probe failed")
)
View Source
var (
	// ErrNotEnforceable is returned when the host gives takt no cgroup subtree of its
	// own, which is what an exec workload's resource limits are enforced with.
	//
	// There is deliberately no degraded mode, for the reason confinement has none: a
	// limit that silently did not apply on some hosts would be a guarantee nothing
	// could reason about. In particular rlimits are not an approximation — RLIMIT_AS
	// caps address space rather than memory used, RLIMIT_NPROC counts the user's
	// processes rather than the workload's, and no rlimit bounds processor time the
	// way a quota does — so the same manifest field would mean something different
	// per runtime.
	ErrNotEnforceable = errors.New("host does not delegate a cgroup subtree to enforce resource limits with")
)

Functions

func Confinable

func Confinable() error

Confinable reports whether this host can confine a workload at all, returning ErrNotConfinable when it cannot.

Asked before a workload is started rather than discovered while starting one, so a host that cannot confine says so instead of leaving a half-created workload behind.

Exported so that a caller can report the same refusal the driver would, without having to start a workload to find out.

func Confine

func Confine()

Confine turns this process into a workload's confined command, when it was started to be one, and otherwise returns so the caller carries on.

Called before anything else a binary does, because this process is not running takt: it is about to become the workload. Anything the caller would otherwise set up would be discarded by the exec a moment later.

It does not return in the case it exists for. Either the process becomes the workload's command, or it reports why it could not and exits.

func Enforceable

func Enforceable() error

Enforceable reports whether this host lets takt enforce resource limits on an exec workload, returning ErrNotEnforceable when it does not.

Asked before a workload is started rather than discovered while starting one, for the reason Confinable is. Exported for the same reason too: the server refuses a manifest asking for limits it cannot enforce, and refusing at apply time needs the same answer starting the workload would get.

Types

type Config

type Config struct {
	// The logger used for driver lifecycle events.
	Logger *slog.Logger
	// The directory beneath which the driver keeps everything, both its own
	// records and the directories it gives workloads.
	Root string
	// Extra paths every workload may read, for a runtime that lives somewhere the
	// host's own directories do not cover.
	//
	// Read-only, and the operator's decision rather than a workload's: the API has
	// no authentication, so a workload able to widen its own confinement would undo
	// it.
	AllowPaths []string
	// Paths beneath the host's own directories that no workload may read: where
	// the server keeps what is its alone, such as its configuration and keys.
	// A workload runs as the server's user, so nothing but the confinement
	// keeps these from it.
	DenyPaths []string
}

The Config type contains fields used to construct a Driver.

type Driver

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

The Driver type runs workloads as processes on the host.

func New

func New(config Config) *Driver

New returns a Driver that runs processes under the root directory in config.

func (*Driver) Confinable added in v0.12.0

func (d *Driver) Confinable() error

Confinable reports whether this host can confine a workload, returning ErrNotConfinable when it cannot. It answers the package-level question for a caller that holds the driver rather than the package, which is what lets the service refuse a manifest at apply rather than the reconciler at start.

func (*Driver) Discard

func (d *Driver) Discard(ctx context.Context, id, workload string) error

Discard ends everything the driver runs for a workload and removes both of its trees, including the output Stop kept.

This is what a delete and an orphan take, where Stop is what a replacement takes. The kept output exists so that an operator can read why the previous attempt failed, and a workload nobody asked for has no such reader.

func (*Driver) DiscardInstance

func (d *Driver) DiscardInstance(ctx context.Context, id, workload string, instance int) error

DiscardInstance ends what the driver runs for one instance of a workload and removes its directories from both trees, including the output Stop kept for it.

This is what removing an instance takes when a workload's count shrinks: the instance is not being replaced, so nothing will read the output a retained record keeps.

func (*Driver) Enforceable

func (d *Driver) Enforceable() error

Enforceable reports what the package-level Enforceable reports, from the driver itself. The server holds drivers rather than packages, and asks the one that would run a workload whether its host can enforce the limits.

func (*Driver) Logs

func (d *Driver) Logs(ctx context.Context, out io.Writer, workload string, options driver.LogOptions) error

Logs writes the tail of a workload's output.

Which output that is depends on options: the version now running by default, and the one a replacement kept when Previous is asked for. The two are never combined, for the reason the docker driver does not combine them — the result would be two runs spliced together with nothing marking the boundary.

Both streams were written to one file, so they come back interleaved in the order the process wrote them. A workload the driver has nothing for writes nothing, since the caller does not know which runtime holds it.

Following keeps writing until the process ends or the caller goes away. Only the version now running is followed: an older one holds an attempt that has finished, so waiting on it would wait for output that is never coming.

Since is ignored. The output file carries no timestamps, so honouring it would mean giving each line a time the driver made up, and a filter over invented times answers a question nobody asked.

func (*Driver) Name

func (d *Driver) Name() string

Name returns the name this driver is registered under.

func (*Driver) Observe

func (d *Driver) Observe(_ context.Context) ([]driver.Instance, error)

Observe reports an instance for every workload the driver has a record of.

The records are the source of truth rather than the supervised set, because a process started by an earlier server has no supervisor and would otherwise be invisible — which would read as absent and have the workload started a second time.

func (*Driver) ObserveWorkload

func (d *Driver) ObserveWorkload(_ context.Context, id, _ string) ([]driver.Instance, error)

ObserveWorkload reports every instance the driver is running for one workload, in the same terms as Observe.

The state tree is keyed by the workload's identifier, so this reads one directory rather than every directory the driver holds. A workload with no records yet has none to report, which is not a failure: it reads as a workload nothing is running for, which is what it is.

The name is unused here, because a record carries its own. It is part of the signature because the docker driver filters on the name, and one interface serves both.

func (*Driver) Probe added in v0.11.0

func (d *Driver) Probe(ctx context.Context, w driver.Workload, probe driver.Probe) error

Probe runs a command health check for one instance of a workload, returning nil when the command exits zero.

The command runs as the instance does. It starts in the instance's working directory, is confined to the same ruleset — the directory, the volumes the instance was started with, the host's own files — through the same trampoline the instance went through, and is given the environment the workload asked for, resolved again for this run. The promise the runtime makes is that nothing a manifest names runs unconfined, and a check is something a manifest names.

Where the instance has a cgroup, the probe gets one of its own beside it under the same limits, rather than joining the instance's. The limits already written there are the command's, and the trampoline that confines the probe is a Go runtime that a small process limit would refuse to start. A cgroup of its own is also what makes the deadline certain: killing it reaches a probe that left its process group, and it is removed once the probe has ended.

Output is captured to a bounded buffer for the error and never reaches the instance's log, which is the workload's own.

func (*Driver) Prune added in v0.11.0

func (d *Driver) Prune(context.Context, []manifest.Spec) error

Prune does nothing. A process has no image to leave behind: what an exec workload runs is a command on the host, which is the operator's to install and remove.

func (*Driver) Release

func (d *Driver) Release()

Release lets every supervised process outlive the driver.

Called at shutdown, so that restarting the server is not the same thing as restarting the workloads it runs. The processes keep running and the next start adopts them from their records.

func (*Driver) Signal

func (d *Driver) Signal(_ context.Context, id, workload, signal string) error

Signal sends the named signal to every process the driver runs for the named workload.

This exists for a workload that mounts a value and asked to be told when it changes rather than replaced.

The process is signalled rather than its group, which is the opposite of what Stop does and deliberately so: stopping a workload has to reach whatever it started, where a reload is for the program that read the file. A group-wide reload would reach children that never asked for one.

func (*Driver) Start

func (d *Driver) Start(ctx context.Context, w driver.Workload) (string, error)

Start runs the workload's command, returning the identifier of the process.

The process is given a directory of its own, a process group of its own, and an environment holding only what the workload asked for. Its output goes to a file in that directory, which is what Logs reads.

It is also confined: the kernel refuses it every path outside its own directory, the volumes it mounts, the values it mounts and the host's own files. A host whose kernel cannot do that is refused here rather than running the workload unconfined.

It runs in a cgroup of its own, which is what its usage is read from and what enforces its resource limits when it named some. Making one takes a delegated subtree. A host without one refuses a workload naming limits rather than running it unlimited, for the reason a host that cannot confine refuses it, and runs one naming none as it would have anyway — without a cgroup, and so without a reading.

func (*Driver) Stop

func (d *Driver) Stop(ctx context.Context, id, workload string) error

Stop ends everything the driver runs for a workload, keeping each instance's most recent output so that it can still be read.

The process group is signalled rather than the process, so that anything the command started is stopped with it. A group given time to stop and still running is killed: a workload that ignores the request must not be able to block the pass that asked.

One version's output is kept per instance, for the reason the docker driver keeps a container: the reconciler stops before it starts, so removing everything here is what used to discard the output of the attempt that just failed. What survives is that version's output.log and its record, marked retained. The working directory goes with the rest, because it holds the symlinks to the workload's volumes and nothing is left to read them.

Every other version goes, in both trees. Keeping one version per instance rather than all of them bounds what a workload crashing in a loop leaves on the disk.

func (*Driver) StopInstance

func (d *Driver) StopInstance(ctx context.Context, id, workload string, instance int) error

StopInstance ends what the driver runs for one instance of a workload, retaining its most recent output exactly as Stop does for every instance.

func (*Driver) Supervises

func (d *Driver) Supervises(workload string) bool

Supervises reports whether the driver is still waiting on a workload's process.

A supervised process is one this server started and can collect an exit code from. A released or adopted one is not, which is the difference between a workload that outlives the server and one that does not.

func (*Driver) Usage added in v0.7.0

func (d *Driver) Usage(_ context.Context, id, _ string) (map[string]driver.Usage, error)

Usage reports what each running process of one workload is consuming, keyed by the identifier its instance is reported under.

The reading is the counters the kernel keeps in the process's cgroup, beside the limits it enforces there when the workload named some. A process with no cgroup of its own — one started on a host that could not make one, or by a server that predates every workload getting one — shares whichever cgroup the server runs in, so there is nothing there that describes the workload rather than the server, and reporting the server's own consumption as the workload's would be worse than reporting nothing.

A retained process has ended and consumes nothing, so it is left out, as is a record whose cgroup cannot be read: one workload's missing reading should not hide the instances beside it.

The name is unused here for the reason it is unused by ObserveWorkload.

func (*Driver) Wait

func (d *Driver) Wait()

Wait blocks until every supervising goroutine has finished.

A supervising goroutine outlives the call that started it: it waits on a process, records how that process ended, and logs it. Nothing about the driver's own methods says when that has happened, so anything tearing down what those goroutines write to — a logger, a directory — has to wait for them first.

This does not stop anything. A process still running keeps its supervisor waiting, so stop or release the work before calling this or it waits for as long as the workload runs.

func (*Driver) Watch

func (d *Driver) Watch(ctx context.Context) (<-chan driver.Event, error)

Watch reports when a supervised process ends.

There is no event stream to subscribe to, so the driver is its own source: the goroutine waiting on a process reports its exit. A process adopted from an earlier server produces no event, and the reconciler's ticker covers that.

The same goroutine rotates the output of every workload naming a cap. It is the one long-lived goroutine the driver has, and it lives for exactly as long as the server is watching the runtime, which is how long output should be rotated for.

Jump to

Keyboard shortcuts

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