backend

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package backend abstracts where a job's container actually runs. The controller speaks only to this interface, so the same submit/run/cancel logic drives local Docker today (docker.go) and Kubernetes later (P6) without change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContainerBackend

type ContainerBackend interface {
	// Launch starts a container and returns its backend-specific ID. The
	// data volume is reused across launches of the same JobID so state
	// and checkpoints survive a restart.
	Launch(ctx context.Context, spec LaunchSpec) (containerID string, err error)
	// Stop requests a graceful stop (SIGTERM), waiting up to timeout
	// before killing. Safe to call on an already-stopped container.
	Stop(ctx context.Context, containerID string, timeout time.Duration) error
	// Status reports the container's current phase and control address.
	Status(ctx context.Context, containerID string) (Status, error)
	// Logs returns up to tail lines of the container's stdout+stderr.
	Logs(ctx context.Context, containerID string, tail int) (string, error)
	// Remove deletes the container (not its data volume).
	Remove(ctx context.Context, containerID string) error
}

ContainerBackend launches and manages one container per job.

type Docker

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

Docker runs each job as a local Docker container using the runner image. The workflow document is copied into the container before start (so it works regardless of where the daemon runs), and /data is backed by a named volume per job so state and checkpoints survive restarts.

func NewDocker

func NewDocker(image string) (*Docker, error)

NewDocker connects to the Docker daemon from the environment. image is the runner image tag to launch (e.g. "mailer-runner:dev").

func (*Docker) HasImage

func (d *Docker) HasImage(ctx context.Context, ref string) (bool, error)

HasImage reports whether the given image reference is present locally, so the CLI can fail fast with a build hint instead of a launch error.

func (*Docker) Launch

func (d *Docker) Launch(ctx context.Context, spec LaunchSpec) (string, error)

func (*Docker) Logs

func (d *Docker) Logs(ctx context.Context, id string, tail int) (string, error)

func (*Docker) Ping

func (d *Docker) Ping(ctx context.Context) error

Ping verifies the daemon is reachable — used at controller startup and to gate integration tests.

func (*Docker) Remove

func (d *Docker) Remove(ctx context.Context, id string) error

func (*Docker) Status

func (d *Docker) Status(ctx context.Context, id string) (Status, error)

func (*Docker) Stop

func (d *Docker) Stop(ctx context.Context, id string, timeout time.Duration) error

type Fake

type Fake struct {

	// LaunchErr, if set, makes the next Launch fail (then clears).
	LaunchErr error
	// contains filtered or unexported fields
}

Fake is an in-memory ContainerBackend for controller unit tests. It records launches and lets a test drive each container's phase without Docker. Safe for concurrent use.

func NewFake

func NewFake() *Fake

NewFake returns a ready fake backend.

func (*Fake) LastEnv

func (f *Fake) LastEnv(id string) map[string]string

LastEnv returns the env a container was launched with (for assertions).

func (*Fake) LastImage added in v0.7.0

func (f *Fake) LastImage(id string) string

LastImage returns the image a container was launched with.

func (*Fake) LastWorkflowDoc added in v0.7.0

func (f *Fake) LastWorkflowDoc(id string) []byte

LastWorkflowDoc returns the workflow document a container was launched with (empty for SDK jobs).

func (*Fake) Launch

func (f *Fake) Launch(ctx context.Context, spec LaunchSpec) (string, error)

func (*Fake) Launched

func (f *Fake) Launched() int

Launched reports how many containers were ever launched.

func (*Fake) Logs

func (f *Fake) Logs(ctx context.Context, id string, tail int) (string, error)

func (*Fake) Remove

func (f *Fake) Remove(ctx context.Context, id string) error

func (*Fake) SetLogs

func (f *Fake) SetLogs(id, logs string)

SetLogs sets the log text a container returns.

func (*Fake) SetPhase

func (f *Fake) SetPhase(id string, phase Phase, exitCode int)

SetPhase forces a container's phase, simulating an exit or crash.

func (*Fake) Status

func (f *Fake) Status(ctx context.Context, id string) (Status, error)

func (*Fake) Stop

func (f *Fake) Stop(ctx context.Context, id string, timeout time.Duration) error

type Kubernetes added in v0.7.0

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

Kubernetes runs each job as a batch/v1 Job on a cluster — the same jobs the Docker backend runs locally, so the controller, reconciler, API, and web UI are unchanged. A Job (not a Deployment) is used so a completed pod is not auto-restarted by Kubernetes: mailer's own reconciler owns restart decisions (backoffLimit is 0).

Per job: a per-job PVC (state + checkpoints, reused across restarts), a ConfigMap (the workflow), an optional Secret (env), and a ClusterIP Service so the controller can reach the agent's control surface.

func NewKubernetes added in v0.7.0

func NewKubernetes(opts KubernetesOptions) (*Kubernetes, error)

NewKubernetes connects using in-cluster config (when running in a Pod) or the kubeconfig file otherwise.

func (*Kubernetes) Launch added in v0.7.0

func (k *Kubernetes) Launch(ctx context.Context, spec LaunchSpec) (string, error)

func (*Kubernetes) Logs added in v0.7.0

func (k *Kubernetes) Logs(ctx context.Context, id string, tail int) (string, error)

func (*Kubernetes) Ping added in v0.7.0

func (k *Kubernetes) Ping(ctx context.Context) error

Ping verifies the API server is reachable.

func (*Kubernetes) Remove added in v0.7.0

func (k *Kubernetes) Remove(ctx context.Context, id string) error

func (*Kubernetes) Status added in v0.7.0

func (k *Kubernetes) Status(ctx context.Context, id string) (Status, error)

func (*Kubernetes) Stop added in v0.7.0

func (k *Kubernetes) Stop(ctx context.Context, id string, timeout time.Duration) error

type KubernetesOptions added in v0.7.0

type KubernetesOptions struct {
	Kubeconfig   string // path; empty → in-cluster, else default loading rules
	Namespace    string // default "default"
	Image        string // runner image (must be pullable by the cluster)
	PVCSize      string // per-job volume size, default "1Gi"
	StorageClass string // optional; empty → cluster default
}

KubernetesOptions configures the backend.

type LaunchSpec

type LaunchSpec struct {
	JobID string // used to name the container and its data volume
	Name  string // human-readable workflow name
	Image string // runner image, e.g. mailer-runner:dev

	// WorkflowDoc is the raw workflow file content. The backend makes it
	// available to the container and points WORKFLOW at it.
	WorkflowDoc []byte

	// Env is the resolved runtime environment (including any secrets).
	// The backend passes it to the container; it is never persisted.
	Env map[string]string

	// ControlPort is the container port the jobagent listens on. The
	// backend publishes it and reports the reachable host port in Status.
	ControlPort int

	// RestoreSavepoint, if set, names a savepoint the runner seeds from
	// before starting (RESTORE_SAVEPOINT). Empty means a fresh start.
	RestoreSavepoint string
}

LaunchSpec is everything the backend needs to start one job container.

type Phase

type Phase string

Phase is the coarse container lifecycle as the backend sees it — distinct from the richer job lifecycle the controller tracks.

const (
	// PhaseRunning: the container process is up.
	PhaseRunning Phase = "running"
	// PhaseExited: the container process has stopped (see ExitCode).
	PhaseExited Phase = "exited"
	// PhaseGone: no such container (never launched, or removed).
	PhaseGone Phase = "gone"
)

type Status

type Status struct {
	Phase    Phase
	ExitCode int    // meaningful when Phase == PhaseExited
	HostPort int    // reachable host port mapped to ControlPort, 0 if none
	Address  string // host:port for the control surface, empty if unreachable
}

Status is a point-in-time container status.

Jump to

Keyboard shortcuts

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