Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iscsi: use global path to retrieve iscsi portal and iqn #18875

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 41 additions & 9 deletions pkg/volume/iscsi/iscsi_util.go
Expand Up @@ -18,6 +18,7 @@ package iscsi

import (
"errors"
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -60,7 +61,7 @@ func getDevicePrefixRefCount(mounter mount.Interface, deviceNamePrefix string) (
// Find the number of references to the device.
refCount := 0
for i := range mps {
if strings.HasPrefix(mps[i].Device, deviceNamePrefix) {
if strings.HasPrefix(mps[i].Path, deviceNamePrefix) {
refCount++
}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskBuilder) 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 {
glog.Errorf("iscsi detach disk: failed to get device from mnt: %s\nError: %v", mntPath, err)
return err
Expand All @@ -133,18 +134,19 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskCleaner, mntPath string) error {
cnt--
// if device is no longer used, see if need to logout the target
if cnt == 0 {
// strip -lun- from device path
ind := strings.LastIndex(device, "-lun-")
prefix := device[:(ind - 1)]
device, prefix, err := extractDeviceAndPrefix(mntPath)
if err != nil {
return err
}
refCount, err := getDevicePrefixRefCount(c.mounter, prefix)

if err == nil && refCount == 0 {
// this portal/iqn are no longer referenced, log out
// extract portal and iqn from device path
ind1 := strings.LastIndex(device, "-iscsi-")
portal := device[(len("/dev/disk/by-path/ip-")):ind1]
iqn := device[ind1+len("-iscsi-") : ind]

portal, iqn, err := extractPortalAndIqn(device)
if err != nil {
return err
}
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"})
if err != nil {
Expand All @@ -154,3 +156,33 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskCleaner, mntPath string) error {
}
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
}
35 changes: 26 additions & 9 deletions pkg/volume/iscsi/iscsi_util_test.go
Expand Up @@ -25,14 +25,14 @@ import (
func TestGetDevicePrefixRefCount(t *testing.T) {
fm := &mount.FakeMounter{
MountPoints: []mount.MountPoint{
{Device: "/dev/disk/by-path/prefix-lun-1",
Path: "/mnt/111"},
{Device: "/dev/disk/by-path/prefix-lun-1",
Path: "/mnt/222"},
{Device: "/dev/disk/by-path/prefix-lun-0",
Path: "/mnt/333"},
{Device: "/dev/disk/by-path/prefix-lun-0",
Path: "/mnt/444"},
{Device: "/dev/sdb",
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-0"},
{Device: "/dev/sdb",
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-1"},
{Device: "/dev/sdb",
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-2"},
{Device: "/dev/sdb",
Path: "/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-3"},
},
}

Expand All @@ -41,7 +41,7 @@ func TestGetDevicePrefixRefCount(t *testing.T) {
expectedRefs int
}{
{
"/dev/disk/by-path/prefix",
"/127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00",
4,
},
}
Expand All @@ -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)
}
}