-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
fix issue in converting aws volume id from mount paths #36840
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ import ( | |
|
||
const ( | ||
// Default mount command if mounter path is not specified | ||
defaultMountCommand = "mount" | ||
defaultMountCommand = "mount" | ||
MountsInGlobalPDPath = "mounts" | ||
) | ||
|
||
type Interface interface { | ||
|
@@ -189,9 +190,15 @@ func getDeviceNameFromMount(mounter Interface, mountPath, pluginDir string) (str | |
glog.V(4).Infof("Directory %s is not mounted", mountPath) | ||
return "", fmt.Errorf("directory %s is not mounted", mountPath) | ||
} | ||
basemountPath := path.Join(pluginDir, MountsInGlobalPDPath) | ||
for _, ref := range refs { | ||
if strings.HasPrefix(ref, pluginDir) { | ||
return path.Base(ref), nil | ||
if strings.HasPrefix(ref, basemountPath) { | ||
volumeID, err := filepath.Rel(basemountPath, ref) | ||
if err != nil { | ||
glog.Errorf("Failed to get volume id from mount %s - %v", mountPath, err) | ||
return "", err | ||
} | ||
return volumeID, nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should return a struct with the mount information. I see there's another overload which returns the ref count, so it's difficult to know exactly where this is used by pure grep. But this is my go-to trick (when in doubt, create a type!); not sure it's needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I plan to change the function name to avoid the confusion in another PR. |
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ var _ volume.ProvisionableVolumePlugin = &awsElasticBlockStorePlugin{} | |
|
||
const ( | ||
awsElasticBlockStorePluginName = "kubernetes.io/aws-ebs" | ||
awsURLNamePrefix = "aws://" | ||
) | ||
|
||
func getPath(uid types.UID, volName string, host volume.VolumeHost) string { | ||
|
@@ -189,10 +190,36 @@ func getVolumeSource( | |
func (plugin *awsElasticBlockStorePlugin) ConstructVolumeSpec(volName, mountPath string) (*volume.Spec, error) { | ||
mounter := plugin.host.GetMounter() | ||
pluginDir := plugin.host.GetPluginDir(plugin.GetPluginName()) | ||
sourceName, err := mounter.GetDeviceNameFromMount(mountPath, pluginDir) | ||
volumeID, err := mounter.GetDeviceNameFromMount(mountPath, pluginDir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// This is a workaround to fix the issue in converting aws volume id from globalPDPath | ||
// There are three aws volume id formats and their volumeID from GetDeviceNameFromMount() are: | ||
// aws:///vol-1234 (aws/vol-1234) | ||
// aws://us-east-1/vol-1234 (aws/us-east-1/vol-1234) | ||
// vol-1234 (vol-1234) | ||
// This code is for converting volume id to aws style volume id for the first two cases. | ||
sourceName := volumeID | ||
if strings.HasPrefix(volumeID, "aws/") { | ||
names := strings.Split(volumeID, "/") | ||
length := len(names) | ||
if length < 2 || length > 3 { | ||
return nil, fmt.Errorf("Failed to get AWS volume id from mount path %q: invalid volume name format %q", mountPath, volumeID) | ||
} | ||
volName := names[length-1] | ||
if !strings.HasPrefix(volName, "vol-") { | ||
return nil, fmt.Errorf("Invalid volume name format for AWS volume (%q) retrieved from mount path %q", volName, mountPath) | ||
} | ||
if length == 2 { | ||
sourceName = awsURLNamePrefix + "" + "/" + volName // empty zone label | ||
} | ||
if length == 3 { | ||
sourceName = awsURLNamePrefix + names[1] + "/" + volName // names[1] is the zone label | ||
} | ||
glog.V(4).Infof("Convert aws volume name from %q to %q ", volumeID, sourceName) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks right. If I'm going to nit-pick, it might be safer to split the string, and then glog.Warning if it doesn't match one of the forms we expect. i.e. we expect either aws/vol-12345678 or aws/us-east-1b/vol-12345678, but we should log if it doesn't look like that. Bonus: code might be easier to understand also! |
||
|
||
awsVolume := &api.Volume{ | ||
Name: volName, | ||
VolumeSource: api.VolumeSource{ | ||
|
@@ -324,12 +351,12 @@ func makeGlobalPDPath(host volume.VolumeHost, volumeID aws.KubernetesVolumeID) s | |
// Clean up the URI to be more fs-friendly | ||
name := string(volumeID) | ||
name = strings.Replace(name, "://", "/", -1) | ||
return path.Join(host.GetPluginDir(awsElasticBlockStorePluginName), "mounts", name) | ||
return path.Join(host.GetPluginDir(awsElasticBlockStorePluginName), mount.MountsInGlobalPDPath, name) | ||
} | ||
|
||
// Reverses the mapping done in makeGlobalPDPath | ||
func getVolumeIDFromGlobalMount(host volume.VolumeHost, globalPath string) (string, error) { | ||
basePath := path.Join(host.GetPluginDir(awsElasticBlockStorePluginName), "mounts") | ||
basePath := path.Join(host.GetPluginDir(awsElasticBlockStorePluginName), mount.MountsInGlobalPDPath) | ||
rel, err := filepath.Rel(basePath, globalPath) | ||
if err != nil { | ||
glog.Errorf("Failed to get volume id from global mount %s - %v", globalPath, err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why lower the priority on these messages? Are they happening very frequently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it prints out log for every reconciler loop