Documentation
¶
Overview ¶
Package runtime abstracts a container runtime (Docker or Podman) behind the small set of operations Ballast needs: discover containers and their mounts, watch the socket for lifecycle changes, exec into a container to quiesce or dump it, and stop or start it for a cold backup.
The interface is deliberately kept free of any tool-specific type. It was shaped against exactly one real consumer (Ballast) before being lifted into github.com/tagwright/core, which is the discipline that keeps the abstraction honest.
The Docker adapter lands first. The Podman adapter follows behind the same interface, talking to Podman's Docker-compatible compat API and absorbing the socket-path and compose-label differences.
Index ¶
- Variables
- type Container
- type ContainerNetwork
- type DockerRuntime
- func (e DockerRuntime) Close() error
- func (e DockerRuntime) Exec(ctx context.Context, id string, spec ExecSpec) (*ExecHandle, error)
- func (e DockerRuntime) Inspect(ctx context.Context, id string) (Container, error)
- func (e DockerRuntime) Kill(ctx context.Context, id string, signal string) error
- func (e DockerRuntime) List(ctx context.Context) ([]Container, error)
- func (e DockerRuntime) ListNetworks(ctx context.Context) ([]Network, error)
- func (e DockerRuntime) Restart(ctx context.Context, id string) error
- func (e DockerRuntime) Start(ctx context.Context, id string) error
- func (e DockerRuntime) Stop(ctx context.Context, id string, timeoutSeconds int) error
- func (e DockerRuntime) Watch(ctx context.Context) (<-chan Event, <-chan error)
- type Event
- type EventType
- type ExecHandle
- type ExecSpec
- type Mount
- type MountType
- type Network
- type NetworkInspector
- type PodmanRuntime
- func (e PodmanRuntime) Close() error
- func (e PodmanRuntime) Exec(ctx context.Context, id string, spec ExecSpec) (*ExecHandle, error)
- func (e PodmanRuntime) Inspect(ctx context.Context, id string) (Container, error)
- func (e PodmanRuntime) Kill(ctx context.Context, id string, signal string) error
- func (e PodmanRuntime) List(ctx context.Context) ([]Container, error)
- func (e PodmanRuntime) ListNetworks(ctx context.Context) ([]Network, error)
- func (e PodmanRuntime) Restart(ctx context.Context, id string) error
- func (e PodmanRuntime) Start(ctx context.Context, id string) error
- func (e PodmanRuntime) Stop(ctx context.Context, id string, timeoutSeconds int) error
- func (e PodmanRuntime) Watch(ctx context.Context) (<-chan Event, <-chan error)
- type Runtime
Constants ¶
This section is empty.
Variables ¶
var ErrNotImplemented = errors.New("runtime: not implemented")
ErrNotImplemented is returned by adapter methods that are not wired up yet.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
ID string
Name string
State string // running, exited, paused, ...
Labels map[string]string
Mounts []Mount
Project string // com.docker.compose.project, empty if not a compose service
Service string // com.docker.compose.service, empty if not a compose service
// Image is the container's image reference. Populated on both List and
// Inspect.
Image string
// LogDriver is the effective logging driver, e.g. "json-file", "local",
// "journald". Inspect-only (the list summary carries no HostConfig), and
// empty when unknown.
LogDriver string
// Env holds the container's environment entries as KEY=VALUE strings.
// Inspect-only. Core surfaces the raw slice: callers that only need the
// names must split it themselves and must not log the values.
Env []string
// Health is the container health status when a HEALTHCHECK is defined,
// e.g. "healthy", "unhealthy", "starting". Empty when the container has no
// healthcheck. Inspect-only.
Health string
// Networks lists the container's network attachments and the IP
// addresses it holds on each. Unlike Image/LogDriver/Env/Health, the
// list summary carries this data at no extra cost (it is already part
// of the same API response), so Networks is populated on both List and
// Inspect.
Networks []ContainerNetwork
}
Container is the normalized view of a container across runtimes.
type ContainerNetwork ¶ added in v0.3.0
ContainerNetwork is one network a container is attached to, along with the IP addresses it holds on that network.
type DockerRuntime ¶
type DockerRuntime struct {
// contains filtered or unexported fields
}
DockerRuntime is the Docker adapter for Runtime. It talks to the Docker Engine API over the socket Ballast mounts read-only, using the request and mapping machinery in engine.go that it shares with PodmanRuntime.
The client is created lazily on first use and cached, so constructing a DockerRuntime never touches the socket: nothing fails until a method that actually needs the daemon is called.
func NewDocker ¶
func NewDocker(socket string) *DockerRuntime
NewDocker returns a Docker adapter bound to the given API socket path.
func (DockerRuntime) Close ¶
func (e DockerRuntime) Close() error
Close releases the underlying client.
func (DockerRuntime) Exec ¶
Exec runs a command inside a running container and returns a handle whose Stdout streams the command's standard output as it is produced, which matters because the caller pipes a live dump into restic --stdin rather than buffering it. Standard error is captured separately and folded into the error Wait returns on a non-zero exit.
func (DockerRuntime) Kill ¶ added in v0.2.0
Kill sends a signal to a running container, e.g. "SIGHUP" to prompt a collector to reload its configuration.
func (DockerRuntime) ListNetworks ¶ added in v0.3.0
ListNetworks returns every network the runtime knows about, with its subnet CIDRs and internal flag. It satisfies NetworkInspector for both DockerRuntime and PodmanRuntime, which embed engineClient.
func (DockerRuntime) Restart ¶ added in v0.2.0
Restart restarts a container, using the runtime's default stop timeout.
type ExecHandle ¶
ExecHandle is a running exec. The caller reads Stdout to completion, then calls Wait to learn the exit code. Stderr is captured separately for logging.
type Mount ¶
type Mount struct {
Type MountType
Name string // named-volume name, empty for binds and tmpfs
Source string // host-side path, empty for tmpfs
Destination string // container-side path
ReadOnly bool
}
Mount is one filesystem mount attached to a container.
type MountType ¶
type MountType string
MountType distinguishes the kinds of mount Ballast cares about.
type Network ¶ added in v0.3.0
type Network struct {
Name string
ID string
Driver string
Internal bool
Subnets []netip.Prefix
Labels map[string]string
}
Network is the normalized view of a container network across runtimes.
type NetworkInspector ¶ added in v0.3.0
type NetworkInspector interface {
// ListNetworks returns every network the runtime knows about, with its
// subnet CIDRs and whether it is marked internal.
ListNetworks(ctx context.Context) ([]Network, error)
}
NetworkInspector is an optional capability a Runtime implementation may satisfy in addition to Runtime. It is kept as a separate interface, rather than a new method on Runtime, so that adding it never breaks an existing consumer's mock or alternate implementation of Runtime: a consumer that wants network introspection type-asserts the value it got back from a constructor (or from Runtime) to NetworkInspector, and a consumer that does not care about networks is unaffected.
DockerRuntime and PodmanRuntime both satisfy NetworkInspector.
type PodmanRuntime ¶
type PodmanRuntime struct {
// contains filtered or unexported fields
}
PodmanRuntime is the Podman adapter for Runtime. Podman's REST API includes a Docker-compatible compat layer (documented against the Docker v1.40 API) on the same socket as its native libpod API, so PodmanRuntime talks to it with the exact request and mapping machinery DockerRuntime uses, in engine.go; the two adapters differ only in their default socket path and in how compose project/service identity is read off a container's labels.
The client is created lazily on first use and cached, so constructing a PodmanRuntime never touches the socket: nothing fails until a method that actually needs the daemon is called.
func NewPodman ¶
func NewPodman(socket string) *PodmanRuntime
NewPodman returns a Podman adapter bound to the given API socket path. An empty socket resolves to a sensible default: the rootless per-user socket derived from XDG_RUNTIME_DIR (or /run/user/<uid> when that is unset, matching systemd's own convention) for a non-root caller, and the rootful system-service socket for a root caller with no XDG_RUNTIME_DIR set.
func (PodmanRuntime) Close ¶
func (e PodmanRuntime) Close() error
Close releases the underlying client.
func (PodmanRuntime) Exec ¶
Exec runs a command inside a running container and returns a handle whose Stdout streams the command's standard output as it is produced, which matters because the caller pipes a live dump into restic --stdin rather than buffering it. Standard error is captured separately and folded into the error Wait returns on a non-zero exit.
func (PodmanRuntime) Kill ¶ added in v0.2.0
Kill sends a signal to a running container, e.g. "SIGHUP" to prompt a collector to reload its configuration.
func (PodmanRuntime) ListNetworks ¶ added in v0.3.0
ListNetworks returns every network the runtime knows about, with its subnet CIDRs and internal flag. It satisfies NetworkInspector for both DockerRuntime and PodmanRuntime, which embed engineClient.
func (PodmanRuntime) Restart ¶ added in v0.2.0
Restart restarts a container, using the runtime's default stop timeout.
type Runtime ¶
type Runtime interface {
// List returns every container the runtime knows about, running or not.
List(ctx context.Context) ([]Container, error)
// Inspect returns a single container by ID or name.
Inspect(ctx context.Context, id string) (Container, error)
// Watch streams lifecycle events until ctx is cancelled. The error channel
// carries a terminal error and is then closed alongside the event channel.
Watch(ctx context.Context) (<-chan Event, <-chan error)
// Exec runs a command inside a running container and returns a handle whose
// Stdout the caller reads (for stream backups it is piped straight into the
// engine's stdin) before calling Wait for the exit code.
Exec(ctx context.Context, id string, spec ExecSpec) (*ExecHandle, error)
// Stop stops a running container, waiting up to timeoutSeconds before a kill.
Stop(ctx context.Context, id string, timeoutSeconds int) error
// Start starts a stopped container.
Start(ctx context.Context, id string) error
// Kill sends a signal to a running container, e.g. "SIGHUP" to prompt a
// collector to reload its configuration.
Kill(ctx context.Context, id string, signal string) error
// Restart restarts a container, using the runtime's default stop timeout.
Restart(ctx context.Context, id string) error
// Close releases the underlying client.
Close() error
}
Runtime is a container runtime Ballast can drive. Implementations must be safe for concurrent use by multiple goroutines.