Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOBS translate underscores #582

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .docs/user-guide/storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ dobs:
statusMaxAttempts: 10
statusInitialDelay: 100ms
statusTimeout: 2m
convertUnderscores: false
```

##### Configuration notes
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions drivers/storage/dobs/dobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
87 changes: 76 additions & 11 deletions drivers/storage/dobs/storage/dobs_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package storage
import (
"fmt"
"strconv"
"strings"
"time"

gofig "github.com/akutz/gofig/types"
Expand All @@ -25,12 +26,14 @@ const (
)

type driver struct {
name string
config gofig.Config
client *godo.Client
maxAttempts int
statusDelay int64
statusTimeout time.Duration
name string
config gofig.Config
client *godo.Client
maxAttempts int
statusDelay int64
statusTimeout time.Duration
defaultRegion *string
convUnderscore bool
}

func init() {
Expand Down Expand Up @@ -71,7 +74,11 @@ func (d *driver) Init(
"statusDelay": fmt.Sprintf(
"%v", time.Duration(d.statusDelay)*time.Nanosecond),
"statusTimeout": d.statusTimeout,
"region": d.config.GetString(do.ConfigRegion),
}

if region := d.config.GetString(do.ConfigRegion); region != "" {
d.defaultRegion = &region
fields["region"] = region
}

if token == "" {
Expand All @@ -86,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
Expand Down Expand Up @@ -155,22 +164,60 @@ func (d *driver) VolumeInspect(
return volume, nil
}

func (d *driver) VolumeInspectByName(
ctx types.Context,
volumeName string,
opts *types.VolumeInspectOpts) (*types.Volume, error) {

region := d.mustRegion(ctx)
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{
Region: *region,
Name: volumeName,
},
)
if err != nil {
return nil, err
}
if len(doVolumes) == 0 {
return nil, apiUtils.NewNotFoundError(volumeName)
}
if len(doVolumes) > 1 {
return nil, goof.New("too many volumes returned")
}

volume, err := d.toTypesVolume(ctx, &doVolumes[0], opts.Attachments)
if err != nil {
return nil, goof.New("error converting to types.Volume")
}
return volume, nil
}

func (d *driver) VolumeCreate(
ctx types.Context,
name string,
opts *types.VolumeCreateOpts) (*types.Volume, error) {

name = d.convUnderscores(name)
fields := map[string]interface{}{
"volumeName": name,
}

if opts.AvailabilityZone == nil || *opts.AvailabilityZone == "" {
instance, err := d.InstanceInspect(ctx, nil)
if err != nil {
return nil, err
opts.AvailabilityZone = d.mustRegion(ctx)
if opts.AvailabilityZone == nil || *opts.AvailabilityZone == "" {
return nil, goof.WithFields(fields,
"No region for volume create")
}
opts.AvailabilityZone = &instance.Region
}
fields["region"] = *opts.AvailabilityZone

if opts.Size == nil {
size := int64(minSizeGiB)
Expand All @@ -192,6 +239,8 @@ func (d *driver) VolumeCreate(

volume, _, err := d.client.Storage.CreateVolume(ctx, volumeReq)
if err != nil {
ctx.WithFields(fields).WithError(err).Error(
"error returned from create volume")
return nil, err
}

Expand Down Expand Up @@ -473,3 +522,19 @@ func (d *driver) waitForAction(
}
return nil
}

func (d *driver) mustRegion(ctx types.Context) *string {
if iid, ok := context.InstanceID(ctx); ok {
if v, ok := iid.Fields[do.InstanceIDFieldRegion]; ok && v != "" {
return &v
}
}
return d.defaultRegion
}

func (d *driver) convUnderscores(name string) string {
if d.convUnderscore {
name = strings.Replace(name, "_", "-", -1)
}
return name
}