Skip to content

Commit c175f2f

Browse files
authored
Merge pull request #1634 from k8s-infra-cherrypick-robot/cherry-pick-1633-to-release-1.13
[release-1.13] Fix nvme path filtering logic for udevadm trigger
2 parents a41e825 + 1b87188 commit c175f2f

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

pkg/deviceutils/device-utils.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ const (
3636
diskScsiGooglePrefix = "scsi-0Google_PersistentDisk_"
3737
diskPartitionSuffix = "-part"
3838
diskSDPath = "/dev/sd"
39-
diskSDPattern = "/dev/sd*"
39+
diskSDGlob = "/dev/sd*"
4040
diskNvmePath = "/dev/nvme"
4141
diskNvmePattern = "^/dev/nvme[0-9]+n[0-9]+$"
42+
diskNvmeGlob = "/dev/nvme*"
4243
// How many times to retry for a consistent read of /proc/mounts.
4344
maxListTries = 3
4445
// Number of fields per line in /proc/mounts as per the fstab man page.
@@ -69,6 +70,8 @@ var (
6970
scsiRegex = regexp.MustCompile(scsiPattern)
7071
// regex to parse google_nvme_id output and extract the serial
7172
nvmeRegex = regexp.MustCompile(nvmePattern)
73+
// regex to filter for disk drive paths from filepath.Glob output of diskNvmeGlob
74+
diskNvmeRegex = regexp.MustCompile(diskNvmePattern)
7275
)
7376

7477
// DeviceUtils are a collection of methods that act on the devices attached
@@ -306,15 +309,28 @@ func getDevFsSerial(devFsPath string) (string, error) {
306309
}
307310
}
308311

312+
func filterAvailableNvmeDevFsPaths(devNvmePaths []string) []string {
313+
// Devices under /dev/nvme need to be filtered for disk drive paths only.
314+
diskNvmePaths := []string{}
315+
for _, devNvmePath := range devNvmePaths {
316+
if diskNvmeRegex.MatchString(devNvmePath) {
317+
diskNvmePaths = append(diskNvmePaths, devNvmePath)
318+
}
319+
}
320+
return diskNvmePaths
321+
}
322+
309323
func findAvailableDevFsPaths() ([]string, error) {
310-
diskSDPaths, err := filepath.Glob(diskSDPattern)
324+
diskSDPaths, err := filepath.Glob(diskSDGlob)
311325
if err != nil {
312-
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskSDPattern, err)
326+
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskSDGlob, err)
313327
}
314-
diskNvmePaths, err := filepath.Glob(diskNvmePattern)
328+
devNvmePaths, err := filepath.Glob(diskNvmeGlob)
315329
if err != nil {
316-
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskNvmePattern, err)
330+
return nil, fmt.Errorf("failed to filepath.Glob(\"%s\"): %w", diskNvmeGlob, err)
317331
}
332+
// Devices under /dev/nvme need to be filtered for disk drive paths only.
333+
diskNvmePaths := filterAvailableNvmeDevFsPaths(devNvmePaths)
318334
return append(diskSDPaths, diskNvmePaths...), nil
319335
}
320336

pkg/deviceutils/device-utils_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package deviceutils
22

33
import (
44
"fmt"
5-
"regexp"
5+
"path/filepath"
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
@@ -64,8 +64,6 @@ func less(a, b fmt.Stringer) bool {
6464
// and may prevent our tests from running on all platforms. See the following test for an example:
6565
// https://github.com/golang/go/blob/d33548d178016122726342911f8e15016a691472/src/syscall/exec_linux_test.go#L250
6666
func TestDiskNvmePattern(t *testing.T) {
67-
nvmeDiskRegex := regexp.MustCompile(diskNvmePattern)
68-
6967
testCases := []struct {
7068
paths []string
7169
wantPaths []string
@@ -97,12 +95,19 @@ func TestDiskNvmePattern(t *testing.T) {
9795
}
9896

9997
for _, tc := range testCases {
100-
gotPaths := []string{}
98+
devPaths := []string{}
99+
101100
for _, path := range tc.paths {
102-
if nvmeDiskRegex.MatchString(path) {
103-
gotPaths = append(gotPaths, path)
101+
ok, err := filepath.Match(diskNvmeGlob, path)
102+
if err != nil {
103+
t.Errorf("Error encountered matching path: %v", path)
104+
}
105+
106+
if ok {
107+
devPaths = append(devPaths, path)
104108
}
105109
}
110+
gotPaths := filterAvailableNvmeDevFsPaths(devPaths)
106111
if diff := cmp.Diff(gotPaths, tc.wantPaths, cmpopts.SortSlices(less)); diff != "" {
107112
t.Errorf("Unexpected NVMe device paths (-got, +want):\n%s", diff)
108113
}

0 commit comments

Comments
 (0)