Documentation
¶
Overview ¶
Package docker provides the driver that runs takt workloads as Docker containers.
Index ¶
- Constants
- Variables
- type Client
- type Config
- type Driver
- func (d *Driver) Confinable() error
- func (d *Driver) Digest(ctx context.Context, ref string) (string, error)
- func (d *Driver) Discard(ctx context.Context, _, workload string) error
- func (d *Driver) DiscardInstance(ctx context.Context, _, workload string, instance int) error
- func (d *Driver) Enforceable() error
- func (d *Driver) Logs(ctx context.Context, out io.Writer, workload string, options driver.LogOptions) error
- func (d *Driver) Name() string
- func (d *Driver) Observe(ctx context.Context) ([]driver.Instance, error)
- func (d *Driver) ObserveWorkload(ctx context.Context, _, name string) ([]driver.Instance, error)
- func (d *Driver) Probe(ctx context.Context, w driver.Workload, probe driver.Probe) error
- func (d *Driver) Prune(ctx context.Context, keep []manifest.Spec) error
- func (d *Driver) Signal(ctx context.Context, _, workload, signal string) error
- func (d *Driver) Start(ctx context.Context, w driver.Workload) (string, error)
- func (d *Driver) Stop(ctx context.Context, _, workload string) error
- func (d *Driver) StopInstance(ctx context.Context, _, workload string, instance int) error
- func (d *Driver) Usage(ctx context.Context, _, name string) (map[string]driver.Usage, error)
- func (d *Driver) Watch(ctx context.Context) (<-chan driver.Event, error)
Constants ¶
const ( // LabelWorkload names the container label holding the workload a container // belongs to. It is how the driver recognises its own work. LabelWorkload = "takt.workload" // LabelSpecHash names the container label holding the hash of the // specification a container was created from. LabelSpecHash = "takt.spec-hash" // LabelVersion names the container label holding the workload version a // container was created from. LabelVersion = "takt.version" // LabelAttempt names the container label holding which attempt at running a // workload a container is. // // The container's name carries it too, but the label is what the driver reads. A // name is an output of this driver rather than an input to it, so nothing parses // one back to learn what it means. // // It is also how a retained container is recognised. Docker fixes a container's // labels when it is created and offers no way to change them, so retention cannot // be written onto a container once something has replaced it. It does not need to // be: a container is superseded exactly when its instance has another container // with a higher attempt, which is a comparison rather than a mark. LabelAttempt = "takt.attempt" // LabelInstance names the container label holding the index of the workload // instance a container runs as. A container without it records the first // instance, which is what every container was before the label existed. LabelInstance = "takt.instance" )
const Name = "container"
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 ¶
var ( // ErrNotContainerWorkload is returned when NewWorkload is given a stored // workload whose specification carries no container block. ErrNotContainerWorkload = errors.New("workload does not describe a container") // ErrNoRunningInstance is returned when a probe is asked of an instance the // driver has no running container 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") // ErrNoWorkloadName is returned when a call that acts on one workload names // none. The driver finds a workload's containers by the value of its label, // and an empty value would match every container the driver owns. ErrNoWorkloadName = errors.New("no workload name given") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
// ImageList should return the images held locally that match the given options.
ImageList(ctx context.Context, options client.ImageListOptions) ([]image.Summary, error)
// ImagePull should pull the named image, returning the progress stream the
// caller must drain and close for the pull to complete.
ImagePull(ctx context.Context, ref string, options client.ImagePullOptions) (io.ReadCloser, error)
// ImageRemove should remove the named image, or untag it when the name is
// one of several tags on it, returning what the daemon deleted and untagged.
ImageRemove(ctx context.Context, ref string, options client.ImageRemoveOptions) ([]image.DeleteResponse, error)
// DistributionInspect should ask the image's registry for its manifest,
// which carries the digest the reference currently resolves to.
DistributionInspect(ctx context.Context, ref, encodedAuth string) (registry.DistributionInspect, error)
// ContainerCreate should create a container from the given configuration.
ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, name string) (container.CreateResponse, error)
// ContainerStart should start the container with the given identifier.
ContainerStart(ctx context.Context, id string, options client.ContainerStartOptions) error
// ContainerStop should stop the container with the given identifier.
ContainerStop(ctx context.Context, id string, options client.ContainerStopOptions) error
// ContainerRemove should remove the container with the given identifier.
ContainerRemove(ctx context.Context, id string, options client.ContainerRemoveOptions) error
// ContainerKill should send the named signal to the container with the given
// identifier.
ContainerKill(ctx context.Context, id, signal string) error
// ContainerList should return the containers matching the given options.
ContainerList(ctx context.Context, options client.ContainerListOptions) ([]container.Summary, error)
// ContainerInspect should return the full state of the container with the
// given identifier.
ContainerInspect(ctx context.Context, id string) (container.InspectResponse, error)
// ContainerLogs should return the log stream for the container with the
// given identifier.
ContainerLogs(ctx context.Context, id string, options client.ContainerLogsOptions) (io.ReadCloser, error)
// ContainerStatsOneShot should return a single reading of the resource usage
// of the container with the given identifier, without waiting for a second
// sample. The caller must close the body.
ContainerStatsOneShot(ctx context.Context, id string) (io.ReadCloser, error)
// ExecCreate should prepare a command to run inside the container with the
// given identifier, returning the identifier of the exec.
ExecCreate(ctx context.Context, id string, options client.ExecCreateOptions) (string, error)
// ExecAttach should start the exec with the given identifier and return its
// output, multiplexed as the engine multiplexes a container's logs. The
// caller must close the stream.
ExecAttach(ctx context.Context, id string) (io.ReadCloser, error)
// ExecInspect should report whether the exec with the given identifier is
// still running, and how it exited once it is not.
ExecInspect(ctx context.Context, id string) (client.ExecInspectResult, error)
// Events should return a stream of engine events matching the given options,
// alongside a channel carrying any error that ends the stream.
Events(ctx context.Context, options client.EventsListOptions) (<-chan events.Message, <-chan error)
// Close should release the client's underlying resources.
Close() error
}
The Client interface describes the Docker Engine surface the Driver uses.
The Docker SDK's own client is awkward to stand in for during tests — its APIClient interface has unexported methods, so it cannot be implemented outside its package — so the driver consumes this narrower interface instead and ships an adapter around the real thing. The adapter also flattens the SDK's single-field result structs, so the driver reads the value it asked for rather than unwrapping one.
func NewClient ¶
NewClient returns a Client talking to the Docker daemon described by the environment, negotiating the API version so that takt works against older daemons rather than failing on an unsupported version.
When host is empty the environment's configuration is used, which falls back to the local socket.
type Config ¶
type Config struct {
// The logger used for driver lifecycle events.
Logger *slog.Logger
// The client used to talk to the Docker daemon.
Client Client
// The address a container's host ports are published on. Empty publishes on
// loopback, which is the narrower of the two things this can mean.
Bind string
// The docker credential file registry credentials are resolved from. Empty
// reads docker's own default location, decided when a pull happens rather
// than here.
ConfigFile string
// Whether Prune removes the images no workload names and no container
// uses. Off leaves every image on the daemon.
Prune bool
// How long an image is left alone after nothing references it, so that a
// tag bumped and reverted within the delay does not pull the image again.
PruneDelay time.Duration
// Reports the current time, which the prune delay is measured against.
// May be nil, in which case the wall clock is used.
Now func() time.Time
// The meter the driver's instruments are created from. May be nil, in
// which case nothing is recorded.
MeterProvider metric.MeterProvider
// The tracer spans are created from. May be nil, in which case no spans
// are recorded.
TracerProvider trace.TracerProvider
}
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 Docker containers.
func (*Driver) Confinable ¶ added in v0.12.0
Confinable reports that this driver can always confine a workload. A container is confined by the daemon, so nothing of the host beyond the daemon is needed.
func (*Driver) Digest ¶
Digest asks the image's registry which digest the given reference currently resolves to.
This is what folds a pull-always workload's image content into its specification hash: the workload service calls it whenever it computes the hash, so a rebuilt tag moves the hash and the instance is replaced through the ordinary stale path. The registry is asked with the credentials the docker credential file holds for it, so a private image resolves wherever a docker pull on the host would.
func (*Driver) Discard ¶
Discard stops and removes everything the driver holds for the named workload, including whatever Stop retained.
This is what a delete and an orphan take, where Stop is what a replacement takes. A retained container exists so that an operator can read why the previous attempt failed. A workload nobody asked for has no such reader, and one left behind is a container the orphan sweep would find on every pass forever.
func (*Driver) DiscardInstance ¶
DiscardInstance stops and removes everything the driver holds for one instance of the named workload, including whatever was retained 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 container keeps.
func (*Driver) Enforceable ¶
Enforceable reports that this driver can always enforce resource limits. A container runs in cgroups of its own, so the limits need nothing of the host that running containers at all does not already need.
func (*Driver) Logs ¶
func (d *Driver) Logs(ctx context.Context, out io.Writer, workload string, options driver.LogOptions) error
Logs writes the output of the containers the driver holds for the named workload to out, limited to the last Tail lines of each.
Which containers those are is what options decides. The current attempt is read by default, and the retained one when Previous is asked for. The two are never combined: the driver holds the attempt now running and the one before it, so writing both would return two runs spliced together with nothing marking the boundary.
Following keeps the read open until the container ends or the caller goes away. Cancellation is an ordinary way for that to happen, so a cancelled read reports no error: the caller who stopped listening already knows why the output stopped.
The output is written as it is read rather than accumulated and returned. A workload's logs are unbounded in principle — a chatty container plus a generous tail is as much memory as the caller asks for — so holding the whole response before sending any of it would let one request decide how much the server uses.
func (*Driver) Observe ¶
Observe returns an instance for every container the driver owns, whatever state it is in.
This is the driver's report of what is actually running, and the only source of that information — the server persists nothing about it.
A container something has already replaced is reported as retained, so that a caller deciding what to run leaves it out while one sweeping up orphans still finds it.
RuntimeHealth is left empty here. Filling it costs an inspect per health-checked container on every pass, and the reconciler — the caller that pays that cost every few seconds — never reads the field. A workload list served from this observation forgoes an image's own verdict rather than paying an inspect per container for a column; the detail view is served by ObserveWorkload, which still fills it.
func (*Driver) ObserveWorkload ¶
ObserveWorkload reports every instance the driver is currently running for one workload, in the same terms as Observe.
The daemon does the filtering, on the label every container the driver starts carries. Reading one workload therefore costs one query for that workload rather than a listing of everything on the host, which is what a read of a single workload used to pay.
The identifier is unused here: a container records the workload's name in its labels, and that is what the filter matches. It is part of the signature because the exec driver keys its state on the identifier, and one interface serves both.
This is the one path that fills RuntimeHealth, because it is the one whose caller renders it: the workload detail view. The inspect that answers it is paid per health-checked container of a single workload, on a request someone made, rather than for the whole host on every reconcile pass.
func (*Driver) Probe ¶ added in v0.11.0
Probe runs a command health check inside the running container of one instance, returning nil when the command exits zero.
The command runs as the container does: in its namespaces and its cgroup, under its capability set, its seccomp profile and its user, with the environment the container already has. The probe's Env is never called, since nothing has to be resolved again for a runtime that keeps it.
The engine offers no way to end an exec, so a probe that outlives the caller's deadline fails here and is left running inside the container until it ends on its own or the container is replaced. The daemon's own health check lives with the same constraint. Output is captured to a bounded buffer for the error and never reaches the container's log.
func (*Driver) Prune ¶ added in v0.11.0
Prune removes the images that no workload in keep names and no container uses, once they have gone unreferenced for the driver's delay. Nothing else on the daemon is touched: not a container, a volume, a network or the build cache.
An image is held by any container that references it, whether or not takt created it and whether or not it is running. That is what bounds what a prune can reach: a container started by hand or by a compose stack beside takt holds its image, and so does the container the driver retains for an instance's output. Docker refuses to remove an image a container holds without being forced, and the driver never forces, so the daemon's own rule is the backstop behind the driver's.
A workload names its image in any state — running, suspended, waiting on a schedule or held in backoff — since a suspended workload resumes and a job recurs, and each would pull its image again if the prune had taken it. A specification naming a runtime other than this one holds nothing.
The delay is measured from when the driver first saw the image unreferenced rather than from when the image was built or pulled: an old image pulled a moment ago is not one that has gone unused.
func (*Driver) Signal ¶
Signal sends the named signal to every container the driver holds for the named workload.
This exists for a workload that mounts a value and asked to be told when it changes rather than replaced. Only a container that is running is signalled: docker refuses to signal one that has stopped, and a stopped container has nothing to reload.
The workload's identifier is not used, for the same reason Stop does not use it.
func (*Driver) Start ¶
Start creates and starts a container for the given workload, pulling its image first when it isn't already present locally.
The container is created with takt's ownership labels, which is the only record that ties it back to a workload — the server stores no container identifier. Docker's own restart policy is deliberately left unset: takt owns restart decisions so that they can be paced by its own backoff and remain visible in the workload's observed state.
func (*Driver) Stop ¶
Stop stops every container the driver holds for the named workload, keeping each instance's most recently created one so that its output can still be read.
The workload's identifier is not used. This driver's ownership is expressed in container labels rather than on disk, so the name is all it needs to find its work.
One container is retained per instance because this is the path a replacement and a restart both take: the reconciler stops before it starts, so removing everything here is what used to discard the output of the attempt that just failed. Docker keeps the logs of a container that exists, so retaining the container is all it takes and takt does not have to invent a retention policy or a size cap of its own.
Exactly one is retained per instance, and every other container the instance has — including whatever the previous stop retained — is removed in the same call. Unbounded retention would be a disk leak on a workload crashing in a loop, and pruning here rather than once a replacement has settled means the invariant holds at every moment rather than between passes. What that discards is the older corpse, where the one being kept is more recent evidence of the same failure.
func (*Driver) StopInstance ¶
StopInstance stops the containers the driver holds for one instance of the named workload, retaining the most recent one exactly as Stop does for every instance.
func (*Driver) Usage ¶ added in v0.7.0
Usage reports what each running container of one workload is consuming, keyed by container ID.
A retained container is left out. It exists so that its output can be read, and what it consumes is not the workload's usage. A container the daemon cannot report on is left out too, because it may have ended between the listing and the read. One missing reading should not hide the others.
The identifier is unused here for the reason it is unused by ObserveWorkload.
func (*Driver) Watch ¶
Watch returns a channel of events describing changes to the containers the driver owns, so that the caller can reconcile sooner than its next scheduled pass.
The returned channel is closed when ctx is cancelled or the underlying event stream fails. Events are a hint only: a caller that misses one still converges on its next pass, so a failed stream degrades responsiveness rather than correctness.