-
Notifications
You must be signed in to change notification settings - Fork 405
/
docker_client.go
415 lines (354 loc) · 14.1 KB
/
docker_client.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
// +build go1.7
package docker
import (
"context"
"errors"
"fmt"
"hash/fnv"
"io"
"os"
"path/filepath"
"strconv"
"time"
"github.com/fnproject/fn/api/common"
"github.com/fsouza/go-dockerclient"
"github.com/sirupsen/logrus"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
"golang.org/x/time/rate"
)
type dockerClient interface {
// Each of these are github.com/fsouza/go-dockerclient methods
AttachToContainerNonBlocking(ctx context.Context, opts docker.AttachToContainerOptions) (docker.CloseWaiter, error)
WaitContainerWithContext(id string, ctx context.Context) (int, error)
StartContainerWithContext(id string, hostConfig *docker.HostConfig, ctx context.Context) error
KillContainer(opts docker.KillContainerOptions) error
CreateContainer(opts docker.CreateContainerOptions) (*docker.Container, error)
RemoveContainer(opts docker.RemoveContainerOptions) error
PauseContainer(id string, ctx context.Context) error
UnpauseContainer(id string, ctx context.Context) error
PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error
InspectImage(ctx context.Context, name string) (*docker.Image, error)
ListImages(opts docker.ListImagesOptions) ([]docker.APIImages, error)
RemoveImage(id string, opts docker.RemoveImageOptions) error
Stats(opts docker.StatsOptions) error
Info(ctx context.Context) (*docker.DockerInfo, error)
LoadImages(ctx context.Context, filePath string) error
ListContainers(opts docker.ListContainersOptions) ([]docker.APIContainers, error)
AddEventListener(ctx context.Context) (chan *docker.APIEvents, error)
RemoveEventListener(ctx context.Context, listener chan *docker.APIEvents) error
}
// TODO: switch to github.com/docker/engine-api
func newClient(ctx context.Context) dockerClient {
// TODO this was much easier, don't need special settings at the moment
// docker, err := docker.NewClient(conf.Docker)
client, err := docker.NewClientFromEnv()
if err != nil {
logrus.WithError(err).Fatal("couldn't create docker client")
}
if err := client.Ping(); err != nil {
logrus.WithError(err).Fatal("couldn't connect to docker daemon")
}
wrap := &dockerWrap{docker: client}
go wrap.listenEventLoop(ctx)
return wrap
}
type dockerWrap struct {
docker *docker.Client
}
var (
apiNameKey = common.MakeKey("api_name")
apiStatusKey = common.MakeKey("api_status")
exitStatusKey = common.MakeKey("exit_status")
eventActionKey = common.MakeKey("event_action")
eventTypeKey = common.MakeKey("event_type")
dockerRetriesMeasure = common.MakeMeasure("docker_api_retries", "docker api retries", "")
dockerExitMeasure = common.MakeMeasure("docker_exits", "docker exit counts", "")
// WARNING: this metric reports total latency per *wrapper* call, which will add up multiple retry latencies per wrapper call.
dockerLatencyMeasure = common.MakeMeasure("docker_api_latency", "Docker wrapper latency", "msecs")
dockerEventsMeasure = common.MakeMeasure("docker_events", "docker events", "")
imageCleanerBusyImgCount = common.MakeMeasure("image_cleaner_busy_img_count", "image cleaner busy image count", "")
imageCleanerBusyImgSize = common.MakeMeasure("image_cleaner_busy_img_size", "image cleaner busy image total size", "By")
imageCleanerIdleImgCount = common.MakeMeasure("image_cleaner_idle_img_count", "image cleaner idle image count", "")
imageCleanerIdleImgSize = common.MakeMeasure("image_cleaner_idle_img_size", "image cleaner idle image total size", "By")
imageCleanerMaxImgSize = common.MakeMeasure("image_cleaner_max_img_size", "image cleaner image max size", "By")
dockerInstanceId = common.MakeMeasure("docker_instance_id", "docker instance id", "")
)
func RecordInstanceId(ctx context.Context, id string) {
h := fnv.New64()
h.Write([]byte(id))
hid := int64(h.Sum64())
stats.Record(ctx, dockerInstanceId.M(hid))
}
func RecordImageCleanerStats(ctx context.Context, sample *ImageCacherStats) {
stats.Record(ctx, imageCleanerBusyImgCount.M(int64(sample.BusyImgCount)))
stats.Record(ctx, imageCleanerBusyImgSize.M(int64(sample.BusyImgTotalSize)))
stats.Record(ctx, imageCleanerIdleImgCount.M(int64(sample.IdleImgCount)))
stats.Record(ctx, imageCleanerIdleImgSize.M(int64(sample.IdleImgTotalSize)))
stats.Record(ctx, imageCleanerMaxImgSize.M(int64(sample.MaxImgTotalSize)))
}
// listenEventLoop listens for docker events and reconnects if necessary
func (d *dockerWrap) listenEventLoop(ctx context.Context) {
limiter := rate.NewLimiter(2.0, 1)
for limiter.Wait(ctx) == nil {
err := d.listenEvents(ctx)
if err != nil {
logrus.WithError(err).Error("listenEvents failed, will retry...")
}
}
}
// listenEvents registers an event listener to docker to stream docker events
// and records these in stats.
func (d *dockerWrap) listenEvents(ctx context.Context) error {
listener, err := d.AddEventListener(ctx)
if err != nil {
return err
}
defer d.RemoveEventListener(ctx, listener)
for {
select {
case ev := <-listener:
if ev == nil {
return errors.New("event listener closed")
}
ctx, err := tag.New(context.Background(),
tag.Upsert(eventActionKey, ev.Action),
tag.Upsert(eventTypeKey, ev.Type),
)
if err != nil {
logrus.WithError(err).Fatalf("cannot add event tags %v=%v %v=%v",
eventActionKey, ev.Action,
eventTypeKey, ev.Type,
)
}
stats.Record(ctx, dockerEventsMeasure.M(0))
case <-ctx.Done():
return nil
}
}
}
// record a retry attempt for the api with status/reason provided
func recordRetry(ctx context.Context, apiName, apiStatus string) {
ctx, err := tag.New(ctx,
tag.Upsert(apiNameKey, apiName),
tag.Upsert(apiStatusKey, apiStatus),
)
if err != nil {
logrus.WithError(err).Fatalf("cannot add tags %v=%v %v=%v", apiNameKey, apiName, apiStatusKey, apiStatus)
}
stats.Record(ctx, dockerRetriesMeasure.M(0))
}
// Create a span/tracker with required context tags
func makeTracker(ctx context.Context, name string) (context.Context, func(error)) {
ctx, err := tag.New(ctx, tag.Upsert(apiNameKey, name))
if err != nil {
logrus.WithError(err).Fatalf("cannot add tag %v=%v", apiNameKey, name)
}
// It would have been nice to pull the latency (end-start) elapsed time
// from Spans but this is hidden from us, so we have to call time.Now()
// twice ourselves.
ctx, span := trace.StartSpan(ctx, name)
start := time.Now()
return ctx, func(err error) {
status := "ok"
if err != nil {
if err == context.Canceled {
status = "canceled"
} else if err == context.DeadlineExceeded {
status = "timeout"
} else if derr, ok := err.(*docker.Error); ok {
status = strconv.FormatInt(int64(derr.Status), 10)
} else {
status = "error"
}
}
ctx, err := tag.New(ctx, tag.Upsert(apiStatusKey, status))
if err != nil {
logrus.WithError(err).Fatalf("cannot add tag %v=%v", apiStatusKey, status)
}
stats.Record(ctx, dockerLatencyMeasure.M(int64(time.Now().Sub(start)/time.Millisecond)))
span.End()
}
}
func RecordWaitContainerResult(ctx context.Context, exitCode int) {
// Tag the metric with error-code or context-cancel/deadline info
exitStr := fmt.Sprintf("exit_%d", exitCode)
if exitCode == 0 && ctx.Err() != nil {
switch ctx.Err() {
case context.DeadlineExceeded:
exitStr = "ctx_deadline"
case context.Canceled:
exitStr = "ctx_canceled"
}
}
newCtx, err := tag.New(ctx,
tag.Upsert(apiNameKey, "docker_wait_container"),
tag.Upsert(exitStatusKey, exitStr),
)
if err != nil {
logrus.WithError(err).Fatalf("cannot add tag %v=%v or tag %v=docker_wait_container", exitStatusKey, exitStr, apiNameKey)
}
stats.Record(newCtx, dockerExitMeasure.M(0))
}
// RegisterViews creates and registers views with provided tag keys
func RegisterViews(tagKeys []string, latencyDist []float64) {
defaultTags := []tag.Key{apiNameKey, apiStatusKey}
exitTags := []tag.Key{apiNameKey, exitStatusKey}
eventTags := []tag.Key{eventActionKey, eventTypeKey}
// add extra tags if not already in default tags for req/resp
for _, key := range tagKeys {
if key != "api_name" && key != "api_status" {
defaultTags = append(defaultTags, common.MakeKey(key))
}
if key != "api_name" && key != "exit_status" {
exitTags = append(exitTags, common.MakeKey(key))
}
}
// docker instance tags
emptyTags := []tag.Key{}
err := view.Register(
common.CreateViewWithTags(dockerRetriesMeasure, view.Count(), defaultTags),
common.CreateViewWithTags(dockerExitMeasure, view.Count(), exitTags),
common.CreateViewWithTags(dockerLatencyMeasure, view.Distribution(latencyDist...), defaultTags),
common.CreateViewWithTags(dockerEventsMeasure, view.Count(), eventTags),
common.CreateViewWithTags(imageCleanerBusyImgCount, view.LastValue(), emptyTags),
common.CreateViewWithTags(imageCleanerBusyImgSize, view.LastValue(), emptyTags),
common.CreateViewWithTags(imageCleanerIdleImgCount, view.LastValue(), emptyTags),
common.CreateViewWithTags(imageCleanerIdleImgSize, view.LastValue(), emptyTags),
common.CreateViewWithTags(imageCleanerMaxImgSize, view.LastValue(), emptyTags),
common.CreateViewWithTags(dockerInstanceId, view.LastValue(), emptyTags),
)
if err != nil {
logrus.WithError(err).Fatal("cannot register view")
}
}
func (d *dockerWrap) AddEventListener(ctx context.Context) (listen chan *docker.APIEvents, err error) {
ctx, closer := makeTracker(ctx, "docker_add_event_listener")
defer func() { closer(err) }()
listen = make(chan *docker.APIEvents)
err = d.docker.AddEventListener(listen)
if err != nil {
return nil, err
}
return listen, nil
}
func (d *dockerWrap) RemoveEventListener(ctx context.Context, listener chan *docker.APIEvents) (err error) {
_, closer := makeTracker(ctx, "docker_remove_event_listener")
defer func() { closer(err) }()
err = d.docker.RemoveEventListener(listener)
return err
}
func (d *dockerWrap) ListContainers(opts docker.ListContainersOptions) (containers []docker.APIContainers, err error) {
_, closer := makeTracker(opts.Context, "docker_list_containers")
defer func() { closer(err) }()
containers, err = d.docker.ListContainers(opts)
return containers, err
}
func (d *dockerWrap) LoadImages(ctx context.Context, filePath string) (err error) {
ctx, closer := makeTracker(ctx, "docker_load_images")
defer func() { closer(err) }()
file, err := os.Open(filepath.Clean(filePath))
if err != nil {
return err
}
defer file.Close()
// No retries here. LoadImage is typically called at startup and we fail/timeout
// at first attempt.
err = d.docker.LoadImage(docker.LoadImageOptions{
InputStream: file,
Context: ctx,
})
return err
}
func (d *dockerWrap) ListImages(opts docker.ListImagesOptions) (images []docker.APIImages, err error) {
_, closer := makeTracker(opts.Context, "docker_list_images")
defer func() { closer(err) }()
images, err = d.docker.ListImages(opts)
return images, err
}
func (d *dockerWrap) Info(ctx context.Context) (info *docker.DockerInfo, err error) {
_, closer := makeTracker(ctx, "docker_info")
defer func() { closer(err) }()
info, err = d.docker.Info()
return info, err
}
func (d *dockerWrap) AttachToContainerNonBlocking(ctx context.Context, opts docker.AttachToContainerOptions) (w docker.CloseWaiter, err error) {
_, closer := makeTracker(ctx, "docker_attach_container")
defer func() { closer(err) }()
w, err = d.docker.AttachToContainerNonBlocking(opts)
return w, err
}
func (d *dockerWrap) WaitContainerWithContext(id string, ctx context.Context) (code int, err error) {
ctx, closer := makeTracker(ctx, "docker_wait_container")
defer func() { closer(err) }()
code, err = d.docker.WaitContainerWithContext(id, ctx)
if err == context.Canceled {
err = nil // ignore ctx.cancel since every normal wait lifecycle ends with cancel
}
return code, err
}
func (d *dockerWrap) StartContainerWithContext(id string, hostConfig *docker.HostConfig, ctx context.Context) (err error) {
ctx, closer := makeTracker(ctx, "docker_start_container")
defer func() { closer(err) }()
err = d.docker.StartContainerWithContext(id, hostConfig, ctx)
return err
}
func (d *dockerWrap) CreateContainer(opts docker.CreateContainerOptions) (c *docker.Container, err error) {
_, closer := makeTracker(opts.Context, "docker_create_container")
defer func() { closer(err) }()
c, err = d.docker.CreateContainer(opts)
return c, err
}
func (d *dockerWrap) KillContainer(opts docker.KillContainerOptions) (err error) {
_, closer := makeTracker(opts.Context, "docker_kill_container")
defer func() { closer(err) }()
err = d.docker.KillContainer(opts)
return err
}
func (d *dockerWrap) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) (err error) {
_, closer := makeTracker(opts.Context, "docker_pull_image")
defer func() { closer(err) }()
err = d.docker.PullImage(opts, auth)
return err
}
func (d *dockerWrap) RemoveImage(image string, opts docker.RemoveImageOptions) (err error) {
_, closer := makeTracker(opts.Context, "docker_remove_image")
defer func() { closer(err) }()
err = d.docker.RemoveImageExtended(image, opts)
return err
}
func (d *dockerWrap) RemoveContainer(opts docker.RemoveContainerOptions) (err error) {
_, closer := makeTracker(opts.Context, "docker_remove_container")
defer func() { closer(err) }()
err = d.docker.RemoveContainer(opts)
return err
}
func (d *dockerWrap) PauseContainer(id string, ctx context.Context) (err error) {
_, closer := makeTracker(ctx, "docker_pause_container")
defer func() { closer(err) }()
err = d.docker.PauseContainer(id)
return err
}
func (d *dockerWrap) UnpauseContainer(id string, ctx context.Context) (err error) {
_, closer := makeTracker(ctx, "docker_unpause_container")
defer func() { closer(err) }()
err = d.docker.UnpauseContainer(id)
return err
}
func (d *dockerWrap) InspectImage(ctx context.Context, name string) (img *docker.Image, err error) {
_, closer := makeTracker(ctx, "docker_inspect_image")
defer func() { closer(err) }()
img, err = d.docker.InspectImage(name)
return img, err
}
func (d *dockerWrap) Stats(opts docker.StatsOptions) (err error) {
_, closer := makeTracker(opts.Context, "docker_stats")
defer func() { closer(err) }()
err = d.docker.Stats(opts)
if err == io.ErrClosedPipe {
err = nil // ignore io closed pipe errors since every normal stats streaming lifecycle ends with io pipe close
}
return err
}