ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Dagger TypeScript SDK 解析:ContainerWithoutUnixSocketOpts 与 withoutUnixSocket() 移除 Unix Socket

Dagger TypeScript SDK 解析:ContainerWithoutUnixSocketOpts 与 withoutUnixSocket() 移除 Unix Socket Dagger TypeScript SDK 解析ContainerWithoutUnixSocketOpts 与 withoutUnixSocket() 移除 Unix Socket【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger本指南以 Dagger 仓库docs/versioned_docs/version-0.21/reference/typescript/api/client.gen/type-aliases/ContainerWithoutUnixSocketOpts.md的类型别名参考文档为核心围绕Container.withoutUnixSocket()的可选参数类型展开。你将理解expand参数的语义按容器内环境变量展开路径中的${VAR}与$VAR、该方法在 TypeScript SDK 中的签名与用法以及它在 Dagger 引擎底层core/schema/container.go是如何被解析与执行的从而能在自己的 Dagger 管道中正确地移除先前挂载的 Unix Socket。一、类型别名速览在 TypeScript SDK 中ContainerWithoutUnixSocketOpts是Container.withoutUnixSocket()方法的可选参数opts类型。原文档定义如下ContainerWithoutUnixSocketOptsobject它只有一个可选属性属性类型可选性说明expandboolean可选按容器中当前定义的环境变量替换路径值中的${VAR}或$VAR例如/$VAR/foo。该定义在 SDK 源码 sdk/typescript/src/api/client.gen.ts#L1194-L1199 中与代码生成产物保持一致export type ContainerWithoutUnixSocketOpts { /** * Replace ${VAR} or $VAR in the value of path according to the current environment variables defined in the container (e.g. /$VAR/foo). */ expand?: boolean }这段源码由 Dagger 的代码生成器产出对应cmd/codegen与 SDK 生成逻辑因此该类型别名与 GraphQL API 的withoutUnixSocket字段签名一一对应你在本地 SDK 中看到的注释与 API 参考文档完全同源。二、所属 APIContainer.withoutUnixSocket()ContainerWithoutUnixSocketOpts的唯一消费方是Container.withoutUnixSocket()方法。在 TypeScript SDK 中其定义位于 sdk/typescript/src/api/client.gen.ts#L6477-L6488/** * Retrieves this container with a previously added Unix socket removed. * param path Location of the socket to remove (e.g., /tmp/socket). * param opts.expand Replace ${VAR} or $VAR in the value of path according to the current environment variables defined in the container (e.g. /$VAR/foo). */ withoutUnixSocket ( path: string, opts?: ContainerWithoutUnixSocketOpts, ): Container { const ctx this._ctx.select(withoutUnixSocket, { path, ...opts }) return new Container(ctx) }参数与返回值path: string要移除的已转发 Unix Socket 路径例如/tmp/socket。opts?: ContainerWithoutUnixSocketOpts可选参数对象仅含expand。返回值Container——一个新的、不再包含该路径下 socket 的容器对象。Dagger 的容器 API 是不可变immutable链式风格withoutUnixSocket不会修改原容器而是返回移除后的新容器需要将返回值继续用于后续WithExec等调用。在 API 参考文档 Container.md 中与之成对出现的还有withUnixSocket(path, source, opts?)该方法Retrieves this container plus a socket forwarded to the given Unix socket path其中path是转发目标路径如/tmp/socketsource是Socket标识。withoutUnixSocket正是withUnixSocket的逆操作用于在挂载之后、执行命令之前移除或替换某个路径上的 socket。三、expand 参数的引擎层实现原理expand参数不是 SDK 层的装饰性选项它直接控制引擎端对path的预处理。在 Dagger 核心 Schema 实现 core/schema/container.go#L4057-L4086 中type containerWithoutUnixSocketArgs struct { Path string Expand bool default:false } func (s *containerSchema) withoutUnixSocket(ctx context.Context, parent dagql.ObjectResult[*core.Container], args containerWithoutUnixSocketArgs) (*core.Container, error) { path, err : expandEnvVar(ctx, parent.Self(), args.Path, args.Expand) if err ! nil { return nil, err } // ... target : absPath(parent.Self().Config.WorkingDir, path) for i, sock : range ctr.Sockets { if sock.ContainerPath ! target { continue } ctr.Sockets slices.Delete(ctr.Sockets, i, i1) break } // ... }关键点有二Expand默认值为false。当expand缺省或显式传false时path原样使用只有显式传true才会触发环境变量展开。路径归一化展开后的路径会基于容器的WorkingDir调用absPath解析为绝对路径再与容器已挂载 socket 记录的ContainerPath逐一比对命中即删除。因此实际使用时推荐直接传入绝对路径如/tmp/socket与withUnixSocket挂载时使用的路径保持一致。expandEnvVar 的具体行为展开逻辑由 core/schema/container.go#L3445-L3485 的expandEnvVar完成func expandEnvVar(ctx context.Context, parent *core.Container, input string, expand bool) (string, error) { if !expand { return input, nil } // 读取容器镜像配置中的环境变量 cfg, err : parent.ImageConfig(ctx) // ... expanded : os.Expand(input, func(k string) string { // 若是 secret 环境变量报错 if slices.Contains(secretEnvs, k) { return , fmt.Errorf(expand cannot be used with secret env variable %q, k) } // 若是 volatile 环境变量报错 if slices.Contains(volatileEnvs, k) { return , fmt.Errorf(expand cannot be used with volatile env variable %q, k) } v, _ : core.LookupEnv(cfg.Env, k) return v }) // ... }其行为可以归纳为展开来源变量值来自容器镜像配置ImageConfig中定义的环境变量而不是宿主机环境。支持两种语法${VAR}与$VAR均会被替换由 Go 标准库os.Expand提供支持例如/$VAR/foo在VAR/run时展开为/run/foo。两种禁止展开的变量若路径中引用的变量是容器内的secret 环境变量通过WithSecretVariable注入或volatile 环境变量通过WithVolatileVariable注入引擎会直接返回错误防止将敏感或易变值泄漏进路径解析。这是使用expand时最容易踩到的边界条件。未定义变量变量不存在时按空字符串处理os.Expand的标准行为因此请确保引用的环境变量已在容器中定义。四、实战示例1. TypeScript 中的基本用法先挂载、再移除的典型流程import { dag, Container } from dagger.io/dagger // 先通过 withUnixSocket 将宿主机 socket 转发到容器内 const ctr: Container dag .container() .from(alpine:latest) .withUnixSocket(/var/run/host.sock, dag.host().unixSocket(/tmp/host.sock)) // 执行需要 socket 的命令 const out await ctr.withExec([nc, -U, /var/run/host.sock]).stdout() // 移除该 socket返回不带 socket 的新容器 const without ctr.withoutUnixSocket(/var/run/host.sock)2. 使用 expand 展开路径变量当 socket 路径依赖容器内环境变量时配合expand: trueconst ctr dag .container() .from(alpine:latest) .withEnvVariable(SOCK_DIR, /run/app) .withUnixSocket(/run/app/api.sock, dag.host().unixSocket(/tmp/api.sock)) // 用 ${SOCK_DIR} 引用容器内环境变量来定位待移除的 socket const without ctr.withoutUnixSocket(${SOCK_DIR}/api.sock, { expand: true, })注意expand: true时引擎用容器内的环境变量做替换若路径引用了 secret/volatile 变量调用会报错。3. 引擎集成测试中的验证仓库集成测试 core/integration/socket_test.go#L63-L96 验证了先挂载、后移除的完整闭环容器先WithUnixSocket(/tmp/test.sock, hostSock)挂载 socketgo run的 echo 程序在/tmp/test.sock上成功收发数据随后调用WithoutUnixSocket(/tmp/test.sock)并执行ls /tmp此时目录中已无 socket 文件stdout 为空证明移除操作真实生效。同一测试还覆盖了同路径重复挂载替换场景ctr.WithUnixSocket(/tmp/test.sock, hostSock)再次挂载同路径后withoutUnixSocket依然能正确移除。此外模块化场景的测试模块 core/integration/testdata/modules/go/call-socket-basic/main.go#L11-L24 展示了完整的挂载 socket → 执行网络命令 → 读取输出链路WithUnixSocket(/var/run/host.sock, sock)后通过netcat连接该 socket。五、使用注意事项与边界行为不可变性withoutUnixSocket返回新容器务必捕获返回值用于后续调用链。路径必须匹配移除时按归一化后的绝对路径与挂载记录比对请保持与withUnixSocket挂载时一致的路径写法避免相对路径与绝对路径不一致导致移除失效。同路径替换若同一路径上先有 socket再执行withUnixSocket重新挂载新 socket 会覆盖旧记录withoutUnixSocket移除的是当前生效的那一条记录。幂等性从 core/schema/container.go#L4072-L4079 的实现可以推断若容器中不存在匹配该路径的 socket循环不会命中删除分支仅返回移除操作后的容器副本操作是幂等的不会报错。expand的安全约束开启后仅展开普通环境变量涉及 secret 或 volatile 变量会直接报错这是引擎刻意为之的防护行为。参考文档范围本文基于version-0.21版本的 TypeScript SDK API 参考ContainerWithoutUnixSocketOpts.md其他版本 SDK 中该类型的定义以对应版本的sdk/typescript/src/api/client.gen.ts生成产物为准。【免费下载链接】daggerAutomation engine to build, test and ship any codebase. Runs locally, in CI, or directly in the cloud项目地址: https://gitcode.com/GitHub_Trending/da/dagger创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表