mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Avoid RbdDiskManager's DetachDisk never execute again
This commit is contained in:
parent
7e7bb5cf3a
commit
d8fc13791a
@ -216,18 +216,39 @@ func (detacher *rbdDetacher) UnmountDevice(deviceMountPath string) error {
|
||||
return err
|
||||
}
|
||||
// Unmount the device from the device mount point.
|
||||
klog.V(4).Infof("rbd: unmouting device mountpoint %s", deviceMountPath)
|
||||
if err = detacher.mounter.Unmount(deviceMountPath); err != nil {
|
||||
return err
|
||||
}
|
||||
klog.V(3).Infof("rbd: successfully umount device mountpath %s", deviceMountPath)
|
||||
|
||||
klog.V(4).Infof("rbd: detaching device %s", devicePath)
|
||||
err = detacher.manager.DetachDisk(detacher.plugin, deviceMountPath, devicePath)
|
||||
notMnt, err := detacher.mounter.IsLikelyNotMountPoint(deviceMountPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
klog.V(3).Infof("rbd: successfully detach device %s", devicePath)
|
||||
if !notMnt {
|
||||
klog.V(4).Infof("rbd: unmouting device mountpoint %s", deviceMountPath)
|
||||
if err = detacher.mounter.Unmount(deviceMountPath); err != nil {
|
||||
return err
|
||||
}
|
||||
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)
|
||||
err = detacher.manager.DetachDisk(detacher.plugin, deviceMountPath, devicePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
klog.V(3).Infof("rbd: successfully detach device %s", devicePath)
|
||||
}
|
||||
err = os.Remove(deviceMountPath)
|
||||
if err != nil {
|
||||
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
|
||||
)
|
||||
|
||||
// A struct contains rbd image info.
|
||||
type rbdImageInfo struct {
|
||||
pool string
|
||||
name string
|
||||
}
|
||||
|
||||
func getDevFromImageAndPool(pool, image string) (string, bool) {
|
||||
device, found := getRbdDevFromImageAndPool(pool, image)
|
||||
if found {
|
||||
@ -789,3 +795,15 @@ func (util *RBDUtil) rbdStatus(b *rbdMounter) (bool, string, error) {
|
||||
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