-
Notifications
You must be signed in to change notification settings - Fork 20
/
driver.go
646 lines (525 loc) · 16.2 KB
/
driver.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package main
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/oauth2"
"github.com/docker/go-plugins-helpers/volume"
metadata "github.com/linode/go-metadata"
"github.com/linode/linodego"
log "github.com/sirupsen/logrus"
)
type linodeVolumeDriver struct {
instanceID int
region string
linodeLabel string
linodeToken string
mountRoot string
mutex *sync.Mutex
linodeAPIPtr *linodego.Client
}
const (
fsTagPrefix = "docker-volume-filesystem-"
)
// Constructor
func newLinodeVolumeDriver(linodeLabel, linodeToken, mountRoot string) linodeVolumeDriver {
driver := linodeVolumeDriver{
linodeToken: linodeToken,
linodeLabel: linodeLabel,
mountRoot: mountRoot,
mutex: &sync.Mutex{},
}
if _, err := driver.linodeAPI(); err != nil {
log.Fatalf("Could not initialize Linode API: %s", err)
}
return driver
}
func (driver *linodeVolumeDriver) linodeAPI() (*linodego.Client, error) {
if driver.linodeToken == "" {
return nil, fmt.Errorf("Linode Token required. Set the token by calling \"docker plugin set <plugin-name> linode-token=<linode token>\"")
}
if driver.linodeAPIPtr != nil {
return driver.linodeAPIPtr, nil
}
driver.linodeAPIPtr = setupLinodeAPI(driver.linodeToken)
if driver.instanceID == 0 {
if err := driver.determineLinodeID(); err != nil {
driver.linodeAPIPtr = nil
return nil, err
}
}
return driver.linodeAPIPtr, nil
}
func setupLinodeAPI(token string) *linodego.Client {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
}
api := linodego.NewClient(oauth2Client)
ua := fmt.Sprintf("docker-volume-linode/%s linodego/%s", VERSION, linodego.Version)
api.SetUserAgent(ua)
return &api
}
func metadataServicesAvailable() bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:80", metadata.APIHost), 2*time.Second)
if err != nil {
return false
}
conn.Close()
return true
}
func (driver *linodeVolumeDriver) determineLinodeID() error {
if metadataServicesAvailable() {
err := driver.determineLinodeIDFromMetadata()
if err != nil {
log.Error(
"Failed to get linode info from Linode metadata service. " +
"Other methods will be used.",
)
}
}
if driver.linodeLabel == "" {
// If the label isn't defined, we should determine the IP through the network interface
log.Info("Using network interface to determine Linode ID")
if err := driver.determineLinodeIDFromNetworking(); err != nil {
return fmt.Errorf("Failed to determine Linode ID from networking: %s\n"+
"If this error continues to occur or if you are using a custom network configuration, "+
"consider using the `linode-label` flag.", err)
}
return nil
}
return driver.determineLinodeIDFromLabel()
}
func (driver *linodeVolumeDriver) determineLinodeIDFromMetadata() error {
client, err := metadata.NewClient(context.Background())
if err != nil {
return err
}
instanceInfo, err := client.GetInstance(context.Background())
if err != nil {
return err
}
driver.instanceID = instanceInfo.ID
driver.region = instanceInfo.Region
driver.linodeLabel = instanceInfo.Label
return nil
}
func (driver *linodeVolumeDriver) determineLinodeIDFromLabel() error {
jsonFilter, _ := json.Marshal(map[string]string{"label": driver.linodeLabel})
listOpts := linodego.NewListOptions(0, string(jsonFilter))
linodes, lErr := driver.linodeAPIPtr.ListInstances(context.Background(), listOpts)
if lErr != nil {
return fmt.Errorf("Could not determine Linode instance ID from Linode label %s due to error: %s", driver.linodeLabel, lErr)
} else if len(linodes) != 1 {
return fmt.Errorf("Could not determine Linode instance ID from Linode label %s", driver.linodeLabel)
}
driver.instanceID = linodes[0].ID
if driver.region == "" {
driver.region = linodes[0].Region
}
return nil
}
func (driver *linodeVolumeDriver) resolveMachineLinkLocal() (string, error) {
// We only want to filter on eth0 for Link Local.
iface, err := net.InterfaceByName("eth0")
if err != nil {
return "", err
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
if ifa, ok := addr.(*net.IPNet); ok {
if ifa.IP.To4() != nil {
continue
}
if !ifa.IP.IsLinkLocalUnicast() {
continue
}
return strings.Split(addr.String(), "/")[0], nil
}
}
return "", fmt.Errorf("no link local ipv6 address found")
}
func (driver *linodeVolumeDriver) determineLinodeIDFromNetworking() error {
linkLocal, err := driver.resolveMachineLinkLocal()
if err != nil {
return fmt.Errorf("failed to determine linode id from networking: %s", err)
}
instances, err := driver.linodeAPIPtr.ListInstances(context.Background(), nil)
if err != nil {
return fmt.Errorf("failed to list instances: %s", err)
}
for _, instance := range instances {
ips, err := driver.linodeAPIPtr.GetInstanceIPAddresses(context.Background(), instance.ID)
if err != nil {
return fmt.Errorf("failed to get ip addresses for instance %d: %s", instance.ID, err)
}
if ips.IPv6.LinkLocal == nil || ips.IPv6.LinkLocal.Address != linkLocal {
continue
}
driver.instanceID = instance.ID
driver.region = instance.Region
return nil
}
return fmt.Errorf("instance with link local address %s not found", linkLocal)
}
// Get implementation
func (driver *linodeVolumeDriver) Get(req *volume.GetRequest) (*volume.GetResponse, error) {
log.Infof("Get(%s)", req.Name)
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return nil, err
}
if linVol == nil {
return nil, fmt.Errorf("got a NIL volume. Volume may not exist")
}
mp := driver.labelToMountPoint(linVol.Label)
vol := linodeVolumeToDockerVolume(*linVol, mp)
resp := &volume.GetResponse{Volume: vol}
log.Infof("Get(): {Name: %s; Mountpoint: %s;}", vol.Name, vol.Mountpoint)
return resp, nil
}
// List implementation
func (driver *linodeVolumeDriver) List() (*volume.ListResponse, error) {
log.Infof("List()")
var jsonFilter []byte
var err error
//
api, err := driver.linodeAPI()
if err != nil {
return nil, err
}
//
var volumes []*volume.Volume
// filters
if jsonFilter, err = json.Marshal(map[string]string{"region": driver.region}); err != nil {
return nil, err
}
listOpts := linodego.NewListOptions(0, string(jsonFilter))
log.Debug("linode api listOpts: ", listOpts)
linVols, err := api.ListVolumes(context.Background(), listOpts)
if err != nil {
return nil, err
}
log.Debugf("Got %d volume count from api", len(linVols))
for _, linVol := range linVols {
mp := driver.labelToMountPoint(linVol.Label)
vol := linodeVolumeToDockerVolume(linVol, mp)
log.Debugf("Volume: %+v", vol)
volumes = append(volumes, vol)
}
log.Infof("List() returning %d volumes", len(volumes))
return &volume.ListResponse{Volumes: volumes}, nil
}
// Create implementation
func (driver *linodeVolumeDriver) Create(req *volume.CreateRequest) error {
log.Infof("Create(%s)", req.Name)
api, err := driver.linodeAPI()
if err != nil {
return err
}
driver.mutex.Lock()
defer driver.mutex.Unlock()
var size int
if sizeOpt, ok := req.Options["size"]; ok {
s, err := strconv.Atoi(sizeOpt)
if err != nil {
return fmt.Errorf("Invalid size")
}
size = s
}
createOpts := linodego.VolumeCreateOptions{
Label: req.Name,
Region: driver.region,
Size: size,
}
if fsOpt, ok := req.Options["filesystem"]; ok {
createOpts.Tags = append(createOpts.Tags, fsTagPrefix+fsOpt)
}
if deleteOpt, ok := req.Options["delete-on-remove"]; ok {
b, err := strconv.ParseBool(deleteOpt)
if err != nil {
return fmt.Errorf("Invalid delete-on-remove argument")
}
if b {
createOpts.Tags = append(createOpts.Tags, "docker-volume-delete-on-remove")
}
}
volume, err := api.CreateVolume(context.Background(), createOpts)
if err != nil {
return fmt.Errorf("Create(%s) Failed: %s", req.Name, err)
}
_, err = driver.linodeAPIPtr.WaitForVolumeStatus(
context.Background(), volume.ID, linodego.VolumeActive, 600,
)
if err != nil {
return fmt.Errorf(
"Failed to wait for volume %d to be active: %w", volume.ID, err,
)
}
return nil
}
// Remove implementation
func (driver *linodeVolumeDriver) Remove(req *volume.RemoveRequest) error {
driver.mutex.Lock()
defer driver.mutex.Unlock()
//
api, err := driver.linodeAPI()
if err != nil {
return err
}
//
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return err
}
// Send detach request
if err := detachAndWait(api, linVol.ID); err != nil {
return err
}
// Optionally send Delete request
for _, t := range linVol.Tags {
if t == "docker-volume-delete-on-remove" {
if err := api.DeleteVolume(context.Background(), linVol.ID); err != nil {
return err
}
break
}
}
return nil
}
// Mount implementation
func (driver *linodeVolumeDriver) Mount(req *volume.MountRequest) (*volume.MountResponse, error) {
log.Infof("Called Mount %s", req.Name)
api, err := driver.linodeAPI()
if err != nil {
return nil, err
}
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return nil, err
}
linVol, err = api.GetVolume(context.Background(), linVol.ID)
if err != nil {
return nil, err
}
// Ensure the volume is not currently mounted
if err := driver.ensureVolumeAttached(linVol.ID); err != nil {
return nil, fmt.Errorf("failed to attach volume: %s", err)
}
// wait for kernel to have block device available
if err := waitForDeviceFileExists(linVol.FilesystemPath, 300); err != nil {
return nil, err
}
// Format block device if no FS found
if GetFSType(linVol.FilesystemPath) == "" {
log.Infof("Formatting device:%s;", linVol.FilesystemPath)
filesystem := "ext4"
for _, tag := range linVol.Tags {
if strings.HasPrefix(tag, fsTagPrefix) {
filesystem = tag[len(fsTagPrefix):]
break
}
}
if err := Format(linVol.FilesystemPath, filesystem); err != nil {
return nil, err
}
}
// Create mount point using label (if not exists)
mp := driver.labelToMountPoint(linVol.Label)
if _, err := os.Stat(mp); os.IsNotExist(err) {
log.Infof("Creating mountpoint directory: %s", mp)
if err = os.MkdirAll(mp, 0o755); err != nil {
return nil, fmt.Errorf("Error creating mountpoint directory(%s): %s", mp, err)
}
}
if err := Mount(linVol.FilesystemPath, mp); err != nil {
return nil, fmt.Errorf("Error mounting volume(%s) to directory(%s): %s", linVol.FilesystemPath, mp, err)
}
log.Infof("Mount Call End: %s", req.Name)
return &volume.MountResponse{Mountpoint: mp}, nil
}
// Path implementation
func (driver *linodeVolumeDriver) Path(req *volume.PathRequest) (*volume.PathResponse, error) {
log.Infof("Path(%s)", req.Name)
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return nil, err
}
mp := driver.labelToMountPoint(linVol.Label)
log.Infof("Path(): %s", mp)
return &volume.PathResponse{Mountpoint: mp}, nil
}
// Unmount implementation
func (driver *linodeVolumeDriver) Unmount(req *volume.UnmountRequest) error {
api, err := driver.linodeAPI()
if err != nil {
return err
}
log.Infof("Unmount(%s)", req.Name)
linVol, err := driver.findVolumeByLabel(req.Name)
if err != nil {
return err
}
if err := Umount(driver.labelToMountPoint(linVol.Label)); err != nil {
return fmt.Errorf("Unable to Unmount(%s): %s", req.Name, err)
}
log.Infof("Unmount(): %s", req.Name)
// The volume is detached from the Linode at unmount
// to allow remote Linodes to infer whether a volume is
// mounted
if err := detachAndWait(api, linVol.ID); err != nil {
return err
}
return nil
}
// Capabilities implementation
func (driver *linodeVolumeDriver) Capabilities() *volume.CapabilitiesResponse {
log.Infof("Capabilities(): Scope: global")
return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "global"}}
}
// labelToMountPoint gets the mount-point for a volume
func (driver *linodeVolumeDriver) labelToMountPoint(volumeLabel string) string {
return path.Join(driver.mountRoot, volumeLabel)
}
// findVolumeByLabel looks up linode volume by label
func (driver *linodeVolumeDriver) findVolumeByLabel(volumeLabel string) (*linodego.Volume, error) {
var jsonFilter []byte
var err error
var linVols []linodego.Volume
api, err := driver.linodeAPI()
if err != nil {
return nil, err
}
if jsonFilter, err = json.Marshal(map[string]string{"label": volumeLabel, "region": driver.region}); err != nil {
return nil, err
}
listOpts := linodego.NewListOptions(0, string(jsonFilter))
if linVols, err = api.ListVolumes(context.Background(), listOpts); err != nil {
return nil, err
}
if len(linVols) != 1 {
return nil, fmt.Errorf("Instance %d Volume with name %s not found", driver.instanceID, volumeLabel)
}
return &linVols[0], nil
}
func detachAndWait(api *linodego.Client, volumeID int) error {
// Send detach request
if err := api.DetachVolume(context.Background(), volumeID); err != nil {
return fmt.Errorf("Error detaching volumeID(%d): %s", volumeID, err)
}
// Wait for linode to have the volume detached
if err := waitForLinodeVolumeDetachment(*api, volumeID, 180); err != nil {
return fmt.Errorf("Error waiting for detachment of volumeID(%d): %s", volumeID, err)
}
return nil
}
func attachAndWait(api *linodego.Client, volumeID int, linodeID int) error {
// attach
attachOpts := linodego.VolumeAttachOptions{LinodeID: linodeID}
if _, err := api.AttachVolume(context.Background(), volumeID, &attachOpts); err != nil {
return fmt.Errorf("Error attaching volume(%d) to linode(%d): %s", volumeID, linodeID, err)
}
if _, err := api.WaitForVolumeLinodeID(context.Background(), volumeID, &linodeID, 300); err != nil {
return fmt.Errorf("Error waiting for attachment of volume(%d) to linode(%d): %s", volumeID, linodeID, err)
}
return nil
}
// ensureVolumeAttached attempts to attach a volume to the current Linode instance
func (driver *linodeVolumeDriver) ensureVolumeAttached(volumeID int) error {
// TODO: validate whether a volume is in use in a local container
api, err := driver.linodeAPI()
if err != nil {
return err
}
// Wait for detachment if already detaching
if err := waitForVolumeNotBusy(api, volumeID); err != nil {
return err
}
// Fetch volume
vol, err := api.GetVolume(context.Background(), volumeID)
if err != nil {
return err
}
// If volume is already attached, do nothing
if vol.LinodeID != nil && *vol.LinodeID == driver.instanceID {
return nil
}
// Forcibly attach the volume if forceAttach is enabled
if forceAttach && vol.LinodeID != nil && *vol.LinodeID != driver.instanceID {
if err := detachAndWait(api, volumeID); err != nil {
return err
}
return attachAndWait(api, volumeID, driver.instanceID)
}
// Throw an error if the instance is not in an attachable state
if vol.LinodeID != nil && *vol.LinodeID != driver.instanceID {
return fmt.Errorf("failed to attach volume: volume is currently attached to linode %d", *vol.LinodeID)
}
return attachAndWait(api, volumeID, driver.instanceID)
}
// waitForVolumeNotBusy checks whether a volume is currently busy.
func waitForVolumeNotBusy(api *linodego.Client, volumeID int) error {
vol, err := api.GetVolume(context.Background(), volumeID)
if err != nil {
return err
}
if vol.LinodeID == nil {
return nil
}
filter := linodego.Filter{}
filter.AddField(linodego.Eq, "entity.id", volumeID)
filter.AddField(linodego.Eq, "entity.type", "volume")
filter.OrderBy = "created"
filter.Order = "desc"
detachFilterStr, err := filter.MarshalJSON()
if err != nil {
return err
}
events, err := api.ListEvents(context.Background(),
&linodego.ListOptions{Filter: string(detachFilterStr)})
if err != nil {
return err
}
for _, event := range events {
if event.Status != "started" {
continue
}
if err := waitForEventFinished(api, event.ID); err != nil {
return err
}
}
return nil
}
func waitForEventFinished(api *linodego.Client, eventID int) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(60)*time.Second)
defer cancel()
ticker := time.NewTicker(2000 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
event, err := api.GetEvent(ctx, eventID)
if err != nil {
return err
}
if event.Status == "finished" || event.Status == "failed" {
return nil
}
case <-ctx.Done():
return fmt.Errorf("error waiting for event(%d) completion: %v", eventID, ctx.Err())
}
}
}