Skip to content

Commit be37f54

Browse files
committed
add additional container selection rule
1 parent 1a1ca13 commit be37f54

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

internal/orchestrator/orchestrator.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,10 +1043,12 @@ func getCurrentUser() string {
10431043
}
10441044

10451045
type deviceResult struct {
1046-
devicePaths []string
1047-
hasVideoDevice bool
1048-
hasSoundDevice bool
1049-
hasGPUDevice bool
1046+
devicePaths []string
1047+
deviceCgroupRules []string
1048+
additionalDeviceVolumes []string
1049+
hasVideoDevice bool
1050+
hasSoundDevice bool
1051+
hasGPUDevice bool
10501052
}
10511053

10521054
func getDevices() (*deviceResult, error) {
@@ -1066,10 +1068,18 @@ func getDevices() (*deviceResult, error) {
10661068
}
10671069
// Verify if there are real video devices (cameras) in /dev/v4l/by-id
10681070
if camDevices := getVideoDevices(); len(camDevices) > 0 {
1071+
res.deviceCgroupRules = append(res.deviceCgroupRules, "c 81:* rwm") // rule for V4L devices
1072+
if paths.New("/dev/v4l").Exist() {
1073+
res.additionalDeviceVolumes = append(res.additionalDeviceVolumes, "/dev/v4l")
1074+
}
10691075
res.hasVideoDevice = true
10701076
}
10711077
// Verify if there are real sound devices in /dev/snd/by-id
10721078
if sndDev := getSoundDevices(); len(sndDev) > 0 {
1079+
res.deviceCgroupRules = append(res.deviceCgroupRules, "c 116:* rwm") // rule for ALSA devices
1080+
if paths.New("/dev/snd").Exist() {
1081+
res.additionalDeviceVolumes = append(res.additionalDeviceVolumes, "/dev/snd")
1082+
}
10731083
res.hasSoundDevice = true
10741084
}
10751085
// Verify if we need to add GPU devices

internal/orchestrator/provision.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -326,23 +326,12 @@ func generateMainComposeFile(
326326
if err = validateDevices(devices, requiredDeviceClasses); err != nil {
327327
return fmt.Errorf("missing required device: %w", err)
328328
}
329-
if devices.hasVideoDevice {
330-
// If we are adding video devices, mount also /dev/v4l if it exists to allow access to by-id/path links
331-
if paths.New("/dev/v4l").Exist() {
329+
if len(devices.additionalDeviceVolumes) > 0 {
330+
for _, devVolume := range devices.additionalDeviceVolumes {
332331
volumes = append(volumes, volume{
333332
Type: "bind",
334-
Source: "/dev/v4l",
335-
Target: "/dev/v4l",
336-
})
337-
}
338-
}
339-
if devices.hasSoundDevice {
340-
// If we are adding sound devices, mount also /dev/snd if it exists to allow access to by-id links
341-
if paths.New("/dev/snd").Exist() {
342-
volumes = append(volumes, volume{
343-
Type: "bind",
344-
Source: "/dev/snd",
345-
Target: "/dev/snd",
333+
Source: devVolume,
334+
Target: devVolume,
346335
})
347336
}
348337
}
@@ -409,7 +398,7 @@ func generateMainComposeFile(
409398

410399
// If there are services that require devices, we need to generate an override compose file
411400
// Write additional file to override devices section in included compose files
412-
if e := generateServicesOverrideFile(app, slices.Collect(maps.Keys(services)), servicesThatRequireDevices, devices.devicePaths, getCurrentUser(), groups, overrideComposeFile, envs); e != nil {
401+
if e := generateServicesOverrideFile(app, slices.Collect(maps.Keys(services)), servicesThatRequireDevices, devices, getCurrentUser(), groups, overrideComposeFile, envs); e != nil {
413402
return e
414403
}
415404

@@ -463,7 +452,7 @@ func extractServicesFromComposeFile(composeFile *paths.Path) (map[string]service
463452
return services, nil
464453
}
465454

466-
func generateServicesOverrideFile(arduinoApp *app.ArduinoApp, services []string, servicesThatRequireDevices []string, devices []string, user string, groups []string, overrideComposeFile *paths.Path, envs helpers.EnvVars) error {
455+
func generateServicesOverrideFile(arduinoApp *app.ArduinoApp, services []string, servicesThatRequireDevices []string, devices *deviceResult, user string, groups []string, overrideComposeFile *paths.Path, envs helpers.EnvVars) error {
467456
if overrideComposeFile.Exist() {
468457
if err := overrideComposeFile.Remove(); err != nil {
469458
return fmt.Errorf("failed to remove existing override compose file: %w", err)
@@ -476,11 +465,12 @@ func generateServicesOverrideFile(arduinoApp *app.ArduinoApp, services []string,
476465
}
477466

478467
type serviceOverride struct {
479-
User string `yaml:"user,omitempty"`
480-
Devices *[]string `yaml:"devices,omitempty"`
481-
GroupAdd *[]string `yaml:"group_add,omitempty"`
482-
Labels map[string]string `yaml:"labels,omitempty"`
483-
Environment map[string]string `yaml:"environment,omitempty"`
468+
User string `yaml:"user,omitempty"`
469+
Devices *[]string `yaml:"devices,omitempty"`
470+
DeviceCgroupRules *[]string `yaml:"device_cgroup_rules,omitempty"`
471+
GroupAdd *[]string `yaml:"group_add,omitempty"`
472+
Labels map[string]string `yaml:"labels,omitempty"`
473+
Environment map[string]string `yaml:"environment,omitempty"`
484474
}
485475
var overrideCompose struct {
486476
Services map[string]serviceOverride `yaml:"services,omitempty"`
@@ -494,8 +484,9 @@ func generateServicesOverrideFile(arduinoApp *app.ArduinoApp, services []string,
494484
DockerAppPathLabel: arduinoApp.FullPath.String(),
495485
},
496486
}
497-
if slices.Contains(servicesThatRequireDevices, svc) {
498-
override.Devices = &devices
487+
if slices.Contains(servicesThatRequireDevices, svc) && devices != nil {
488+
override.Devices = &devices.devicePaths
489+
override.DeviceCgroupRules = &devices.deviceCgroupRules
499490
override.GroupAdd = &groups
500491
}
501492
override.Environment = envs

0 commit comments

Comments
 (0)