This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
task.go
520 lines (440 loc) · 14.6 KB
/
task.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package docker
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"strings"
"time"
"github.com/docker/distribution/reference"
"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/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
goUnits "github.com/docker/go-units"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/docs"
"github.com/oklog/ulid/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
wpdockerclient "github.com/hashicorp/waypoint/builtin/docker/client"
)
// TaskLauncher uses `docker build` to build a Docker iamge.
type TaskLauncher struct {
config TaskLauncherConfig
}
// BuildFunc implements component.TaskLauncher
func (b *TaskLauncher) StartTaskFunc() interface{} {
return b.StartTask
}
// BuildFunc implements component.TaskLauncher
func (b *TaskLauncher) StopTaskFunc() interface{} {
return b.StopTask
}
// WatchFunc implements component.TaskLauncher
func (b *TaskLauncher) WatchTaskFunc() interface{} {
return b.WatchTask
}
type TaskResources struct {
// How many CPU shares to allocate to each task
CpuShares int64 `hcl:"cpu,optional"`
// How much memory to allocate to each task
MemoryLimit string `hcl:"memory,optional"`
}
// TaskLauncherConfig is the configuration structure for the task plugin.
type TaskLauncherConfig struct {
// A list of folders to mount to the container.
Binds []string `hcl:"binds,optional"`
// ClientConfig allow the user to specify the connection to the Docker
// engine. By default we try to load this from env vars:
// DOCKER_HOST to set the url to the docker server.
// DOCKER_API_VERSION to set the version of the API to reach, leave empty for latest.
// DOCKER_CERT_PATH to load the TLS certificates from.
// DOCKER_TLS_VERIFY to enable or disable TLS verification, off by default.
ClientConfig *ClientConfig `hcl:"client_config,block"`
// Force pull the image from the remote repository
ForcePull bool `hcl:"force_pull,optional"`
// A map of key/value pairs, stored in docker as a string. Each key/value pair must
// be unique. Validiation occurs at the docker layer, not in Waypoint. Label
// keys are alphanumeric strings which may contain periods (.) and hyphens (-).
// See the docker docs for more info: https://docs.docker.com/config/labels-custom-metadata/
Labels map[string]string `hcl:"labels,optional"`
// An array of strings with network names to connect the container to
Networks []string `hcl:"networks,optional"`
// Resources configures the resource constraints such as cpu and memory for the
// created containers.
Resources *TaskResources `hcl:"resources,block"`
// Environment variables that are meant to configure the application in a static
// way. This might be start an image in a specific mode,
// selected via environment variable. Most configuration should use the waypoint
// config commands.
StaticEnvVars map[string]string `hcl:"static_environment,optional"`
// Keep containers around after the task finishes. This allows the ability to debug
// the containers and see their logs with the native docker tools.
DebugContainers bool `hcl:"debug_containers,optional"`
}
func (b *TaskLauncher) Documentation() (*docs.Documentation, error) {
doc, err := docs.New(
docs.FromConfig(&TaskLauncherConfig{}),
docs.FromFunc(b.StartTaskFunc()),
)
if err != nil {
return nil, err
}
doc.Description(`
Launch a Docker container as a task.
If a Docker server is available (either locally or via environment variables
such as "DOCKER_HOST"), then it will be used to start the container.
`)
doc.Example(`
task {
use "docker" {
force_pull = true
}
}
`)
doc.SetField(
"binds",
"A 'source:destination' list of folders to mount onto the container from the host.",
docs.Summary(
"A list of folders to mount onto the container from the host. The expected",
"format for each string entry in the list is `source:destination`. So",
"for example: `binds: [\"host_folder/scripts:/scripts\"]`",
),
)
doc.SetField(
"labels",
"A map of key/value pairs to label the docker container with.",
docs.Summary(
"A map of key/value pair(s), stored in docker as a string. Each key/value pair must",
"be unique. Validiation occurs at the docker layer, not in Waypoint. Label",
"keys are alphanumeric strings which may contain periods (.) and hyphens (-).",
),
)
doc.SetField(
"networks",
"A list of strings with network names to connect the container to.",
docs.Default("waypoint"),
docs.Summary(
"A list of networks to connect the container to. By default the container",
"will always connect to the `waypoint` network.",
),
)
doc.SetField(
"resources",
"The resources that the tasks should use.",
docs.SubFields(func(d *docs.SubFieldDoc) {
d.SetField("cpu", "The cpu shares that the tasks should use")
d.SetField("memory", "The amount of memory to use. Defined as '512MB', '44kB', etc.")
}),
)
doc.SetField(
"static_environment",
"environment variables to expose to the application",
docs.Summary(
"These variables are used to control all of a container's modes,",
"such as configuring it to start a web app vs a background worker.",
"These environment variables should not be common",
"configuration variables normally set in `waypoint config`.",
),
)
return doc, nil
}
// TaskLauncher implements Configurable
func (b *TaskLauncher) Config() (interface{}, error) {
return &b.config, nil
}
func (b *TaskLauncher) setupImage(
ctx context.Context,
log hclog.Logger,
cli *client.Client,
img string,
) error {
args := filters.NewArgs()
args.Add("reference", img)
// only pull if image is not in current registry so check to see if the image is present
// if force then skip this check
if !b.config.ForcePull {
sum, err := cli.ImageList(context.Background(), types.ImageListOptions{Filters: args})
if err != nil {
return status.Errorf(codes.FailedPrecondition, "unable to list images in local Docker cache: %s", err)
}
log.Debug("image list", "images", len(sum))
// if we have images do not pull
if len(sum) > 0 {
log.Info("reusing existing image for task", "image", img, "id", sum[0].ID)
return nil
}
}
named, err := reference.ParseNormalizedNamed(img)
if err != nil {
return status.Errorf(codes.InvalidArgument, "unable to parse image name: %s", img)
}
img = named.Name()
out, err := cli.ImagePull(context.Background(), img, types.ImagePullOptions{})
if err != nil {
return status.Errorf(codes.FailedPrecondition, "unable to pull image: %s", err)
}
var stdout bytes.Buffer
err = jsonmessage.DisplayJSONMessagesStream(out, &stdout, 0, false, nil)
if err != nil {
log.Error("error pulling image for task", "image", img, "output", stdout.String())
return status.Errorf(codes.Internal, "unable to stream build logs to the terminal: %s", err)
} else {
log.Debug("finished pulling image for task", "image", img, "output", stdout.String())
}
log.Info("pulled image for task", "image", img)
return nil
}
func (b *TaskLauncher) setupNetworking(
ctx context.Context,
cli *client.Client,
) (string, error) {
nets, err := cli.NetworkList(ctx, types.NetworkListOptions{
Filters: filters.NewArgs(filters.Arg("label", "use=waypoint")),
})
if err != nil {
return "", status.Errorf(codes.FailedPrecondition, "unable to list Docker networks: %s", err)
}
if len(nets) > 1 {
// We use whichever network has the use=waypoint label, allowing the user to configure
// a network themselves with whatever name they wish.
return nets[0].Name, nil
}
// If we have a network already we're done. If we don't have a net, create it.
if len(nets) == 0 {
_, err = cli.NetworkCreate(ctx, "waypoint", types.NetworkCreate{
Driver: "bridge",
CheckDuplicate: true,
Internal: false,
Attachable: true,
Labels: map[string]string{
"use": "waypoint",
},
})
if err != nil {
return "", status.Errorf(codes.FailedPrecondition, "unable to create Docker network: %s", err)
}
}
return "waypoint", nil
}
// StopTask signals to docker to stop the container created previously
func (b *TaskLauncher) StopTask(
ctx context.Context,
log hclog.Logger,
ti *TaskInfo,
) error {
log = log.With("container_id", ti.Id)
log.Debug("connecting to Docker")
cli, err := wpdockerclient.NewClientWithOpts(client.FromEnv)
if err != nil {
return status.Errorf(codes.FailedPrecondition, "unable to create Docker client: %s", err)
}
cli.NegotiateAPIVersion(ctx)
log.Debug("stopping container")
if err := cli.ContainerStop(ctx, ti.Id, nil); err != nil {
// We're going to ignore this error other than logging it, just
// so we can try to remove it below. We want to do everything we can
// to remove this container.
log.Warn("error stopping container", "err", err)
}
// If we're debugging, we do NOT remove the container so that
// an operator can come in and inspect it.
if b.config.DebugContainers {
log.Info("not removing container, debug containers is enabled")
return nil
}
log.Debug("removing container")
if err := cli.ContainerRemove(ctx, ti.Id, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
if strings.Contains(err.Error(), "already in progress") {
// "removal of container already in progress" is the error when
// the daemon is removing this for some reason (auto remove or
// other). This is not an error.
err = nil
} else if strings.Contains(strings.ToLower(err.Error()), "no such container") {
// Container is already removed
err = nil
}
if err != nil {
log.Warn("error removing container", "err", err)
return err
}
}
return nil
}
// StartTask creates a docker container for the task.
func (b *TaskLauncher) StartTask(
ctx context.Context,
log hclog.Logger,
tli *component.TaskLaunchInfo,
) (*TaskInfo, error) {
cli, err := wpdockerclient.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "unable to create Docker client: %s", err)
}
cli.NegotiateAPIVersion(ctx)
err = b.setupImage(ctx, log, cli, tli.OciUrl)
if err != nil {
return nil, err
}
netName, err := b.setupNetworking(ctx, cli)
if err != nil {
return nil, err
}
randId, err := ulid.New(ulid.Now(), rand.Reader)
if err != nil {
return nil, err
}
name := fmt.Sprintf("waypoint-task-%s", randId)
var env []string
for k, v := range tli.EnvironmentVariables {
env = append(env, k+"="+v)
}
// This is here to help kaniko detect that this is a docker container.
// See https://github.com/GoogleContainerTools/kaniko/blob/7e3954ac734534ce5ce68ad6300a2d3143d82f40/vendor/github.com/genuinetools/bpfd/proc/proc.go#L138
// for more info.
env = append(env, "container=docker")
log.Debug(
"spawn docker container for task",
"oci-url", tli.OciUrl,
"arguments", tli.Arguments,
"environment", env,
"autoremove", !b.config.DebugContainers,
)
var memory int64
var cpuShares int64
if b.config.Resources != nil {
if b.config.Resources.MemoryLimit != "" {
memory, err = goUnits.FromHumanSize(b.config.Resources.MemoryLimit)
if err != nil {
return nil, err
}
}
cpuShares = b.config.Resources.CpuShares
}
cc, err := cli.ContainerCreate(
ctx,
&container.Config{
Env: env,
Cmd: tli.Arguments,
Entrypoint: tli.Entrypoint,
Image: tli.OciUrl,
},
&container.HostConfig{
Binds: append(
[]string{"/var/run/docker.sock:/var/run/docker.sock"},
b.config.Binds...,
),
AutoRemove: !b.config.DebugContainers,
Resources: container.Resources{
CPUShares: cpuShares,
Memory: memory,
},
},
&network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
netName: {},
},
},
nil,
name,
)
if err != nil {
return nil, err
}
if b.config.Networks != nil {
for _, net := range b.config.Networks {
err = cli.NetworkConnect(ctx, net, cc.ID, &network.EndpointSettings{})
if err != nil {
return nil, status.Errorf(
codes.Internal,
"unable to connect container to additional networks: %s",
err)
}
}
}
err = cli.ContainerStart(ctx, cc.ID, types.ContainerStartOptions{})
if err != nil {
return nil, err
}
log.Info("launched task container", "id", cc.ID, "name", name)
ti := &TaskInfo{
Id: cc.ID,
}
return ti, nil
}
// WatchTask implements TaskLauncher
func (p *TaskLauncher) WatchTask(
ctx context.Context,
log hclog.Logger,
ti *TaskInfo,
ui terminal.UI,
) (*component.TaskResult, error) {
cli, err := wpdockerclient.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, status.Errorf(codes.FailedPrecondition, "unable to create Docker client: %s", err)
}
cli.NegotiateAPIVersion(ctx)
// Accumulate our result on this
var result component.TaskResult
// Grab the logs reader
logsR, err := cli.ContainerLogs(ctx, ti.Id, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
})
if err != nil {
return nil, err
}
// Get our writers for the UI
outW, errW, err := ui.OutputWriters()
if err != nil {
return nil, err
}
// Start a goroutine to copy our logs. The goroutine will exit on its own
// when EOF or when this RPC ends because the UI will EOF.
logsDoneCh := make(chan struct{})
go func() {
defer close(logsDoneCh)
_, err := stdcopy.StdCopy(outW, errW, logsR)
if err != nil && err != io.EOF {
log.Warn("error reading container logs", "err", err)
ui.Output("Error reading container logs: %s", err, terminal.WithErrorStyle())
}
}()
// Wait for the container to exit
waitCh, errCh := cli.ContainerWait(ctx, ti.Id, container.WaitConditionNotRunning)
select {
case err := <-errCh:
// Error talking to Docker daemon.
return nil, err
case info := <-waitCh:
result.ExitCode = int(info.StatusCode)
// If we got an error, it is from the process (not Docker)
if err := info.Error; err != nil {
log.Warn("error from container process: %s", err.Message)
// We also write it to the UI so that it is more easily
// seen in UIs.
ui.Output("Error reported by container: %s", err.Message, terminal.WithErrorStyle())
}
// Wait for our logs to end
log.Debug("container exited, waiting for logs to finish", "code", info.StatusCode)
select {
case <-logsDoneCh:
case <-time.After(1 * time.Minute):
// They should never continue for 1 minute after the container
// exited. To avoid hanging a runner process, lets warn and exit.
log.Error("container logs never exited! please look into this")
}
}
return &result, nil
}
var _ component.TaskLauncher = (*TaskLauncher)(nil)