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) {
metaDevices := discover.NewDeviceDiscoverer(
logger,
lookup.NewCharDeviceLocator(logger, root),
lookup.NewCharDeviceLocator(
lookup.WithLogger(logger),
lookup.WithRoot(root),
),
root,
[]string{
"/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.
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)
}

View File

@ -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,

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) {
allDevices := NewDeviceDiscoverer(
logger,
lookup.NewCharDeviceLocator(logger, root),
lookup.NewCharDeviceLocator(
lookup.WithLogger(logger),
lookup.WithRoot(root),
),
root,
[]string{
"/dev/dri/card*",

View File

@ -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.

View File

@ -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)
})

View File

@ -32,6 +32,7 @@ type file struct {
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