Skip to content
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

Fix USB enumeration #28

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion deviceplugin/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ type GenericPlugin struct {
// metrics
deviceGauge prometheus.Gauge
allocationsCounter prometheus.Counter
enableUSBDiscovery bool
}

// NewGenericPlugin creates a new plugin for a generic device.
func NewGenericPlugin(ds *DeviceSpec, pluginDir string, logger log.Logger, reg prometheus.Registerer) Plugin {
func NewGenericPlugin(ds *DeviceSpec, pluginDir string, logger log.Logger, reg prometheus.Registerer, enableUSBDiscovery bool) Plugin {
if logger == nil {
logger = log.NewNopLogger()
}
Expand All @@ -115,6 +116,7 @@ func NewGenericPlugin(ds *DeviceSpec, pluginDir string, logger log.Logger, reg p
Name: "generic_device_plugin_allocations_total",
Help: "The total number of device allocations made by this device plugin.",
}),
enableUSBDiscovery: enableUSBDiscovery,
}

if reg != nil {
Expand All @@ -129,6 +131,11 @@ func (gp *GenericPlugin) discover() (devices []device, err error) {
if err != nil {
return nil, fmt.Errorf("failed to discover path devices: %w", err)
}

if !gp.enableUSBDiscovery {
return path, nil
}

usb, err := gp.discoverUSB()
if err != nil {
return nil, fmt.Errorf("failed to discover usb devices: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions deviceplugin/usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func queryUSBDeviceCharacteristicsByDirectory(dir os.DirEntry) (result *usbDevic
func enumerateUSBDevices(dir string) (specs []usbDevice, err error) {
allDevs, err := os.ReadDir(dir)
if err != nil {
return
return []usbDevice{}, err
}

// Set up a WaitGroup with a buffered channel for results
Expand Down Expand Up @@ -243,7 +243,8 @@ func (gp *GenericPlugin) discoverUSB() (devices []device, err error) {
var paths []string
usbDevs, err := enumerateUSBDevices(usbDevicesDir)
if err != nil {
return devices, err
level.Warn(gp.logger).Log("msg", fmt.Sprintf("failed to enumerate usb devices: %s", err.Error()))
squat marked this conversation as resolved.
Show resolved Hide resolved
return devices, nil
}
for _, dev := range group.USBSpecs {
matches, err := searchUSBDevices(&usbDevs, dev.Vendor, dev.Product)
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,17 @@ func Main() error {
pluginPath := viper.GetString("plugin-directory")
for i := range deviceSpecs {
d := deviceSpecs[i]

enableUSBDiscovery := false
for _, g := range d.Groups {
if len(g.USBSpecs) > 0 {
enableUSBDiscovery = true
break
}
}

ctx, cancel := context.WithCancel(context.Background())
gp := deviceplugin.NewGenericPlugin(d, pluginPath, log.With(logger, "resource", d.Name), prometheus.WrapRegistererWith(prometheus.Labels{"resource": d.Name}, r))
gp := deviceplugin.NewGenericPlugin(d, pluginPath, log.With(logger, "resource", d.Name), prometheus.WrapRegistererWith(prometheus.Labels{"resource": d.Name}, r), enableUSBDiscovery)
// Start the generic device plugin server.
g.Add(func() error {
logger.Log("msg", fmt.Sprintf("Starting the generic-device-plugin for %q.", d.Name))
Expand Down