mirror of
				https://github.com/NVIDIA/nvidia-container-toolkit
				synced 2025-06-26 18:18:24 +00:00 
			
		
		
		
	Allow locator to be marked as optional
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
		
							parent
							
								
									27c82c19ea
								
							
						
					
					
						commit
						408eeae70f
					
				| @ -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", | ||||
|  | ||||
| @ -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) | ||||
| } | ||||
|  | ||||
| @ -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, | ||||
|  | ||||
| @ -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*", | ||||
|  | ||||
| @ -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.
 | ||||
|  | ||||
| @ -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) | ||||
| 		}) | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user