Merge pull request #70002 from andyzhangx/getdisklun

fix azure disk attachment error on Linux
This commit is contained in:
k8s-ci-robot 2018-10-24 10:53:51 -07:00 committed by GitHub
commit 3abb9f0ad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 8 deletions

View File

@ -152,8 +152,6 @@ func (a *azureDiskAttacher) VolumesAreAttached(specs []*volume.Spec, nodeName ty
}
func (a *azureDiskAttacher) WaitForAttach(spec *volume.Spec, devicePath string, _ *v1.Pod, timeout time.Duration) (string, error) {
var err error
volumeSource, _, err := getVolumeSource(spec)
if err != nil {
return "", err
@ -167,13 +165,22 @@ func (a *azureDiskAttacher) WaitForAttach(spec *volume.Spec, devicePath string,
nodeName := types.NodeName(a.plugin.host.GetHostName())
diskName := volumeSource.DiskName
glog.V(2).Infof("azureDisk - WaitForAttach: begin to GetDiskLun by diskName(%s), DataDiskURI(%s), nodeName(%s), devicePath(%s)",
diskName, volumeSource.DataDiskURI, nodeName, devicePath)
lun, err := diskController.GetDiskLun(diskName, volumeSource.DataDiskURI, nodeName)
if err != nil {
return "", err
var lun int32
if runtime.GOOS == "windows" {
glog.V(2).Infof("azureDisk - WaitForAttach: begin to GetDiskLun by diskName(%s), DataDiskURI(%s), nodeName(%s), devicePath(%s)",
diskName, volumeSource.DataDiskURI, nodeName, devicePath)
lun, err = diskController.GetDiskLun(diskName, volumeSource.DataDiskURI, nodeName)
if err != nil {
return "", err
}
glog.V(2).Infof("azureDisk - WaitForAttach: GetDiskLun succeeded, got lun(%v)", lun)
} else {
lun, err = getDiskLUN(devicePath)
if err != nil {
return "", err
}
}
glog.V(2).Infof("azureDisk - WaitForAttach: GetDiskLun succeeded, got lun(%v)", lun)
exec := a.plugin.host.GetExec(a.plugin.GetPluginName())
io := &osIOHandler{}

View File

@ -21,6 +21,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
libstrings "strings"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2018-07-01/storage"
@ -59,6 +61,8 @@ var (
string(api.AzureSharedBlobDisk),
string(api.AzureDedicatedBlobDisk),
string(api.AzureManagedDisk))
lunPathRE = regexp.MustCompile(`/dev/disk/azure/scsi(?:.*)/lun(.+)`)
)
func getPath(uid types.UID, volName string, host volume.VolumeHost) string {
@ -201,3 +205,25 @@ func strFirstLetterToUpper(str string) string {
}
return libstrings.ToUpper(string(str[0])) + str[1:]
}
// getDiskLUN : deviceInfo could be a LUN number or a device path, e.g. /dev/disk/azure/scsi1/lun2
func getDiskLUN(deviceInfo string) (int32, error) {
var diskLUN string
if len(deviceInfo) <= 2 {
diskLUN = deviceInfo
} else {
// extract the LUN num from a device path
matches := lunPathRE.FindStringSubmatch(deviceInfo)
if len(matches) == 2 {
diskLUN = matches[1]
} else {
return -1, fmt.Errorf("cannot parse deviceInfo: %s", deviceInfo)
}
}
lun, err := strconv.Atoi(diskLUN)
if err != nil {
return -1, err
}
return int32(lun), nil
}

View File

@ -187,3 +187,58 @@ func TestNormalizeStorageAccountType(t *testing.T) {
assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err))
}
}
func TestGetDiskLUN(t *testing.T) {
tests := []struct {
deviceInfo string
expectedLUN int32
expectError bool
}{
{
deviceInfo: "0",
expectedLUN: 0,
expectError: false,
},
{
deviceInfo: "10",
expectedLUN: 10,
expectError: false,
},
{
deviceInfo: "11d",
expectedLUN: -1,
expectError: true,
},
{
deviceInfo: "999",
expectedLUN: -1,
expectError: true,
},
{
deviceInfo: "",
expectedLUN: -1,
expectError: true,
},
{
deviceInfo: "/dev/disk/azure/scsi1/lun2",
expectedLUN: 2,
expectError: false,
},
{
deviceInfo: "/dev/disk/azure/scsi0/lun12",
expectedLUN: 12,
expectError: false,
},
{
deviceInfo: "/dev/disk/by-id/scsi1/lun2",
expectedLUN: -1,
expectError: true,
},
}
for _, test := range tests {
result, err := getDiskLUN(test.deviceInfo)
assert.Equal(t, result, test.expectedLUN)
assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err))
}
}