From 93f388ee10ae1f7c0fd6cb2f2779f8e4b635c6b0 Mon Sep 17 00:00:00 2001 From: Travis Rhoden Date: Thu, 22 Jun 2017 10:34:03 -0700 Subject: [PATCH] Add option to tranlate _ to - for DOBS New config option, dobs.convertUnderscores, enables the DOBS driver to replace any underscores with dashes during volume creation. When a VolumeInspectByName request comes in, that name is also converted, and the volume that was created with the dashes is returned. It is important to note that the returned data from the driver is always correct -- the name is never displayed as having underscores instead of dashes, because no such volume exists. --- .docs/user-guide/storage-providers.md | 7 +++++ drivers/storage/dobs/dobs.go | 12 ++++++-- drivers/storage/dobs/storage/dobs_storage.go | 29 +++++++++++++++----- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/.docs/user-guide/storage-providers.md b/.docs/user-guide/storage-providers.md index 61f08017..d391d149 100644 --- a/.docs/user-guide/storage-providers.md +++ b/.docs/user-guide/storage-providers.md @@ -817,6 +817,7 @@ dobs: statusMaxAttempts: 10 statusInitialDelay: 100ms statusTimeout: 2m + convertUnderscores: false ``` ##### Configuration notes @@ -831,6 +832,12 @@ dobs: - `statusTimeout` is a maximum length of time that polling for volume status can occur. This serves as a backstop against a stuck request of malfunctioning API that never returns. +- `convertUnderscores` is a boolean flag that controls whether the driver will + automatically convert underscores to dashes during a volume create request. + Digital Ocean does not allow underscores in the volume name, but some + container orchestrators (e.g. Docker Swarm) automatically prefix volume names + with a string containing a dash. This flag enables such requests to proceed, + but with the volume name modified. !!! note The DigitalOcean service currently only supports block storage volumes in diff --git a/drivers/storage/dobs/dobs.go b/drivers/storage/dobs/dobs.go index 0c89f656..7771f777 100644 --- a/drivers/storage/dobs/dobs.go +++ b/drivers/storage/dobs/dobs.go @@ -11,8 +11,9 @@ const ( // Name is the name of the driver Name = "dobs" - defaultStatusMaxAttempts = 10 - defaultStatusInitDelay = "100ms" + defaultStatusMaxAttempts = 10 + defaultStatusInitDelay = "100ms" + defaultConvertUnderscores = false /* This is hard deadline when waiting for the volume status to change to a desired state. At minimum is has to be more than the expontential @@ -54,6 +55,11 @@ const ( // ConfigStatusTimeout is the key for the time duration for a timeout // on how long to wait for a desired volume status to appears ConfigStatusTimeout = Name + ".statusTimeout" + + // ConfigConvertUnderscores is the key for a boolean flag on whether + // incoming requests that have names with underscores should be + // converted to dashes to satisfy DO naming requirements + ConfigConvertUnderscores = Name + ".convertUnderscores" ) func init() { @@ -72,5 +78,7 @@ func registerConfig() { ConfigStatusInitDelay) r.Key(gofig.String, "", defaultStatusTimeout, "Status Timeout", ConfigStatusTimeout) + r.Key(gofig.Bool, "", defaultConvertUnderscores, + "Convert Underscores", ConfigConvertUnderscores) gofigCore.Register(r) } diff --git a/drivers/storage/dobs/storage/dobs_storage.go b/drivers/storage/dobs/storage/dobs_storage.go index 517c0c84..b0fa526f 100644 --- a/drivers/storage/dobs/storage/dobs_storage.go +++ b/drivers/storage/dobs/storage/dobs_storage.go @@ -5,6 +5,7 @@ package storage import ( "fmt" "strconv" + "strings" "time" gofig "github.com/akutz/gofig/types" @@ -25,13 +26,14 @@ const ( ) type driver struct { - name string - config gofig.Config - client *godo.Client - maxAttempts int - statusDelay int64 - statusTimeout time.Duration - defaultRegion *string + name string + config gofig.Config + client *godo.Client + maxAttempts int + statusDelay int64 + statusTimeout time.Duration + defaultRegion *string + convUnderscore bool } func init() { @@ -91,6 +93,8 @@ func (d *driver) Init( } d.client = client + d.convUnderscore = d.config.GetBool(do.ConfigConvertUnderscores) + ctx.WithFields(fields).Info("storage driver initialized") return nil @@ -169,6 +173,9 @@ func (d *driver) VolumeInspectByName( if region == nil || *region == "" { return nil, goof.New("No region provided or configured") } + + volumeName = d.convUnderscores(volumeName) + doVolumes, _, err := d.client.Storage.ListVolumes( ctx, &godo.ListVolumeParams{ @@ -198,6 +205,7 @@ func (d *driver) VolumeCreate( name string, opts *types.VolumeCreateOpts) (*types.Volume, error) { + name = d.convUnderscores(name) fields := map[string]interface{}{ "volumeName": name, } @@ -523,3 +531,10 @@ func (d *driver) mustRegion(ctx types.Context) *string { } return d.defaultRegion } + +func (d *driver) convUnderscores(name string) string { + if d.convUnderscore { + name = strings.Replace(name, "_", "-", -1) + } + return name +}