generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 19
Fix PrepareResourceClaim to read the rdmadev from sysfs path as well #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
neaggarwMS
wants to merge
1
commit into
kubernetes-sigs:main
Choose a base branch
from
neaggarwMS:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package driver | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| // TestGetRdmaDeviceFromNetdevSysfs tests the sysfs fallback logic | ||
| // of getRdmaDeviceFromNetdev using a mock sysfs structure. | ||
| func TestGetRdmaDeviceFromNetdevSysfs(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| ifName string | ||
| setupFunc func(t *testing.T, baseDir string) | ||
| want string | ||
| wantErr bool | ||
| errContains string | ||
| }{ | ||
| { | ||
| name: "valid RDMA device found", | ||
| ifName: "eth0", | ||
| setupFunc: func(t *testing.T, baseDir string) { | ||
| // Create mock sysfs structure: /sys/class/net/eth0/device/infiniband/mlx5_0 | ||
| rdmaDir := filepath.Join(baseDir, "eth0", "device", "infiniband", "mlx5_0") | ||
| if err := os.MkdirAll(rdmaDir, 0755); err != nil { | ||
| t.Fatalf("failed to create mock sysfs dir: %v", err) | ||
| } | ||
| }, | ||
| want: "mlx5_0", | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "multiple RDMA devices returns first", | ||
| ifName: "eth1", | ||
| setupFunc: func(t *testing.T, baseDir string) { | ||
| // Create mock sysfs structure with multiple RDMA devices | ||
| for _, rdmaDev := range []string{"mlx5_0", "mlx5_1"} { | ||
| rdmaDir := filepath.Join(baseDir, "eth1", "device", "infiniband", rdmaDev) | ||
| if err := os.MkdirAll(rdmaDir, 0755); err != nil { | ||
| t.Fatalf("failed to create mock sysfs dir: %v", err) | ||
| } | ||
| } | ||
| }, | ||
| want: "", // Returns first found, but order is not guaranteed | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "no RDMA device - infiniband dir missing", | ||
| ifName: "eth2", | ||
| setupFunc: func(t *testing.T, baseDir string) { | ||
| // Create mock sysfs structure without infiniband dir | ||
| deviceDir := filepath.Join(baseDir, "eth2", "device") | ||
| if err := os.MkdirAll(deviceDir, 0755); err != nil { | ||
| t.Fatalf("failed to create mock sysfs dir: %v", err) | ||
| } | ||
| }, | ||
| want: "", | ||
| wantErr: true, | ||
| errContains: "no RDMA device for eth2", | ||
| }, | ||
| { | ||
| name: "no RDMA device - empty infiniband dir", | ||
| ifName: "eth3", | ||
| setupFunc: func(t *testing.T, baseDir string) { | ||
| // Create mock sysfs structure with empty infiniband dir | ||
| rdmaDir := filepath.Join(baseDir, "eth3", "device", "infiniband") | ||
| if err := os.MkdirAll(rdmaDir, 0755); err != nil { | ||
| t.Fatalf("failed to create mock sysfs dir: %v", err) | ||
| } | ||
| }, | ||
| want: "", | ||
| wantErr: true, | ||
| errContains: "no RDMA device found for eth3", | ||
| }, | ||
| { | ||
| name: "interface does not exist", | ||
| ifName: "nonexistent", | ||
| setupFunc: func(t *testing.T, baseDir string) { | ||
| // Don't create anything | ||
| }, | ||
| want: "", | ||
| wantErr: true, | ||
| errContains: "no RDMA device for nonexistent", | ||
| }, | ||
| { | ||
| name: "only files in infiniband dir, no directories", | ||
| ifName: "eth4", | ||
| setupFunc: func(t *testing.T, baseDir string) { | ||
| // Create mock sysfs structure with only files (no directories) | ||
| rdmaDir := filepath.Join(baseDir, "eth4", "device", "infiniband") | ||
| if err := os.MkdirAll(rdmaDir, 0755); err != nil { | ||
| t.Fatalf("failed to create mock sysfs dir: %v", err) | ||
| } | ||
| // Create a file instead of directory | ||
| filePath := filepath.Join(rdmaDir, "somefile") | ||
| if err := os.WriteFile(filePath, []byte("test"), 0644); err != nil { | ||
| t.Fatalf("failed to create mock file: %v", err) | ||
| } | ||
| }, | ||
| want: "", | ||
| wantErr: true, | ||
| errContains: "no RDMA device found for eth4", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Create temporary directory to mock /sys/class/net | ||
| tmpDir := t.TempDir() | ||
|
|
||
| // Setup mock sysfs structure | ||
| tc.setupFunc(t, tmpDir) | ||
|
|
||
| // Call the sysfs fallback helper with the temp dir | ||
| got, err := getRdmaDeviceFromSysfs(tmpDir, tc.ifName) | ||
|
|
||
| // Check error conditions | ||
| if tc.wantErr { | ||
| if err == nil { | ||
| t.Errorf("getRdmaDeviceFromSysfs() expected error, got nil") | ||
| return | ||
| } | ||
| if tc.errContains != "" && !strings.Contains(err.Error(), tc.errContains) { | ||
| t.Errorf("getRdmaDeviceFromSysfs() error = %v, want error containing %q", err, tc.errContains) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Errorf("getRdmaDeviceFromSysfs() unexpected error: %v", err) | ||
| return | ||
| } | ||
|
|
||
| // For the "multiple RDMA devices" case, just check we got something valid | ||
| if tc.name == "multiple RDMA devices returns first" { | ||
| if got != "mlx5_0" && got != "mlx5_1" { | ||
| t.Errorf("getRdmaDeviceFromSysfs() = %v, want mlx5_0 or mlx5_1", got) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if got != tc.want { | ||
| t.Errorf("getRdmaDeviceFromSysfs() = %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // getRdmaDeviceFromSysfs is a testable helper that implements the sysfs fallback logic | ||
| // with a configurable base path instead of hardcoded /sys/class/net. | ||
| // This mirrors the fallback logic in getRdmaDeviceFromNetdev. | ||
| func getRdmaDeviceFromSysfs(basePath, ifName string) (string, error) { | ||
| rdmaDir := filepath.Join(basePath, ifName, "device/infiniband") | ||
|
|
||
| entries, err := os.ReadDir(rdmaDir) | ||
| if err != nil { | ||
| return "", fmt.Errorf("no RDMA device for %s: %w", ifName, err) | ||
| } | ||
|
|
||
| for _, entry := range entries { | ||
| if entry.IsDir() { | ||
| return entry.Name(), nil // Return first RDMA device found (e.g., "mlx5_0") | ||
| } | ||
| } | ||
|
|
||
| return "", fmt.Errorf("no RDMA device found for %s", ifName) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we consider creating this as function in
pkg/inventory/sysfs.goas all sysnet operations added in that fileThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
Extending this further, can we please refactor the existing function
dranet/pkg/inventory/sysfs.go
Line 112 in 97d40e9
IsRDmaDeviceForNetdeviceandGetRdmaDeviceForNetdevice, whereIsRDmaDeviceForNetdeviceis a simple wrapper overGetRdmaDeviceForNetdevice(Ref. https://github.com/Mellanox/rdmamap/blob/37bd11cc4c57da931b7b117f829fb663d46ce480/rdma_map.go#L348-L368