Rework for readability

This commit is contained in:
Carter McKinnon 2022-06-15 00:57:54 +00:00
parent 983a570b9e
commit efc7df4afe

View File

@ -31,7 +31,7 @@ import (
func CleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool) error { func CleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool) error {
pathExists, pathErr := PathExists(mountPath) pathExists, pathErr := PathExists(mountPath)
if !pathExists && pathErr == nil { if !pathExists && pathErr == nil {
klog.Warningf("Warning: Unmount skipped because path does not exist: %v", mountPath) klog.Warningf("Warning: mount cleanup skipped because path does not exist: %v", mountPath)
return nil return nil
} }
corruptedMnt := IsCorruptedMnt(pathErr) corruptedMnt := IsCorruptedMnt(pathErr)
@ -44,40 +44,40 @@ func CleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointC
func CleanupMountWithForce(mountPath string, mounter MounterForceUnmounter, extensiveMountPointCheck bool, umountTimeout time.Duration) error { func CleanupMountWithForce(mountPath string, mounter MounterForceUnmounter, extensiveMountPointCheck bool, umountTimeout time.Duration) error {
pathExists, pathErr := PathExists(mountPath) pathExists, pathErr := PathExists(mountPath)
if !pathExists && pathErr == nil { if !pathExists && pathErr == nil {
klog.Warningf("Warning: Unmount skipped because path does not exist: %v", mountPath) klog.Warningf("Warning: mount cleanup skipped because path does not exist: %v", mountPath)
return nil return nil
} }
corruptedMnt := IsCorruptedMnt(pathErr) corruptedMnt := IsCorruptedMnt(pathErr)
if pathErr != nil && !corruptedMnt { if pathErr != nil && !corruptedMnt {
return fmt.Errorf("Error checking path: %v", pathErr) return fmt.Errorf("Error checking path: %v", pathErr)
} }
var notMnt bool
var err error if corruptedMnt || mounter.canSafelySkipMountPointCheck() {
if !mounter.canSafelySkipMountPointCheck() && !corruptedMnt { klog.V(4).Infof("unmounting %q", mountPath)
notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) if err := mounter.UnmountWithForce(mountPath, umountTimeout); err != nil {
// if mountPath was not a mount point - we would have attempted to remove mountPath return err
// and hence return errors if any. }
return removePath(mountPath)
}
notMnt, err := removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck)
// if mountPath is not a mount point, it's just been removed or there was an error
if err != nil || notMnt { if err != nil || notMnt {
return err return err
} }
}
// Unmount the mount path
klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath) klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath)
if err := mounter.UnmountWithForce(mountPath, umountTimeout); err != nil { if err := mounter.UnmountWithForce(mountPath, umountTimeout); err != nil {
return err return err
} }
if mounter.canSafelySkipMountPointCheck() {
return removePath(mountPath)
}
notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck)
// mountPath is not a mount point we should return whatever error we saw // if mountPath is not a mount point, it's either just been removed or there was an error
if notMnt { if notMnt {
return err return err
} }
return fmt.Errorf("Failed to unmount path %v", mountPath) // mountPath is still a mount point
return fmt.Errorf("failed to cleanup mount point %v", mountPath)
} }
// doCleanupMountPoint unmounts the given path and // doCleanupMountPoint unmounts the given path and
@ -88,33 +88,32 @@ func CleanupMountWithForce(mountPath string, mounter MounterForceUnmounter, exte
// if corruptedMnt is true, it means that the mountPath is a corrupted mountpoint, and the mount point check // if corruptedMnt is true, it means that the mountPath is a corrupted mountpoint, and the mount point check
// will be skipped. The mount point check will also be skipped if the mounter supports it. // will be skipped. The mount point check will also be skipped if the mounter supports it.
func doCleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool, corruptedMnt bool) error { func doCleanupMountPoint(mountPath string, mounter Interface, extensiveMountPointCheck bool, corruptedMnt bool) error {
var notMnt bool if corruptedMnt || mounter.canSafelySkipMountPointCheck() {
var err error klog.V(4).Infof("unmounting %q", mountPath)
if !mounter.canSafelySkipMountPointCheck() && !corruptedMnt { if err := mounter.Unmount(mountPath); err != nil {
notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) return err
// if mountPath was not a mount point - we would have attempted to remove mountPath }
// and hence return errors if any. return removePath(mountPath)
}
notMnt, err := removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck)
// if mountPath is not a mount point, it's just been removed or there was an error
if err != nil || notMnt { if err != nil || notMnt {
return err return err
} }
}
// Unmount the mount path
klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath) klog.V(4).Infof("%q is a mountpoint, unmounting", mountPath)
if err := mounter.Unmount(mountPath); err != nil { if err := mounter.Unmount(mountPath); err != nil {
return err return err
} }
if mounter.canSafelySkipMountPointCheck() {
return removePath(mountPath)
}
notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck) notMnt, err = removePathIfNotMountPoint(mountPath, mounter, extensiveMountPointCheck)
// mountPath is not a mount point we should return whatever error we saw // if mountPath is not a mount point, it's either just been removed or there was an error
if notMnt { if notMnt {
return err return err
} }
return fmt.Errorf("Failed to unmount path %v", mountPath) // mountPath is still a mount point
return fmt.Errorf("failed to cleanup mount point %v", mountPath)
} }
// removePathIfNotMountPoint verifies if given mountPath is a mount point if not it attempts // removePathIfNotMountPoint verifies if given mountPath is a mount point if not it attempts