Skip to content

Commit ab97aba

Browse files
committed
Merge branch 'dev' into 'master'
Fix access to files on subdirectorties on /dev See merge request arm-research/smarter/smarter-device-manager!15
2 parents 2dfd0f1 + 8adb22d commit ab97aba

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ The smarter-device-manager starts by reading a YAML configuration file. This con
1717
nummaxdevices: 10
1818
```
1919

20+
Devices in subdirectories have the slash replaced with underscore in the
21+
resource name, due to kubernetes naming restrictions: e.g. `/dev/net/tun`
22+
becomes `smarter-devices/net_tun`.
23+
2024
The default config file provided will enable most of the devices available on a Raspberry Pi (vers 1-4) or equivalent boards. I2C, SPI, video devices, sound and others would be enabled. The config file can be replaced using a configmap to enable or disable access to different devices, like accelerators, GPUs, etc.
2125

2226
The node will show the devices it recognizes as resources in the node object in Kubernetes. The example below shows a raspberry PI.

main.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,33 @@ func init() {
5656
}
5757

5858
func readDevDirectory(dirToList string) (files []string, err error) {
59-
f, err := os.Open(dirToList)
60-
if err != nil {
61-
return nil, err
62-
}
63-
files, err = f.Readdirnames(-1)
64-
f.Close()
65-
if err != nil {
66-
return nil, err
67-
}
59+
var foundFiles []string
60+
61+
f, err := os.Open(dirToList)
62+
if err != nil {
63+
return nil, err
64+
}
65+
files, err = f.Readdirnames(-1)
66+
if err != nil {
67+
f.Close()
68+
return nil, err
69+
}
70+
f.Close()
71+
for _, subDir := range files {
72+
foundFiles = append(foundFiles, subDir)
73+
filesDir, err := readDevDirectory(dirToList+"/"+subDir)
74+
if err == nil {
75+
for _, fileName := range filesDir {
76+
foundFiles = append(foundFiles, subDir+"/"+fileName)
77+
}
78+
}
79+
}
80+
81+
return foundFiles, nil
82+
}
6883

69-
return files, nil
84+
func sanitizeName(path string) string {
85+
return strings.Replace(path, "/", "_" ,-1)
7086
}
7187

7288
func findDevicesPattern(listDevices []string, pattern string) ([]string,error) {
@@ -151,9 +167,10 @@ func main() {
151167
if len(foundDevices) > 0 {
152168
for _, deviceToCreate := range foundDevices {
153169
var newDevice DeviceInstance
170+
deviceSafeName := sanitizeName(deviceToCreate)
154171
newDevice.deviceType = deviceFileType
155-
newDevice.deviceName = "smarter-devices/" + deviceToCreate
156-
newDevice.socketName = pluginapi.DevicePluginPath + "smarter-" + deviceToCreate + ".sock"
172+
newDevice.deviceName = "smarter-devices/" + deviceSafeName
173+
newDevice.socketName = pluginapi.DevicePluginPath + "smarter-" + deviceSafeName + ".sock"
157174
newDevice.deviceFile = "/dev/" + deviceToCreate
158175
newDevice.numDevices = deviceToTest.NumMaxDevices
159176
listDevicesAvailable = append(listDevicesAvailable, newDevice)

0 commit comments

Comments
 (0)