Implemented suggestions for #39202 fix to facilitate kubelet upgrade. The detachDisk behavior is now preserved for pods that were created before the kubelet upgrade.

This commit is contained in:
Cristian Pop 2017-02-14 22:50:26 +02:00
parent 2aaeefeeb8
commit b23b475498
2 changed files with 34 additions and 31 deletions

View File

@ -92,7 +92,7 @@ func getDevicePrefixRefCount(mounter mount.Interface, deviceNamePrefix string) (
// make a directory like /var/lib/kubelet/plugins/kubernetes.io/iscsi/iface_name/portal-some_iqn-lun-lun_id
func makePDNameInternal(host volume.VolumeHost, portal string, iqn string, lun string, iface string) string {
return path.Join(host.GetPluginDir(iscsiPluginName), iface, portal+"-"+iqn+"-lun-"+lun)
return path.Join(host.GetPluginDir(iscsiPluginName), "iface-"+iface, portal+"-"+iqn+"-lun-"+lun)
}
type ISCSIUtil struct{}
@ -216,24 +216,29 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskUnmounter, mntPath string) error {
return err
}
refCount, err := getDevicePrefixRefCount(c.mounter, prefix)
if err == nil && refCount == 0 {
// this portal/iqn/iface are no longer referenced, log out
// extract iface from mount path
iface, err := extractIface(mntPath)
if err != nil {
return err
}
// extract portal and iqn from device path
// This portal/iqn/iface is no longer referenced, log out.
// Extract the portal and iqn from device path.
portal, iqn, err := extractPortalAndIqn(device)
if err != nil {
return err
}
glog.Infof("iscsi: log out target %s iqn %s iface %s", portal, iqn, iface)
// logout may fail as no session may exist for the portal/IQN on the specified interface
out, err := c.plugin.execCommand("iscsiadm", []string{"-m", "node", "-p", portal, "-T", iqn, "-I", iface, "--logout"})
if err != nil {
glog.Errorf("iscsi: failed to detach disk Error: %s", string(out))
// Extract the iface from the mountPath and use it to log out. If the iface
// is not found, maintain the previous behavior to facilitate kubelet upgrade.
// Logout may fail as no session may exist for the portal/IQN on the specified interface.
iface, found := extractIface(mntPath)
if found {
glog.Infof("iscsi: log out target %s iqn %s iface %s", portal, iqn, iface)
out, err := c.plugin.execCommand("iscsiadm", []string{"-m", "node", "-p", portal, "-T", iqn, "-I", iface, "--logout"})
if err != nil {
glog.Errorf("iscsi: failed to detach disk Error: %s", string(out))
}
} else {
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 {
glog.Errorf("iscsi: failed to detach disk Error: %s", string(out))
}
}
}
}
@ -272,21 +277,15 @@ func extractDeviceAndPrefix(mntPath string) (string, string, error) {
return device, prefix, nil
}
func extractIface(mntPath string) (string, error) {
ind := strings.LastIndex(mntPath, "/")
if ind < 0 {
return "", fmt.Errorf("iscsi detach disk: malformatted mnt path: %s", mntPath)
func extractIface(mntPath string) (string, bool) {
re := regexp.MustCompile(`.+/iface-([^/]+)/.+`)
re_output := re.FindStringSubmatch(mntPath)
if re_output != nil {
return re_output[1], true
}
baseMntPath := mntPath[:ind]
ind = strings.LastIndex(baseMntPath, "/")
if ind < 0 {
return "", fmt.Errorf("iscsi detach disk: malformatted mnt path: %s", mntPath)
}
iface := baseMntPath[(ind + 1):]
return iface, nil
return "", false
}
func extractPortalAndIqn(device string) (string, string, error) {

View File

@ -57,7 +57,7 @@ func TestGetDevicePrefixRefCount(t *testing.T) {
func TestExtractDeviceAndPrefix(t *testing.T) {
devicePath := "127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00"
mountPrefix := "/var/lib/kubelet/plugins/kubernetes.io/iscsi/default/" + devicePath
mountPrefix := "/var/lib/kubelet/plugins/kubernetes.io/iscsi/iface-default/" + devicePath
lun := "-lun-0"
device, prefix, err := extractDeviceAndPrefix(mountPrefix + lun)
if err != nil || device != (devicePath+lun) || prefix != mountPrefix {
@ -68,9 +68,13 @@ func TestExtractDeviceAndPrefix(t *testing.T) {
func TestExtractIface(t *testing.T) {
ifaceName := "default"
devicePath := "127.0.0.1:3260-iqn.2014-12.com.example:test.tgt00-lun-0"
iface, err := extractIface("/var/lib/kubelet/plugins/kubernetes.io/iscsi/" + ifaceName + "/" + devicePath)
if err != nil || iface != ifaceName {
t.Errorf("extractIface: expected %s, got %v %s", ifaceName, err, iface)
iface, found := extractIface("/var/lib/kubelet/plugins/kubernetes.io/iscsi/iface-" + ifaceName + "/" + devicePath)
if !found || iface != ifaceName {
t.Errorf("extractIface: expected %s and %t, got %s and %t", ifaceName, true, iface, found)
}
iface, found = extractIface("/var/lib/kubelet/plugins/kubernetes.io/iscsi/" + devicePath)
if found || iface != "" {
t.Errorf("extractIface: expected %s and %t, got %s and %t", "", false, iface, found)
}
}