Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
c8c7139d
Commit
c8c7139d
authored
Aug 15, 2017
by
allencloud
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor codes in volume iscsi to improve readability
Signed-off-by:
allencloud
<
allen.sun@daocloud.io
>
parent
6109b401
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
35 deletions
+42
-35
iscsi_util.go
pkg/volume/iscsi/iscsi_util.go
+42
-35
No files found.
pkg/volume/iscsi/iscsi_util.go
View file @
c8c7139d
...
@@ -46,7 +46,9 @@ var (
...
@@ -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 {
...
@@ -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 {
...
@@ -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
...
@@ -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
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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) {
...
@@ -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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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) {
...
@@ -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
_o
utput
:=
re
.
FindStringSubmatch
(
mntPath
)
re
O
utput
:=
re
.
FindStringSubmatch
(
mntPath
)
if
re
_o
utput
!=
nil
{
if
re
O
utput
!=
nil
{
return
re
_o
utput
[
1
],
true
return
re
O
utput
[
1
],
true
}
}
return
""
,
false
return
""
,
false
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment