Documentation
¶
Overview ¶
Package runtime provides the RuntimeDriver interface and its docker implementation.
Package runtime provides the RuntimeDriver interface and implementations for managing containerized workloads on behalf of AgentPaaS.
Index ¶
- Constants
- Variables
- func ContainerName(role, id string) string
- func HasSecretLeak(output string) bool
- func IsOwned(labels map[string]string) bool
- func IsUnrelatedContainer(info ContainerInfo) bool
- func Labels(resourceType, runID string) map[string]string
- func NetworkName(role, id string) string
- func ResourceTypeFromLabels(labels map[string]string) string
- func RunIDFromLabels(labels map[string]string) string
- func SanitizeDebugOutput(output string) string
- func SanitizeDockerInspect(inspectJSON string) string
- type ContainerID
- type ContainerInfo
- type ContainerNetworkInfo
- type ContainerSpec
- type ContainerStats
- type ContainerStatus
- type DockerRuntime
- func (d *DockerRuntime) Create(ctx context.Context, spec ContainerSpec) (ContainerID, error)
- func (d *DockerRuntime) CreateNetwork(ctx context.Context, spec NetworkSpec) (NetworkID, error)
- func (d *DockerRuntime) Exec(ctx context.Context, id ContainerID, cmd []string) (string, string, int, error)
- func (d *DockerRuntime) ExecWithStdin(ctx context.Context, id ContainerID, cmd []string, stdinData []byte) (string, string, int, error)
- func (d *DockerRuntime) InspectContainerIP(ctx context.Context, id ContainerID, networkID string) (string, error)
- func (d *DockerRuntime) InspectContainerNetworks(ctx context.Context, id ContainerID) ([]ContainerNetworkInfo, error)
- func (d *DockerRuntime) InspectNetwork(ctx context.Context, id NetworkID) (NetworkInfo, error)
- func (d *DockerRuntime) ListContainers(ctx context.Context, labelFilters ...string) ([]ContainerInfo, error)
- func (d *DockerRuntime) ListNetworks(ctx context.Context, labelFilters ...string) ([]NetworkInfo, error)
- func (d *DockerRuntime) Logs(ctx context.Context, id ContainerID, opts LogOptions) (io.ReadCloser, error)
- func (d *DockerRuntime) Remove(ctx context.Context, id ContainerID, force bool) error
- func (d *DockerRuntime) RemoveNetwork(ctx context.Context, id NetworkID) error
- func (d *DockerRuntime) Start(ctx context.Context, id ContainerID) error
- func (d *DockerRuntime) Stats(ctx context.Context, id ContainerID) (ContainerStats, error)
- func (d *DockerRuntime) Status(ctx context.Context, id ContainerID) (ContainerStatus, error)
- func (d *DockerRuntime) Stop(ctx context.Context, id ContainerID, timeout *time.Duration) error
- type LogOptions
- type MCPContainerInfo
- type NetworkID
- type NetworkInfo
- type NetworkSpec
- type RuntimeDriver
Constants ¶
const ( // LabelManagedBy identifies resources managed by AgentPaaS. LabelManagedBy = "agentpaas.managed-by" // LabelResourceType identifies the type of AgentPaaS resource // (agent, gateway, net-internal, net-egress). LabelResourceType = "agentpaas.resource-type" // LabelRunID identifies the agent run that owns this resource. LabelRunID = "agentpaas.run-id" // LabelMCPServerID identifies which MCP server a container represents. LabelMCPServerID = "agentpaas.mcp-server-id" )
Label keys for AgentPaaS-owned Docker resources. These labels enable reconciliation to discover only AgentPaaS-owned containers and networks.
const ( ResourceTypeAgent = "agent" ResourceTypeGateway = "gateway" ResourceTypeMCP = "mcp" ResourceTypeNetInternal = "net-internal" ResourceTypeNetEgress = "net-egress" )
Resource type constants for LabelResourceType.
const GatewayImage = "ghcr.io/agentgateway/agentgateway:v1.3.0"
GatewayImage is the official agentgateway Docker image for the egress gateway. Pinned to v1.3.0 (matches third_party/agentgateway/VERSION).
const ManagedByValue = "agentpaas"
ManagedByValue is the value of LabelManagedBy for all AgentPaaS-managed resources.
Variables ¶
var ( // ErrInvalidSpec is returned when the ContainerSpec fails validation // (e.g., empty Image or invalid network ID). ErrInvalidSpec = errors.New("invalid container spec") // ErrContainerNotFound is returned when the target container does not // exist or has been removed. ErrContainerNotFound = errors.New("container not found") // ErrNetworkNotFound is returned when the target network does not // exist or has been removed. ErrNetworkNotFound = errors.New("network not found") // ErrStatsNotReady is returned when Docker container stats lack a prior // CPU sample (precpu_stats are zero on the first stats read). ErrStatsNotReady = errors.New("container stats not ready") )
Sentinel errors returned by RuntimeDriver implementations. Drivers should wrap these with additional context using fmt.Errorf and %w.
var ContainerPrefixes = map[string]string{
"agent": "agentpaas-agent-",
"gateway": "agentpaas-gateway-",
"mcp": "agentpaas-mcp-",
}
ContainerPrefixes map role types to their container name prefixes.
var ErrNotOwned = errors.New("resource is not owned by AgentPaaS")
ErrNotOwned is returned when an operation targets a resource that is not owned by AgentPaaS.
var NetworkPrefixes = map[string]string{
"internal": "agentpaas-net-internal-",
"egress": "agentpaas-net-egress-",
}
NetworkPrefixes map network role types to their network name prefixes.
Functions ¶
func ContainerName ¶
ContainerName returns a deterministic container name for the given role and run ID. For known roles (agent, gateway) the format is "agentpaas-<role>-<id>", e.g. "agentpaas-agent-run-abc123". For unknown roles (which may contain hyphens), underscore separates role from ID to prevent ambiguity: "agentpaas-<role>_<id>".
func HasSecretLeak ¶
HasSecretLeak returns true if the given output contains any pattern that looks like an unredacted secret. Used in tests to verify debug output is safe.
func IsOwned ¶
IsOwned returns true if the given Docker labels indicate the resource is owned by AgentPaaS. A resource is considered owned if it has a label "agentpaas.managed-by" with value "agentpaas".
func IsUnrelatedContainer ¶
func IsUnrelatedContainer(info ContainerInfo) bool
IsUnrelatedContainer returns true if the given container is NOT owned by AgentPaaS. Used to verify reconciliation never touches unrelated resources.
func Labels ¶
Labels returns a deterministic set of AgentPaaS ownership labels for a resource of the given type and run ID. The returned map includes:
- agentpaas.managed-by → "agentpaas"
- agentpaas.resource-type → <resourceType>
- agentpaas.run-id → <runID>
func NetworkName ¶
NetworkName returns a deterministic Docker network name for the given role and run ID. For known roles (internal, egress) the format is "agentpaas-net-<role>-<id>", e.g. "agentpaas-net-internal-run-abc123". For unknown roles, underscore separates role from ID: "agentpaas-net-<role>_<id>".
func ResourceTypeFromLabels ¶
ResourceTypeFromLabels extracts the resource type from AgentPaaS labels. Returns empty string if the label is not present.
func RunIDFromLabels ¶
RunIDFromLabels extracts the run ID from AgentPaaS resource labels. Returns empty string if the label is not present.
func SanitizeDebugOutput ¶
SanitizeDebugOutput scans the given debug output (Docker inspect JSON, logs, config dumps) for secret patterns and replaces matching values with "[REDACTED]".
This is used to ensure that Docker inspect, runtime logs, and network config dumps never expose raw secret values.
func SanitizeDockerInspect ¶
SanitizeDockerInspect redacts secrets from a Docker container inspect JSON string. It specifically targets the Environment variables and Labels sections which are the most common sources of secret leakage.
Types ¶
type ContainerID ¶
type ContainerID string
ContainerID is a unique identifier for a container managed by the runtime driver. Implementations use Docker container IDs, which are hex strings.
func ReconcileAfterCrash ¶
func ReconcileAfterCrash(ctx context.Context, driver RuntimeDriver) ([]ContainerID, error)
ReconcileAfterCrash performs startup reconciliation after a daemon crash. It discovers all AgentPaaS-owned containers via the managed-by label and removes any agent container whose gateway is absent (stopped or removed) and any MCP sidecar whose owning agent is absent.
The reconciliation respects the following rules:
- Only resources with agentpaas.managed-by=agentpaas are considered
- Only agent and MCP containers are removed
- An agent container is removed only if NO running gateway container (agentpaas.resource-type=gateway) exists for the same run ID
- An MCP container is removed only if NO running agent container (agentpaas.resource-type=agent) exists for the same run ID
- Unrelated Docker resources are never touched
ReconcileAfterCrash returns a summary of actions taken (containers removed).
type ContainerInfo ¶
type ContainerInfo struct {
// ID is the Docker container ID.
ID string
// Name is the Docker container name.
Name string
// Status is the current lifecycle status of the container.
Status ContainerStatus
// Labels are the Docker labels attached to the container.
Labels map[string]string
// RunID is the AgentPaaS run identifier extracted from labels.
RunID string
// ResourceType is the AgentPaaS resource type (agent, gateway, etc.).
ResourceType string
}
ContainerInfo contains summary information about a Docker container, typically returned by ListContainers for reconciliation and discovery.
type ContainerNetworkInfo ¶
type ContainerNetworkInfo struct {
// ID is the Docker network ID.
ID string
// Name is the Docker network name.
Name string
// IPAddress is the container's IP on this network.
IPAddress string
// Aliases are network-scoped DNS aliases for the container.
Aliases []string
}
ContainerNetworkInfo describes a single network attachment on a container.
type ContainerSpec ¶
type ContainerSpec struct {
// Image is the container image reference (e.g., "nginx:latest").
Image string
// Command is the entrypoint command override for the container.
Command []string
// Env is a list of environment variable assignments (KEY=VALUE format).
Env []string
// Labels are Docker labels to apply to the container for ownership
// tracking and reconciliation.
Labels map[string]string
// NetworkIDs is a list of Docker network IDs to attach the container to.
// Multiple network IDs enable dual-homing (e.g., gateway sidecar on
// both internal and egress networks). For single-network containers
// (e.g., agent), pass exactly one ID.
NetworkIDs []string
// User is the container-internal user to run as. When empty, the runtime
// enforces a non-root default (uid 64000). Callers may override for
// gateway containers that need privileged operations.
User string
// MemoryLimitBytes is the maximum memory the container may consume, in
// bytes. 0 means no limit (gateway containers may need more). Populated
// from agent.yaml by the upper orchestrator.
MemoryLimitBytes int64
// NanoCPUs is the CPU quota in nanoseconds of CPU time per period.
// 0 means no limit. Populated from agent.yaml by the upper orchestrator.
NanoCPUs int64
// Binds is a list of host-path:container-path bind mounts. Each entry
// follows the Docker --volume format: "host_path:container_path" or
// "host_path:container_path:ro" for read-only. Used to mount audit
// volumes and other host directories into the container.
Binds []string
// CapAdd lists Linux capabilities to add after the runtime's CapDrop ALL
// baseline. Used by the orchestrator for agent egress firewall (NET_ADMIN).
CapAdd []string
}
ContainerSpec defines the parameters for creating a container. Hardening flags (User, ReadonlyRootfs, tmpfs, CapDrop, no-new-privileges, PidsLimit, IPv6 disable) are applied automatically by DockerRuntime.Create based on P1 security policy — callers do not need to set them in the spec. MemoryLimitBytes and NanoCPUs are populated from agent.yaml configuration by the upper orchestrator layer.
type ContainerStats ¶
type ContainerStats struct {
// CPUPercent is the CPU usage percentage (0.0-100.0).
CPUPercent float64
// MemoryMB is the current memory usage in megabytes.
MemoryMB float64
// PIDs is the number of processes running inside the container.
PIDs int
}
ContainerStats represents a snapshot of container resource usage.
type ContainerStatus ¶
type ContainerStatus int
ContainerStatus represents the current lifecycle state of a container.
const ( // ContainerStatusUnknown indicates the container state could not be // determined or is not a known status value. ContainerStatusUnknown ContainerStatus = iota // ContainerStatusRunning indicates the container is actively running. ContainerStatusRunning // ContainerStatusStopped indicates the container has exited or was // stopped. ContainerStatusStopped // ContainerStatusPaused indicates the container process is suspended. ContainerStatusPaused // ContainerStatusRemoved indicates the container has been removed and // no longer exists. ContainerStatusRemoved )
func (ContainerStatus) String ¶
func (s ContainerStatus) String() string
String returns a human-readable representation of the container status.
type DockerRuntime ¶
type DockerRuntime struct {
// contains filtered or unexported fields
}
DockerRuntime is a Docker Engine implementation of RuntimeDriver that delegates method calls to the Docker Engine API.
func NewDockerRuntime ¶
func NewDockerRuntime() (*DockerRuntime, error)
NewDockerRuntime creates a new DockerRuntime backed by the Docker Engine API. It discovers the Docker daemon through environment variables (DOCKER_HOST, DOCKER_API_VERSION) or defaults to the local socket.
func NewDockerRuntimeWithDriver ¶
func NewDockerRuntimeWithDriver(driver RuntimeDriver) *DockerRuntime
NewDockerRuntimeWithDriver returns a DockerRuntime that delegates all operations to driver. Intended for unit tests that need to capture ContainerSpec without a live Docker daemon.
func (*DockerRuntime) Create ¶
func (d *DockerRuntime) Create(ctx context.Context, spec ContainerSpec) (ContainerID, error)
Create provisions a new Docker container from the given spec and returns its ContainerID. The container is created but not started; call Start to begin execution.
func (*DockerRuntime) CreateNetwork ¶
func (d *DockerRuntime) CreateNetwork(ctx context.Context, spec NetworkSpec) (NetworkID, error)
CreateNetwork provisions a new Docker network from the given spec and returns its NetworkID.
func (*DockerRuntime) Exec ¶
func (d *DockerRuntime) Exec(ctx context.Context, id ContainerID, cmd []string) (string, string, int, error)
Exec runs a command inside a running container and returns stdout, stderr, and the process exit code.
func (*DockerRuntime) ExecWithStdin ¶
func (d *DockerRuntime) ExecWithStdin(ctx context.Context, id ContainerID, cmd []string, stdinData []byte) (string, string, int, error)
ExecWithStdin runs a command inside a running container with stdin data fed through the Docker multiplexed attach protocol. stdinData is written to the container process's stdin and then EOF is signaled. The credential value in stdinData NEVER appears in process args or logs.
func (*DockerRuntime) InspectContainerIP ¶
func (d *DockerRuntime) InspectContainerIP(ctx context.Context, id ContainerID, networkID string) (string, error)
InspectContainerIP returns the IP address of a container on a specific network. Returns empty string if the container is not attached to the network.
func (*DockerRuntime) InspectContainerNetworks ¶
func (d *DockerRuntime) InspectContainerNetworks(ctx context.Context, id ContainerID) ([]ContainerNetworkInfo, error)
InspectContainerNetworks returns the list of networks a container is attached to. Used for topology assertions.
func (*DockerRuntime) InspectNetwork ¶
func (d *DockerRuntime) InspectNetwork(ctx context.Context, id NetworkID) (NetworkInfo, error)
InspectNetwork returns detailed information about a Docker network.
func (*DockerRuntime) ListContainers ¶
func (d *DockerRuntime) ListContainers(ctx context.Context, labelFilters ...string) ([]ContainerInfo, error)
ListContainers returns all Docker containers matching the given label filters. Each filter should be in "key=value" format. The results include each container's ID, name, status, and Docker labels.
func (*DockerRuntime) ListNetworks ¶
func (d *DockerRuntime) ListNetworks(ctx context.Context, labelFilters ...string) ([]NetworkInfo, error)
ListNetworks returns all Docker networks matching the given label filters. Each filter should be in "key=value" format.
func (*DockerRuntime) Logs ¶
func (d *DockerRuntime) Logs(ctx context.Context, id ContainerID, opts LogOptions) (io.ReadCloser, error)
Logs returns a reader for a Docker container's stdout/stderr output.
func (*DockerRuntime) Remove ¶
func (d *DockerRuntime) Remove(ctx context.Context, id ContainerID, force bool) error
Remove deletes a container. If force is true, the container is killed first if running.
func (*DockerRuntime) RemoveNetwork ¶
func (d *DockerRuntime) RemoveNetwork(ctx context.Context, id NetworkID) error
RemoveNetwork deletes a Docker network.
func (*DockerRuntime) Start ¶
func (d *DockerRuntime) Start(ctx context.Context, id ContainerID) error
Start begins execution of a previously created container.
func (*DockerRuntime) Stats ¶
func (d *DockerRuntime) Stats(ctx context.Context, id ContainerID) (ContainerStats, error)
Stats returns resource usage for a Docker container.
func (*DockerRuntime) Status ¶
func (d *DockerRuntime) Status(ctx context.Context, id ContainerID) (ContainerStatus, error)
Status reports the current Docker container lifecycle status.
func (*DockerRuntime) Stop ¶
func (d *DockerRuntime) Stop(ctx context.Context, id ContainerID, timeout *time.Duration) error
Stop halts a running container. If timeout is non-nil, the runtime waits at most that long for graceful shutdown before force-killing.
type LogOptions ¶
type LogOptions struct {
// Tail is the number of recent log lines to return. 0 means all.
Tail int
// Follow streams log output as the container produces it.
Follow bool
// Since returns logs only after this timestamp.
Since *time.Time
}
LogOptions controls how container logs are fetched.
type MCPContainerInfo ¶
type MCPContainerInfo struct {
ContainerID ContainerID
ServerID string
RunID string
}
MCPContainerInfo describes an MCP sidecar discovered during reconciliation.
func ReconcileMCPServers ¶
func ReconcileMCPServers(ctx context.Context, driver RuntimeDriver) ([]MCPContainerInfo, error)
ReconcileMCPServers discovers MCP-labeled containers and returns their server IDs and run IDs for lifecycle reconciliation after daemon restart.
type NetworkID ¶
type NetworkID string
NetworkID is a unique identifier for a Docker network managed by the runtime driver. Implementations use Docker network IDs (hex strings or short IDs).
type NetworkInfo ¶
type NetworkInfo struct {
// ID is the Docker network ID.
ID string
// Name is the Docker network name.
Name string
// Internal indicates whether the network is an internal bridge (no
// external access).
Internal bool
// Labels are the Docker labels attached to the network.
Labels map[string]string
}
NetworkInfo contains details about a Docker network, typically from an inspect operation.
type NetworkSpec ¶
type NetworkSpec struct {
// Name is the Docker network name.
Name string
// Internal, when true, creates an internal bridge network that has no
// external access. The agent's internal bridge uses this to prevent
// direct egress.
Internal bool
// Labels are Docker labels to apply to the network for ownership
// tracking and reconciliation.
Labels map[string]string
}
NetworkSpec defines the parameters for creating a Docker network.
type RuntimeDriver ¶
type RuntimeDriver interface {
// Create provisions a new container from the given spec and returns
// its ContainerID. The container is created but not started; call
// Start to begin execution.
Create(ctx context.Context, spec ContainerSpec) (ContainerID, error)
// Start begins execution of a previously created container.
Start(ctx context.Context, id ContainerID) error
// Stop halts a running container. If timeout is non-nil, the runtime
// waits at most that long for graceful shutdown before force-killing.
Stop(ctx context.Context, id ContainerID, timeout *time.Duration) error
// Remove deletes a container. If force is true, the container is
// killed first if running.
Remove(ctx context.Context, id ContainerID, force bool) error
// Status returns the current lifecycle status of the container.
Status(ctx context.Context, id ContainerID) (ContainerStatus, error)
// Stats returns a snapshot of the container's resource usage.
Stats(ctx context.Context, id ContainerID) (ContainerStats, error)
// Logs returns a reader for the container's stdout/stderr output.
// The caller MUST close the returned reader when done.
Logs(ctx context.Context, id ContainerID, opts LogOptions) (io.ReadCloser, error)
// Exec runs a command inside a running container and returns stdout, stderr,
// and exit code. Used by the daemon to invoke the harness /invoke endpoint
// from inside the container (the harness binds to 127.0.0.1 only).
Exec(ctx context.Context, id ContainerID, cmd []string) (stdout, stderr string, exitCode int, err error)
// CreateNetwork provisions a new Docker network from the given spec and
// returns its NetworkID. An internal:true network restricts external
// access (used for the per-agent internal bridge).
CreateNetwork(ctx context.Context, spec NetworkSpec) (NetworkID, error)
// RemoveNetwork deletes a Docker network. Returns ErrNetworkNotFound
// if the network does not exist.
RemoveNetwork(ctx context.Context, id NetworkID) error
// InspectNetwork returns detailed information about a Docker network,
// including its Internal flag and labels.
InspectNetwork(ctx context.Context, id NetworkID) (NetworkInfo, error)
// InspectContainerNetworks returns the list of networks a container is
// attached to, with network names and IDs. Used for topology assertions.
InspectContainerNetworks(ctx context.Context, id ContainerID) ([]ContainerNetworkInfo, error)
// InspectContainerIP returns the IP address of a container on a specific
// network. Returns empty string if the container is not attached to the
// network.
InspectContainerIP(ctx context.Context, id ContainerID, networkID string) (string, error)
// ListContainers returns all containers matching the given label filters.
// Each filter is a "key=value" pair. Results are returned as a slice of
// ContainerInfo structs containing ID, name, status, and labels.
ListContainers(ctx context.Context, labelFilters ...string) ([]ContainerInfo, error)
// ListNetworks returns all Docker networks matching the given label filters.
// Each filter is a "key=value" pair.
ListNetworks(ctx context.Context, labelFilters ...string) ([]NetworkInfo, error)
}
RuntimeDriver is the abstraction over container runtimes (Docker, etc.) that AgentPaaS uses to manage agent and gateway containers, Docker networks, and container lifecycle operations.