runtime

package
v0.1.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

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

Label constants re-exported for backward compatibility.

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.

Functions

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 AgentInfo

type AgentInfo struct {
	Name          string
	ContainerID   string
	ContainerName string
	Uses          []string // MCP servers this agent depends on
}

AgentInfo is provided for backward compatibility. Deprecated: Use AgentResult instead.

type AgentResult

type AgentResult struct {
	Name       string     // Logical name
	WorkloadID WorkloadID // Runtime ID
	Uses       []string   // 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
}

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 ContainerStatus

type ContainerStatus struct {
	ID            string
	Name          string
	Image         string
	State         string
	Status        string
	Type          string // "mcp-server", "resource", or "agent"
	MCPServerName string // Name of the MCP server, resource, or agent
	Topology      string
}

ContainerStatus holds status information for a container. Deprecated: Use WorkloadStatus instead.

func ToLegacyStatuses

func ToLegacyStatuses(statuses []WorkloadStatus) []ContainerStatus

ToLegacyStatuses converts []WorkloadStatus to []ContainerStatus for backward compatibility.

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 DockerClientGetter

type DockerClientGetter interface {
	Client() dockerclient.DockerClient
}

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

type LegacyUpResult

type LegacyUpResult struct {
	MCPServers []MCPServerInfo
	Agents     []AgentInfo
}

LegacyUpResult provides backward-compatible result format.

type MCPServerInfo

type MCPServerInfo struct {
	Name            string
	ContainerID     string   // Empty for external/local process/SSH servers
	ContainerName   string   // Empty for external/local process/SSH servers
	ContainerPort   int      // 0 for external/local process/SSH servers
	HostPort        int      // 0 for external/local process/SSH servers
	External        bool     // True if external server (no container)
	LocalProcess    bool     // True if local process server (no container)
	SSH             bool     // True if SSH server (remote process over SSH)
	URL             string   // Full URL for external servers
	Command         []string // Command for local process or SSH servers
	SSHHost         string   // SSH hostname (for SSH servers)
	SSHUser         string   // SSH username (for SSH servers)
	SSHPort         int      // SSH port (for SSH servers, 0 = default 22)
	SSHIdentityFile string   // SSH identity file path (for SSH servers)
}

MCPServerInfo is provided for backward compatibility. Deprecated: Use MCPServerResult instead.

type MCPServerResult

type MCPServerResult struct {
	Name       string     // Logical name
	WorkloadID WorkloadID // Runtime ID (empty for external/local/SSH)
	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

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

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

type NetworkOptions

type NetworkOptions struct {
	Driver   string // Network driver (e.g., "bridge")
	Topology 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 (*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, topology string) error

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

func (*Orchestrator) Runtime

func (o *Orchestrator) Runtime() WorkloadRuntime

Runtime returns the underlying WorkloadRuntime for advanced use cases.

func (*Orchestrator) SetLogger

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

SetLogger sets the logger for orchestration operations.

func (*Orchestrator) Status

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

Status returns information about managed workloads.

func (*Orchestrator) Up

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

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

type Runtime

type Runtime = Orchestrator

Runtime is an alias for Orchestrator for backward compatibility. Deprecated: Use Orchestrator instead.

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 topology.

func (*UpResult) ToLegacyResult

func (r *UpResult) ToLegacyResult() *LegacyUpResult

ToLegacyResult converts UpResult to LegacyUpResult for backward compatibility.

type WorkloadConfig

type WorkloadConfig struct {
	// Identity
	Name     string       // Logical name (e.g., "postgres", "weather-server")
	Topology string       // Topology 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 {
	Topology string            // Filter by topology 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 topology.
	ListNetworks(ctx context.Context, topology 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
	Topology string       // Topology 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