/
image.go
366 lines (321 loc) · 9.74 KB
/
image.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package dockerutil
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/stdcopy"
"go.uber.org/zap"
)
// Image is a docker image.
type Image struct {
log *zap.Logger
client *client.Client
// NOTE: it might make sense for Image to have an ibc.DockerImage field,
// but for now it is probably better to not have internal/dockerutil depend on ibc.
repository, tag string
networkID string
testName string
}
// NewImage returns a valid Image.
//
// "pool" and "networkID" are likely from DockerSetup.
// "testName" is from a (*testing.T).Name() and should match the t.Name() from DockerSetup to ensure proper cleanup.
//
// Most arguments (except tag) must be non-zero values or this function panics.
// If tag is absent, defaults to "latest".
// Currently, only public docker images are supported.
func NewImage(logger *zap.Logger, cli *client.Client, networkID string, testName string, repository, tag string) *Image {
if logger == nil {
panic(errors.New("nil logger"))
}
if cli == nil {
panic(errors.New("client cannot be nil"))
}
if networkID == "" {
panic(errors.New("networkID cannot be empty"))
}
if testName == "" {
panic("testName cannot be empty")
}
if repository == "" {
panic(errors.New("repository cannot be empty"))
}
if tag == "" {
tag = "latest"
}
i := &Image{
client: cli,
networkID: networkID,
repository: repository,
tag: tag,
testName: testName,
}
// Assign log after creating, so the imageRef method can be used.
i.log = logger.With(
zap.String("image", i.imageRef()),
zap.String("test_name", testName),
)
return i
}
// ContainerOptions optionally configures starting a Container.
type ContainerOptions struct {
// bind mounts: https://docs.docker.com/storage/bind-mounts/
Binds []string
// Environment variables
Env []string
// If blank, defaults to the container's default user.
User string
// If non-zero, will limit the amount of log lines returned.
LogTail uint64
}
// ContainerExecResult is a wrapper type that wraps an exit code and associated output from stderr & stdout, along with
// an error in the case of some error occurring during container execution.
type ContainerExecResult struct {
Err error // Err is nil, unless some error occurs during the container lifecycle.
ExitCode int
Stdout, Stderr []byte
}
// Run creates and runs a container invoking "cmd". The container resources are removed after exit.
//
// Run blocks until the command completes. Thus, Run is not suitable for daemons or servers. Use Start instead.
// A non-zero status code returns an error.
func (image *Image) Run(ctx context.Context, cmd []string, opts ContainerOptions) ContainerExecResult {
c, err := image.Start(ctx, cmd, opts)
if err != nil {
return ContainerExecResult{
Err: err,
ExitCode: -1,
Stdout: nil,
Stderr: nil,
}
}
return c.Wait(ctx, opts.LogTail)
}
func (image *Image) imageRef() string {
return image.repository + ":" + image.tag
}
// ensurePulled can only pull public images.
func (image *Image) ensurePulled(ctx context.Context) error {
ref := image.imageRef()
_, _, err := image.client.ImageInspectWithRaw(ctx, ref)
if err != nil {
rc, err := image.client.ImagePull(ctx, ref, types.ImagePullOptions{})
if err != nil {
return fmt.Errorf("pull image %s: %w", ref, err)
}
_, _ = io.Copy(io.Discard, rc)
_ = rc.Close()
}
return nil
}
func (image *Image) createContainer(ctx context.Context, containerName, hostName string, cmd []string, opts ContainerOptions) (string, error) {
// Although this shouldn't happen because the name includes randomness, in reality there seems to intermittent
// chances of collisions.
containers, err := image.client.ContainerList(ctx, types.ContainerListOptions{
All: true,
Filters: filters.NewArgs(filters.Arg("name", containerName)),
})
if err != nil {
return "", fmt.Errorf("unable to list containers: %w", err)
}
for _, c := range containers {
if err := image.client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}); err != nil {
return "", fmt.Errorf("unable to remove container %s: %w", containerName, err)
}
}
cc, err := image.client.ContainerCreate(
ctx,
&container.Config{
Image: image.imageRef(),
Entrypoint: []string{},
Cmd: cmd,
Env: opts.Env,
Hostname: hostName,
User: opts.User,
Labels: map[string]string{CleanupLabel: image.testName},
},
&container.HostConfig{
Binds: opts.Binds,
PublishAllPorts: true, // Because we publish all ports, no need to expose specific ports.
AutoRemove: false,
},
&network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
image.networkID: {},
},
},
nil,
containerName,
)
if err != nil {
return "", err
}
return cc.ID, nil
}
// Start pulls the image if not present, creates a container, and runs it.
func (image *Image) Start(ctx context.Context, cmd []string, opts ContainerOptions) (*Container, error) {
if len(cmd) == 0 {
panic(errors.New("cmd cannot be empty"))
}
if err := image.ensurePulled(ctx); err != nil {
return nil, image.wrapErr(err)
}
var (
containerName = SanitizeContainerName(image.testName + "-" + RandLowerCaseLetterString(6))
hostName = CondenseHostName(containerName)
logger = image.log.With(
zap.String("command", strings.Join(cmd, " ")),
zap.String("hostname", hostName),
zap.String("container", containerName),
)
)
cID, err := image.createContainer(ctx, containerName, hostName, cmd, opts)
if err != nil {
return nil, image.wrapErr(fmt.Errorf("create container %s: %w", containerName, err))
}
logger.Info("About to start container")
err = StartContainer(ctx, image.client, cID)
if err != nil {
return nil, image.wrapErr(fmt.Errorf("start container %s: %w", containerName, err))
}
return &Container{
Name: containerName,
Hostname: hostName,
log: logger,
image: image,
containerID: cID,
}, nil
}
func (image *Image) wrapErr(err error) error {
return fmt.Errorf("image %s:%s: %w", image.repository, image.tag, err)
}
// Container is a docker container. Use (*Image).Start to create a new container.
type Container struct {
Name string
Hostname string
log *zap.Logger
image *Image
containerID string
}
// Wait blocks until the container exits. Calling wait is not suitable for daemons and servers.
// A non-zero status code returns an error.
//
// Wait implicitly calls Stop.
// If logTail is non-zero, the stdout and stderr logs will be truncated at the end to that number of lines.
func (c *Container) Wait(ctx context.Context, logTail uint64) ContainerExecResult {
waitCh, errCh := c.image.client.ContainerWait(ctx, c.containerID, container.WaitConditionNotRunning)
var exitCode int
select {
case <-ctx.Done():
return ContainerExecResult{
Err: ctx.Err(),
ExitCode: 1,
Stdout: nil,
Stderr: nil,
}
case err := <-errCh:
return ContainerExecResult{
Err: err,
ExitCode: 1,
Stdout: nil,
Stderr: nil,
}
case res := <-waitCh:
exitCode = int(res.StatusCode)
if res.Error != nil {
return ContainerExecResult{
Err: errors.New(res.Error.Message),
ExitCode: exitCode,
Stdout: nil,
Stderr: nil,
}
}
}
var (
stdoutBuf = new(bytes.Buffer)
stderrBuf = new(bytes.Buffer)
)
logOpts := types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
}
if logTail != 0 {
logOpts.Tail = strconv.FormatUint(logTail, 10)
}
rc, err := c.image.client.ContainerLogs(ctx, c.containerID, logOpts)
if err != nil {
return ContainerExecResult{
Err: err,
ExitCode: exitCode,
Stdout: nil,
Stderr: nil,
}
}
defer func() { _ = rc.Close() }()
// Logs are multiplexed into one stream; see docs for ContainerLogs.
_, err = stdcopy.StdCopy(stdoutBuf, stderrBuf, rc)
if err != nil {
return ContainerExecResult{
Err: err,
ExitCode: exitCode,
Stdout: nil,
Stderr: nil,
}
}
_ = rc.Close()
err = c.Stop(10 * time.Second)
if err != nil {
c.log.Error("Failed to stop and remove container", zap.Error(err), zap.String("container_id", c.containerID))
}
if exitCode != 0 {
out := strings.Join([]string{stdoutBuf.String(), stderrBuf.String()}, " ")
return ContainerExecResult{
Err: fmt.Errorf("exit code %d: %s", exitCode, out),
ExitCode: exitCode,
Stdout: nil,
Stderr: nil,
}
}
return ContainerExecResult{
Err: nil,
ExitCode: exitCode,
Stdout: stdoutBuf.Bytes(),
Stderr: stderrBuf.Bytes(),
}
}
// Stop gives the container up to timeout to stop and remove itself from the network.
func (c *Container) Stop(timeout time.Duration) error {
// Use timeout*2 to give both stop and remove container operations a chance to complete.
ctx, cancel := context.WithTimeout(context.Background(), timeout*2)
defer cancel()
err := c.image.client.ContainerStop(ctx, c.containerID, &timeout)
if err != nil {
// Only return the error if it didn't match an already stopped, or a missing container.
if !(errdefs.IsNotModified(err) || errdefs.IsNotFound(err)) {
return c.image.wrapErr(fmt.Errorf("stop container %s: %w", c.Name, err))
}
}
// RemoveContainerOptions duplicates (*dockertest.Resource).Prune.
err = c.image.client.ContainerRemove(ctx, c.containerID, types.ContainerRemoveOptions{
Force: true,
RemoveVolumes: true,
})
if err != nil && !errdefs.IsNotFound(err) {
return c.image.wrapErr(fmt.Errorf("remove container %s: %w", c.Name, err))
}
return nil
}