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