containerruntime

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromSlashForHost added in v0.2.0

func FromSlashForHost(p string) string

FromSlashForHost 把可能存在的正斜杠宿主路径转成当前操作系统习惯的分隔符。 Windows 上返回 C:\a\b,Linux 上返回 /a/b,可直接给 filepath/os 使用。

func IsBindVolumeType added in v0.2.3

func IsBindVolumeType(volumeType string) bool

IsBindVolumeType 判断 volume 类型是否 bind(Source 是宿主路径)。

volume / named 的 Source 是卷名,tmpfs 没有 Source,都不参与路径换算。

func IsRuntimeMonitoring

func IsRuntimeMonitoring(runtimeID string) bool

IsRuntimeMonitoring reports whether runtimeID is currently monitored.

func IsWindowsHostPath added in v0.2.0

func IsWindowsHostPath(p string) bool

IsWindowsHostPath 报告 p 是否是 Windows 宿主路径(含盘符或 UNC 前缀)。

仅凭字符串判断而不看 runtime.GOOS,原因是:

  • 路径可能来自数据库 / 配置文件 / 前端表单,由别的操作系统写入;
  • Docker bind 语法的转义规则只取决于「宿主是 Windows 还是 Unix」, 而宿主形态可以从路径本身看出来。

func MarkIfNotMonitoring

func MarkIfNotMonitoring(runtimeID string) bool

MarkIfNotMonitoring atomically marks runtimeID as monitored if it is not already. Returns true if the runtimeID was newly marked.

func MarkRuntimeMonitoring

func MarkRuntimeMonitoring(runtimeID string)

MarkRuntimeMonitoring marks runtimeID as actively monitored. It is reference-counted so multiple monitor goroutines for the same runtimeID can coexist safely.

func MaskEndpoint added in v0.2.4

func MaskEndpoint(raw string) string

MaskEndpoint 把地址里的 user:password@ 部分替换成 user:***@,避免凭据泄露到前端。

func RuntimeMonitoringSnapshot

func RuntimeMonitoringSnapshot() map[string]int

RuntimeMonitoringSnapshot returns a copy of the process-global monitoring table. Key is runtimeID and value is monitoring reference count.

func SetMonitoringRegistry

func SetMonitoringRegistry(reg MonitoringRegistry)

SetMonitoringRegistry replaces the process-global monitoring registry. Passing nil is ignored.

func ToContainerPath added in v0.2.0

func ToContainerPath(p string) string

ToContainerPath 把宿主路径转成容器内的 POSIX 路径。

容器(linux 镜像)里没有盘符概念,所以 Windows 宿主路径需要重写成容器路径:

  • 盘符前缀 C:\ / C:/ 去掉,反斜杠换成 /,例如 D:\workspace\go-project\gobrave\.gobrave → /workspace/go-project/gobrave/.gobrave
  • UNC 路径 \\server\share\dir → //server/share/dir

已经是 POSIX 路径(Linux/macOS 宿主)时原样返回,保证 Linux 行为零变化。

func ToHostPath added in v0.2.0

func ToHostPath(containerPath, windowsDrivePrefix string) string

ToHostPath 把容器路径映射回宿主路径(Windows 时需要 C:/... 形式)。

幂等:p 本身已是宿主路径(含盘符或 UNC)时只统一分隔符、直接返回, 否则会把 "D:\a" 再拼一次盘符得到 "D:\D:\a"(用户实际遇到的 mkdir 报错)。

func ToWSLDaemonPath added in v0.2.3

func ToWSLDaemonPath(p string) string

ToWSLDaemonPath 把 Windows 宿主路径转成 WSL 内 dockerd 视角的路径。

Windows 上的 gobrave 连接 WSL2 里运行的 dockerd(DOCKER_HOST=tcp://...)时, bind mount 的 Source 必须由 daemon 所在机器解析。WSL2 默认把 Windows 盘符挂载到 /mnt/<drive>/ 下,因此:

  • D:\gobrave\.gobrave → /mnt/d/gobrave/.gobrave
  • C:/Users/foo → /mnt/c/Users/foo

已经是 POSIX 路径(Linux 宿主 / WSL 原生路径)时原样返回,保证 Linux 行为零变化。

func UnmarkRuntimeMonitoring

func UnmarkRuntimeMonitoring(runtimeID string)

UnmarkRuntimeMonitoring releases one monitoring reference for runtimeID.

Types

type MonitoringRegistry

type MonitoringRegistry interface {
	Mark(runtimeID string)
	Unmark(runtimeID string)
	IsMonitoring(runtimeID string) bool
	MarkIfNotMonitoring(runtimeID string) bool
	Snapshot() map[string]int
}

MonitoringRegistry abstracts runtime monitoring membership operations. Implementations can be in-memory, Redis-backed, etc.

func NewInMemoryMonitoringRegistry

func NewInMemoryMonitoringRegistry() MonitoringRegistry

type PathMapper added in v0.2.3

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

PathMapper 是跨平台路径语义的唯一入口。

系统里同时存在三种路径,混用是 Windows 上各种「丢盘符 / 多盘符 / daemon 找不到源」 问题的根源。这里用「来源」而不是「运行时 OS」来区分,并把换算全部收敛在本类型:

宿主路径   Windows 上是 D:\a\b,Linux 上是 /a/b;用于落盘和 bind mount 的 Source。
容器路径   容器内 POSIX:/a/b;用于 mount 的 Target、工作目录、命令与 env 取值。
daemon 路径 docker daemon 所在机器的路径(WSL2 下 D:\a → /mnt/d/a),仅 bind Source 用。

约定:storage.base_dir 是唯一可信的宿主锚点——盘符由它决定,容器路径由它派生。 因此「容器路径 → 宿主路径」只在路径确实位于 storage.base_dir 之下时才补盘符, 其余 POSIX 路径(/var/run/docker.sock、镜像内路径)原样返回。

值为 nil 时所有方法都可安全调用,等价于「不做任何转换」。

func NewPathMapper added in v0.2.3

func NewPathMapper(baseDir string, wslDaemon bool) *PathMapper

NewPathMapper 按 storage.base_dir 与 daemon 形态构建路径映射器。

baseDir 为空时只做容器路径归一(Container/Join 仍然可用),不做宿主路径还原。

func (*PathMapper) Container added in v0.2.3

func (m *PathMapper) Container(p string) string

Container 把任意路径归一成容器内 POSIX 路径(幂等)。

func (*PathMapper) Daemon added in v0.2.3

func (m *PathMapper) Daemon(p string) string

Daemon 把 bind Source 翻译成 docker daemon 视角的路径;非 WSL daemon 时原样返回。

func (*PathMapper) Host added in v0.2.3

func (m *PathMapper) Host(p string) string

Host 把任意路径还原成当前宿主可用的路径。

  • 已是宿主路径(盘符 / UNC):只统一分隔符,幂等,避免 D:\ 被二次拼成 D:\D:\;
  • storage 派生出的容器路径:补回 base_dir 的盘符;
  • 其他 POSIX 路径(docker.sock / 容器内路径):原样返回,不做猜测。

func (*PathMapper) Join added in v0.2.3

func (m *PathMapper) Join(base string, elems ...string) string

Join 以容器路径语义拼接(与路径包无关,避免 Windows 上产生反斜杠)。

func (*PathMapper) NormalizeVolumes added in v0.2.3

func (m *PathMapper) NormalizeVolumes(volumes []types.ContainerVolume)

NormalizeVolumes 把 volume 的路径契约收敛为一处:bind 的 Source 是宿主路径, Target 是容器内路径。模板变量展开后 Source 可能是容器路径(Windows 上没盘符), 在任何落盘 / 交给 runtime 之前统一还原。

func (*PathMapper) WSLDaemon added in v0.2.3

func (m *PathMapper) WSLDaemon() bool

WSLDaemon 报告是否按 WSL2 daemon 语义翻译 bind Source。

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Get

func (r *Registry) Get(name string) Runtime

func (*Registry) List

func (r *Registry) List() []Runtime

func (*Registry) Register

func (r *Registry) Register(name string, rt Runtime)

type Runtime

type Runtime interface {
	Name() string

	Create(ctx context.Context, spec *types.ContainerSpec) (string, error)

	Start(ctx context.Context, runtimeID string) error

	Stop(ctx context.Context, runtimeID string) error

	Pause(ctx context.Context, runtimeID string) error

	Resume(ctx context.Context, runtimeID string) error

	Delete(ctx context.Context, runtimeID string) error

	Logs(ctx context.Context, runtimeID string, tail int) (string, error)

	SetEventHandler(handler RuntimeEventHandler)

	Exec(ctx context.Context, runtimeID string, cmd []string) (string, error)
}

type RuntimeDescriber added in v0.1.4

type RuntimeDescriber interface {
	Describe(ctx context.Context, runtimeID string) (*RuntimeDescription, error)
}

RuntimeDescriber 是可选扩展接口。实现该接口的运行时可通过 runtimeID 返回 资源的详情(docker inspect / kubectl describe)。

type RuntimeDescription added in v0.1.4

type RuntimeDescription struct {
	// Kind 标识资源类型,例如 container / deployment / job / service。
	Kind string `json:"kind"`
	// Name 资源名称(docker 容器名,或 k8s workload/service 名)。
	Name string `json:"name"`
	// Format 描述 Raw 的格式,便于前端渲染,例如 json / yaml / text。
	Format string `json:"format"`
	// Raw 原始 inspect/describe 输出,保持后端运行时的原生格式。
	Raw string `json:"raw"`
}

RuntimeDescription 描述一个运行时的详情信息(对应 docker inspect / kubectl describe)。

type RuntimeEndpointReporter added in v0.2.4

type RuntimeEndpointReporter interface {
	// Endpoint 返回(脱敏后的)连接地址及地址来源,例如 unix:///var/run/docker.sock + env。
	Endpoint() (address string, source string)
}

RuntimeEndpointReporter 是可选扩展接口:运行时报告它实际连接到哪个 daemon / apiserver。 返回的地址用于前端展示,实现方需自行脱敏(见 MaskEndpoint)。

type RuntimeEvent

type RuntimeEvent struct {
	Type      string
	RuntimeID string
	Message   string
}

type RuntimeEventHandler

type RuntimeEventHandler interface {
	OnEvent(event RuntimeEvent)
}

type RuntimeHealth added in v0.2.2

type RuntimeHealth struct {
	// Name 运行时名称(docker / k8s / k3s)。
	Name string `json:"name"`
	// Registered 是否在运行时注册表中(即启动装配阶段是否成功注册)。
	Registered bool `json:"registered"`
	// Healthy 依赖探测是否通过;未注册时必然为 false。
	Healthy bool `json:"healthy"`
	// Checkable 运行时是否实现了 RuntimeHealthChecker(false 表示只做了注册检查)。
	Checkable bool `json:"checkable"`
	// Error 检测失败原因,正常时为空。
	Error string `json:"error,omitempty"`

	// HostOS 后端进程所在宿主操作系统(runtime.GOOS:linux / windows / darwin)。
	HostOS string `json:"host_os"`
	// HostArch 后端进程所在宿主 CPU 架构(runtime.GOARCH)。
	HostArch string `json:"host_arch"`
	// Endpoint 运行时实际连接地址(已脱敏,密码以 *** 代替)。
	Endpoint string `json:"endpoint,omitempty"`
	// EndpointSource 地址来源:explicit / env / default / kubeconfig / in-cluster。
	EndpointSource string `json:"endpoint_source,omitempty"`
	// ServerVersion 对端版本号(docker daemon / kubernetes apiserver)。
	ServerVersion string `json:"server_version,omitempty"`

	// ProvisionEnabled 是否启用运行时预置(container.docker.provision.enabled)。
	// 为 true 时前端才展示「容器运行时安装向导」入口。
	ProvisionEnabled bool `json:"provision_enabled"`
	// BashExecutionMode bash 工具执行位置:host(宿主机)/ container(容器)。
	BashExecutionMode string `json:"bash_execution_mode"`
	// BashContainerTemplateID container 模式下 bash 工具使用的容器模板 ID。
	BashContainerTemplateID int64 `json:"bash_container_template_id,string"`
}

RuntimeHealth 描述运行时注册 / 可用性与宿主环境信息。

type RuntimeHealthChecker added in v0.2.2

type RuntimeHealthChecker interface {
	// HealthCheck 探测运行时依赖是否可用;返回 error 表示不可用(包含具体原因)。
	// ctx 由调用方设置超时,实现应尽量快速返回。
	HealthCheck(ctx context.Context) error
}

RuntimeHealthChecker 是可选扩展接口:运行时探测自身依赖(docker daemon / k8s apiserver) 是否可用,供启动自检与配置检测接口使用。未实现该接口的运行时按「已注册即正常」处理。

type RuntimeImageManager

type RuntimeImageManager interface {
	EnsureImage(ctx context.Context, image string, pullPolicy string) error
}

RuntimeImageManager is an optional extension interface for runtime-side image lifecycle operations used by ImageManager.

type RuntimeInspection

type RuntimeInspection struct {
	IPAddress string
	NodeName  string

	// HostAddress / HostPort 是容器端口发布到宿主后的可达地址(仅 PublishPort 时非空)。
	// IPAddress 是容器网络内地址,HostAddress 是 gobrave 所在主机能访问到的地址,
	// 两者语义不同:WSL2 NAT 下前者(172.17.x.x)不可路由,后者才是可用的。
	HostAddress string
	HostPort    int
}

RuntimeInspection carries runtime-specific inspect data used by manager/service.

type RuntimeInspector

type RuntimeInspector interface {
	Inspect(ctx context.Context, runtimeID string) (*RuntimeInspection, error)
}

RuntimeInspector is an optional extension interface. Implementations can expose inspect metadata (such as container internal IP) without forcing all runtimes.

type RuntimeMonitor

type RuntimeMonitor interface {
	Monitor(ctx context.Context, runtimeID string) error
}

RuntimeMonitor is an optional extension interface for reconnecting runtime lifecycle monitoring after process restarts.

type RuntimeTerminal added in v0.2.0

type RuntimeTerminal interface {
	OpenTerminal(ctx context.Context, runtimeID string, opts TerminalOptions) (TerminalSession, error)
}

RuntimeTerminal 是可选扩展接口:支持交互式 exec 的运行时实现它。

它与 Runtime.Exec 的区别:Exec 是非交互式的一次性命令(无 stdin / 无 TTY), 这里则等价于 `kubectl exec -it` / `docker exec -it`。

type RuntimeVersionReporter added in v0.2.4

type RuntimeVersionReporter interface {
	ServerVersion(ctx context.Context) (string, error)
}

RuntimeVersionReporter 是可选扩展接口:报告对端(docker daemon / apiserver)版本号。

type TerminalOptions added in v0.2.0

type TerminalOptions struct {
	// Command 是要在容器内执行的命令,例如 []string{"/bin/bash", "-l"}。
	Command []string
	// TTY 为 true 时申请伪终端(交互式 shell 必须为 true)。
	TTY bool
	// Rows / Cols 是初始终端尺寸。
	Rows uint16
	Cols uint16
}

TerminalOptions 描述一次交互式终端会话。

type TerminalSession added in v0.2.0

type TerminalSession interface {
	io.Reader
	io.Writer
	// Resize 调整终端尺寸(行 / 列)。
	Resize(rows, cols uint16) error
	// Close 关闭会话与底层连接。
	Close() error
}

TerminalSession 是一条双向的终端流:Read 读容器输出,Write 写用户输入。

Directories

Path Synopsis
Package provision 统一管理容器运行时的「安装 / 启动」生命周期。
Package provision 统一管理容器运行时的「安装 / 启动」生命周期。
types
Package types 是 provision 各实现共享的最小类型集合。
Package types 是 provision 各实现共享的最小类型集合。
wsl

Jump to

Keyboard shortcuts

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