From 2efe67e3a92169ebd71f13cbe2058253faa7b74e Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Tue, 21 Jun 2022 10:44:42 +0200 Subject: [PATCH] Fix iSCSI over ipv6 Addresses in the Kubernetes API objects (PV, Pod) have `[]` around IPv6 addresses, while addresses in /dev/ and /sys/ have addresses without them. Add/remove `[]` as needed. --- pkg/volume/iscsi/iscsi_util.go | 10 ++++++++-- pkg/volume/iscsi/iscsi_util_test.go | 5 +++++ pkg/volume/util/device_util_linux.go | 8 ++++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/volume/iscsi/iscsi_util.go b/pkg/volume/iscsi/iscsi_util.go index e2f62f72061..b51127aa92f 100644 --- a/pkg/volume/iscsi/iscsi_util.go +++ b/pkg/volume/iscsi/iscsi_util.go @@ -406,10 +406,16 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) { klog.Errorf("iscsi: could not find transport name in iface %s", b.Iface) return "", fmt.Errorf("could not parse iface file for %s", b.Iface) } + + addr := tp + if strings.HasPrefix(tp, "[") { + // Delete [] from IP address, links in /dev/disk/by-path do not have it. + addr = strings.NewReplacer("[", "", "]", "").Replace(tp) + } if iscsiTransport == "tcp" { - devicePath = strings.Join([]string{"/dev/disk/by-path/ip", tp, "iscsi", b.Iqn, "lun", b.Lun}, "-") + devicePath = strings.Join([]string{"/dev/disk/by-path/ip", addr, "iscsi", b.Iqn, "lun", b.Lun}, "-") } else { - devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", tp, "iscsi", b.Iqn, "lun", b.Lun}, "-") + devicePath = strings.Join([]string{"/dev/disk/by-path/pci", "*", "ip", addr, "iscsi", b.Iqn, "lun", b.Lun}, "-") } if exist := waitForPathToExist(&devicePath, deviceDiscoveryTimeout, iscsiTransport); !exist { diff --git a/pkg/volume/iscsi/iscsi_util_test.go b/pkg/volume/iscsi/iscsi_util_test.go index 8aa3a2bce54..1015de62751 100644 --- a/pkg/volume/iscsi/iscsi_util_test.go +++ b/pkg/volume/iscsi/iscsi_util_test.go @@ -68,6 +68,11 @@ func TestExtractPortalAndIqn(t *testing.T) { if err != nil || portal != "127.0.0.1:3260" || iqn != "eui.02004567A425678D" { t.Errorf("extractPortalAndIqn: got %v %s %s", err, portal, iqn) } + devicePath = "[2001:db8:0:f101::1]:3260-iqn.2014-12.com.example:test.tgt00-lun-0" + portal, iqn, err = extractPortalAndIqn(devicePath) + if err != nil || portal != "[2001:db8:0:f101::1]:3260" || iqn != "iqn.2014-12.com.example:test.tgt00" { + t.Errorf("extractPortalAndIqn: got %v %s %s", err, portal, iqn) + } } func TestRemoveDuplicate(t *testing.T) { diff --git a/pkg/volume/util/device_util_linux.go b/pkg/volume/util/device_util_linux.go index ae7078b23ab..a0929df5fbf 100644 --- a/pkg/volume/util/device_util_linux.go +++ b/pkg/volume/util/device_util_linux.go @@ -22,6 +22,7 @@ package util import ( "errors" "fmt" + "net" "os" "path/filepath" "strconv" @@ -199,12 +200,11 @@ func (handler *deviceHandler) GetISCSIPortalHostMapForTarget(targetIqn string) ( // Add entries to the map for both the current and persistent portals // pointing to the SCSI host for those connections - portal := strings.TrimSpace(string(addr)) + ":" + - strings.TrimSpace(string(port)) + // JoinHostPort will add `[]` around IPv6 addresses. + portal := net.JoinHostPort(strings.TrimSpace(string(addr)), strings.TrimSpace(string(port))) portalHostMap[portal] = hostNumber - persistentPortal := strings.TrimSpace(string(persistentAddr)) + ":" + - strings.TrimSpace(string(persistentPort)) + persistentPortal := net.JoinHostPort(strings.TrimSpace(string(persistentAddr)), strings.TrimSpace(string(persistentPort))) portalHostMap[persistentPortal] = hostNumber } }