Skip to content

Commit

Permalink
Utilize metadata service to get Linode info; Wait for Volume Active o…
Browse files Browse the repository at this point in the history
…n Creation (#146)
  • Loading branch information
zliang-akamai authored Apr 10, 2024
1 parent 00e5825 commit fdcb6a0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
49 changes: 45 additions & 4 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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"+
Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -556,7 +598,6 @@ func waitForVolumeNotBusy(api *linodego.Client, volumeID int) error {
filter.Order = "desc"

detachFilterStr, err := filter.MarshalJSON()

if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion fs_utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"os/exec"

"strings"

log "github.com/sirupsen/logrus"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=

0 comments on commit fdcb6a0

Please sign in to comment.