diff --git a/cmd/nvidia-ctk/cdi/generate/common.go b/cmd/nvidia-ctk/cdi/generate/common.go index 7775acfb..212c483f 100644 --- a/cmd/nvidia-ctk/cdi/generate/common.go +++ b/cmd/nvidia-ctk/cdi/generate/common.go @@ -30,7 +30,10 @@ import ( func NewCommonDiscoverer(logger *logrus.Logger, root string, nvidiaCTKPath string, nvmllib nvml.Interface) (discover.Discover, error) { metaDevices := discover.NewDeviceDiscoverer( logger, - lookup.NewCharDeviceLocator(logger, root), + lookup.NewCharDeviceLocator( + lookup.WithLogger(logger), + lookup.WithRoot(root), + ), root, []string{ "/dev/nvidia-modeset", diff --git a/internal/discover/char_devices.go b/internal/discover/char_devices.go index 83f9d843..bd83e995 100644 --- a/internal/discover/char_devices.go +++ b/internal/discover/char_devices.go @@ -28,7 +28,10 @@ var _ Discover = (*charDevices)(nil) // NewCharDeviceDiscoverer creates a discoverer which locates the specified set of device nodes. func NewCharDeviceDiscoverer(logger *logrus.Logger, devices []string, root string) Discover { - locator := lookup.NewCharDeviceLocator(logger, root) + locator := lookup.NewCharDeviceLocator( + lookup.WithLogger(logger), + lookup.WithRoot(root), + ) return NewDeviceDiscoverer(logger, locator, root, devices) } diff --git a/internal/discover/csv.go b/internal/discover/csv.go index 6235f44f..efd01e5a 100644 --- a/internal/discover/csv.go +++ b/internal/discover/csv.go @@ -35,7 +35,7 @@ func NewFromCSVFiles(logger *logrus.Logger, files []string, root string) (Discov symlinkLocator := lookup.NewSymlinkLocator(logger, root) locators := map[csv.MountSpecType]lookup.Locator{ - csv.MountSpecDev: lookup.NewCharDeviceLocator(logger, root), + csv.MountSpecDev: lookup.NewCharDeviceLocator(lookup.WithLogger(logger), lookup.WithRoot(root)), csv.MountSpecDir: lookup.NewDirectoryLocator(logger, root), // Libraries and symlinks are handled in the same way csv.MountSpecLib: symlinkLocator, diff --git a/internal/discover/graphics.go b/internal/discover/graphics.go index 7847314c..b0a6690e 100644 --- a/internal/discover/graphics.go +++ b/internal/discover/graphics.go @@ -181,7 +181,10 @@ func (d drmDevicesByPath) getSpecificLinkArgs(devices []Device) ([]string, error func newDRMDeviceDiscoverer(logger *logrus.Logger, devices image.VisibleDevices, root string) (Discover, error) { allDevices := NewDeviceDiscoverer( logger, - lookup.NewCharDeviceLocator(logger, root), + lookup.NewCharDeviceLocator( + lookup.WithLogger(logger), + lookup.WithRoot(root), + ), root, []string{ "/dev/dri/card*", diff --git a/internal/lookup/device.go b/internal/lookup/device.go index dfa774ea..8ec6c4c9 100644 --- a/internal/lookup/device.go +++ b/internal/lookup/device.go @@ -19,8 +19,6 @@ package lookup import ( "fmt" "os" - - "github.com/sirupsen/logrus" ) const ( @@ -29,13 +27,14 @@ const ( // NewCharDeviceLocator creates a Locator that can be used to find char devices at the specified root. A logger is // also specified. -func NewCharDeviceLocator(logger *logrus.Logger, root string) Locator { - return NewFileLocator( - WithLogger(logger), - WithRoot(root), +func NewCharDeviceLocator(opts ...Option) Locator { + opts = append(opts, WithSearchPaths("", devRoot), WithFilter(assertCharDevice), ) + return NewFileLocator( + opts..., + ) } // assertCharDevice checks whether the specified path is a char device and returns an error if this is not the case. diff --git a/internal/lookup/device_test.go b/internal/lookup/device_test.go index 7062e51e..223e51e8 100644 --- a/internal/lookup/device_test.go +++ b/internal/lookup/device_test.go @@ -47,7 +47,10 @@ func TestCharDeviceLocator(t *testing.T) { for i, tc := range testCases { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - f := NewCharDeviceLocator(logger, tc.root).(*file) + f := NewCharDeviceLocator( + WithLogger(logger), + WithRoot(tc.root), + ).(*file) require.EqualValues(t, tc.expectedPrefixes, f.prefixes) }) diff --git a/internal/lookup/file.go b/internal/lookup/file.go index 0ddf0c08..c382c468 100644 --- a/internal/lookup/file.go +++ b/internal/lookup/file.go @@ -27,11 +27,12 @@ import ( // file can be used to locate file (or file-like elements) at a specified set of // prefixes. The validity of a file is determined by a filter function. type file struct { - logger *log.Logger - root string - prefixes []string - filter func(string) error - count int + logger *log.Logger + root string + prefixes []string + filter func(string) error + count int + isOptional bool } // Option defines a function for passing options to the NewFileLocator() call @@ -73,6 +74,14 @@ func WithCount(count int) Option { } } +// WithOptional sets the optional flag for the file locator +// If the optional flag is set, the locator will not return an error if the file is not found. +func WithOptional(optional bool) Option { + return func(f *file) { + f.isOptional = optional + } +} + // NewFileLocator creates a Locator that can be used to find files with the specified options. func NewFileLocator(opts ...Option) Locator { return newFileLocator(opts...) @@ -158,7 +167,7 @@ visit: } } - if len(filenames) == 0 { + if !p.isOptional && len(filenames) == 0 { return nil, fmt.Errorf("pattern %v not found", pattern) } return filenames, nil