Skip to content

Commit b9723c8

Browse files
authored
Merge pull request #134 from 41ks/alex.melhem/support-mount-dirs
Add support for mountPath as directory
2 parents e18de07 + 70e7542 commit b9723c8

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Usage of generic-device-plugin:
9494
A "count" can be specified to allow a discovered device group to be scheduled multiple times.
9595
For example, to permit allocation of the FUSE device 10 times: {"name": "fuse", "groups": [{"count": 10, "paths": [{"path": "/dev/fuse"}]}]}
9696
Note: if omitted, "count" is assumed to be 1
97+
If mountPath is a directory, the device will be mounted to the directory with the name of the device.
98+
For example, to expose the serial devices to the /dev/serial directory: {"name": "serial", "groups": [{"paths": [{"path": "/dev/ttyUSB*", "mountPath": "/dev/serial/"}]}]}
9799
--domain string The domain to use when when declaring devices. (default "squat.ai")
98100
--listen string The address at which to listen for health and metrics. (default ":8080")
99101
--log-level string Log level to use. Possible values: all, debug, info, warn, error, none (default "info")

config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ For example, to allocate and mount an audio capture device: {"name": "capture",
4646
For example, to expose a CH340 serial converter: {"name": "ch340", "groups": [{"usb": [{"vendor": "1a86", "product": "7523"}]}]}
4747
A "count" can be specified to allow a discovered device group to be scheduled multiple times.
4848
For example, to permit allocation of the FUSE device 10 times: {"name": "fuse", "groups": [{"count": 10, "paths": [{"path": "/dev/fuse"}]}]}
49-
Note: if omitted, "count" is assumed to be 1`)
49+
Note: if omitted, "count" is assumed to be 1
50+
If mountPath is a directory, the device will be mounted to the directory with the name of the device.
51+
For example, to expose the serial devices to the /dev/serial directory: {"name": "serial", "groups": [{"paths": [{"path": "/dev/ttyUSB*", "mountPath": "/dev/serial/"}]}]}`)
5052
flag.String("plugin-directory", v1beta1.DevicePluginPath, "The directory in which to create plugin sockets.")
5153
flag.String("log-level", logLevelInfo, fmt.Sprintf("Log level to use. Possible values: %s", availableLogLevels))
5254
flag.String("listen", ":8080", "The address at which to listen for health and metrics.")

deviceplugin/path.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
"crypto/sha1"
1919
"fmt"
2020
"io/fs"
21+
"path/filepath"
2122
"sort"
2223
"strconv"
24+
"strings"
2325

2426
"k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
2527
)
@@ -107,6 +109,9 @@ func (gp *GenericPlugin) discoverPath() ([]device, error) {
107109
if mountPath == "" {
108110
mountPath = paths[k][i]
109111
}
112+
if strings.HasSuffix(mountPath, "/") {
113+
mountPath = mountPath + filepath.Base(paths[k][i])
114+
}
110115
switch path.Type {
111116
case DevicePathType:
112117
d.deviceSpecs = append(d.deviceSpecs, &v1beta1.DeviceSpec{

deviceplugin/path_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,63 @@ func TestDiscoverPaths(t *testing.T) {
121121
},
122122
err: nil,
123123
},
124+
{
125+
name: "multiple with mount directory",
126+
ds: &DeviceSpec{
127+
Name: "serial",
128+
Groups: []*Group{
129+
{
130+
Paths: []*Path{
131+
{
132+
Path: "/dev/ttyUSB*",
133+
MountPath: "/dev/serial/",
134+
},
135+
},
136+
},
137+
},
138+
},
139+
fs: fstest.MapFS{
140+
"dev/ttyUSB0": {},
141+
"dev/ttyUSB1": {},
142+
"dev/ttyUSB2": {},
143+
"dev/ttyUSB3": {},
144+
},
145+
out: []device{
146+
{
147+
deviceSpecs: []*v1beta1.DeviceSpec{
148+
{
149+
ContainerPath: "/dev/serial/ttyUSB0",
150+
HostPath: "/dev/ttyUSB0",
151+
},
152+
},
153+
},
154+
{
155+
deviceSpecs: []*v1beta1.DeviceSpec{
156+
{
157+
ContainerPath: "/dev/serial/ttyUSB1",
158+
HostPath: "/dev/ttyUSB1",
159+
},
160+
},
161+
},
162+
{
163+
deviceSpecs: []*v1beta1.DeviceSpec{
164+
{
165+
ContainerPath: "/dev/serial/ttyUSB2",
166+
HostPath: "/dev/ttyUSB2",
167+
},
168+
},
169+
},
170+
{
171+
deviceSpecs: []*v1beta1.DeviceSpec{
172+
{
173+
ContainerPath: "/dev/serial/ttyUSB3",
174+
HostPath: "/dev/ttyUSB3",
175+
},
176+
},
177+
},
178+
},
179+
err: nil,
180+
},
124181
{
125182
name: "only one exists",
126183
ds: &DeviceSpec{

0 commit comments

Comments
 (0)