From b900ad91d6f0008dfe57285abeaaf46cd60dc59a Mon Sep 17 00:00:00 2001 From: Huamin Chen Date: Tue, 13 Oct 2015 14:50:49 -0400 Subject: [PATCH] iscsi: use global path to retrieve iscsi portal and iqn Signed-off-by: Huamin Chen --- pkg/volume/iscsi/iscsi_util.go | 50 +++++++++++++++++++++++------ pkg/volume/iscsi/iscsi_util_test.go | 35 ++++++++++++++------ 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/pkg/volume/iscsi/iscsi_util.go b/pkg/volume/iscsi/iscsi_util.go index b3cafdbe9243..4b547dde199b 100644 --- a/pkg/volume/iscsi/iscsi_util.go +++ b/pkg/volume/iscsi/iscsi_util.go @@ -18,6 +18,7 @@ package iscsi import ( "errors" + "fmt" "os" "path" "strings" @@ -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++ } } @@ -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 @@ -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 { @@ -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 +} diff --git a/pkg/volume/iscsi/iscsi_util_test.go b/pkg/volume/iscsi/iscsi_util_test.go index d0531a9d7a62..3b7a00d2b117 100644 --- a/pkg/volume/iscsi/iscsi_util_test.go +++ b/pkg/volume/iscsi/iscsi_util_test.go @@ -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"}, }, } @@ -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, }, } @@ -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) + } +}