mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #79940 from jeremyxu2010/avoid_detachdisk_never_execute_again
Avoid RbdDiskManager's DetachDisk function never be executed again
This commit is contained in:
commit
30c9f097ca
@ -216,18 +216,39 @@ func (detacher *rbdDetacher) UnmountDevice(deviceMountPath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Unmount the device from the device mount point.
|
// Unmount the device from the device mount point.
|
||||||
|
notMnt, err := detacher.mounter.IsLikelyNotMountPoint(deviceMountPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !notMnt {
|
||||||
klog.V(4).Infof("rbd: unmouting device mountpoint %s", deviceMountPath)
|
klog.V(4).Infof("rbd: unmouting device mountpoint %s", deviceMountPath)
|
||||||
if err = detacher.mounter.Unmount(deviceMountPath); err != nil {
|
if err = detacher.mounter.Unmount(deviceMountPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
klog.V(3).Infof("rbd: successfully umount device mountpath %s", deviceMountPath)
|
klog.V(3).Infof("rbd: successfully umount device mountpath %s", deviceMountPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get devicePath from deviceMountPath if devicePath is empty
|
||||||
|
if devicePath == "" {
|
||||||
|
rbdImageInfo, err := getRbdImageInfo(deviceMountPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
found := false
|
||||||
|
devicePath, found = getRbdDevFromImageAndPool(rbdImageInfo.pool, rbdImageInfo.name)
|
||||||
|
if !found {
|
||||||
|
klog.Warningf("rbd: can't found devicePath for %v. Device is already unmounted, Image %v, Pool %v", deviceMountPath, rbdImageInfo.pool, rbdImageInfo.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if devicePath != "" {
|
||||||
klog.V(4).Infof("rbd: detaching device %s", devicePath)
|
klog.V(4).Infof("rbd: detaching device %s", devicePath)
|
||||||
err = detacher.manager.DetachDisk(detacher.plugin, deviceMountPath, devicePath)
|
err = detacher.manager.DetachDisk(detacher.plugin, deviceMountPath, devicePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
klog.V(3).Infof("rbd: successfully detach device %s", devicePath)
|
klog.V(3).Infof("rbd: successfully detach device %s", devicePath)
|
||||||
|
}
|
||||||
err = os.Remove(deviceMountPath)
|
err = os.Remove(deviceMountPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -707,3 +707,34 @@ func TestGetRbdImageSize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetRbdImageInfo(t *testing.T) {
|
||||||
|
tmpDir, err := utiltesting.MkTmpdir("rbd_test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error creating temp dir: %v", err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
|
for i, c := range []struct {
|
||||||
|
DeviceMountPath string
|
||||||
|
TargetRbdImageInfo *rbdImageInfo
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
DeviceMountPath: fmt.Sprintf("%s/plugins/kubernetes.io/rbd/rbd/pool1-image-image1", tmpDir),
|
||||||
|
TargetRbdImageInfo: &rbdImageInfo{pool: "pool1", name: "image1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
DeviceMountPath: fmt.Sprintf("%s/plugins/kubernetes.io/rbd/mounts/pool2-image-image2", tmpDir),
|
||||||
|
TargetRbdImageInfo: &rbdImageInfo{pool: "pool2", name: "image2"},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
rbdImageInfo, err := getRbdImageInfo(c.DeviceMountPath)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Case %d: getRbdImageInfo failed: %v", i, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(rbdImageInfo, c.TargetRbdImageInfo) {
|
||||||
|
t.Errorf("Case %d: unexpected RbdImageInfo, wanted %v, got %v", i, c.TargetRbdImageInfo, rbdImageInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -57,6 +57,12 @@ const (
|
|||||||
rbdImageSizeUnitMiB = 1024 * 1024
|
rbdImageSizeUnitMiB = 1024 * 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A struct contains rbd image info.
|
||||||
|
type rbdImageInfo struct {
|
||||||
|
pool string
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
func getDevFromImageAndPool(pool, image string) (string, bool) {
|
func getDevFromImageAndPool(pool, image string) (string, bool) {
|
||||||
device, found := getRbdDevFromImageAndPool(pool, image)
|
device, found := getRbdDevFromImageAndPool(pool, image)
|
||||||
if found {
|
if found {
|
||||||
@ -789,3 +795,15 @@ func (util *RBDUtil) rbdStatus(b *rbdMounter) (bool, string, error) {
|
|||||||
return false, output, nil
|
return false, output, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getRbdImageInfo try to get rbdImageInfo from deviceMountPath.
|
||||||
|
func getRbdImageInfo(deviceMountPath string) (*rbdImageInfo, error) {
|
||||||
|
deviceMountedPathSeps := strings.Split(filepath.Base(deviceMountPath), "-image-")
|
||||||
|
if len(deviceMountedPathSeps) != 2 {
|
||||||
|
return nil, fmt.Errorf("Can't found devicePath for %s ", deviceMountPath)
|
||||||
|
}
|
||||||
|
return &rbdImageInfo{
|
||||||
|
pool: deviceMountedPathSeps[0],
|
||||||
|
name: deviceMountedPathSeps[1],
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user