Allow locator to be marked as optional

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-01-23 16:01:29 +01:00
parent 27c82c19ea
commit 408eeae70f
7 changed files with 37 additions and 17 deletions

View File

@ -30,7 +30,10 @@ import (
func NewCommonDiscoverer(logger *logrus.Logger, root string, nvidiaCTKPath string, nvmllib nvml.Interface) (discover.Discover, error) { func NewCommonDiscoverer(logger *logrus.Logger, root string, nvidiaCTKPath string, nvmllib nvml.Interface) (discover.Discover, error) {
metaDevices := discover.NewDeviceDiscoverer( metaDevices := discover.NewDeviceDiscoverer(
logger, logger,
lookup.NewCharDeviceLocator(logger, root), lookup.NewCharDeviceLocator(
lookup.WithLogger(logger),
lookup.WithRoot(root),
),
root, root,
[]string{ []string{
"/dev/nvidia-modeset", "/dev/nvidia-modeset",

View File

@ -28,7 +28,10 @@ var _ Discover = (*charDevices)(nil)
// NewCharDeviceDiscoverer creates a discoverer which locates the specified set of device nodes. // NewCharDeviceDiscoverer creates a discoverer which locates the specified set of device nodes.
func NewCharDeviceDiscoverer(logger *logrus.Logger, devices []string, root string) Discover { 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) return NewDeviceDiscoverer(logger, locator, root, devices)
} }

View File

@ -35,7 +35,7 @@ func NewFromCSVFiles(logger *logrus.Logger, files []string, root string) (Discov
symlinkLocator := lookup.NewSymlinkLocator(logger, root) symlinkLocator := lookup.NewSymlinkLocator(logger, root)
locators := map[csv.MountSpecType]lookup.Locator{ 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), csv.MountSpecDir: lookup.NewDirectoryLocator(logger, root),
// Libraries and symlinks are handled in the same way // Libraries and symlinks are handled in the same way
csv.MountSpecLib: symlinkLocator, csv.MountSpecLib: symlinkLocator,

View File

@ -181,7 +181,10 @@ func (d drmDevicesByPath) getSpecificLinkArgs(devices []Device) ([]string, error
func newDRMDeviceDiscoverer(logger *logrus.Logger, devices image.VisibleDevices, root string) (Discover, error) { func newDRMDeviceDiscoverer(logger *logrus.Logger, devices image.VisibleDevices, root string) (Discover, error) {
allDevices := NewDeviceDiscoverer( allDevices := NewDeviceDiscoverer(
logger, logger,
lookup.NewCharDeviceLocator(logger, root), lookup.NewCharDeviceLocator(
lookup.WithLogger(logger),
lookup.WithRoot(root),
),
root, root,
[]string{ []string{
"/dev/dri/card*", "/dev/dri/card*",

View File

@ -19,8 +19,6 @@ package lookup
import ( import (
"fmt" "fmt"
"os" "os"
"github.com/sirupsen/logrus"
) )
const ( 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 // NewCharDeviceLocator creates a Locator that can be used to find char devices at the specified root. A logger is
// also specified. // also specified.
func NewCharDeviceLocator(logger *logrus.Logger, root string) Locator { func NewCharDeviceLocator(opts ...Option) Locator {
return NewFileLocator( opts = append(opts,
WithLogger(logger),
WithRoot(root),
WithSearchPaths("", devRoot), WithSearchPaths("", devRoot),
WithFilter(assertCharDevice), 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. // assertCharDevice checks whether the specified path is a char device and returns an error if this is not the case.

View File

@ -47,7 +47,10 @@ func TestCharDeviceLocator(t *testing.T) {
for i, tc := range testCases { for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 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) require.EqualValues(t, tc.expectedPrefixes, f.prefixes)
}) })

View File

@ -27,11 +27,12 @@ import (
// file can be used to locate file (or file-like elements) at a specified set of // 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. // prefixes. The validity of a file is determined by a filter function.
type file struct { type file struct {
logger *log.Logger logger *log.Logger
root string root string
prefixes []string prefixes []string
filter func(string) error filter func(string) error
count int count int
isOptional bool
} }
// Option defines a function for passing options to the NewFileLocator() call // 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. // NewFileLocator creates a Locator that can be used to find files with the specified options.
func NewFileLocator(opts ...Option) Locator { func NewFileLocator(opts ...Option) Locator {
return newFileLocator(opts...) 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 nil, fmt.Errorf("pattern %v not found", pattern)
} }
return filenames, nil return filenames, nil