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

GCE translate underscores #580

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 @@ -1013,6 +1013,7 @@ gcepd:
statusMaxAttempts: 10
statusInitialDelay: 100ms
statusTimeout: 2m
convertUnderscores: false
```

##### Configuration Notes
Expand Down Expand Up @@ -1050,6 +1051,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)
}
30 changes: 29 additions & 1 deletion 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 @@ -300,7 +303,7 @@ func (d *driver) Volumes(
return vols, nil
}

// VolumeInspect inspects a single volume.
// VolumeInspect inspects a single volume by ID.
func (d *driver) VolumeInspect(
ctx types.Context,
volumeID string,
Expand Down Expand Up @@ -335,6 +338,22 @@ func (d *driver) VolumeInspect(
return vols[0], nil
}

// VolumeInspectByName inspects a single volume by name.
func (d *driver) VolumeInspectByName(
ctx types.Context,
volumeName string,
opts *types.VolumeInspectOpts) (*types.Volume, error) {

volumeName = d.convUnderscores(volumeName)

// For GCE, name and ID are the same
return d.VolumeInspect(
ctx,
volumeName,
opts,
)
}

// VolumeCreate creates a new volume.
func (d *driver) VolumeCreate(
ctx types.Context,
Expand Down Expand Up @@ -371,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 @@ -1058,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