ops.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package container
  2. import (
  3. "maps"
  4. "strings"
  5. "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/api/types/mount"
  7. "github.com/docker/docker/api/types/network"
  8. "github.com/docker/docker/api/types/strslice"
  9. "github.com/docker/go-connections/nat"
  10. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  11. )
  12. // WithName sets the name of the container
  13. func WithName(name string) func(*TestContainerConfig) {
  14. return func(c *TestContainerConfig) {
  15. c.Name = name
  16. }
  17. }
  18. // WithLinks sets the links of the container
  19. func WithLinks(links ...string) func(*TestContainerConfig) {
  20. return func(c *TestContainerConfig) {
  21. c.HostConfig.Links = links
  22. }
  23. }
  24. // WithImage sets the image of the container
  25. func WithImage(image string) func(*TestContainerConfig) {
  26. return func(c *TestContainerConfig) {
  27. c.Config.Image = image
  28. }
  29. }
  30. // WithCmd sets the comannds of the container
  31. func WithCmd(cmds ...string) func(*TestContainerConfig) {
  32. return func(c *TestContainerConfig) {
  33. c.Config.Cmd = strslice.StrSlice(cmds)
  34. }
  35. }
  36. // WithNetworkMode sets the network mode of the container
  37. func WithNetworkMode(mode string) func(*TestContainerConfig) {
  38. return func(c *TestContainerConfig) {
  39. c.HostConfig.NetworkMode = container.NetworkMode(mode)
  40. }
  41. }
  42. // WithSysctls sets sysctl options for the container
  43. func WithSysctls(sysctls map[string]string) func(*TestContainerConfig) {
  44. return func(c *TestContainerConfig) {
  45. c.HostConfig.Sysctls = maps.Clone(sysctls)
  46. }
  47. }
  48. // WithExposedPorts sets the exposed ports of the container
  49. func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
  50. return func(c *TestContainerConfig) {
  51. c.Config.ExposedPorts = map[nat.Port]struct{}{}
  52. for _, port := range ports {
  53. c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
  54. }
  55. }
  56. }
  57. // WithTty sets the TTY mode of the container
  58. func WithTty(tty bool) func(*TestContainerConfig) {
  59. return func(c *TestContainerConfig) {
  60. c.Config.Tty = tty
  61. }
  62. }
  63. // WithWorkingDir sets the working dir of the container
  64. func WithWorkingDir(dir string) func(*TestContainerConfig) {
  65. return func(c *TestContainerConfig) {
  66. c.Config.WorkingDir = dir
  67. }
  68. }
  69. // WithMount adds an mount
  70. func WithMount(m mount.Mount) func(*TestContainerConfig) {
  71. return func(c *TestContainerConfig) {
  72. c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
  73. }
  74. }
  75. // WithVolume sets the volume of the container
  76. func WithVolume(target string) func(*TestContainerConfig) {
  77. return func(c *TestContainerConfig) {
  78. if c.Config.Volumes == nil {
  79. c.Config.Volumes = map[string]struct{}{}
  80. }
  81. c.Config.Volumes[target] = struct{}{}
  82. }
  83. }
  84. // WithBind sets the bind mount of the container
  85. func WithBind(src, target string) func(*TestContainerConfig) {
  86. return func(c *TestContainerConfig) {
  87. c.HostConfig.Binds = append(c.HostConfig.Binds, src+":"+target)
  88. }
  89. }
  90. // WithBindRaw sets the bind mount of the container
  91. func WithBindRaw(s string) func(*TestContainerConfig) {
  92. return func(c *TestContainerConfig) {
  93. c.HostConfig.Binds = append(c.HostConfig.Binds, s)
  94. }
  95. }
  96. // WithTmpfs sets a target path in the container to a tmpfs, with optional options
  97. // (separated with a colon).
  98. func WithTmpfs(targetAndOpts string) func(config *TestContainerConfig) {
  99. return func(c *TestContainerConfig) {
  100. if c.HostConfig.Tmpfs == nil {
  101. c.HostConfig.Tmpfs = make(map[string]string)
  102. }
  103. target, opts, _ := strings.Cut(targetAndOpts, ":")
  104. c.HostConfig.Tmpfs[target] = opts
  105. }
  106. }
  107. func WithMacAddress(networkName, mac string) func(config *TestContainerConfig) {
  108. return func(c *TestContainerConfig) {
  109. if c.NetworkingConfig.EndpointsConfig == nil {
  110. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  111. }
  112. if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
  113. c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
  114. }
  115. c.NetworkingConfig.EndpointsConfig[networkName].MacAddress = mac
  116. }
  117. }
  118. // WithIPv4 sets the specified ip for the specified network of the container
  119. func WithIPv4(networkName, ip string) func(*TestContainerConfig) {
  120. return func(c *TestContainerConfig) {
  121. if c.NetworkingConfig.EndpointsConfig == nil {
  122. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  123. }
  124. if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
  125. c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
  126. }
  127. if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
  128. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
  129. }
  130. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv4Address = ip
  131. }
  132. }
  133. // WithIPv6 sets the specified ip6 for the specified network of the container
  134. func WithIPv6(networkName, ip string) func(*TestContainerConfig) {
  135. return func(c *TestContainerConfig) {
  136. if c.NetworkingConfig.EndpointsConfig == nil {
  137. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  138. }
  139. if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil {
  140. c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{}
  141. }
  142. if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil {
  143. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{}
  144. }
  145. c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv6Address = ip
  146. }
  147. }
  148. func WithEndpointSettings(nw string, config *network.EndpointSettings) func(*TestContainerConfig) {
  149. return func(c *TestContainerConfig) {
  150. if c.NetworkingConfig.EndpointsConfig == nil {
  151. c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{}
  152. }
  153. if _, ok := c.NetworkingConfig.EndpointsConfig[nw]; !ok {
  154. c.NetworkingConfig.EndpointsConfig[nw] = config
  155. }
  156. }
  157. }
  158. // WithLogDriver sets the log driver to use for the container
  159. func WithLogDriver(driver string) func(*TestContainerConfig) {
  160. return func(c *TestContainerConfig) {
  161. c.HostConfig.LogConfig.Type = driver
  162. }
  163. }
  164. // WithAutoRemove sets the container to be removed on exit
  165. func WithAutoRemove(c *TestContainerConfig) {
  166. c.HostConfig.AutoRemove = true
  167. }
  168. // WithPidsLimit sets the container's "pids-limit
  169. func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
  170. return func(c *TestContainerConfig) {
  171. if c.HostConfig == nil {
  172. c.HostConfig = &container.HostConfig{}
  173. }
  174. c.HostConfig.PidsLimit = limit
  175. }
  176. }
  177. // WithRestartPolicy sets container's restart policy
  178. func WithRestartPolicy(policy container.RestartPolicyMode) func(c *TestContainerConfig) {
  179. return func(c *TestContainerConfig) {
  180. c.HostConfig.RestartPolicy.Name = policy
  181. }
  182. }
  183. // WithUser sets the user
  184. func WithUser(user string) func(c *TestContainerConfig) {
  185. return func(c *TestContainerConfig) {
  186. c.Config.User = user
  187. }
  188. }
  189. // WithAdditionalGroups sets the additional groups for the container
  190. func WithAdditionalGroups(groups ...string) func(c *TestContainerConfig) {
  191. return func(c *TestContainerConfig) {
  192. c.HostConfig.GroupAdd = groups
  193. }
  194. }
  195. // WithPrivileged sets privileged mode for the container
  196. func WithPrivileged(privileged bool) func(*TestContainerConfig) {
  197. return func(c *TestContainerConfig) {
  198. if c.HostConfig == nil {
  199. c.HostConfig = &container.HostConfig{}
  200. }
  201. c.HostConfig.Privileged = privileged
  202. }
  203. }
  204. // WithCgroupnsMode sets the cgroup namespace mode for the container
  205. func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
  206. return func(c *TestContainerConfig) {
  207. if c.HostConfig == nil {
  208. c.HostConfig = &container.HostConfig{}
  209. }
  210. c.HostConfig.CgroupnsMode = container.CgroupnsMode(mode)
  211. }
  212. }
  213. // WithExtraHost sets the user defined IP:Host mappings in the container's
  214. // /etc/hosts file
  215. func WithExtraHost(extraHost string) func(*TestContainerConfig) {
  216. return func(c *TestContainerConfig) {
  217. c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
  218. }
  219. }
  220. // WithPlatform specifies the desired platform the image should have.
  221. func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) {
  222. return func(c *TestContainerConfig) {
  223. c.Platform = p
  224. }
  225. }
  226. // WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
  227. func WithWindowsDevice(device string) func(*TestContainerConfig) {
  228. return func(c *TestContainerConfig) {
  229. c.HostConfig.Devices = append(c.HostConfig.Devices, container.DeviceMapping{PathOnHost: device})
  230. }
  231. }
  232. // WithIsolation specifies the isolation technology to apply to the container
  233. func WithIsolation(isolation container.Isolation) func(*TestContainerConfig) {
  234. return func(c *TestContainerConfig) {
  235. c.HostConfig.Isolation = isolation
  236. }
  237. }
  238. // WithConsoleSize sets the initial console size of the container
  239. func WithConsoleSize(width, height uint) func(*TestContainerConfig) {
  240. return func(c *TestContainerConfig) {
  241. c.HostConfig.ConsoleSize = [2]uint{height, width}
  242. }
  243. }
  244. // WithRuntime sets the runtime to use to start the container
  245. func WithRuntime(name string) func(*TestContainerConfig) {
  246. return func(c *TestContainerConfig) {
  247. c.HostConfig.Runtime = name
  248. }
  249. }
  250. // WithCDIDevices sets the CDI devices to use to start the container
  251. func WithCDIDevices(cdiDeviceNames ...string) func(*TestContainerConfig) {
  252. return func(c *TestContainerConfig) {
  253. request := container.DeviceRequest{
  254. Driver: "cdi",
  255. DeviceIDs: cdiDeviceNames,
  256. }
  257. c.HostConfig.DeviceRequests = append(c.HostConfig.DeviceRequests, request)
  258. }
  259. }
  260. func WithCapability(capabilities ...string) func(*TestContainerConfig) {
  261. return func(c *TestContainerConfig) {
  262. c.HostConfig.CapAdd = append(c.HostConfig.CapAdd, capabilities...)
  263. }
  264. }
  265. func WithDropCapability(capabilities ...string) func(*TestContainerConfig) {
  266. return func(c *TestContainerConfig) {
  267. c.HostConfig.CapDrop = append(c.HostConfig.CapDrop, capabilities...)
  268. }
  269. }
  270. func WithSecurityOpt(opt string) func(*TestContainerConfig) {
  271. return func(c *TestContainerConfig) {
  272. c.HostConfig.SecurityOpt = append(c.HostConfig.SecurityOpt, opt)
  273. }
  274. }
  275. // WithPIDMode sets the PID-mode for the container.
  276. func WithPIDMode(mode container.PidMode) func(c *TestContainerConfig) {
  277. return func(c *TestContainerConfig) {
  278. c.HostConfig.PidMode = mode
  279. }
  280. }
  281. func WithStopSignal(stopSignal string) func(c *TestContainerConfig) {
  282. return func(c *TestContainerConfig) {
  283. c.Config.StopSignal = stopSignal
  284. }
  285. }
  286. func WithContainerWideMacAddress(address string) func(c *TestContainerConfig) {
  287. return func(c *TestContainerConfig) {
  288. c.Config.MacAddress = address //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
  289. }
  290. }