runtime

package
v0.1.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LabelManaged   = "gridctl.managed"
	LabelStack     = "gridctl.stack"
	LabelMCPServer = "gridctl.mcp-server"
	LabelResource  = "gridctl.resource"
	LabelAgent     = "gridctl.agent"
)

Label constants for identifying gridctl-managed resources.

Variables

View Source
var (
	ErrWorkloadNotFound   = errors.New("workload not found")
	ErrNetworkNotFound    = errors.New("network not found")
	ErrRuntimeUnavailable = errors.New("runtime unavailable")
	ErrNotSupported       = errors.New("operation not supported by this runtime")
	ErrInvalidConfig      = errors.New("invalid workload configuration")
)

Sentinel errors for runtime operations.

View Source
var GetContainerHostPortFunc func(ctx context.Context, cli dockerclient.DockerClient, containerID string, containerPort int) (int, error)

GetContainerHostPort is a backward-compatible helper. This is set by the docker package at init time.

View Source
var NewFunc func() (*Orchestrator, error)

NewFunc is the type for a factory function that creates an Orchestrator. This is set by the docker package at init time to avoid import cycles.

View Source
var NewWithInfoFunc func(info *RuntimeInfo) (*Orchestrator, error)

NewWithInfoFunc is the type for a factory function that creates an Orchestrator with runtime info (for explicit runtime selection or auto-detection).

Functions

func ApplyVolumeLabels

func ApplyVolumeLabels(volumes []string, info *RuntimeInfo) []string

ApplyVolumeLabels appends :Z to volume mounts when Podman + SELinux.

func GetContainerHostPort

func GetContainerHostPort(ctx context.Context, cli dockerclient.DockerClient, containerID string, containerPort int) (int, error)

GetContainerHostPort returns the host port for a container.

Types

type AgentResult

type AgentResult struct {
	Name       string                // Logical name
	WorkloadID WorkloadID            // Runtime ID
	Uses       []config.ToolSelector // MCP servers this agent depends on
}

AgentResult is the runtime-agnostic result for an agent.

type BuildOptions

type BuildOptions struct {
	SourceType string            // "git" or "local"
	URL        string            // Git URL
	Ref        string            // Git ref/branch
	Path       string            // Local path
	Dockerfile string            // Dockerfile path within context
	Tag        string            // Image tag
	BuildArgs  map[string]string // Build arguments
	NoCache    bool              // Force rebuild
	Logger     *slog.Logger      // Logger for build operations (optional)
}

BuildOptions for the Builder interface.

type BuildResult

type BuildResult struct {
	ImageTag string // Image tag
	Cached   bool   // Whether build was cached
}

BuildResult from a build operation.

type Builder

type Builder interface {
	Build(ctx context.Context, opts BuildOptions) (*BuildResult, error)
}

Builder handles image/artifact building. This is kept separate from WorkloadRuntime as image building is a distinct concern.

type DependencyGraph

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

DependencyGraph represents a directed acyclic graph of dependencies. It provides topological sorting to determine correct startup order.

func NewDependencyGraph

func NewDependencyGraph() *DependencyGraph

NewDependencyGraph creates an empty dependency graph.

func (*DependencyGraph) AddEdge

func (g *DependencyGraph) AddEdge(from, to string)

AddEdge adds a dependency edge: from depends on to. This means 'to' must be started before 'from'.

func (*DependencyGraph) AddNode

func (g *DependencyGraph) AddNode(name string)

AddNode adds a node to the graph.

func (*DependencyGraph) GetDependencies

func (g *DependencyGraph) GetDependencies(name string) []string

GetDependencies returns the direct dependencies of a node.

func (*DependencyGraph) HasNode

func (g *DependencyGraph) HasNode(name string) bool

HasNode returns true if the node exists in the graph.

func (*DependencyGraph) Sort

func (g *DependencyGraph) Sort() ([]string, error)

Sort returns nodes in topological order (dependencies first). Nodes without dependencies come first, dependent nodes come later. Uses Kahn's algorithm: O(V + E) time complexity.

type DetectOptions

type DetectOptions struct {
	Explicit string // Value from --runtime flag or GRIDCTL_RUNTIME env var
}

DetectOptions configures runtime detection.

type DockerClientGetter

type DockerClientGetter interface {
	Client() dockerclient.DockerClient
}

DockerClientGetter is an interface for types that can return a Docker client.

type LoggerSetter

type LoggerSetter interface {
	SetLogger(logger *slog.Logger)
}

LoggerSetter is an interface for types that accept a logger.

type MCPServerResult

type MCPServerResult struct {
	Name       string     // Logical name
	WorkloadID WorkloadID // Runtime ID (empty for external/local/SSH/OpenAPI)
	Endpoint   string     // How to reach it (URL or host:port)
	HostPort   int        // Host port if applicable

	// Server type flags (exactly one should be true for non-container servers)
	External     bool // URL-based external server
	LocalProcess bool // Local stdio process
	SSH          bool // SSH-based remote process
	OpenAPI      bool // OpenAPI-based adapter server

	// For non-container servers
	URL             string   // External server URL
	Command         []string // Local process or SSH command
	SSHHost         string
	SSHUser         string
	SSHPort         int
	SSHIdentityFile string

	// For OpenAPI servers
	OpenAPIConfig *config.OpenAPIConfig // OpenAPI configuration for gateway to use
}

MCPServerResult is the runtime-agnostic result for an MCP server.

type NetworkOptions

type NetworkOptions struct {
	Driver string // Network driver (e.g., "bridge")
	Stack  string // For labeling/cleanup purposes
}

NetworkOptions for network creation.

type Orchestrator

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

Orchestrator manages the lifecycle of gridctl workloads. It uses a WorkloadRuntime to start/stop workloads and a Builder for image builds.

func New

func New() (*Orchestrator, error)

New creates a new Orchestrator with a DockerRuntime. This is the backward-compatible constructor that delegates to the registered factory.

func NewOrchestrator

func NewOrchestrator(runtime WorkloadRuntime, builder Builder) *Orchestrator

NewOrchestrator creates an Orchestrator with the given runtime and builder.

func NewWithInfo

func NewWithInfo(info *RuntimeInfo) (*Orchestrator, error)

NewWithInfo creates a new Orchestrator using explicit RuntimeInfo.

func (*Orchestrator) Close

func (o *Orchestrator) Close() error

Close closes the runtime.

func (*Orchestrator) DockerClient

func (o *Orchestrator) DockerClient() dockerclient.DockerClient

DockerClient returns the Docker client for use by other components. This is needed for MCP gateway stdio transport and container logs.

func (*Orchestrator) Down

func (o *Orchestrator) Down(ctx context.Context, stack string) error

Down stops and removes all managed workloads and networks for a stack.

func (*Orchestrator) Runtime

func (o *Orchestrator) Runtime() WorkloadRuntime

Runtime returns the underlying WorkloadRuntime for advanced use cases.

func (*Orchestrator) RuntimeInfo

func (o *Orchestrator) RuntimeInfo() *RuntimeInfo

RuntimeInfo returns the detected runtime info, or nil if not set.

func (*Orchestrator) SetLogger

func (o *Orchestrator) SetLogger(logger *slog.Logger)

SetLogger sets the logger for orchestration operations. If the underlying runtime supports SetLogger, the logger is propagated.

func (*Orchestrator) SetRuntimeInfo

func (o *Orchestrator) SetRuntimeInfo(info *RuntimeInfo)

SetRuntimeInfo stores runtime detection info for runtime-aware behavior.

func (*Orchestrator) Status

func (o *Orchestrator) Status(ctx context.Context, stack string) ([]WorkloadStatus, error)

Status returns information about managed workloads.

func (*Orchestrator) Up

func (o *Orchestrator) Up(ctx context.Context, stack *config.Stack, opts UpOptions) (*UpResult, error)

Up starts all MCP servers and resources defined in the stack.

type RuntimeInfo

type RuntimeInfo struct {
	Type       RuntimeType
	SocketPath string
	HostAlias  string // "host.docker.internal" or "host.containers.internal"
	Version    string // Runtime version for feature gating
	SELinux    bool   // Whether SELinux is enforcing
}

RuntimeInfo holds detected runtime configuration.

func DetectRuntime

func DetectRuntime(opts DetectOptions) (*RuntimeInfo, error)

DetectRuntime probes for an available container runtime. Priority: explicit selection > DOCKER_HOST > Docker socket > Podman sockets.

func (*RuntimeInfo) CLIName

func (info *RuntimeInfo) CLIName() string

CLIName returns the CLI binary name for user-facing messages.

func (*RuntimeInfo) DisplayName

func (info *RuntimeInfo) DisplayName() string

DisplayName returns a human-readable name for the runtime.

func (*RuntimeInfo) DockerHost

func (info *RuntimeInfo) DockerHost() string

DockerHost returns the DOCKER_HOST value for connecting to this runtime.

func (*RuntimeInfo) HostAliasHostname

func (info *RuntimeInfo) HostAliasHostname() string

HostAliasHostname returns just the hostname part of the host alias.

func (*RuntimeInfo) IsExperimental

func (info *RuntimeInfo) IsExperimental() bool

IsExperimental returns true if this runtime is experimental.

func (*RuntimeInfo) IsRootless

func (info *RuntimeInfo) IsRootless() bool

IsRootless returns true if this appears to be a rootless Podman socket.

type RuntimeType

type RuntimeType string

RuntimeType identifies the container runtime.

const (
	RuntimeDocker RuntimeType = "docker"
	RuntimePodman RuntimeType = "podman"
)

type UpOptions

type UpOptions struct {
	NoCache     bool // Force rebuild of source-based images
	BasePort    int  // Base port for host port allocation (default: 9000)
	GatewayPort int  // Port for MCP gateway (for agent MCP_ENDPOINT injection)
}

UpOptions contains options for the Up operation.

type UpResult

type UpResult struct {
	MCPServers []MCPServerResult
	Agents     []AgentResult
}

UpResult contains the result of starting a stack.

type WorkloadConfig

type WorkloadConfig struct {
	// Identity
	Name  string       // Logical name (e.g., "postgres", "weather-server")
	Stack string       // Stack this workload belongs to
	Type  WorkloadType // Type of workload

	// Image/artifact
	Image string // Container image or artifact reference

	// Execution
	Command []string          // Override entrypoint/command
	Env     map[string]string // Environment variables

	// Networking
	NetworkName string // Network to join
	ExposedPort int    // Port the workload exposes (0 if none)
	HostPort    int    // Desired host port (0 for auto-assign)

	// Storage
	Volumes []string // Volume mounts (format: "host:container" or "host:container:mode")

	// Transport-specific
	Transport string // "http", "stdio", "sse"

	// Labels for identification and filtering
	Labels map[string]string
}

WorkloadConfig is the runtime-agnostic configuration for starting a workload.

type WorkloadFilter

type WorkloadFilter struct {
	Stack  string            // Filter by stack name
	Labels map[string]string // Additional label filters
}

WorkloadFilter for querying workloads.

type WorkloadID

type WorkloadID string

WorkloadID uniquely identifies a workload across runtimes. For Docker this is a container ID, for K8s a pod UID, for processes a PID.

type WorkloadRuntime

type WorkloadRuntime interface {
	// Start starts a workload and returns its status.
	Start(ctx context.Context, cfg WorkloadConfig) (*WorkloadStatus, error)

	// Stop stops a running workload by its ID.
	Stop(ctx context.Context, id WorkloadID) error

	// Remove removes a stopped workload.
	Remove(ctx context.Context, id WorkloadID) error

	// Status returns the current status of a workload.
	Status(ctx context.Context, id WorkloadID) (*WorkloadStatus, error)

	// Exists checks if a workload exists by name.
	Exists(ctx context.Context, name string) (exists bool, id WorkloadID, err error)

	// List returns all workloads matching the filter.
	List(ctx context.Context, filter WorkloadFilter) ([]WorkloadStatus, error)

	// GetHostPort returns the host port for a workload's exposed port.
	GetHostPort(ctx context.Context, id WorkloadID, exposedPort int) (int, error)

	// EnsureNetwork creates the network if it doesn't exist.
	EnsureNetwork(ctx context.Context, name string, opts NetworkOptions) error

	// ListNetworks returns all managed networks for a stack.
	ListNetworks(ctx context.Context, stack string) ([]string, error)

	// RemoveNetwork removes a network by name.
	RemoveNetwork(ctx context.Context, name string) error

	// EnsureImage ensures the image is available locally.
	EnsureImage(ctx context.Context, imageName string) error

	// Ping checks if the runtime is accessible.
	Ping(ctx context.Context) error

	// Close releases runtime resources.
	Close() error
}

WorkloadRuntime is the interface for managing workload lifecycles. Implementations include Docker, Kubernetes, local processes, etc.

type WorkloadState

type WorkloadState string

WorkloadState represents the current state of a workload.

const (
	WorkloadStateRunning  WorkloadState = "running"
	WorkloadStateStopped  WorkloadState = "stopped"
	WorkloadStateFailed   WorkloadState = "failed"
	WorkloadStateCreating WorkloadState = "creating"
	WorkloadStateUnknown  WorkloadState = "unknown"
)

type WorkloadStatus

type WorkloadStatus struct {
	// Identity
	ID    WorkloadID   // Runtime-assigned unique identifier
	Name  string       // Logical name from config
	Stack string       // Stack name
	Type  WorkloadType // Type of workload

	// State
	State   WorkloadState // Running, Stopped, Failed, etc.
	Message string        // Human-readable status message (e.g., "Up 5 minutes")

	// Networking
	Endpoint string // How to reach this workload (e.g., "localhost:9000")
	HostPort int    // Actual host port (if port-mapped)

	// Metadata
	Image  string            // Image/artifact that's running
	Labels map[string]string // All labels
}

WorkloadStatus is the runtime-agnostic status of a running workload.

type WorkloadType

type WorkloadType string

WorkloadType identifies the kind of workload.

const (
	WorkloadTypeMCPServer WorkloadType = "mcp-server"
	WorkloadTypeAgent     WorkloadType = "agent"
	WorkloadTypeResource  WorkloadType = "resource"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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