From 28dad550698b0acff3e38207d5bd234fd2001bbc Mon Sep 17 00:00:00 2001 From: Travis Rhoden Date: Wed, 21 Jun 2017 11:25:01 -0700 Subject: [PATCH] Add option to translate _ to - for GCEPD 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. --- drivers/storage/gcepd/gcepd.go | 12 ++++++++++-- drivers/storage/gcepd/storage/gce_storage.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/drivers/storage/gcepd/gcepd.go b/drivers/storage/gcepd/gcepd.go index d3dd7eca..ce2d6653 100644 --- a/drivers/storage/gcepd/gcepd.go +++ b/drivers/storage/gcepd/gcepd.go @@ -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 @@ -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() { @@ -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) } diff --git a/drivers/storage/gcepd/storage/gce_storage.go b/drivers/storage/gcepd/storage/gce_storage.go index 2003c88f..af1f673b 100644 --- a/drivers/storage/gcepd/storage/gce_storage.go +++ b/drivers/storage/gcepd/storage/gce_storage.go @@ -58,6 +58,7 @@ type driver struct { maxAttempts int statusDelay int64 statusTimeout time.Duration + convUnderscore bool } func init() { @@ -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 } @@ -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, @@ -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") @@ -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,