Skip to content

Commit 9a8f07d

Browse files
committed
remove VM API call dep in azure disk WaitForAttach
add comment add unit test for WaitForAttach fnc add unit test for WaitForAttach Func
1 parent df3e59f commit 9a8f07d

File tree

6 files changed

+117
-72
lines changed

6 files changed

+117
-72
lines changed

pkg/volume/azure_dd/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ filegroup(
6060
go_test(
6161
name = "go_default_test",
6262
srcs = [
63+
"attacher_test.go",
6364
"azure_common_test.go",
6465
"azure_dd_block_test.go",
6566
"azure_dd_test.go",

pkg/volume/azure_dd/attacher.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"runtime"
2424
"strconv"
25+
"strings"
2526
"time"
2627

2728
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
@@ -133,36 +134,24 @@ func (a *azureDiskAttacher) VolumesAreAttached(specs []*volume.Spec, nodeName ty
133134
}
134135

135136
func (a *azureDiskAttacher) WaitForAttach(spec *volume.Spec, devicePath string, _ *v1.Pod, timeout time.Duration) (string, error) {
136-
volumeSource, _, err := getVolumeSource(spec)
137-
if err != nil {
138-
return "", err
137+
// devicePath could be a LUN number or
138+
// "/dev/disk/azure/scsi1/lunx", "/dev/sdx" on Linux node
139+
// "/dev/diskx" on Windows node
140+
if strings.HasPrefix(devicePath, "/dev/") {
141+
return devicePath, nil
139142
}
140143

141-
diskController, err := getDiskController(a.plugin.host)
144+
volumeSource, _, err := getVolumeSource(spec)
142145
if err != nil {
143146
return "", err
144147
}
145148

146149
nodeName := types.NodeName(a.plugin.host.GetHostName())
147150
diskName := volumeSource.DiskName
148151

149-
lun := int32(-1)
150-
if runtime.GOOS != "windows" {
151-
// on Linux, usually devicePath is like "/dev/disk/azure/scsi1/lun2", get LUN directly
152-
lun, err = getDiskLUN(devicePath)
153-
if err != nil {
154-
klog.V(2).Infof("azureDisk - WaitForAttach: getDiskLUN(%s) failed with error: %v", devicePath, err)
155-
}
156-
}
157-
158-
if lun < 0 {
159-
klog.V(2).Infof("azureDisk - WaitForAttach: begin to GetDiskLun by diskName(%s), DataDiskURI(%s), nodeName(%s), devicePath(%s)",
160-
diskName, volumeSource.DataDiskURI, nodeName, devicePath)
161-
lun, err = diskController.GetDiskLun(diskName, volumeSource.DataDiskURI, nodeName)
162-
if err != nil {
163-
return "", err
164-
}
165-
klog.V(2).Infof("azureDisk - WaitForAttach: GetDiskLun succeeded, got lun(%v)", lun)
152+
lun, err := strconv.Atoi(devicePath)
153+
if err != nil {
154+
return "", fmt.Errorf("parse %s failed with error: %v, diskName: %s, nodeName: %s", devicePath, err, diskName, nodeName)
166155
}
167156

168157
exec := a.plugin.host.GetExec(a.plugin.GetPluginName())
@@ -249,6 +238,14 @@ func (attacher *azureDiskAttacher) MountDevice(spec *volume.Spec, devicePath str
249238
if notMnt {
250239
diskMounter := util.NewSafeFormatAndMountFromHost(azureDataDiskPluginName, attacher.plugin.host)
251240
mountOptions := util.MountOptionFromSpec(spec, options...)
241+
if runtime.GOOS == "windows" {
242+
// only parse devicePath on Windows node
243+
diskNum, err := getDiskNum(devicePath)
244+
if err != nil {
245+
return err
246+
}
247+
devicePath = diskNum
248+
}
252249
err = diskMounter.FormatAndMount(devicePath, deviceMountPath, *volumeSource.FSType, mountOptions)
253250
if err != nil {
254251
if cleanErr := os.Remove(deviceMountPath); cleanErr != nil {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package azure_dd
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
"time"
23+
24+
"github.com/stretchr/testify/assert"
25+
26+
"k8s.io/api/core/v1"
27+
"k8s.io/kubernetes/pkg/volume"
28+
)
29+
30+
func createVolSpec(name string, readOnly bool) *volume.Spec {
31+
return &volume.Spec{
32+
Volume: &v1.Volume{
33+
VolumeSource: v1.VolumeSource{
34+
AzureDisk: &v1.AzureDiskVolumeSource{
35+
DiskName: name,
36+
ReadOnly: &readOnly,
37+
},
38+
},
39+
},
40+
}
41+
}
42+
func TestWaitForAttach(t *testing.T) {
43+
tests := []struct {
44+
devicePath string
45+
expected string
46+
expectError bool
47+
}{
48+
{
49+
devicePath: "/dev/disk/azure/scsi1/lun0",
50+
expected: "/dev/disk/azure/scsi1/lun0",
51+
expectError: false,
52+
},
53+
{
54+
devicePath: "/dev/sdc",
55+
expected: "/dev/sdc",
56+
expectError: false,
57+
},
58+
{
59+
devicePath: "/dev/disk0",
60+
expected: "/dev/disk0",
61+
expectError: false,
62+
},
63+
}
64+
65+
attacher := azureDiskAttacher{}
66+
spec := createVolSpec("fakedisk", false)
67+
68+
for _, test := range tests {
69+
result, err := attacher.WaitForAttach(spec, test.devicePath, nil, 3000*time.Millisecond)
70+
assert.Equal(t, result, test.expected)
71+
assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err))
72+
}
73+
}

pkg/volume/azure_dd/azure_common.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"path/filepath"
2424
"regexp"
25-
"strconv"
2625
libstrings "strings"
2726

2827
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
@@ -62,7 +61,9 @@ var (
6261
string(api.AzureDedicatedBlobDisk),
6362
string(api.AzureManagedDisk))
6463

65-
lunPathRE = regexp.MustCompile(`/dev/disk/azure/scsi(?:.*)/lun(.+)`)
64+
// only for Windows node
65+
winDiskNumRE = regexp.MustCompile(`/dev/disk(.+)`)
66+
winDiskNumFormat = "/dev/disk%d"
6667
)
6768

6869
func getPath(uid types.UID, volName string, host volume.VolumeHost) string {
@@ -206,24 +207,12 @@ func strFirstLetterToUpper(str string) string {
206207
return libstrings.ToUpper(string(str[0])) + str[1:]
207208
}
208209

209-
// getDiskLUN : deviceInfo could be a LUN number or a device path, e.g. /dev/disk/azure/scsi1/lun2
210-
func getDiskLUN(deviceInfo string) (int32, error) {
211-
var diskLUN string
212-
if len(deviceInfo) <= 2 {
213-
diskLUN = deviceInfo
214-
} else {
215-
// extract the LUN num from a device path
216-
matches := lunPathRE.FindStringSubmatch(deviceInfo)
217-
if len(matches) == 2 {
218-
diskLUN = matches[1]
219-
} else {
220-
return -1, fmt.Errorf("cannot parse deviceInfo: %s", deviceInfo)
221-
}
222-
}
223-
224-
lun, err := strconv.Atoi(diskLUN)
225-
if err != nil {
226-
return -1, err
210+
// getDiskNum : extract the disk num from a device path,
211+
// deviceInfo format could be like this: e.g. /dev/disk2
212+
func getDiskNum(deviceInfo string) (string, error) {
213+
matches := winDiskNumRE.FindStringSubmatch(deviceInfo)
214+
if len(matches) == 2 {
215+
return matches[1], nil
227216
}
228-
return int32(lun), nil
217+
return "", fmt.Errorf("cannot parse deviceInfo: %s, correct format: /dev/disk?", deviceInfo)
229218
}

pkg/volume/azure_dd/azure_common_test.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -183,57 +183,42 @@ func TestNormalizeStorageAccountType(t *testing.T) {
183183
}
184184
}
185185

186-
func TestGetDiskLUN(t *testing.T) {
186+
func TestGetDiskNum(t *testing.T) {
187187
tests := []struct {
188188
deviceInfo string
189-
expectedLUN int32
189+
expectedNum string
190190
expectError bool
191191
}{
192192
{
193-
deviceInfo: "0",
194-
expectedLUN: 0,
193+
deviceInfo: "/dev/disk0",
194+
expectedNum: "0",
195195
expectError: false,
196196
},
197197
{
198-
deviceInfo: "10",
199-
expectedLUN: 10,
198+
deviceInfo: "/dev/disk99",
199+
expectedNum: "99",
200200
expectError: false,
201201
},
202-
{
203-
deviceInfo: "11d",
204-
expectedLUN: -1,
205-
expectError: true,
206-
},
207-
{
208-
deviceInfo: "999",
209-
expectedLUN: -1,
210-
expectError: true,
211-
},
212202
{
213203
deviceInfo: "",
214-
expectedLUN: -1,
204+
expectedNum: "",
215205
expectError: true,
216206
},
217207
{
218-
deviceInfo: "/dev/disk/azure/scsi1/lun2",
219-
expectedLUN: 2,
220-
expectError: false,
221-
},
222-
{
223-
deviceInfo: "/dev/disk/azure/scsi0/lun12",
224-
expectedLUN: 12,
225-
expectError: false,
208+
deviceInfo: "/dev/disk",
209+
expectedNum: "",
210+
expectError: true,
226211
},
227212
{
228-
deviceInfo: "/dev/disk/by-id/scsi1/lun2",
229-
expectedLUN: -1,
213+
deviceInfo: "999",
214+
expectedNum: "",
230215
expectError: true,
231216
},
232217
}
233218

234219
for _, test := range tests {
235-
result, err := getDiskLUN(test.deviceInfo)
236-
assert.Equal(t, result, test.expectedLUN)
220+
result, err := getDiskNum(test.deviceInfo)
221+
assert.Equal(t, result, test.expectedNum)
237222
assert.Equal(t, err != nil, test.expectError, fmt.Sprintf("error msg: %v", err))
238223
}
239224
}

pkg/volume/azure_dd/azure_common_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func findDiskByLun(lun int, iohandler ioHandler, exec mount.Exec) (string, error
8484
if d, ok := v["number"]; ok {
8585
if diskNum, ok := d.(float64); ok {
8686
klog.V(2).Infof("azureDisk Mount: got disk number(%d) by LUN(%d)", int(diskNum), lun)
87-
return strconv.Itoa(int(diskNum)), nil
87+
return fmt.Sprintf(winDiskNumFormat, int(diskNum)), nil
8888
}
8989
klog.Warningf("LUN(%d) found, but could not get disk number(%q), location: %q", lun, d, location)
9090
}

0 commit comments

Comments
 (0)