Skip to content

Commit fdcb6a0

Browse files
Utilize metadata service to get Linode info; Wait for Volume Active on Creation (#146)
1 parent 00e5825 commit fdcb6a0

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.20-alpine as builder
1+
FROM golang:1.22-alpine as builder
22
ENV GO111MODULE=on
33
ARG VERSION=0
44
COPY . /docker-volume-linode

driver.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"golang.org/x/oauth2"
1717

1818
"github.com/docker/go-plugins-helpers/volume"
19+
metadata "github.com/linode/go-metadata"
1920
"github.com/linode/linodego"
2021
log "github.com/sirupsen/logrus"
2122
)
@@ -84,10 +85,30 @@ func setupLinodeAPI(token string) *linodego.Client {
8485
return &api
8586
}
8687

88+
func metadataServicesAvailable() bool {
89+
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:80", metadata.APIHost), 2*time.Second)
90+
if err != nil {
91+
return false
92+
}
93+
94+
conn.Close()
95+
return true
96+
}
97+
8798
func (driver *linodeVolumeDriver) determineLinodeID() error {
99+
if metadataServicesAvailable() {
100+
err := driver.determineLinodeIDFromMetadata()
101+
if err != nil {
102+
log.Error(
103+
"Failed to get linode info from Linode metadata service. " +
104+
"Other methods will be used.",
105+
)
106+
}
107+
}
108+
88109
if driver.linodeLabel == "" {
89110
// If the label isn't defined, we should determine the IP through the network interface
90-
log.Infof("Using network interface to determine Linode ID")
111+
log.Info("Using network interface to determine Linode ID")
91112

92113
if err := driver.determineLinodeIDFromNetworking(); err != nil {
93114
return fmt.Errorf("Failed to determine Linode ID from networking: %s\n"+
@@ -98,6 +119,28 @@ func (driver *linodeVolumeDriver) determineLinodeID() error {
98119
return nil
99120
}
100121

122+
return driver.determineLinodeIDFromLabel()
123+
}
124+
125+
func (driver *linodeVolumeDriver) determineLinodeIDFromMetadata() error {
126+
client, err := metadata.NewClient(context.Background())
127+
if err != nil {
128+
return err
129+
}
130+
131+
instanceInfo, err := client.GetInstance(context.Background())
132+
if err != nil {
133+
return err
134+
}
135+
136+
driver.instanceID = instanceInfo.ID
137+
driver.region = instanceInfo.Region
138+
driver.linodeLabel = instanceInfo.Label
139+
140+
return nil
141+
}
142+
143+
func (driver *linodeVolumeDriver) determineLinodeIDFromLabel() error {
101144
jsonFilter, _ := json.Marshal(map[string]string{"label": driver.linodeLabel})
102145
listOpts := linodego.NewListOptions(0, string(jsonFilter))
103146
linodes, lErr := driver.linodeAPIPtr.ListInstances(context.Background(), listOpts)
@@ -292,7 +335,6 @@ func (driver *linodeVolumeDriver) Create(req *volume.CreateRequest) error {
292335

293336
// Remove implementation
294337
func (driver *linodeVolumeDriver) Remove(req *volume.RemoveRequest) error {
295-
296338
driver.mutex.Lock()
297339
defer driver.mutex.Unlock()
298340

@@ -374,7 +416,7 @@ func (driver *linodeVolumeDriver) Mount(req *volume.MountRequest) (*volume.Mount
374416
mp := driver.labelToMountPoint(linVol.Label)
375417
if _, err := os.Stat(mp); os.IsNotExist(err) {
376418
log.Infof("Creating mountpoint directory: %s", mp)
377-
if err = os.MkdirAll(mp, 0755); err != nil {
419+
if err = os.MkdirAll(mp, 0o755); err != nil {
378420
return nil, fmt.Errorf("Error creating mountpoint directory(%s): %s", mp, err)
379421
}
380422
}
@@ -556,7 +598,6 @@ func waitForVolumeNotBusy(api *linodego.Client, volumeID int) error {
556598
filter.Order = "desc"
557599

558600
detachFilterStr, err := filter.MarshalJSON()
559-
560601
if err != nil {
561602
return err
562603
}

fs_utils_linux.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"os/exec"
5-
65
"strings"
76

87
log "github.com/sirupsen/logrus"

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.21
44

55
require (
66
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651
7+
github.com/linode/go-metadata v0.2.0
78
github.com/linode/linodego v1.32.0
89
github.com/sirupsen/logrus v1.9.3
910
golang.org/x/oauth2 v0.19.0
@@ -12,7 +13,7 @@ require (
1213
require (
1314
github.com/Microsoft/go-winio v0.6.1 // indirect
1415
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
15-
github.com/docker/go-connections v0.4.0 // indirect
16+
github.com/docker/go-connections v0.5.0 // indirect
1617
github.com/go-resty/resty/v2 v2.12.0 // indirect
1718
golang.org/x/mod v0.8.0 // indirect
1819
golang.org/x/net v0.22.0 // indirect

go.sum

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8-
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
9-
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
8+
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
9+
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
1010
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 h1:YcvzLmdrP/b8kLAGJ8GT7bdncgCAiWxJZIlt84D+RJg=
1111
github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA=
1212
github.com/go-resty/resty/v2 v2.12.0 h1:rsVL8P90LFvkUYq/V5BTVe203WfRIU4gvcf+yfzJzGA=
@@ -15,6 +15,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1515
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1616
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
1717
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
18+
github.com/linode/go-metadata v0.2.0 h1:hlWzkYLa80ikA0NmFX2hcwhcnWFol8F3UIvJnOgdKw4=
19+
github.com/linode/go-metadata v0.2.0/go.mod h1:XraDbSwms0+CtA7/Qh7agkSvGDc6H0s782kpX9MdMu0=
1820
github.com/linode/linodego v1.32.0 h1:OmZzB3iON6uu84VtLFf64uKmAQqJJarvmsVguroioPI=
1921
github.com/linode/linodego v1.32.0/go.mod h1:y8GDP9uLVH4jTB9qyrgw79qfKdYJmNCGUOJmfuiOcmI=
2022
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -82,5 +84,6 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
8284
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8385
gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI=
8486
gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
85-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
8687
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
88+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
89+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)