mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #15562 from rootfs/iscsi-umount
Auto commit by PR queue bot
This commit is contained in:
commit
372fb373aa
@ -18,6 +18,7 @@ package iscsi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
@ -60,7 +61,7 @@ func getDevicePrefixRefCount(mounter mount.Interface, deviceNamePrefix string) (
|
|||||||
// Find the number of references to the device.
|
// Find the number of references to the device.
|
||||||
refCount := 0
|
refCount := 0
|
||||||
for i := range mps {
|
for i := range mps {
|
||||||
if strings.HasPrefix(mps[i].Device, deviceNamePrefix) {
|
if strings.HasPrefix(mps[i].Path, deviceNamePrefix) {
|
||||||
refCount++
|
refCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,7 +122,7 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskBuilder) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (util *ISCSIUtil) DetachDisk(c iscsiDiskCleaner, mntPath string) error {
|
func (util *ISCSIUtil) DetachDisk(c iscsiDiskCleaner, mntPath string) error {
|
||||||
device, cnt, err := mount.GetDeviceNameFromMount(c.mounter, mntPath)
|
_, cnt, err := mount.GetDeviceNameFromMount(c.mounter, mntPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("iscsi detach disk: failed to get device from mnt: %s\nError: %v", mntPath, err)
|
glog.Errorf("iscsi detach disk: failed to get device from mnt: %s\nError: %v", mntPath, err)
|
||||||
return err
|
return err
|
||||||
@ -133,18 +134,19 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskCleaner, mntPath string) error {
|
|||||||
cnt--
|
cnt--
|
||||||
// if device is no longer used, see if need to logout the target
|
// if device is no longer used, see if need to logout the target
|
||||||
if cnt == 0 {
|
if cnt == 0 {
|
||||||
// strip -lun- from device path
|
device, prefix, err := extractDeviceAndPrefix(mntPath)
|
||||||
ind := strings.LastIndex(device, "-lun-")
|
if err != nil {
|
||||||
prefix := device[:(ind - 1)]
|
return err
|
||||||
|
}
|
||||||
refCount, err := getDevicePrefixRefCount(c.mounter, prefix)
|
refCount, err := getDevicePrefixRefCount(c.mounter, prefix)
|
||||||
|
|
||||||
if err == nil && refCount == 0 {
|
if err == nil && refCount == 0 {
|
||||||
// this portal/iqn are no longer referenced, log out
|
// this portal/iqn are no longer referenced, log out
|
||||||
// extract portal and iqn from device path
|
// extract portal and iqn from device path
|
||||||
ind1 := strings.LastIndex(device, "-iscsi-")
|
portal, iqn, err := extractPortalAndIqn(device)
|
||||||
portal := device[(len("/dev/disk/by-path/ip-")):ind1]
|
if err != nil {
|
||||||
iqn := device[ind1+len("-iscsi-") : ind]
|
return err
|
||||||
|
}
|
||||||
glog.Infof("iscsi: log out target %s iqn %s", portal, iqn)
|
glog.Infof("iscsi: log out target %s iqn %s", portal, iqn)
|
||||||
out, err := c.plugin.execCommand("iscsiadm", []string{"-m", "node", "-p", portal, "-T", iqn, "--logout"})
|
out, err := c.plugin.execCommand("iscsiadm", []string{"-m", "node", "-p", portal, "-T", iqn, "--logout"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -154,3 +156,33 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskCleaner, mntPath string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractDeviceAndPrefix(mntPath string) (string, string, error) {
|
||||||
|
ind := strings.LastIndex(mntPath, "/")
|
||||||
|
if ind < 0 {
|
||||||
|
return "", "", fmt.Errorf("iscsi detach disk: malformatted mnt path: %s", mntPath)
|
||||||
|
}
|
||||||
|
device := mntPath[(ind + 1):]
|
||||||
|
// strip -lun- from device path
|
||||||
|
ind = strings.LastIndex(device, "-lun-")
|
||||||
|
if ind < 0 {
|
||||||
|
return "", "", fmt.Errorf("iscsi detach disk: malformatted mnt path: %s", mntPath)
|
||||||
|
}
|
||||||
|
prefix := device[:ind]
|
||||||
|
return device, prefix, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractPortalAndIqn(device string) (string, string, error) {
|
||||||
|
ind1 := strings.Index(device, "-")
|
||||||
|
if ind1 < 0 {
|
||||||
|
return "", "", fmt.Errorf("iscsi detach disk: no portal in %s", device)
|
||||||
|
}
|
||||||
|
portal := device[0:ind1]
|
||||||
|
ind2 := strings.Index(device, "iqn.")
|
||||||
|
if ind2 < 0 {
|
||||||
|
return "", "", fmt.Errorf("iscsi detach disk: no iqn in %s", device)
|
||||||
|
}
|
||||||
|
ind := strings.LastIndex(device, "-lun-")
|
||||||
|
iqn := device[ind2:ind]
|
||||||
|
return portal, iqn, nil
|
||||||
|
}
|
||||||
|
@ -25,14 +25,14 @@ import (
|
|||||||
func TestGetDevicePrefixRefCount(t *testing.T) {
|
func TestGetDevicePrefixRefCount(t *testing.T) {
|
||||||
fm := &mount.FakeMounter{
|
fm := &mount.FakeMounter{
|
||||||
MountPoints: []mount.MountPoint{
|
MountPoints: []mount.MountPoint{
|
||||||
{Device: "/dev/disk/by-path/prefix-lun-1",
|
{Device: "/dev/sdb",
|
||||||
Path: "/mnt/111"},
|
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-0"},
|
||||||
{Device: "/dev/disk/by-path/prefix-lun-1",
|
{Device: "/dev/sdb",
|
||||||
Path: "/mnt/222"},
|
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-1"},
|
||||||
{Device: "/dev/disk/by-path/prefix-lun-0",
|
{Device: "/dev/sdb",
|
||||||
Path: "/mnt/333"},
|
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-2"},
|
||||||
{Device: "/dev/disk/by-path/prefix-lun-0",
|
{Device: "/dev/sdb",
|
||||||
Path: "/mnt/444"},
|
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-3"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ func TestGetDevicePrefixRefCount(t *testing.T) {
|
|||||||
expectedRefs int
|
expectedRefs int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"/dev/disk/by-path/prefix",
|
"/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00",
|
||||||
4,
|
4,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -52,3 +52,20 @@ func TestGetDevicePrefixRefCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExtractDeviceAndPrefix(t *testing.T) {
|
||||||
|
devicePath := "127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00"
|
||||||
|
lun := "-lun-0"
|
||||||
|
device, prefix, err := extractDeviceAndPrefix("/var/lib/kubelet/plugins/kubernetes.io/iscsi/" + devicePath + lun)
|
||||||
|
if err != nil || device != (devicePath+lun) || prefix != devicePath {
|
||||||
|
t.Errorf("extractDeviceAndPrefix: expected %s and %s, got %v %s and %s", devicePath+lun, devicePath, err, device, prefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExtractPortalAndIqn(t *testing.T) {
|
||||||
|
devicePath := "127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-0"
|
||||||
|
portal, iqn, err := extractPortalAndIqn(devicePath)
|
||||||
|
if err != nil || portal != "127.0.0.1:3260" || iqn != "iqn.2014-12.com.example:test.tgt00" {
|
||||||
|
t.Errorf("extractPortalAndIqn: got %v %s %s", err, portal, iqn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user