模块定位
pkg/bindings 是 Podman REST API 的 Go 客户端,为每种资源(containers/images/pods/volumes/networks/secrets/system/artifacts/manifests/kube)提供一组纯函数,封装 HTTP 调用并反序列化响应。它是 Tunnel 模式下 pkg/domain/infra/tunnel 的底层——Tunnel 实现的方法体就是调这里的函数。它与 pkg/api 服务端对称:每个 bindings 函数的 endpoint 字符串对应服务端一条路由。
模块架构
type Connection struct {
URI *url.URL
Client *http.Client
tls bool
}
Connection 不直接被持有,而是塞进 context.Context(clientKey/versionKey/machineModeKey 三个 value key,connection.go:43-47)。每个 endpoint 函数通过 bindings.GetClient(ctx) 取回。APIResponse(:30)包裹 *http.Response + 原始 *http.Request,便于流式端点(Stats/Logs)回查 context。按资源分子包(containers/、images/、pods/ 等),每个文件暴露同构签名 (ctx, nameOrID, *XxxOptions) (T, error)。
调用链路
以 ContainerCreate 为例:
pkg/domain/infra/tunnel/containers.go:539 ContainerCreate(ctx, *specgen.SpecGenerator)
└─ containers.CreateWithSpec(ic.ClientCtx, s, nil) [pkg/bindings/containers/create.go:14]
├─ bindings.GetClient(ctx) connection.go:66 ── 从 ctx 取 *Connection
├─ jsoniter.MarshalToString(s) ── SpecGenerator 序列化为 body
└─ conn.DoRequest(ctx, stringReader,
http.MethodPost, "/containers/create", nil, nil) connection.go:429
├─ 拼 URL: http://d/v<API-Version>/libpod/containers/create
├─ http.NewRequestWithContext + Header(API-Version)
└─ c.Client.Do(req) (最多 3 次重试,:485-495)
← response.Process(&ccr) errors.go:30 ── JSON unmarshal 到 types.ContainerCreateResponse
← tunnel 包装: entities.ContainerCreateReport{Id: response.ID} tunnel/containers.go:547
endpoint 路径 /v<n.n.n>/libpod/containers/create,方法 POST。服务端对应路由在 pkg/api/server/register_containers.go:768。
方法速查表
| 方法 | 一行职责 | 关键设计决策 |
|---|---|---|
GetClient in connection.go:66 |
从 ctx 取连接 | 连接存 context 而非字段 |
DoRequest in connection.go:429 |
发 HTTP | 网络错误最多 3 次重试,context 错误不重试 |
pingNewConnection in connection.go:379 |
建连时协商版本 | GET /_ping 读 Libpod-API-Version |
APIResponse.ProcessWithError in errors.go:30 |
错误转换 | 按状态码分流,409 用 conflict 模型 |
核心实现
连接存 context
Connection 被放进 context 而非作为字段持有,让 endpoint 函数保持纯函数签名 (ctx, ...) (T, error)——调用方只需传一个已注入连接的 ctx(由 bindings.NewConnectionWithOptions 在 runtime_tunnel.go 构造)。每请求把 client 当前 API 版本写进 URL 前缀(:437-443),服务端 VersionedPath 据此分流 compat/libpod handler。
API 版本协商
pingNewConnection(connection.go:379)连接时打 GET /_ping,读 Libpod-API-Version 头,与 version.APIVersion[Libpod][MinimalAPI] 比较,server 过旧直接报错。协商一次存进 ctx,后续每请求复用。
错误模型与重试
APIResponse.ProcessWithError(errors.go:30)按状态码分流:2xx/3xx unmarshal body;409 用调用方传入的 conflict 模型;其它 JSON 用 errorhandling.ErrorModel,非 JSON 包成带 ResponseCode 的 ErrorModel。APIVersionError(:69)用于”endpoint 在此 server 版本不存在”的显式短路。DoRequest 对网络错误最多重试 3 次,但 context/timeout 错误不重试。
手写而非生成
pkg/bindings/generator/generator.go(//go:build ignore)仅用 AST 解析 + text/template 生成 types_*_options.go 的 With/Get/Changed/ToParams 样板**;endpoint 调用本身(List/Create/Inspect)全部手写,因为流式(Stats/Logs/Attach)、重试、错误模型差异化大,模板难覆盖。这与 k8s client-go 的 OpenAPI 全量生成路子不同。
设计模式
| 模式 | 位置 | 为什么用 |
|---|---|---|
| 门面 | 每资源一个子包,纯函数 | 隐藏 HTTP 细节 |
| DTO 映射 | bindings 返回 entities 类型,tunnel 再映射 | 与 ABI 接口对齐 |
| API 版本协商 | pingNewConnection |
一次协商,每请求复用 |
| 错误转换 | APIResponse.Process |
HTTP 错误→Go error |
模块间交互
bindings import pkg/domain/entities/{types,reports},返回类型直接用 entities DTO——并非完全解耦,但只 import libpod/define 取纯数据结构(如 InspectContainerData),不 import libpod 运行时。被 pkg/domain/infra/tunnel 大量调用。与 pkg/api 服务端对称镜像。交互方式 HTTP(unix/tcp/ssh tunnel)。
扩展方式
新增一个 API 调用(以 “container checkpoint —leave-running” 为例):
pkg/bindings/containers/checkpoint.go加func CheckpointLeave(ctx, nameOrID, *CheckpointOptions) error,内部conn.DoRequest(ctx, nil, http.MethodPost, "/containers/%s/checkpoint_leave", params, nil, nameOrID)+response.Process(nil);types_checkpoint_options.go加字段(如LeaveRunning *bool),跑go generate重新生成 With/Get/ToParams。- 服务端
register_containers.go加路由 +pkg/api/handlers加 handler 调 libpod。 pkg/domain/infra/tunnel加ContainerCheckpointLeave调新 bindings;pkg/domain/infra/abi加 ABI 实现;entities加接口方法。