forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
466 lines (431 loc) · 13.8 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
package docker
import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/argoproj/argo-workflows/v3/errors"
"github.com/argoproj/argo-workflows/v3/util"
"github.com/argoproj/argo-workflows/v3/util/file"
"github.com/argoproj/argo-workflows/v3/workflow/common"
)
var errContainerNotExist = fmt.Errorf("container does not exist") // sentinel error
var started = time.Now()
type DockerExecutor struct {
namespace string
podName string
containers map[string]ctr // containerName -> ctr
}
type ctr struct {
containerID string
status string
createdAt time.Time
}
func NewDockerExecutor(namespace, podName string) (*DockerExecutor, error) {
return &DockerExecutor{namespace, podName, make(map[string]ctr)}, nil
}
func (d *DockerExecutor) GetFileContents(containerName string, sourcePath string) (string, error) {
// Uses docker cp command to return contents of the file
// NOTE: docker cp CONTAINER:SRC_PATH DEST_PATH|- streams the contents of the resource
// as a tar archive to STDOUT if using - as DEST_PATH. Thus, we need to extract the
// content from the tar archive and output into stdout. In this way, we do not need to
// create and copy the content into a file from the wait container.
containerID, err := d.getContainerID(containerName)
if err != nil {
return "", err
}
dockerCpCmd := fmt.Sprintf("docker cp -a %s:%s - | tar -ax -O", containerID, sourcePath)
out, err := common.RunShellCommand(dockerCpCmd)
if err != nil {
return "", err
}
return string(out), nil
}
func (d *DockerExecutor) CopyFile(containerName string, sourcePath string, destPath string, compressionLevel int) error {
log.Infof("Archiving %s:%s to %s", containerName, sourcePath, destPath)
containerID, err := d.getContainerID(containerName)
if err != nil {
return err
}
dockerCpCmd := getDockerCpCmd(containerID, sourcePath, compressionLevel, destPath)
_, err = common.RunShellCommand(dockerCpCmd)
if err != nil {
return err
}
copiedFile, err := os.Open(filepath.Clean(destPath))
if err != nil {
return err
}
defer util.Close(copiedFile)
gzipReader, err := file.GetGzipReader(copiedFile)
if err != nil {
return err
}
if !file.ExistsInTar(sourcePath, tar.NewReader(gzipReader)) {
errMsg := fmt.Sprintf("path %s does not exist in archive %s", sourcePath, destPath)
log.Warn(errMsg)
return errors.Errorf(errors.CodeNotFound, errMsg)
}
log.Infof("Archiving completed")
return nil
}
type cmdCloser struct {
io.Reader
cmd *exec.Cmd
}
func (c *cmdCloser) Close() error {
err := c.cmd.Wait()
if err != nil {
return errors.InternalWrapError(err)
}
return nil
}
func (d *DockerExecutor) GetOutputStream(ctx context.Context, containerName string, combinedOutput bool) (io.ReadCloser, error) {
containerID, err := d.getContainerID(containerName)
if err != nil {
return nil, err
}
cmd := exec.Command("docker", "logs", containerID)
log.Info(cmd.Args)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, errors.InternalWrapError(err)
}
if !combinedOutput {
err = cmd.Start()
if err != nil {
return nil, errors.InternalWrapError(err)
}
return stdout, nil
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, errors.InternalWrapError(err)
}
err = cmd.Start()
if err != nil {
return nil, errors.InternalWrapError(err)
}
wg := &sync.WaitGroup{}
wg.Add(2)
reader, writer := io.Pipe()
go func() {
defer wg.Done()
_, _ = io.Copy(writer, stdout)
}()
go func() {
defer wg.Done()
_, _ = io.Copy(writer, stderr)
}()
go func() {
defer writer.Close()
wg.Wait()
}()
return &cmdCloser{Reader: reader, cmd: cmd}, nil
}
func (d *DockerExecutor) GetExitCode(ctx context.Context, containerName string) (string, error) {
containerID, err := d.getContainerID(containerName)
if err != nil {
return "", err
}
cmd := exec.Command("docker", "inspect", containerID, "--format='{{.State.ExitCode}}'")
reader, err := cmd.StdoutPipe()
if err != nil {
return "", errors.InternalWrapError(err, "Could not pipe STDOUT")
}
err = cmd.Start()
if err != nil {
return "", errors.InternalWrapError(err, "Could not start command")
}
defer func() { _ = reader.Close() }()
bytes, err := ioutil.ReadAll(reader)
if err != nil {
return "", errors.InternalWrapError(err, "Could not read from STDOUT")
}
out := string(bytes)
// Trims off a single newline for user convenience
outputLen := len(out)
if outputLen > 0 && out[outputLen-1] == '\n' {
out = out[:outputLen-1]
}
exitCode := strings.Trim(out, `'`)
// Ensure exit code is an int
if _, err := strconv.Atoi(exitCode); err != nil {
log.Warningf("Was not able to parse exit code output '%s' as int: %s", exitCode, err)
return "", nil
}
return exitCode, nil
}
func (d *DockerExecutor) Wait(ctx context.Context, containerNames []string) error {
go func() {
err := d.pollContainerIDs(ctx, containerNames)
if err != nil {
log.WithError(err).Error("failed to poll container IDs")
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if d.haveContainers(containerNames) {
containerIDs, err := d.getContainerIDs(containerNames)
if err != nil {
return err
}
_, err = common.RunCommand("docker", append([]string{"wait"}, containerIDs...)...)
if err != nil && strings.Contains(err.Error(), "No such container") {
// e.g. reason could be ContainerCannotRun
log.WithError(err).Info("ignoring error as container may have been re-created and therefore container ID may have changed")
time.Sleep(time.Second)
continue
}
if err != nil {
return err
}
completed, err := d.validateCompleted(ctx, containerNames)
if err != nil {
return err
}
if completed {
// We waited until everything completed!
return nil
}
}
time.Sleep(time.Second)
}
}
}
// After docker wait, sometimes a container can still be in "Created" state.
// https://github.com/argoproj/argo-workflows/issues/6352
// To workaround this issue, validate containers actually completed.
func (d *DockerExecutor) validateCompleted(ctx context.Context, containerNames []string) (bool, error) {
containers, err := d.listContainers()
if err != nil {
return false, err
}
for _, name := range containerNames {
container, ok := containers[name]
if !ok {
// ignore containers no longer found
continue
}
if container.status == "Created" {
// For containers with status == "Created", there are two
// possibilities:
// 1. the container will start running soon
// 2. the container failed to start, for example,
// because its entrypoint is set to an invalid path.
// You can reproduce this behavior by running:
// docker inspect $(docker run -d --entrypoint invalid argoproj/argosay:v2) | less
// In this case, we can distinguish the container by
// checking its exit code.
code, err := d.GetExitCode(ctx, name)
if err != nil {
return false, err
}
if code == "0" {
// If code is zero, it's case #1, the container
// will start soon.
log.Infof("unexpected: container %q still has state %q after docker wait", name, container.status)
return false, nil
} else {
// If code is non-zero, then it's case #2, the
// container failed to start, but its state is
// stuck at "Created".
// Therefore, we can treat this container as
// completed.
continue
}
}
}
return true, nil
}
func (d *DockerExecutor) listContainers() (map[string]ctr, error) {
output, err := common.RunCommand(
"docker",
"ps",
"--all", // container could have already exited, but there could also have been two containers for the same pod (old container not yet cleaned-up)
"--no-trunc", // display long container IDs
"--format={{.Status}}|{{.Label \"io.kubernetes.container.name\"}}|{{.ID}}|{{.CreatedAt}}", // similar to `Up 3 hours,main,035a98c4e72e,2021-03-08 17:25:15 -0800 PST`
// https://github.com/kubernetes/kubernetes/blob/ca6bdba014f0a98efe0e0dd4e15f57d1c121d6c9/pkg/kubelet/dockertools/labels.go#L37
"--filter=label=io.kubernetes.pod.namespace="+d.namespace,
"--filter=label=io.kubernetes.pod.name="+d.podName,
)
if err != nil {
return nil, err
}
containers := make(map[string]ctr)
for _, l := range strings.Split(string(output), "\n") {
parts := strings.Split(strings.TrimSpace(l), "|")
if len(parts) != 4 {
continue
}
status := strings.SplitN(parts[0], " ", 2)[0] // Created,Exited,Up,
containerName := parts[1]
if containerName == "POD" {
continue
}
containerID := parts[2]
if containerID == "" {
continue
}
createdAt, err := time.Parse("2006-01-02 15:04:05 -0700 MST", parts[3])
if err != nil {
return nil, err
}
containers[containerName] = ctr{containerID: containerID, status: status, createdAt: createdAt}
}
log.WithField("containers", containers).Info("listed containers")
return containers, nil
}
func (d *DockerExecutor) pollContainerIDs(ctx context.Context, containerNames []string) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
containers, err := d.listContainers()
if err != nil {
return err
}
for containerName, c := range containers {
if d.containers[containerName].containerID == c.containerID { // already found
continue
}
d.containers[containerName] = c
log.Infof("mapped container name %q to container ID %q (created at %v, status %s)", containerName, c.containerID, c.createdAt, c.status)
}
}
// sidecars start after the main containers, so we can't just exit once we know about all the main containers,
// we need a bit more time
if !d.haveContainers(containerNames) || time.Since(started) < 30*time.Second {
time.Sleep(1 * time.Second) // this is a hard-loop because containers can run very short periods of time
} else {
time.Sleep(10 * time.Second)
}
}
}
func (d *DockerExecutor) haveContainers(containerNames []string) bool {
for _, n := range containerNames {
if _, ok := d.containers[n]; !ok {
return false
}
}
return true
}
func (d *DockerExecutor) getContainerID(containerName string) (string, error) {
if ctr, ok := d.containers[containerName]; ok {
return ctr.containerID, nil
}
return "", errContainerNotExist
}
// killContainers kills a list of containerNames first with a SIGTERM then with a SIGKILL after a grace period
func (d *DockerExecutor) Kill(ctx context.Context, containerNames []string, terminationGracePeriodDuration time.Duration) error {
containerIDs, err := d.getContainerIDs(containerNames)
if err != nil {
return err
}
if len(containerIDs) == 0 { // they may have already terminated
log.Info("zero container IDs, assuming all containers have exited successfully")
return nil
}
killArgs := append([]string{"kill", "--signal", "TERM"}, containerIDs...)
// docker kill will return with an error if a container has terminated already, which is not an error in this case.
// We therefore ignore any error. docker wait that follows will re-raise any other error with the container.
_, err = common.RunCommand("docker", killArgs...)
if err != nil {
log.Warningf("Ignored error from 'docker kill --signal TERM': %s", err)
}
waitArgs := append([]string{"wait"}, containerIDs...)
waitCmd := exec.Command("docker", waitArgs...)
log.Info(waitCmd.Args)
if err := waitCmd.Start(); err != nil {
return errors.InternalWrapError(err)
}
// waitCh needs buffer of 1 so it can always send the result of waitCmd.Wait() without blocking.
// Otherwise, if the terminationGracePeriodSeconds elapses and the forced kill branch is run, there would
// be no receiver for waitCh and the goroutine would block forever
waitCh := make(chan error, 1)
go func() {
defer close(waitCh)
waitCh <- waitCmd.Wait()
}()
select {
case err = <-waitCh:
// waitCmd completed
case <-time.After(terminationGracePeriodDuration):
log.Infof("Timed out (%v) for containers to terminate gracefully. Killing forcefully", terminationGracePeriodDuration)
forceKillArgs := append([]string{"kill", "--signal", "KILL"}, containerIDs...)
forceKillCmd := exec.Command("docker", forceKillArgs...)
log.Info(forceKillCmd.Args)
// same as kill case above, we ignore any error
if err := forceKillCmd.Run(); err != nil {
log.Warningf("Ignored error from 'docker kill --signal KILL': %s", err)
}
}
if err != nil {
return errors.InternalWrapError(err)
}
log.Infof("Containers %s killed successfully", containerIDs)
return nil
}
func (d *DockerExecutor) ListContainerNames(ctx context.Context) ([]string, error) {
containers, err := d.listContainers()
if err != nil {
return nil, err
}
var containerNames []string
for n := range containers {
containerNames = append(containerNames, n)
}
return containerNames, nil
}
func (d *DockerExecutor) getContainerIDs(containerNames []string) ([]string, error) {
var containerIDs []string
for _, n := range containerNames {
containerID, err := d.getContainerID(n)
if err == errContainerNotExist {
continue
}
if err != nil {
return nil, err
}
containerIDs = append(containerIDs, containerID)
}
return containerIDs, nil
}
// getDockerCpCmd uses os-specific code to run `docker cp` and gzip/7zip to copy gzipped data from another
// container.
func getDockerCpCmd(containerID, sourcePath string, compressionLevel int, destPath string) string {
gzipCmd := "gzip %s > %s"
levelFlagParam := "-"
if runtime.GOOS == "windows" {
gzipCmd = "7za.exe a -tgzip -si %s %s"
levelFlagParam = "-mx"
}
var levelFlag string
switch compressionLevel {
case gzip.NoCompression:
// best we can do - if we skip gzip it's a different file
levelFlag = levelFlagParam + "1"
case gzip.DefaultCompression:
// use cmd default
levelFlag = ""
default:
// -1 through -9 (or error)
levelFlag = levelFlagParam + strconv.Itoa(compressionLevel)
}
return fmt.Sprintf("docker cp -a %s:%s - | %s", containerID, sourcePath, fmt.Sprintf(gzipCmd, levelFlag, destPath))
}