mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 00:07:50 +00:00
Merge pull request #85705 from ralfonso/iscsi_refcounter_block_fix
Fix iscsi refcounter in the case of no Block iscsi volumes
This commit is contained in:
commit
e941b46b22
@ -42,6 +42,7 @@ go_test(
|
|||||||
],
|
],
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
|
"//pkg/kubelet/config:go_default_library",
|
||||||
"//pkg/volume:go_default_library",
|
"//pkg/volume:go_default_library",
|
||||||
"//pkg/volume/testing:go_default_library",
|
"//pkg/volume/testing:go_default_library",
|
||||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||||
|
@ -926,9 +926,13 @@ func isSessionBusy(host volume.VolumeHost, portal, iqn string) bool {
|
|||||||
// getVolCount returns the number of volumes in use by the kubelet.
|
// getVolCount returns the number of volumes in use by the kubelet.
|
||||||
// It does so by counting the number of directories prefixed by the given portal and IQN.
|
// It does so by counting the number of directories prefixed by the given portal and IQN.
|
||||||
func getVolCount(dir, portal, iqn string) (int, error) {
|
func getVolCount(dir, portal, iqn string) (int, error) {
|
||||||
// The topmost dirs are named after the ifaces, e.g., iface-default or iface-127.0.0.1:3260:pv0
|
// For FileSystem volumes, the topmost dirs are named after the ifaces, e.g., iface-default or iface-127.0.0.1:3260:pv0.
|
||||||
|
// For Block volumes, the default topmost dir is volumeDevices.
|
||||||
contents, err := ioutil.ReadDir(dir)
|
contents, err := ioutil.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,8 +23,9 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/utils/exec/testing"
|
testingexec "k8s.io/utils/exec/testing"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/kubelet/config"
|
||||||
"k8s.io/kubernetes/pkg/volume"
|
"k8s.io/kubernetes/pkg/volume"
|
||||||
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
volumetest "k8s.io/kubernetes/pkg/volume/testing"
|
||||||
)
|
)
|
||||||
@ -350,55 +351,78 @@ func TestClonedIfaceUpdateError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetVolCount(t *testing.T) {
|
func TestGetVolCount(t *testing.T) {
|
||||||
|
// This will create a dir structure like this:
|
||||||
|
// /tmp/refcounter555814673
|
||||||
|
// ├── iface-127.0.0.1:3260:pv1
|
||||||
|
// │ └── 127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-3
|
||||||
|
// └── iface-127.0.0.1:3260:pv2
|
||||||
|
// │ ├── 127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-2
|
||||||
|
// │ └── 192.168.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-1
|
||||||
|
// └── volumeDevices
|
||||||
|
// └── 192.168.0.2:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-4
|
||||||
|
// └── 192.168.0.3:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-5
|
||||||
|
|
||||||
|
baseDir, err := createFakePluginDirs()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error creating fake plugin dir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer os.RemoveAll(baseDir)
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
baseDir string
|
||||||
portal string
|
portal string
|
||||||
iqn string
|
iqn string
|
||||||
count int
|
count int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "wrong portal, no volumes",
|
name: "wrong portal, no volumes",
|
||||||
|
baseDir: baseDir,
|
||||||
portal: "192.168.0.2:3260", // incorrect IP address
|
portal: "192.168.0.2:3260", // incorrect IP address
|
||||||
iqn: "iqn.2003-01.io.k8s:e2e.volume-1",
|
iqn: "iqn.2003-01.io.k8s:e2e.volume-1",
|
||||||
count: 0,
|
count: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "wrong iqn, no volumes",
|
name: "wrong iqn, no volumes",
|
||||||
|
baseDir: baseDir,
|
||||||
portal: "127.0.0.1:3260",
|
portal: "127.0.0.1:3260",
|
||||||
iqn: "iqn.2003-01.io.k8s:e2e.volume-3", // incorrect volume
|
iqn: "iqn.2003-01.io.k8s:e2e.volume-3", // incorrect volume
|
||||||
count: 0,
|
count: 0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "single volume",
|
name: "single volume",
|
||||||
|
baseDir: baseDir,
|
||||||
portal: "192.168.0.1:3260",
|
portal: "192.168.0.1:3260",
|
||||||
iqn: "iqn.2003-01.io.k8s:e2e.volume-1",
|
iqn: "iqn.2003-01.io.k8s:e2e.volume-1",
|
||||||
count: 1,
|
count: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "two volumes",
|
name: "two volumes",
|
||||||
|
baseDir: baseDir,
|
||||||
portal: "127.0.0.1:3260",
|
portal: "127.0.0.1:3260",
|
||||||
iqn: "iqn.2003-01.io.k8s:e2e.volume-1",
|
iqn: "iqn.2003-01.io.k8s:e2e.volume-1",
|
||||||
count: 2,
|
count: 2,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "volumeDevices (block) volume",
|
||||||
|
baseDir: filepath.Join(baseDir, config.DefaultKubeletVolumeDevicesDirName),
|
||||||
|
portal: "192.168.0.2:3260",
|
||||||
|
iqn: "iqn.2003-01.io.k8s:e2e.volume-1-lun-4",
|
||||||
|
count: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nonexistent path",
|
||||||
|
baseDir: filepath.Join(baseDir, "this_path_should_not_exist"),
|
||||||
|
portal: "127.0.0.1:3260",
|
||||||
|
iqn: "iqn.2003-01.io.k8s:e2e.unknown",
|
||||||
|
count: 0,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will create a dir structure like this:
|
|
||||||
// /tmp/refcounter555814673
|
|
||||||
// ├── iface-127.0.0.1:3260:pv1
|
|
||||||
// │ └── 127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-3
|
|
||||||
// └── iface-127.0.0.1:3260:pv2
|
|
||||||
// ├── 127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-2
|
|
||||||
// └── 192.168.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-1
|
|
||||||
|
|
||||||
baseDir, err := createFakePluginDir()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("error creating fake plugin dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(baseDir)
|
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
count, err := getVolCount(baseDir, tc.portal, tc.iqn)
|
count, err := getVolCount(tc.baseDir, tc.portal, tc.iqn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected no error, got %v", err)
|
t.Errorf("expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
@ -409,7 +433,7 @@ func TestGetVolCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createFakePluginDir() (string, error) {
|
func createFakePluginDirs() (string, error) {
|
||||||
dir, err := ioutil.TempDir("", "refcounter")
|
dir, err := ioutil.TempDir("", "refcounter")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -419,6 +443,8 @@ func createFakePluginDir() (string, error) {
|
|||||||
"iface-127.0.0.1:3260:pv1/127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-3",
|
"iface-127.0.0.1:3260:pv1/127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-3",
|
||||||
"iface-127.0.0.1:3260:pv2/127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-2",
|
"iface-127.0.0.1:3260:pv2/127.0.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-2",
|
||||||
"iface-127.0.0.1:3260:pv2/192.168.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-1",
|
"iface-127.0.0.1:3260:pv2/192.168.0.1:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-1",
|
||||||
|
filepath.Join(config.DefaultKubeletVolumeDevicesDirName, "iface-127.0.0.1:3260/192.168.0.2:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-4"),
|
||||||
|
filepath.Join(config.DefaultKubeletVolumeDevicesDirName, "iface-127.0.0.1:3260/192.168.0.3:3260-iqn.2003-01.io.k8s:e2e.volume-1-lun-5"),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range subdirs {
|
for _, d := range subdirs {
|
||||||
|
Loading…
Reference in New Issue
Block a user