Skip to content

Commit

Permalink
Add option to translate _ to - for GCEPD
Browse files Browse the repository at this point in the history
New config option, gcepd.convertUnderscores, enables the GCEPD 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.
  • Loading branch information
codenrhoden committed Jun 21, 2017
1 parent f30c7cf commit 28dad55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions drivers/storage/gcepd/gcepd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const (
// Name is the provider's name.
Name = "gcepd"

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 @@ -61,6 +62,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 GCE naming requirements
ConfigConvertUnderscores = Name + ".convertUnderscores"
)

func init() {
Expand All @@ -80,6 +86,8 @@ func init() {
ConfigStatusInitDelay)
r.Key(gofig.String, "", defaultStatusTimeout, "Status Timeout",
ConfigStatusTimeout)
r.Key(gofig.Bool, "", defaultConvertUnderscores,
"Convert Underscores", ConfigConvertUnderscores)

gofigCore.Register(r)
}
14 changes: 14 additions & 0 deletions drivers/storage/gcepd/storage/gce_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type driver struct {
maxAttempts int
statusDelay int64
statusTimeout time.Duration
convUnderscore bool
}

func init() {
Expand Down Expand Up @@ -142,6 +143,8 @@ func (d *driver) Init(context types.Context, config gofig.Config) error {
return err
}

d.convUnderscore = d.config.GetBool(gcepd.ConfigConvertUnderscores)

context.Info("storage driver initialized")
return nil
}
Expand Down Expand Up @@ -341,6 +344,8 @@ func (d *driver) VolumeInspectByName(
volumeName string,
opts *types.VolumeInspectOpts) (*types.Volume, error) {

volumeName = d.convUnderscores(volumeName)

// For GCE, name and ID are the same
return d.VolumeInspect(
ctx,
Expand Down Expand Up @@ -385,6 +390,8 @@ func (d *driver) VolumeCreate(

}

volumeName = d.convUnderscores(volumeName)
fields["volumeName"] = volumeName
if !utils.IsValidDiskName(&volumeName) {
return nil, goof.WithFields(fields,
"Volume name does not meet GCE naming requirements")
Expand Down Expand Up @@ -1072,6 +1079,13 @@ func (d *driver) detachVolume(
return asyncErr
}

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

func getLabels(tag *string) map[string]string {
labels := map[string]string{
tagKey: *tag,
Expand Down

0 comments on commit 28dad55

Please sign in to comment.