Remove klog for output error instead return err with context

This commit is contained in:
Masaki Kimura 2019-11-04 23:00:25 +00:00
parent 7abb704e7b
commit bee6514d79
2 changed files with 32 additions and 34 deletions

View File

@ -82,13 +82,13 @@ func (v VolumePathHandler) MapDevice(devicePath string, mapPath string, linkName
// podDeviceMapPath/linkName: pods/{podUid}/{DefaultKubeletVolumeDevicesDirName}/{escapeQualifiedPluginName}/{volumeName}
// linkName: {volumeName}
if len(devicePath) == 0 {
return fmt.Errorf("Failed to map device to map path. devicePath is empty")
return fmt.Errorf("failed to map device to map path. devicePath is empty")
}
if len(mapPath) == 0 {
return fmt.Errorf("Failed to map device to map path. mapPath is empty")
return fmt.Errorf("failed to map device to map path. mapPath is empty")
}
if !filepath.IsAbs(mapPath) {
return fmt.Errorf("The map path should be absolute: map path: %s", mapPath)
return fmt.Errorf("the map path should be absolute: map path: %s", mapPath)
}
klog.V(5).Infof("MapDevice: devicePath %s", devicePath)
klog.V(5).Infof("MapDevice: mapPath %s", mapPath)
@ -97,11 +97,10 @@ func (v VolumePathHandler) MapDevice(devicePath string, mapPath string, linkName
// Check and create mapPath
_, err := os.Stat(mapPath)
if err != nil && !os.IsNotExist(err) {
klog.Errorf("cannot validate map path: %s", mapPath)
return err
return fmt.Errorf("cannot validate map path: %s: %v", mapPath, err)
}
if err = os.MkdirAll(mapPath, 0750); err != nil {
return fmt.Errorf("Failed to mkdir %s, error %v", mapPath, err)
return fmt.Errorf("failed to mkdir %s: %v", mapPath, err)
}
if bindMount {
@ -117,17 +116,17 @@ func mapBindMountDevice(v VolumePathHandler, devicePath string, mapPath string,
file, err := os.Stat(linkPath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("Failed to stat file %s: %v", linkPath, err)
return fmt.Errorf("failed to stat file %s: %v", linkPath, err)
}
klog.Warningf("Warning: Path to bind mount %v has not yet been created", linkPath)
// Create file
newFile, err := os.OpenFile(linkPath, os.O_CREATE|os.O_RDWR, 0750)
if err != nil {
return err
return fmt.Errorf("failed to open file %s: %v", linkPath, err)
}
if err := newFile.Close(); err != nil {
return err
return fmt.Errorf("failed to close file %s: %v", linkPath, err)
}
} else {
// Check if device file
@ -143,8 +142,7 @@ func mapBindMountDevice(v VolumePathHandler, devicePath string, mapPath string,
// Bind mount file
mounter := &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: mount.NewOSExec()}
if err := mounter.Mount(devicePath, linkPath, "" /* fsType */, []string{"bind"}); err != nil {
klog.Errorf("Failed to bind mount devicePath: %s to linkPath %s: %v", devicePath, linkPath, err)
return err
return fmt.Errorf("failed to bind mount devicePath: %s to linkPath %s: %v", devicePath, linkPath, err)
}
return nil
@ -156,7 +154,7 @@ func mapSymlinkDevice(v VolumePathHandler, devicePath string, mapPath string, li
// stale across node reboot.
linkPath := filepath.Join(mapPath, string(linkName))
if err := os.Remove(linkPath); err != nil && !os.IsNotExist(err) {
return err
return fmt.Errorf("failed to remove file %s: %v", linkPath, err)
}
return os.Symlink(devicePath, linkPath)
}
@ -164,7 +162,7 @@ func mapSymlinkDevice(v VolumePathHandler, devicePath string, mapPath string, li
// UnmapDevice removes a symbolic link associated to block device under specified map path
func (v VolumePathHandler) UnmapDevice(mapPath string, linkName string, bindMount bool) error {
if len(mapPath) == 0 {
return fmt.Errorf("Failed to unmap device from map path. mapPath is empty")
return fmt.Errorf("failed to unmap device from map path. mapPath is empty")
}
klog.V(5).Infof("UnmapDevice: mapPath %s", mapPath)
klog.V(5).Infof("UnmapDevice: linkName %s", linkName)
@ -189,13 +187,12 @@ func unmapBindMountDevice(v VolumePathHandler, mapPath string, linkName string)
// Unmount file
mounter := &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: mount.NewOSExec()}
if err := mounter.Unmount(linkPath); err != nil {
klog.Errorf("Failed to unmount linkPath %s: %v", linkPath, err)
return err
return fmt.Errorf("failed to unmount linkPath %s: %v", linkPath, err)
}
// Remove file
if err := os.Remove(linkPath); err != nil && !os.IsNotExist(err) {
return err
return fmt.Errorf("failed to remove file %s: %v", linkPath, err)
}
return nil
@ -216,12 +213,12 @@ func unmapSymlinkDevice(v VolumePathHandler, mapPath string, linkName string) er
// RemoveMapPath removes a file or directory on specified map path
func (v VolumePathHandler) RemoveMapPath(mapPath string) error {
if len(mapPath) == 0 {
return fmt.Errorf("Failed to remove map path. mapPath is empty")
return fmt.Errorf("failed to remove map path. mapPath is empty")
}
klog.V(5).Infof("RemoveMapPath: mapPath %s", mapPath)
err := os.RemoveAll(mapPath)
if err != nil && !os.IsNotExist(err) {
return err
return fmt.Errorf("failed to remove directory %s: %v", mapPath, err)
}
return nil
}
@ -237,7 +234,7 @@ func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) {
return false, nil
}
// Return error from Lstat()
return false, err
return false, fmt.Errorf("failed to Lstat file %s: %v", mapPath, err)
}
// If file exits and it's symbolic link, return true and no error
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
@ -259,7 +256,7 @@ func (v VolumePathHandler) IsBindMountExist(mapPath string) (bool, error) {
}
// Return error from Lstat()
return false, err
return false, fmt.Errorf("failed to Lstat file %s: %v", mapPath, err)
}
// If file exits and it's device, return true and no error
if fi.Mode()&os.ModeDevice == os.ModeDevice {
@ -274,7 +271,7 @@ func (v VolumePathHandler) GetDeviceBindMountRefs(devPath string, mapPath string
var refs []string
files, err := ioutil.ReadDir(mapPath)
if err != nil {
return nil, fmt.Errorf("Directory cannot read %v", err)
return nil, fmt.Errorf("directory cannot read %v", err)
}
for _, file := range files {
if file.Mode()&os.ModeDevice != os.ModeDevice {

View File

@ -36,7 +36,7 @@ import (
func (v VolumePathHandler) AttachFileDevice(path string) (string, error) {
blockDevicePath, err := v.GetLoopDevice(path)
if err != nil && err.Error() != ErrDeviceNotFound {
return "", err
return "", fmt.Errorf("GetLoopDevice failed for path %s: %v", path, err)
}
// If no existing loop device for the path, create one
@ -44,7 +44,7 @@ func (v VolumePathHandler) AttachFileDevice(path string) (string, error) {
klog.V(4).Infof("Creating device for path: %s", path)
blockDevicePath, err = makeLoopDevice(path)
if err != nil {
return "", err
return "", fmt.Errorf("makeLoopDevice failed for path %s: %v", path, err)
}
}
return blockDevicePath, nil
@ -56,15 +56,15 @@ func (v VolumePathHandler) DetachFileDevice(path string) error {
loopPath, err := v.GetLoopDevice(path)
if err != nil {
if err.Error() == ErrDeviceNotFound {
klog.Warningf("DetachFileDevice: Couldn't find loopback device which takes file descriptor lock. device path: %q", path)
klog.Warningf("couldn't find loopback device which takes file descriptor lock. Skip detaching device. device path: %q", path)
} else {
return err
return fmt.Errorf("GetLoopDevice failed for path %s: %v", path, err)
}
} else {
if len(loopPath) != 0 {
err = removeLoopDevice(loopPath)
if err != nil {
return err
return fmt.Errorf("removeLoopDevice failed for path %s: %v", path, err)
}
}
}
@ -86,7 +86,7 @@ func (v VolumePathHandler) GetLoopDevice(path string) (string, error) {
out, err := cmd.CombinedOutput()
if err != nil {
klog.V(2).Infof("Failed device discover command for path %s: %v %s", path, err, out)
return "", err
return "", fmt.Errorf("losetup -j %s failed: %v", path, err)
}
return parseLosetupOutputForDevice(out, path)
}
@ -97,7 +97,7 @@ func makeLoopDevice(path string) (string, error) {
out, err := cmd.CombinedOutput()
if err != nil {
klog.V(2).Infof("Failed device create command for path: %s %v %s ", path, err, out)
return "", err
return "", fmt.Errorf("losetup -f --show %s failed: %v", path, err)
}
// losetup -f --show {path} returns device in the format:
@ -119,7 +119,7 @@ func removeLoopDevice(device string) error {
return nil
}
klog.V(2).Infof("Failed to remove loopback device: %s: %v %s", device, err, out)
return err
return fmt.Errorf("losetup -d %s failed: %v", device, err)
}
return nil
}
@ -162,6 +162,7 @@ func parseLosetupOutputForDevice(output []byte, path string) (string, error) {
// FindGlobalMapPathUUIDFromPod finds {pod uuid} bind mount under globalMapPath
// corresponding to map path symlink, and then return global map path with pod uuid.
// (See pkg/volume/volume.go for details on a global map path and a pod device map path.)
// ex. mapPath symlink: pods/{podUid}}/{DefaultKubeletVolumeDevicesDirName}/{escapeQualifiedPluginName}/{volumeName} -> /dev/sdX
// globalMapPath/{pod uuid} bind mount: plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumePluginDependentPath}/{pod uuid} -> /dev/sdX
func (v VolumePathHandler) FindGlobalMapPathUUIDFromPod(pluginDir, mapPath string, podUID types.UID) (string, error) {
@ -180,7 +181,7 @@ func (v VolumePathHandler) FindGlobalMapPathUUIDFromPod(pluginDir, mapPath strin
return nil
})
if err != nil {
return "", err
return "", fmt.Errorf("FindGlobalMapPathUUIDFromPod failed: %v", err)
}
klog.V(5).Infof("FindGlobalMapPathFromPod: globalMapPathUUID %s", globalMapPathUUID)
// Return path contains global map path + {pod uuid}
@ -197,18 +198,18 @@ func compareBindMountAndSymlinks(global, pod string) (bool, error) {
// Get the major/minor number for global path
devNumGlobal, err := getDeviceMajorMinor(global)
if err != nil {
return false, err
return false, fmt.Errorf("getDeviceMajorMinor failed for path %s: %v", global, err)
}
// Get the symlinked device from the pod path
devPod, err := os.Readlink(pod)
if err != nil {
return false, err
return false, fmt.Errorf("failed to readlink path %s: %v", pod, err)
}
// Get the major/minor number for the symlinked device from the pod path
devNumPod, err := getDeviceMajorMinor(devPod)
if err != nil {
return false, err
return false, fmt.Errorf("getDeviceMajorMinor failed for path %s: %v", devPod, err)
}
klog.V(5).Infof("CompareBindMountAndSymlinks: devNumGlobal %s, devNumPod %s", devNumGlobal, devNumPod)
@ -229,7 +230,7 @@ func getDeviceMajorMinor(path string) (string, error) {
out, err := cmd.CombinedOutput()
if err != nil {
klog.V(2).Infof("Failed to stat path: %s %v %s ", path, err, out)
return "", err
return "", fmt.Errorf("stat -c %%t:%%T %s failed: %v", path, err)
}
// stat -c "%t:%T" {path} outputs following format(major:minor in hex):