forked from cockroachdb/cockroach
/
docker.go
495 lines (455 loc) · 15.1 KB
/
docker.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package cluster
import (
"encoding/binary"
"fmt"
"io"
"math"
"net"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/go-connections/nat"
isatty "github.com/mattn/go-isatty"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
const matchNone = "^$"
// Retrieve the IP address of docker itself.
func dockerIP() net.IP {
host := os.Getenv("DOCKER_HOST")
if host == "" {
host = client.DefaultDockerHost
}
u, err := url.Parse(host)
if err != nil {
panic(err)
}
if u.Scheme == "unix" {
return net.IPv4(127, 0, 0, 1)
}
h, _, err := net.SplitHostPort(u.Host)
if err != nil {
panic(err)
}
return net.ParseIP(h)
}
// Container provides the programmatic interface for a single docker
// container.
type Container struct {
id string
name string
cluster *LocalCluster
}
// Name returns the container's name.
func (c Container) Name() string {
return c.name
}
func hasImage(l *LocalCluster, ref string) bool {
name := strings.Split(ref, ":")[0]
images, err := l.client.ImageList(context.Background(), types.ImageListOptions{MatchName: name})
if err != nil {
log.Fatal(context.TODO(), err)
}
for _, image := range images {
for _, repoTag := range image.RepoTags {
// The Image.RepoTags field contains strings of the form <repo>:<tag>.
if ref == repoTag {
return true
}
}
}
for _, image := range images {
for _, tag := range image.RepoTags {
log.Infof(context.TODO(), "ImageList %s %s", tag, image.ID)
}
}
return false
}
func pullImage(l *LocalCluster, ref string, options types.ImagePullOptions) error {
// HACK: on CircleCI, docker pulls the image on the first access from an
// acceptance test even though that image is already present. So we first
// check to see if our image is present in order to avoid this slowness.
if hasImage(l, ref) {
log.Infof(context.TODO(), "ImagePull %s already exists", ref)
return nil
}
log.Infof(context.TODO(), "ImagePull %s starting", ref)
defer log.Infof(context.TODO(), "ImagePull %s complete", ref)
rc, err := l.client.ImagePull(context.Background(), ref, options)
if err != nil {
return err
}
defer rc.Close()
out := os.Stderr
outFd := out.Fd()
isTerminal := isatty.IsTerminal(outFd)
return jsonmessage.DisplayJSONMessagesStream(rc, out, outFd, isTerminal, nil)
}
// createContainer creates a new container using the specified
// options. Per the docker API, the created container is not running
// and must be started explicitly. Note that the passed-in hostConfig
// will be augmented with the necessary settings to use the network
// defined by l.createNetwork().
func createContainer(
l *LocalCluster,
containerConfig container.Config,
hostConfig container.HostConfig,
containerName string,
) (*Container, error) {
hostConfig.NetworkMode = container.NetworkMode(l.networkID)
// Disable DNS search under the host machine's domain. This can
// catch upstream wildcard DNS matching and result in odd behavior.
hostConfig.DNSSearch = []string{"."}
resp, err := l.client.ContainerCreate(context.Background(), &containerConfig, &hostConfig, nil, containerName)
if err != nil {
return nil, err
}
return &Container{
id: resp.ID,
name: containerName,
cluster: l,
}, nil
}
func maybePanic(err error) {
if err != nil {
panic(err)
}
}
// Remove removes the container from docker. It is an error to remove a running
// container.
func (c *Container) Remove() error {
return c.cluster.client.ContainerRemove(context.Background(), c.id, types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
})
}
// Kill stops a running container, without removing it.
func (c *Container) Kill() error {
// Paused containers cannot be killed. Attempt to unpause it first
// (which might fail) before killing.
_ = c.Unpause()
if err := c.cluster.client.ContainerKill(context.Background(), c.id, "9"); err != nil && !strings.Contains(err.Error(), "is not running") {
return err
}
c.cluster.expectEvent(c, eventDie)
return nil
}
// Start starts a non-running container.
//
// TODO(pmattis): Generalize the setting of parameters here.
func (c *Container) Start() error {
return c.cluster.client.ContainerStart(context.Background(), c.id, types.ContainerStartOptions{})
}
// Pause pauses a running container.
func (c *Container) Pause() error {
return c.cluster.client.ContainerPause(context.Background(), c.id)
}
// TODO(pmattis): Container.Pause is neither used or tested. Silence unused
// warning.
var _ = (*Container).Pause
// Unpause resumes a paused container.
func (c *Container) Unpause() error {
return c.cluster.client.ContainerUnpause(context.Background(), c.id)
}
// Restart restarts a running container.
// Container will be killed after 'timeout' seconds if it fails to stop.
func (c *Container) Restart(timeout *time.Duration) error {
var exp []string
if ci, err := c.Inspect(); err != nil {
return err
} else if ci.State.Running {
exp = append(exp, eventDie)
}
if err := c.cluster.client.ContainerRestart(context.Background(), c.id, timeout); err != nil {
return err
}
c.cluster.expectEvent(c, append(exp, eventRestart)...)
return nil
}
// Stop a running container.
func (c *Container) Stop(timeout *time.Duration) error {
if err := c.cluster.client.ContainerStop(context.Background(), c.id, timeout); err != nil {
return err
}
c.cluster.expectEvent(c, eventDie)
return nil
}
// TODO(pmattis): Container.Stop is neither used or tested. Silence unused
// warning.
var _ = (*Container).Stop
// Wait waits for a running container to exit.
func (c *Container) Wait() error {
exitCode, err := c.cluster.client.ContainerWait(context.Background(), c.id)
if err == nil && exitCode != 0 {
err = errors.Errorf("non-zero exit code: %d", exitCode)
}
if err != nil {
if err := c.Logs(os.Stderr); err != nil {
log.Warning(context.TODO(), err)
}
}
return err
}
// Logs outputs the containers logs to the given io.Writer.
func (c *Container) Logs(w io.Writer) error {
rc, err := c.cluster.client.ContainerLogs(context.Background(), c.id, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
})
if err != nil {
return err
}
defer rc.Close()
// The docker log output is not quite plaintext: each line has a
// prefix consisting of one byte file descriptor (stdout vs stderr),
// three bytes padding, four byte length. We could use this to
// disentangle stdout and stderr if we wanted to output them into
// separate streams, but we don't really care.
for {
var header uint64
if err := binary.Read(rc, binary.BigEndian, &header); err == io.EOF {
break
} else if err != nil {
return err
}
size := header & math.MaxUint32
if _, err := io.CopyN(w, rc, int64(size)); err != nil {
return err
}
}
return nil
}
// Inspect retrieves detailed info about a container.
func (c *Container) Inspect() (types.ContainerJSON, error) {
return c.cluster.client.ContainerInspect(context.Background(), c.id)
}
// Addr returns the TCP address to connect to.
func (c *Container) Addr(port nat.Port) *net.TCPAddr {
containerInfo, err := c.Inspect()
if err != nil {
log.Error(context.TODO(), err)
return nil
}
bindings, ok := containerInfo.NetworkSettings.Ports[port]
if !ok || len(bindings) == 0 {
return nil
}
portNum, err := strconv.Atoi(bindings[0].HostPort)
if err != nil {
log.Error(context.TODO(), err)
return nil
}
return &net.TCPAddr{
IP: dockerIP(),
Port: portNum,
}
}
// resilientDockerClient handles certain recoverable Docker usage errors.
//
// For example, `ContainerCreate` will fail if a container with the requested
// name already exists. resilientDockerClient will catch this, delete the
// existing container and try again.
type resilientDockerClient struct {
client.APIClient
}
func (cli resilientDockerClient) ContainerCreate(
ctx context.Context,
config *container.Config,
hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig,
containerName string,
) (container.ContainerCreateCreatedBody, error) {
response, err := cli.APIClient.ContainerCreate(ctx, config, hostConfig, networkingConfig, containerName)
if err != nil && strings.Contains(err.Error(), "already in use") {
log.Infof(ctx, "unable to create container %s: %v", containerName, err)
containers, cerr := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
Limit: -1, // no limit, see docker/docker/client/container_list.go
})
if cerr != nil {
log.Infof(ctx, "unable to list containers: %v", cerr)
return container.ContainerCreateCreatedBody{}, err
}
for _, c := range containers {
for _, n := range c.Names {
// The container names begin with a "/".
n = strings.TrimPrefix(n, "/")
if n != containerName {
continue
}
log.Infof(ctx, "trying to remove %s", c.ID)
options := types.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
}
if rerr := cli.ContainerRemove(ctx, c.ID, options); rerr != nil {
log.Infof(ctx, "unable to remove container: %v", rerr)
return container.ContainerCreateCreatedBody{}, err
}
return cli.ContainerCreate(ctx, config, hostConfig, networkingConfig, containerName)
}
}
log.Warningf(ctx, "error indicated existing container %s, "+
"but none found:\nerror: %s\ncontainers: %+v",
containerName, err, containers)
// We likely raced with a previous (late) removal of the container.
// Return a timeout so a higher level can retry and hopefully
// succeed (or get stuck in an infinite loop, at which point at
// least we'll have gathered an additional bit of information).
return response, context.DeadlineExceeded
}
return response, err
}
func (cli resilientDockerClient) ContainerRemove(
ctx context.Context, container string, options types.ContainerRemoveOptions,
) error {
return cli.APIClient.ContainerRemove(ctx, container, options)
}
// retryingDockerClient proxies the Docker client api and retries problematic
// calls.
//
// Sometimes http requests to the Docker server, on circleci in particular, will
// hang indefinitely and non-deterministically. This leads to flaky tests. To
// avoid this, we wrap some of them in a timeout and retry loop.
type retryingDockerClient struct {
// Implements client.APIClient, but we use that it's resilient.
resilientDockerClient
attempts int
timeout time.Duration
}
// retry invokes the supplied function with time-limited contexts as long as
// returned error is a context timeout. When needing more than one attempt to
// get a (non-timeout) result, any errors matching retryErrorsRE are swallowed.
//
// For example, retrying a container removal could fail on the second attempt
// if the first request timed out (but still executed).
func retry(
ctx context.Context,
attempts int,
timeout time.Duration,
name string,
retryErrorsRE string,
f func(ctx context.Context) error,
) error {
for i := 0; i < attempts; i++ {
timeoutCtx, _ := context.WithTimeout(ctx, timeout)
err := f(timeoutCtx)
if err != nil {
if err == context.DeadlineExceeded {
continue
} else if i > 0 && retryErrorsRE != matchNone {
if regexp.MustCompile(retryErrorsRE).MatchString(err.Error()) {
log.Infof(ctx, "%s: swallowing expected error after retry: %v",
name, err)
return nil
}
}
}
return err
}
return fmt.Errorf("%s: exceeded %d tries with a %s timeout", name, attempts, timeout)
}
func (cli retryingDockerClient) ContainerCreate(
ctx context.Context,
config *container.Config,
hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig,
containerName string,
) (container.ContainerCreateCreatedBody, error) {
var ret container.ContainerCreateCreatedBody
err := retry(ctx, cli.attempts, cli.timeout,
"ContainerCreate", matchNone,
func(timeoutCtx context.Context) error {
var err error
ret, err = cli.resilientDockerClient.ContainerCreate(timeoutCtx, config, hostConfig, networkingConfig, containerName)
_ = ret // silence incorrect unused warning
return err
})
return ret, err
}
func (cli retryingDockerClient) ContainerStart(
ctx context.Context, container string, options types.ContainerStartOptions,
) error {
return retry(ctx, cli.attempts, cli.timeout, "ContainerStart", matchNone,
func(timeoutCtx context.Context) error {
return cli.resilientDockerClient.ContainerStart(timeoutCtx, container, options)
})
}
func (cli retryingDockerClient) ContainerRemove(
ctx context.Context, container string, options types.ContainerRemoveOptions,
) error {
return retry(ctx, cli.attempts, cli.timeout, "ContainerRemove", "No such container",
func(timeoutCtx context.Context) error {
return cli.resilientDockerClient.ContainerRemove(timeoutCtx, container, options)
})
}
func (cli retryingDockerClient) ContainerKill(ctx context.Context, container, signal string) error {
return retry(ctx, cli.attempts, cli.timeout, "ContainerKill",
"Container .* is not running",
func(timeoutCtx context.Context) error {
return cli.resilientDockerClient.ContainerKill(timeoutCtx, container, signal)
})
}
func (cli retryingDockerClient) ContainerWait(ctx context.Context, container string) (int64, error) {
var ret int64
return ret, retry(ctx, cli.attempts, cli.timeout, "ContainerWait", matchNone,
func(timeoutCtx context.Context) error {
var err error
ret, err = cli.resilientDockerClient.ContainerWait(timeoutCtx, container)
_ = ret // silence incorrect unused warning
return err
})
}
func (cli retryingDockerClient) ImageList(
ctx context.Context, options types.ImageListOptions,
) ([]types.ImageSummary, error) {
var ret []types.ImageSummary
return ret, retry(ctx, cli.attempts, cli.timeout, "ImageList", matchNone,
func(timeoutCtx context.Context) error {
var err error
ret, err = cli.resilientDockerClient.ImageList(timeoutCtx, options)
_ = ret // silence incorrect unused warning
return err
})
}
func (cli retryingDockerClient) ImagePull(
ctx context.Context, ref string, options types.ImagePullOptions,
) (io.ReadCloser, error) {
// Image pulling is potentially slow. Make sure the timeout is sufficient.
timeout := cli.timeout
if minTimeout := 2 * time.Minute; timeout < minTimeout {
timeout = minTimeout
}
var ret io.ReadCloser
return ret, retry(ctx, cli.attempts, timeout, "ImagePull", matchNone,
func(timeoutCtx context.Context) error {
var err error
ret, err = cli.resilientDockerClient.ImagePull(timeoutCtx, ref, options)
_ = ret // silence incorrect unused warning
return err
})
}