Skip to content

Commit 4b2c96c

Browse files
authored
Merge pull request kubernetes#79977 from bertinatto/fix_iscsi_cleanup
Fix iSCSI storage plugin cleanup in block volumes
2 parents 601b793 + 80652c8 commit 4b2c96c

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

pkg/volume/iscsi/iscsi.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ type iscsiDisk struct {
280280
Portals []string
281281
Iqn string
282282
Lun string
283+
InitIface string
283284
Iface string
284285
chapDiscovery bool
285286
chapSession bool
@@ -551,12 +552,18 @@ func createISCSIDisk(spec *volume.Spec, podUID types.UID, plugin *iscsiPlugin, m
551552
return nil, err
552553
}
553554

555+
initIface := iface
556+
if initiatorName != "" {
557+
iface = bkportal[0] + ":" + spec.Name()
558+
}
559+
554560
return &iscsiDisk{
555561
podUID: podUID,
556562
VolName: spec.Name(),
557563
Portals: bkportal,
558564
Iqn: iqn,
559565
Lun: lun,
566+
InitIface: initIface,
560567
Iface: iface,
561568
chapDiscovery: chapDiscovery,
562569
chapSession: chapSession,

pkg/volume/iscsi/iscsi_util.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -296,27 +296,23 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
296296
var iscsiTransport string
297297
var lastErr error
298298

299-
out, err := b.exec.Run("iscsiadm", "-m", "iface", "-I", b.Iface, "-o", "show")
299+
out, err := b.exec.Run("iscsiadm", "-m", "iface", "-I", b.InitIface, "-o", "show")
300300
if err != nil {
301-
klog.Errorf("iscsi: could not read iface %s error: %s", b.Iface, string(out))
301+
klog.Errorf("iscsi: could not read iface %s error: %s", b.InitIface, string(out))
302302
return "", err
303303
}
304304

305305
iscsiTransport = extractTransportname(string(out))
306306

307307
bkpPortal := b.Portals
308308

309-
// create new iface and copy parameters from pre-configured iface to the created iface
309+
// If the initiator name was set, the iface isn't created yet,
310+
// so create it and copy parameters from the pre-configured one
310311
if b.InitiatorName != "" {
311-
// new iface name is <target portal>:<volume name>
312-
newIface := bkpPortal[0] + ":" + b.VolName
313-
err = cloneIface(b, newIface)
314-
if err != nil {
315-
klog.Errorf("iscsi: failed to clone iface: %s error: %v", b.Iface, err)
312+
if err = cloneIface(b); err != nil {
313+
klog.Errorf("iscsi: failed to clone iface: %s error: %v", b.InitIface, err)
316314
return "", err
317315
}
318-
// update iface name
319-
b.Iface = newIface
320316
}
321317

322318
// Lock the target while we login to avoid races between 2 volumes that share the same
@@ -860,10 +856,13 @@ func parseIscsiadmShow(output string) (map[string]string, error) {
860856
return params, nil
861857
}
862858

863-
func cloneIface(b iscsiDiskMounter, newIface string) error {
859+
func cloneIface(b iscsiDiskMounter) error {
864860
var lastErr error
861+
if b.InitIface == b.Iface {
862+
return fmt.Errorf("iscsi: cannot clone iface with same name: %s", b.InitIface)
863+
}
865864
// get pre-configured iface records
866-
out, err := b.exec.Run("iscsiadm", "-m", "iface", "-I", b.Iface, "-o", "show")
865+
out, err := b.exec.Run("iscsiadm", "-m", "iface", "-I", b.InitIface, "-o", "show")
867866
if err != nil {
868867
lastErr = fmt.Errorf("iscsi: failed to show iface records: %s (%v)", string(out), err)
869868
return lastErr
@@ -877,22 +876,22 @@ func cloneIface(b iscsiDiskMounter, newIface string) error {
877876
// update initiatorname
878877
params["iface.initiatorname"] = b.InitiatorName
879878
// create new iface
880-
out, err = b.exec.Run("iscsiadm", "-m", "iface", "-I", newIface, "-o", "new")
879+
out, err = b.exec.Run("iscsiadm", "-m", "iface", "-I", b.Iface, "-o", "new")
881880
if err != nil {
882881
exit, ok := err.(utilexec.ExitError)
883882
if ok && exit.ExitStatus() == iscsiadmErrorSessExists {
884-
klog.Infof("iscsi: there is a session already logged in with iface %s", newIface)
883+
klog.Infof("iscsi: there is a session already logged in with iface %s", b.Iface)
885884
} else {
886885
lastErr = fmt.Errorf("iscsi: failed to create new iface: %s (%v)", string(out), err)
887886
return lastErr
888887
}
889888
}
890889
// update new iface records
891890
for key, val := range params {
892-
_, err = b.exec.Run("iscsiadm", "-m", "iface", "-I", newIface, "-o", "update", "-n", key, "-v", val)
891+
_, err = b.exec.Run("iscsiadm", "-m", "iface", "-I", b.Iface, "-o", "update", "-n", key, "-v", val)
893892
if err != nil {
894-
b.exec.Run("iscsiadm", "-m", "iface", "-I", newIface, "-o", "delete")
895-
lastErr = fmt.Errorf("iscsi: failed to update iface records: %s (%v). iface(%s) will be used", string(out), err, b.Iface)
893+
b.exec.Run("iscsiadm", "-m", "iface", "-I", b.Iface, "-o", "delete")
894+
lastErr = fmt.Errorf("iscsi: failed to update iface records: %s (%v). iface(%s) will be used", string(out), err, b.InitIface)
896895
break
897896
}
898897
}

pkg/volume/iscsi/iscsi_util_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ func TestClonedIface(t *testing.T) {
276276
plugin := plugins[0]
277277
fakeMounter := iscsiDiskMounter{
278278
iscsiDisk: &iscsiDisk{
279+
Iface: "192.168.1.10:pv0001",
279280
plugin: plugin.(*iscsiPlugin)},
280281
exec: fakeExec,
281282
}
282-
newIface := "192.168.1.10:pv0001"
283-
cloneIface(fakeMounter, newIface)
283+
cloneIface(fakeMounter)
284284
if cmdCount != 4 {
285285
t.Errorf("expected 4 CombinedOutput() calls, got %d", cmdCount)
286286
}
@@ -305,11 +305,11 @@ func TestClonedIfaceShowError(t *testing.T) {
305305
plugin := plugins[0]
306306
fakeMounter := iscsiDiskMounter{
307307
iscsiDisk: &iscsiDisk{
308+
Iface: "192.168.1.10:pv0001",
308309
plugin: plugin.(*iscsiPlugin)},
309310
exec: fakeExec,
310311
}
311-
newIface := "192.168.1.10:pv0001"
312-
cloneIface(fakeMounter, newIface)
312+
cloneIface(fakeMounter)
313313
if cmdCount != 1 {
314314
t.Errorf("expected 1 CombinedOutput() calls, got %d", cmdCount)
315315
}
@@ -350,11 +350,11 @@ func TestClonedIfaceUpdateError(t *testing.T) {
350350
plugin := plugins[0]
351351
fakeMounter := iscsiDiskMounter{
352352
iscsiDisk: &iscsiDisk{
353+
Iface: "192.168.1.10:pv0001",
353354
plugin: plugin.(*iscsiPlugin)},
354355
exec: fakeExec,
355356
}
356-
newIface := "192.168.1.10:pv0001"
357-
cloneIface(fakeMounter, newIface)
357+
cloneIface(fakeMounter)
358358
if cmdCount != 5 {
359359
t.Errorf("expected 5 CombinedOutput() calls, got %d", cmdCount)
360360
}

0 commit comments

Comments
 (0)