runtime

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package runtime is the function worker port (ADR-0011): create, start, stop, observe, and harvest logs from a worker instance (one replica of a function).

The port is driver-independent and imports no container library. Two drivers implement it: a cross-platform process driver (internal/runtime/process — the dev/e2e/CI driver) and a Linux-only containerd/runc driver (internal/runtime/containerd). The controller maps Function/Revision/RuntimeClass (ADR-0003) into a WorkerSpec; this port does not read resources, and functions never call it (internal-plane only).

Index

Constants

This section is empty.

Variables

View Source
var ErrImageUnavailable = errors.New("image is not available")

ErrImageUnavailable is wrapped, with kind fault.NotFound, by a driver's Create when the worker image is absent: a ref the driver may not pull, or a pull that finds no such image (ADR-0149 Decision 3).

Functions

This section is empty.

Types

type Exit added in v0.4.0

type Exit struct {
	Cause  ExitCause
	Code   int
	Signal int
}

Exit is how an instance ended; zero until it ends. Code 3 is a shim that cannot load its handler (ADR-0037).

type ExitCause added in v0.4.0

type ExitCause string

ExitCause is how a worker instance ended (ADR-0160).

const (
	// ExitUnknown means the instance has not ended, or its end could not be read.
	ExitUnknown ExitCause = ""
	// ExitByStop means Stop ended the instance, or ran after it ended.
	ExitByStop ExitCause = "stop"
	// ExitByCode means the process exited on its own with Exit.Code.
	ExitByCode ExitCause = "code"
	// ExitBySignal means a signal Stop did not send, Exit.Signal, ended the process.
	ExitBySignal ExitCause = "signal"
)

type Instance

type Instance struct {
	ID        InstanceID
	Namespace v1alpha1.NamespaceName
	Name      v1alpha1.ObjectName
	OwnerKind v1alpha1.Kind       // as created (ADR-0152)
	Revision  v1alpha1.ObjectName // as created (ADR-0143)
	Replica   int
	PID       int
	State     State
	IP        string // worker endpoint host: netns IP (containerd) or 127.0.0.1 (process driver, ADR-0030)
	Port      int    // worker HTTP port: the shim's listening port (ADR-0030); 0 until resolved
	CreatedAt time.Time
	StartedAt time.Time // the last successful Start; zero before the first (ADR-0183)
	Listened  bool      // wrote its port to FUNCD_PORTFILE since its last Start; stays true after it ends (ADR-0160)
	Exit      Exit
}

Instance is the observed state of one worker.

type InstanceID

type InstanceID string

InstanceID identifies one worker instance (a function replica).

func NewInstanceID

func NewInstanceID(ns v1alpha1.NamespaceName, name, revision v1alpha1.ObjectName, replica int) InstanceID

NewInstanceID builds the canonical id "<ns>/<name>/r<replica>", or "<ns>/<name>/<revision>/r<replica>" for a worker of a Revision, so two revisions of one replica can run at once (ADR-0143).

type Limits

type Limits struct {
	MemoryBytes int64
	CPUs        float64
}

Limits bounds a worker's resources (0 = unlimited).

type LogCaptureFunc

type LogCaptureFunc func(spec WorkerSpec, r io.ReadCloser)

LogCaptureFunc receives one instance's structured telemetry channel (ADR-0081 Path B): r yields the NDJSON records the runtime shim writes to its log channel. The composition root supplies it (wiring a funclog Pump); the implementation owns reading and closing r. spec identifies the instance whose logs these are.

type LogCapturer

type LogCapturer interface {
	// SetLogCapture installs (or clears, with nil) the per-instance log-channel hook.
	SetLogCapture(LogCaptureFunc)
}

LogCapturer is the OPTIONAL capability a runtime driver implements to deliver per-instance structured logs (ADR-0081). funcd sets the hook after building the funclog sink; a driver that does not implement it simply captures no Path B telemetry. Keeping it off the Runtime port keeps the port free of the observability concern (the port imports no observability package).

type Mount

type Mount struct {
	Source   string // host path
	Target   string // container path
	ReadOnly bool
}

Mount is a host→container bind mount (ADR-0032). Container drivers (containerd) honor it; the process driver ignores it (no container filesystem). Used to deliver the function artifact read-only into the curated-image worker.

type OutputCaptureFunc added in v0.6.0

type OutputCaptureFunc func(spec WorkerSpec, out *workerpipe.Output)

OutputCaptureFunc receives one worker run's raw stdout and stderr (ADR-0168 Path A) before the worker writes. For a worker funcd stores it takes both Readers and reads them on its own goroutines, else none; it must not block.

type OutputCapturer added in v0.6.0

type OutputCapturer interface {
	SetOutputCapture(OutputCaptureFunc)
}

OutputCapturer is the capability both drivers implement to hand funcd each run's raw output (ADR-0168). nil clears the hook; with no hook a run keeps only its tail.

type Runtime

type Runtime interface {
	// Create makes a worker from spec without starting it. An empty spec.OwnerKind is fault.Invalid; an ID held by an
	// instance of another OwnerKind, even when that instance has exited, or on containerd by a leftover container
	// labelled with another kind, is fault.Conflict (ADR-0152).
	Create(ctx context.Context, spec WorkerSpec) (Instance, error)
	// Start runs the worker's process.
	Start(ctx context.Context, id InstanceID) error
	// Stop terminates the process (SIGTERM, then SIGKILL); it is idempotent.
	Stop(ctx context.Context, id InstanceID) error
	// Status returns the observed instance; fault.NotFound if unknown.
	Status(ctx context.Context, id InstanceID) (Instance, error)
	// Logs returns the current or last run's tail of stdout and stderr (ADR-0168), empty before the first Start, with an
	// ended run's last line: stdout's lines, then stderr's.
	Logs(ctx context.Context, id InstanceID) (io.ReadCloser, error)
	// Exec runs a command in the instance's context (best-effort in V1).
	Exec(ctx context.Context, id InstanceID, cmd []string) error
	// List returns the instances in a namespace.
	List(ctx context.Context, ns v1alpha1.NamespaceName) ([]Instance, error)
	// Remove forgets an instance after Stop has released it, with its per-instance files: Status then reports
	// fault.NotFound and List omits it. An instance Stop has not released — running, created, or exited on its own —
	// is fault.Conflict; an unknown one is a no-op (ADR-0143).
	Remove(ctx context.Context, id InstanceID) error
	// Close releases the driver's resources.
	Close() error
}

Runtime is the worker port: create/start/stop/observe one function replica. Errors are api/fault; every method is ctx-first; the port imports no container library. Implementations are internal-plane only — functions never call them.

type State

type State string

State is the lifecycle state of a worker instance.

const (
	// StateCreated means the worker exists but its process is not started.
	StateCreated State = "created"
	// StateRunning means the worker process is running.
	StateRunning State = "running"
	// StateStopped means the worker process exited or was stopped.
	StateStopped State = "stopped"
	// StateFailed means the worker process exited abnormally.
	StateFailed State = "failed"
)

func (State) Terminal added in v0.1.3

func (s State) Terminal() bool

Terminal reports whether an instance in this state has exited (Stopped or Failed).

type WorkerSpec

type WorkerSpec struct {
	Namespace v1alpha1.NamespaceName
	Name      v1alpha1.ObjectName
	OwnerKind v1alpha1.Kind       // the kind whose reconciler created the worker; required (ADR-0152)
	Revision  v1alpha1.ObjectName // the Revision this worker runs (ADR-0143); "" outside the Function lifecycle
	Replica   int
	Members   int               // the members a pool worker hosts (ADR-0046); 0 for a solo worker
	Image     string            // runtime image (containerd driver)
	Command   []string          // launch command / container args
	Env       map[string]string // typed-flat; no any
	Mounts    []Mount           // host→container bind mounts (container drivers; ADR-0032)
	Limits    Limits
}

WorkerSpec is the driver-independent request to run one function replica. The controller assembles it from Function/Revision/RuntimeClass (ADR-0003).

Directories

Path Synopsis
Package containerd implements the runtime.Runtime port over containerd + runc on Linux (ADR-0011): pull a curated runtime image, run it as a runc container with conservative OCI defaults (no added caps, no_new_privileges, optional limits), attach a per-worker netns with default-deny lateral (go-cni bridge + firewall), and read each task's stdout and stderr through FIFOs (ADR-0168).
Package containerd implements the runtime.Runtime port over containerd + runc on Linux (ADR-0011): pull a curated runtime image, run it as a runc container with conservative OCI defaults (no added caps, no_new_privileges, optional limits), attach a per-worker netns with default-deny lateral (go-cni bridge + firewall), and read each task's stdout and stderr through FIFOs (ADR-0168).
Package ctrmanager brings up the container runtime funcd will drive (ADR-0054).
Package ctrmanager brings up the container runtime funcd will drive (ADR-0054).
Package embedimg carries funcd's curated runtime images baked into the binary (ADR-0054).
Package embedimg carries funcd's curated runtime images baked into the binary (ADR-0054).
Package process implements the runtime.Runtime port by supervising functions as plain OS child processes (ADR-0011).
Package process implements the runtime.Runtime port by supervising functions as plain OS child processes (ADR-0011).
Package procreg is the saved worker registry (ADR-0167): the process driver and the dev catalog engine record every process they start, so the next open reaps what a crashed run left behind.
Package procreg is the saved worker registry (ADR-0167): the process driver and the dev catalog engine record every process they start, so the next open reaps what a crashed run left behind.
Package provision is funcd's TEMPORARY container-runtime self-provisioning (ADR-0056): `funcd install` downloads the pinned, SHA-256-verified static binaries the managed-containerd path needs — crun (the OCI runtime the runc-v2 shim execs), containerd (+ its runc-v2 shim + ctr), and the CNI plugins — laying crun/containerd into funcd's bin dir and the CNI plugins into /opt/cni/bin.
Package provision is funcd's TEMPORARY container-runtime self-provisioning (ADR-0056): `funcd install` downloads the pinned, SHA-256-verified static binaries the managed-containerd path needs — crun (the OCI runtime the runc-v2 shim execs), containerd (+ its runc-v2 shim + ctr), and the CNI plugins — laying crun/containerd into funcd's bin dir and the CNI plugins into /opt/cni/bin.
Package runtimecontract is the shared conformance suite for the runtime.Runtime port (ADR-0011).
Package runtimecontract is the shared conformance suite for the runtime.Runtime port (ADR-0011).
Package workerpipe (ADR-0168): funcd's end of a worker's raw stdout and stderr, read as written into a bounded queue for funclog and a bounded tail.
Package workerpipe (ADR-0168): funcd's end of a worker's raw stdout and stderr, read as written into a bounded queue for funclog and a bounded tail.

Jump to

Keyboard shortcuts

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