Skip to content

Commit

Permalink
Merge pull request #580 from codenrhoden/enhancement/gce_translate_un…
Browse files Browse the repository at this point in the history
…derscores

GCE translate underscores
  • Loading branch information
akutz authored Jun 26, 2017
2 parents 2565f3a + 4d2ab33 commit b156064
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .docs/user-guide/storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,7 @@ gcepd:
statusMaxAttempts: 10
statusInitialDelay: 100ms
statusTimeout: 2m
convertUnderscores: false
```

##### Configuration Notes
Expand Down Expand Up @@ -1071,6 +1072,12 @@ gcepd:
* `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.
GCE 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.

#### Runtime behavior
* The GCEPD driver enforces the GCE requirements for disk sizing and naming.
Expand Down
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 b156064

Please sign in to comment.