Merge pull request #50688 from allencloud/refactor-code-in-volume-iscsi

Automatic merge from submit-queue (batch tested with PRs 49861, 50933, 51380, 50688, 51305)

refactor codes in volume iscsi to improve readability

Signed-off-by: allencloud <allen.sun@daocloud.io>



**What this PR does / why we need it**:
This PR refactors some codes in pkg/volume/iscsi. What is specific, this PR takes advantage of return fast to make codes indent less. As a result the readability of codes will improve a little bit.

What I did:
1.  refactor codes in volume iscsi to improve readability.
2.  change a keyword of `delete` into `deleteArgs` to reduce ambiguousness.
3.  make some variables camel case. 

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
NONE

**Special notes for your reviewer**:
NONE

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-08-27 22:20:54 -07:00 committed by GitHub
commit 855f3e78e8

View File

@ -46,7 +46,9 @@ var (
) )
func updateISCSIDiscoverydb(b iscsiDiskMounter, tp string) error { func updateISCSIDiscoverydb(b iscsiDiskMounter, tp string) error {
if b.chap_discovery { if !b.chap_discovery {
return nil
}
out, err := b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "-o", "update", "-n", "discovery.sendtargets.auth.authmethod", "-v", "CHAP") out, err := b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "-o", "update", "-n", "discovery.sendtargets.auth.authmethod", "-v", "CHAP")
if err != nil { if err != nil {
return fmt.Errorf("iscsi: failed to update discoverydb with CHAP, output: %v", string(out)) return fmt.Errorf("iscsi: failed to update discoverydb with CHAP, output: %v", string(out))
@ -61,12 +63,14 @@ func updateISCSIDiscoverydb(b iscsiDiskMounter, tp string) error {
} }
} }
} }
}
return nil return nil
} }
func updateISCSINode(b iscsiDiskMounter, tp string) error { func updateISCSINode(b iscsiDiskMounter, tp string) error {
if b.chap_session { if !b.chap_session {
return nil
}
out, err := b.exec.Run("iscsiadm", "-m", "node", "-p", tp, "-T", b.Iqn, "-I", b.Iface, "-o", "update", "-n", "node.session.auth.authmethod", "-v", "CHAP") out, err := b.exec.Run("iscsiadm", "-m", "node", "-p", tp, "-T", b.Iqn, "-I", b.Iface, "-o", "update", "-n", "node.session.auth.authmethod", "-v", "CHAP")
if err != nil { if err != nil {
return fmt.Errorf("iscsi: failed to update node with CHAP, output: %v", string(out)) return fmt.Errorf("iscsi: failed to update node with CHAP, output: %v", string(out))
@ -81,7 +85,6 @@ func updateISCSINode(b iscsiDiskMounter, tp string) error {
} }
} }
} }
}
return nil return nil
} }
@ -96,7 +99,10 @@ func waitForPathToExist(devicePath *string, maxRetries int, deviceTransport stri
} }
func waitForPathToExistInternal(devicePath *string, maxRetries int, deviceTransport string, osStat StatFunc, filepathGlob GlobFunc) bool { func waitForPathToExistInternal(devicePath *string, maxRetries int, deviceTransport string, osStat StatFunc, filepathGlob GlobFunc) bool {
if devicePath != nil { if devicePath == nil {
return false
}
for i := 0; i < maxRetries; i++ { for i := 0; i < maxRetries; i++ {
var err error var err error
if deviceTransport == "tcp" { if deviceTransport == "tcp" {
@ -123,7 +129,6 @@ func waitForPathToExistInternal(devicePath *string, maxRetries int, deviceTransp
} }
time.Sleep(time.Second) time.Sleep(time.Second)
} }
}
return false return false
} }
@ -231,13 +236,18 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
if iscsiTransport == "" { if iscsiTransport == "" {
glog.Errorf("iscsi: could not find transport name in iface %s", b.Iface) glog.Errorf("iscsi: could not find transport name in iface %s", b.Iface)
return "", fmt.Errorf("Could not parse iface file for %s", b.Iface) return "", fmt.Errorf("Could not parse iface file for %s", b.Iface)
} else if iscsiTransport == "tcp" { }
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", tp, "iscsi", b.Iqn, "lun", b.lun}, "-")
} else { } 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", tp, "iscsi", b.Iqn, "lun", b.lun}, "-")
} }
exist := waitForPathToExist(&devicePath, 1, iscsiTransport)
if exist == false { if exist := waitForPathToExist(&devicePath, 1, iscsiTransport); exist {
glog.V(4).Infof("iscsi: devicepath (%s) exists", devicePath)
devicePaths = append(devicePaths, devicePath)
continue
}
// build discoverydb and discover iscsi target // build discoverydb and discover iscsi target
b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "-o", "new") b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "-o", "new")
// update discoverydb with CHAP secret // update discoverydb with CHAP secret
@ -246,7 +256,7 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
lastErr = fmt.Errorf("iscsi: failed to update discoverydb to portal %s error: %v", tp, err) lastErr = fmt.Errorf("iscsi: failed to update discoverydb to portal %s error: %v", tp, err)
continue continue
} }
out, err := b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "--discover") out, err = b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "--discover")
if err != nil { if err != nil {
// delete discoverydb record // delete discoverydb record
b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "-o", "delete") b.exec.Run("iscsiadm", "-m", "discoverydb", "-t", "sendtargets", "-p", tp, "-I", b.Iface, "-o", "delete")
@ -267,8 +277,7 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
lastErr = fmt.Errorf("iscsi: failed to attach disk: Error: %s (%v)", string(out), err) lastErr = fmt.Errorf("iscsi: failed to attach disk: Error: %s (%v)", string(out), err)
continue continue
} }
exist = waitForPathToExist(&devicePath, 10, iscsiTransport) if exist := waitForPathToExist(&devicePath, 10, iscsiTransport); !exist {
if !exist {
glog.Errorf("Could not attach disk: Timeout after 10s") glog.Errorf("Could not attach disk: Timeout after 10s")
// update last error // update last error
lastErr = fmt.Errorf("Could not attach disk: Timeout after 10s") lastErr = fmt.Errorf("Could not attach disk: Timeout after 10s")
@ -276,10 +285,6 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
} else { } else {
devicePaths = append(devicePaths, devicePath) devicePaths = append(devicePaths, devicePath)
} }
} else {
glog.V(4).Infof("iscsi: devicepath (%s) exists", devicePath)
devicePaths = append(devicePaths, devicePath)
}
} }
if len(devicePaths) == 0 { if len(devicePaths) == 0 {
@ -348,14 +353,19 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskUnmounter, mntPath string) error {
return err return err
} }
cnt-- cnt--
if cnt != 0 {
return nil
}
// 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 {
device, prefix, err := extractDeviceAndPrefix(mntPath) device, prefix, err := extractDeviceAndPrefix(mntPath)
if err != nil { if err != nil {
return err return err
} }
refCount, err := getDevicePrefixRefCount(c.mounter, prefix) refCount, err := getDevicePrefixRefCount(c.mounter, prefix)
if err == nil && refCount == 0 { if err != nil || refCount != 0 {
return nil
}
var bkpPortal []string var bkpPortal []string
var volName, iqn, iface, initiatorName string var volName, iqn, iface, initiatorName string
found := true found := true
@ -384,20 +394,20 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskUnmounter, mntPath string) error {
} }
for _, portal := range portals { for _, portal := range portals {
logout := []string{"-m", "node", "-p", portal, "-T", iqn, "--logout"} logoutArgs := []string{"-m", "node", "-p", portal, "-T", iqn, "--logout"}
delete := []string{"-m", "node", "-p", portal, "-T", iqn, "-o", "delete"} deleteArgs := []string{"-m", "node", "-p", portal, "-T", iqn, "-o", "delete"}
if found { if found {
logout = append(logout, []string{"-I", iface}...) logoutArgs = append(logoutArgs, []string{"-I", iface}...)
delete = append(delete, []string{"-I", iface}...) deleteArgs = append(deleteArgs, []string{"-I", iface}...)
} }
glog.Infof("iscsi: log out target %s iqn %s iface %s", portal, iqn, iface) glog.Infof("iscsi: log out target %s iqn %s iface %s", portal, iqn, iface)
out, err := c.exec.Run("iscsiadm", logout...) out, err := c.exec.Run("iscsiadm", logoutArgs...)
if err != nil { if err != nil {
glog.Errorf("iscsi: failed to detach disk Error: %s", string(out)) glog.Errorf("iscsi: failed to detach disk Error: %s", string(out))
} }
// Delete the node record // Delete the node record
glog.Infof("iscsi: delete node record target %s iqn %s", portal, iqn) glog.Infof("iscsi: delete node record target %s iqn %s", portal, iqn)
out, err = c.exec.Run("iscsiadm", delete...) out, err = c.exec.Run("iscsiadm", deleteArgs...)
if err != nil { if err != nil {
glog.Errorf("iscsi: failed to delete node record Error: %s", string(out)) glog.Errorf("iscsi: failed to delete node record Error: %s", string(out))
} }
@ -405,27 +415,24 @@ func (util *ISCSIUtil) DetachDisk(c iscsiDiskUnmounter, mntPath string) error {
// Delete the iface after all sessions have logged out // Delete the iface after all sessions have logged out
// If the iface is not created via iscsi plugin, skip to delete // If the iface is not created via iscsi plugin, skip to delete
if initiatorName != "" && found && iface == (portals[0]+":"+volName) { if initiatorName != "" && found && iface == (portals[0]+":"+volName) {
delete := []string{"-m", "iface", "-I", iface, "-o", "delete"} deleteArgs := []string{"-m", "iface", "-I", iface, "-o", "delete"}
out, err := c.exec.Run("iscsiadm", delete...) out, err := c.exec.Run("iscsiadm", deleteArgs...)
if err != nil { if err != nil {
glog.Errorf("iscsi: failed to delete iface Error: %s", string(out)) glog.Errorf("iscsi: failed to delete iface Error: %s", string(out))
} }
} }
}
}
return nil return nil
} }
func extractTransportname(ifaceOutput string) (iscsiTransport string) { func extractTransportname(ifaceOutput string) (iscsiTransport string) {
re := regexp.MustCompile(`iface.transport_name = (.*)\n`) re := regexp.MustCompile(`iface.transport_name = (.*)\n`)
rex_output := re.FindStringSubmatch(ifaceOutput) rexOutput := re.FindStringSubmatch(ifaceOutput)
if rex_output != nil { if rexOutput == nil {
iscsiTransport = rex_output[1]
} else {
return "" return ""
} }
iscsiTransport = rexOutput[1]
// While iface.transport_name is a required parameter, handle it being unspecified anyways // While iface.transport_name is a required parameter, handle it being unspecified anyways
if iscsiTransport == "<empty>" { if iscsiTransport == "<empty>" {
@ -452,9 +459,9 @@ func extractDeviceAndPrefix(mntPath string) (string, string, error) {
func extractIface(mntPath string) (string, bool) { func extractIface(mntPath string) (string, bool) {
re := regexp.MustCompile(`.+/iface-([^/]+)/.+`) re := regexp.MustCompile(`.+/iface-([^/]+)/.+`)
re_output := re.FindStringSubmatch(mntPath) reOutput := re.FindStringSubmatch(mntPath)
if re_output != nil { if reOutput != nil {
return re_output[1], true return reOutput[1], true
} }
return "", false return "", false