From fdcb6a0d45468c4615d83fd5cfe385d56bda3773 Mon Sep 17 00:00:00 2001 From: Zhiwei Liang <121905282+zliang-akamai@users.noreply.github.com> Date: Wed, 10 Apr 2024 11:30:05 -0400 Subject: [PATCH] Utilize metadata service to get Linode info; Wait for Volume Active on Creation (#146) --- Dockerfile | 2 +- driver.go | 49 +++++++++++++++++++++++++++++++++++++++++++---- fs_utils_linux.go | 1 - go.mod | 3 ++- go.sum | 9 ++++++--- 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4590e0a..696f3af 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.20-alpine as builder +FROM golang:1.22-alpine as builder ENV GO111MODULE=on ARG VERSION=0 COPY . /docker-volume-linode diff --git a/driver.go b/driver.go index 0397b72..a0dc1b6 100644 --- a/driver.go +++ b/driver.go @@ -16,6 +16,7 @@ import ( "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" ) @@ -84,10 +85,30 @@ func setupLinodeAPI(token string) *linodego.Client { 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.Infof("Using network interface to determine Linode ID") + 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"+ @@ -98,6 +119,28 @@ func (driver *linodeVolumeDriver) determineLinodeID() error { 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) @@ -292,7 +335,6 @@ func (driver *linodeVolumeDriver) Create(req *volume.CreateRequest) error { // Remove implementation func (driver *linodeVolumeDriver) Remove(req *volume.RemoveRequest) error { - driver.mutex.Lock() defer driver.mutex.Unlock() @@ -374,7 +416,7 @@ func (driver *linodeVolumeDriver) Mount(req *volume.MountRequest) (*volume.Mount 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, 0755); err != nil { + if err = os.MkdirAll(mp, 0o755); err != nil { return nil, fmt.Errorf("Error creating mountpoint directory(%s): %s", mp, err) } } @@ -556,7 +598,6 @@ func waitForVolumeNotBusy(api *linodego.Client, volumeID int) error { filter.Order = "desc" detachFilterStr, err := filter.MarshalJSON() - if err != nil { return err } diff --git a/fs_utils_linux.go b/fs_utils_linux.go index 9dfd556..96a5fbf 100644 --- a/fs_utils_linux.go +++ b/fs_utils_linux.go @@ -2,7 +2,6 @@ package main import ( "os/exec" - "strings" log "github.com/sirupsen/logrus" diff --git a/go.mod b/go.mod index b58e28b..b6983b9 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 + github.com/linode/go-metadata v0.2.0 github.com/linode/linodego v1.32.0 github.com/sirupsen/logrus v1.9.3 golang.org/x/oauth2 v0.19.0 @@ -12,7 +13,7 @@ require ( require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect - github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-connections v0.5.0 // indirect github.com/go-resty/resty/v2 v2.12.0 // indirect golang.org/x/mod v0.8.0 // indirect golang.org/x/net v0.22.0 // indirect diff --git a/go.sum b/go.sum index feec909..b12977c 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,8 @@ github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf/go.mod h1:F5haX7 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= -github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c= +github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc= github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651 h1:YcvzLmdrP/b8kLAGJ8GT7bdncgCAiWxJZIlt84D+RJg= github.com/docker/go-plugins-helpers v0.0.0-20211224144127-6eecb7beb651/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA= 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= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww= github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg= +github.com/linode/go-metadata v0.2.0 h1:hlWzkYLa80ikA0NmFX2hcwhcnWFol8F3UIvJnOgdKw4= +github.com/linode/go-metadata v0.2.0/go.mod h1:XraDbSwms0+CtA7/Qh7agkSvGDc6H0s782kpX9MdMu0= github.com/linode/linodego v1.32.0 h1:OmZzB3iON6uu84VtLFf64uKmAQqJJarvmsVguroioPI= github.com/linode/linodego v1.32.0/go.mod h1:y8GDP9uLVH4jTB9qyrgw79qfKdYJmNCGUOJmfuiOcmI= 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 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI= gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=