forked from moby/moby
/
ops.go
243 lines (213 loc) · 7.37 KB
/
ops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package container
import (
"fmt"
"strings"
containertypes "github.com/docker/docker/api/types/container"
mounttypes "github.com/docker/docker/api/types/mount"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat"
specs "github.com/opencontainers/image-spec/specs-go/v1"
)
// WithName sets the name of the container
func WithName(name string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Name = name
}
}
// WithLinks sets the links of the container
func WithLinks(links ...string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Links = links
}
}
// WithImage sets the image of the container
func WithImage(image string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.Image = image
}
}
// WithCmd sets the comannds of the container
func WithCmd(cmds ...string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.Cmd = strslice.StrSlice(cmds)
}
}
// WithNetworkMode sets the network mode of the container
func WithNetworkMode(mode string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.NetworkMode = containertypes.NetworkMode(mode)
}
}
// WithExposedPorts sets the exposed ports of the container
func WithExposedPorts(ports ...string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.ExposedPorts = map[nat.Port]struct{}{}
for _, port := range ports {
c.Config.ExposedPorts[nat.Port(port)] = struct{}{}
}
}
}
// WithTty sets the TTY mode of the container
func WithTty(tty bool) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.Tty = tty
}
}
// WithWorkingDir sets the working dir of the container
func WithWorkingDir(dir string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.WorkingDir = dir
}
}
// WithMount adds an mount
func WithMount(m mounttypes.Mount) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Mounts = append(c.HostConfig.Mounts, m)
}
}
// WithVolume sets the volume of the container
func WithVolume(target string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.Config.Volumes == nil {
c.Config.Volumes = map[string]struct{}{}
}
c.Config.Volumes[target] = struct{}{}
}
}
// WithBind sets the bind mount of the container
func WithBind(src, target string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Binds = append(c.HostConfig.Binds, fmt.Sprintf("%s:%s", src, target))
}
}
// WithTmpfs sets a target path in the container to a tmpfs
func WithTmpfs(target string) func(config *TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig.Tmpfs == nil {
c.HostConfig.Tmpfs = make(map[string]string)
}
spec := strings.SplitN(target, ":", 2)
var opts string
if len(spec) > 1 {
opts = spec[1]
}
c.HostConfig.Tmpfs[spec[0]] = opts
}
}
// WithIPv4 sets the specified ip for the specified network of the container
func WithIPv4(network, ip string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.NetworkingConfig.EndpointsConfig == nil {
c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
}
if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
}
if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
}
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv4Address = ip
}
}
// WithIPv6 sets the specified ip6 for the specified network of the container
func WithIPv6(network, ip string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.NetworkingConfig.EndpointsConfig == nil {
c.NetworkingConfig.EndpointsConfig = map[string]*networktypes.EndpointSettings{}
}
if v, ok := c.NetworkingConfig.EndpointsConfig[network]; !ok || v == nil {
c.NetworkingConfig.EndpointsConfig[network] = &networktypes.EndpointSettings{}
}
if c.NetworkingConfig.EndpointsConfig[network].IPAMConfig == nil {
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig = &networktypes.EndpointIPAMConfig{}
}
c.NetworkingConfig.EndpointsConfig[network].IPAMConfig.IPv6Address = ip
}
}
// WithLogDriver sets the log driver to use for the container
func WithLogDriver(driver string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.LogConfig.Type = driver
}
}
// WithAutoRemove sets the container to be removed on exit
func WithAutoRemove(c *TestContainerConfig) {
c.HostConfig.AutoRemove = true
}
// WithPidsLimit sets the container's "pids-limit
func WithPidsLimit(limit *int64) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
}
c.HostConfig.PidsLimit = limit
}
}
// WithRestartPolicy sets container's restart policy
func WithRestartPolicy(policy string) func(c *TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.RestartPolicy.Name = policy
}
}
// WithUser sets the user
func WithUser(user string) func(c *TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Config.User = user
}
}
// WithPrivileged sets privileged mode for the container
func WithPrivileged(privileged bool) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
}
c.HostConfig.Privileged = privileged
}
}
// WithCgroupnsMode sets the cgroup namespace mode for the container
func WithCgroupnsMode(mode string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
if c.HostConfig == nil {
c.HostConfig = &containertypes.HostConfig{}
}
c.HostConfig.CgroupnsMode = containertypes.CgroupnsMode(mode)
}
}
// WithExtraHost sets the user defined IP:Host mappings in the container's
// /etc/hosts file
func WithExtraHost(extraHost string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost)
}
}
// WithPlatform specifies the desired platform the image should have.
func WithPlatform(p *specs.Platform) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.Platform = p
}
}
// WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI
func WithWindowsDevice(device string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Devices = append(c.HostConfig.Devices, containertypes.DeviceMapping{PathOnHost: device})
}
}
// WithIsolation specifies the isolation technology to apply to the container
func WithIsolation(isolation containertypes.Isolation) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Isolation = isolation
}
}
// WithConsoleSize sets the initial console size of the container
func WithConsoleSize(width, height uint) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.ConsoleSize = [2]uint{height, width}
}
}
// WithRuntime sets the runtime to use to start the container
func WithRuntime(name string) func(*TestContainerConfig) {
return func(c *TestContainerConfig) {
c.HostConfig.Runtime = name
}
}