Compare commits

...

15 Commits

Author SHA1 Message Date
Alexandre Peixoto Ferreira
fe5dca8f1c
Use v1.20.11 in YAML files
Signed-off-by: Alexandre Peixoto Ferreira <alexandref75@gmail.com>
2022-09-11 10:42:00 -05:00
Alexandre Peixoto Ferreira
07f4b88b53
Compile device-manager statically and use scratch base image
Signed-off-by: Alexandre Peixoto Ferreira <alexandref75@gmail.com>
2022-09-11 09:39:34 -05:00
Alexandre Peixoto Ferreira
d65b7208db
Fix characteres on filenames not allowed on resources v2
Signed-off-by: Alexandre Peixoto Ferreira <alexandref75@gmail.com>
2022-07-18 13:33:38 -05:00
Alexandre Peixoto Ferreira
fe9b26d283
Fix sanitizing filenames
Signed-off-by: Alexandre Peixoto Ferreira <alexandref75@gmail.com>
2022-07-18 12:45:02 -05:00
Alexandre Peixoto Ferreira
e20fb9b58f
Update version tusing an up-to-date alpine image
Signed-off-by: Alexandre Peixoto Ferreira <alexandref75@gmail.com>
2022-07-07 14:33:32 -05:00
Alexandre Ferreira
741d8edc23 Merge branch 'dev' into 'master'
Fix examples to tag 1.20.7

See merge request arm-research/smarter/smarter-device-manager!19
2021-05-18 21:18:07 +00:00
Alexandre Ferreira
261f70121a Fix examples to tag 1.20.7 2021-05-18 16:16:55 -05:00
Alexandre Ferreira
8a304aa5ee Merge branch 'dev' into 'master'
Fix recursion problem

See merge request arm-research/smarter/smarter-device-manager!18
2021-05-18 21:16:01 +00:00
Alexandre Ferreira
1421f564e5 Fix recursion problem 2021-05-18 16:06:16 -05:00
Alexandre Ferreira
567ac37994 Merge branch 'dev' into 'master'
Fix opening /dev/<files> to see if it is a directory (dangerous and it can take too long)

See merge request arm-research/smarter/smarter-device-manager!17
2021-05-18 19:55:29 +00:00
Alexandre Ferreira
a6c2189a98 Fix opening /dev/<files> to see if it is a directory (dangerous and it can take too long) 2021-05-18 14:45:08 -05:00
Alexandre Ferreira
1d718e57be Merge branch 'dev' into 'master'
Fix examples to add the new version

See merge request arm-research/smarter/smarter-device-manager!16
2021-05-18 17:42:05 +00:00
Alexandre Ferreira
733ac32871 Fix examples to add the new version 2021-05-18 12:32:19 -05:00
Alexandre Ferreira
ab97aba78f Merge branch 'dev' into 'master'
Fix access to files on subdirectorties on /dev

See merge request arm-research/smarter/smarter-device-manager!15
2021-05-18 17:22:59 +00:00
Alexandre Ferreira
8adb22dc29 Fix access to files on subdirectorties on /dev 2021-05-18 12:11:31 -05:00
12 changed files with 74 additions and 29 deletions

View File

@ -10,15 +10,13 @@ COPY . .
RUN echo $PATH;export CGO_LDFLAGS_ALLOW='-Wl,--unresolved-symbols=ignore-in-object-files' && \
go mod init arm.com/smarter-device-management && go mod tidy && go mod vendor && \
go build -ldflags="-s -w" .
CGO_ENABLED=0 go build -ldflags='-s -w -extldflags="-static"' .
FROM alpine
RUN apk update && apk upgrade
FROM scratch
WORKDIR /root
COPY conf.yaml /root/config/conf.yaml
COPY --from=build /arm.com/smarter-device-management/smarter-device-management /usr/bin/smarter-device-management
CMD ["smarter-device-management","-logtostderr=true","-v=0"]
CMD ["/usr/bin/smarter-device-management","-logtostderr=true","-v=0"]

View File

@ -17,6 +17,10 @@ The smarter-device-manager starts by reading a YAML configuration file. This con
nummaxdevices: 10
```
Devices in subdirectories have the slash replaced with underscore in the
resource name, due to kubernetes naming restrictions: e.g. `/dev/net/tun`
becomes `smarter-devices/net_tun`.
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.
The node will show the devices it recognizes as resources in the node object in Kubernetes. The example below shows a raspberry PI.

73
main.go
View File

@ -55,18 +55,60 @@ func init() {
flag.Parse()
}
func readDevDirectory(dirToList string) (files []string, err error) {
f, err := os.Open(dirToList)
if err != nil {
return nil, err
}
files, err = f.Readdirnames(-1)
f.Close()
if err != nil {
return nil, err
}
func readDevDirectory(dirToList string, allowedRecursions uint8) (files []string, err error) {
var foundFiles []string
return files, nil
fType, err := os.Stat(dirToList)
if err != nil {
return nil, err
}
if !fType.IsDir() {
return nil, nil
}
f, err := os.Open(dirToList)
if err != nil {
return nil, err
}
files, err = f.Readdirnames(-1)
if err != nil {
f.Close()
return nil, err
}
f.Close()
for _, subDir := range files {
foundFiles = append(foundFiles, subDir)
if allowedRecursions > 0 {
filesDir, err := readDevDirectory(dirToList+"/"+subDir,allowedRecursions-1)
if err == nil {
for _, fileName := range filesDir {
foundFiles = append(foundFiles, subDir+"/"+fileName)
}
}
}
}
return foundFiles, nil
}
func sanitizeName(path string) string {
sanitizeChar := func(r rune) rune {
switch {
case r >= 'A' && r <= 'Z':
return r
case r >= 'a' && r <= 'z':
return r
case r >= '0' && r <= '9':
return r
case r == '_':
return r
case r == '-':
return r
}
return '_'
}
return strings.Map(sanitizeChar, path)
}
func findDevicesPattern(listDevices []string, pattern string) ([]string,error) {
@ -102,13 +144,13 @@ func main() {
}
glog.V(0).Info("Reading existing devices on /dev")
ExistingDevices, err := readDevDirectory("/dev")
ExistingDevices, err := readDevDirectory("/dev",10)
if err != nil {
glog.Errorf(err.Error())
os.Exit(1)
}
ExistingDevicesSys, err := readDevDirectory("/sys/devices")
ExistingDevicesSys, err := readDevDirectory("/sys/devices",0)
if err != nil {
glog.Errorf(err.Error())
os.Exit(1)
@ -151,9 +193,10 @@ func main() {
if len(foundDevices) > 0 {
for _, deviceToCreate := range foundDevices {
var newDevice DeviceInstance
deviceSafeName := sanitizeName(deviceToCreate)
newDevice.deviceType = deviceFileType
newDevice.deviceName = "smarter-devices/" + deviceToCreate
newDevice.socketName = pluginapi.DevicePluginPath + "smarter-" + deviceToCreate + ".sock"
newDevice.deviceName = "smarter-devices/" + deviceSafeName
newDevice.socketName = pluginapi.DevicePluginPath + "smarter-" + deviceSafeName + ".sock"
newDevice.deviceFile = "/dev/" + deviceToCreate
newDevice.numDevices = deviceToTest.NumMaxDevices
listDevicesAvailable = append(listDevicesAvailable, newDevice)

View File

@ -15,7 +15,7 @@ spec:
nodeName: smarter-jetson-xavier-4bcc2584
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -15,7 +15,7 @@ spec:
nodeName: <replace with node to run>
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -15,7 +15,7 @@ spec:
nodeName: <replace with node to run>
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -34,7 +34,7 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -34,7 +34,7 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -34,7 +34,7 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -34,7 +34,7 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -34,7 +34,7 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false

View File

@ -34,7 +34,7 @@ spec:
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: smarter-device-manager
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.1.2
image: registry.gitlab.com/arm-research/smarter/smarter-device-manager:v1.20.11
imagePullPolicy: IfNotPresent
securityContext:
allowPrivilegeEscalation: false