Use options for NewLibraryLocator

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-08-15 14:29:22 +02:00
parent 64f554ef41
commit 5505886655
4 changed files with 28 additions and 26 deletions

View File

@ -49,10 +49,10 @@ func NewDRMNodesDiscoverer(logger logger.Interface, devices image.VisibleDevices
// NewGraphicsMountsDiscoverer creates a discoverer for the mounts required by graphics tools such as vulkan.
func NewGraphicsMountsDiscoverer(logger logger.Interface, driverRoot string, nvidiaCTKPath string) (Discover, error) {
locator, err := lookup.NewLibraryLocator(logger, driverRoot)
if err != nil {
return nil, fmt.Errorf("failed to construct library locator: %v", err)
}
locator := lookup.NewLibraryLocator(
lookup.WithLogger(logger),
lookup.WithRoot(driverRoot),
)
libraries := NewMounts(
logger,
locator,

View File

@ -58,13 +58,10 @@ func New(opts ...Options) lookup.Locator {
c.driverRoot = "/"
}
// TODO: Do we want to set the Count to 1 here?
l, _ := lookup.NewLibraryLocator(
c.logger,
c.driverRoot,
c.Locator = lookup.NewLibraryLocator(
lookup.WithLogger(c.logger),
lookup.WithRoot(c.driverRoot),
)
c.Locator = l
return c
}

View File

@ -30,12 +30,9 @@ type ldcacheLocator struct {
var _ Locator = (*ldcacheLocator)(nil)
// NewLibraryLocator creates a library locator using the specified logger.
func NewLibraryLocator(logger logger.Interface, root string) (Locator, error) {
// We construct a symlink locator for expected library locations.
symlinkLocator := NewSymlinkLocator(
WithLogger(logger),
WithRoot(root),
// NewLibraryLocator creates a library locator using the specified options.
func NewLibraryLocator(opts ...Option) Locator {
opts = append(opts,
WithSearchPaths([]string{
"/",
"/usr/lib64",
@ -50,24 +47,28 @@ func NewLibraryLocator(logger logger.Interface, root string) (Locator, error) {
"/lib/aarch64-linux-gnu/nvidia/current",
}...),
)
// We construct a symlink locator for expected library locations.
symlinkLocator := NewSymlinkLocator(opts...)
l := First(
symlinkLocator,
newLdcacheLocator(logger, root),
newLdcacheLocator(opts...),
)
return l, nil
return l
}
func newLdcacheLocator(logger logger.Interface, root string) Locator {
cache, err := ldcache.New(logger, root)
func newLdcacheLocator(opts ...Option) Locator {
b := newBuilder(opts...)
cache, err := ldcache.New(b.logger, b.root)
if err != nil {
// If we failed to open the LDCache, we default to a symlink locator.
logger.Warningf("Failed to load ldcache: %v", err)
b.logger.Warningf("Failed to load ldcache: %v", err)
return nil
}
return &ldcacheLocator{
logger: logger,
logger: b.logger,
cache: cache,
}
}

View File

@ -43,7 +43,10 @@ func TestLDCacheLocator(t *testing.T) {
require.NoError(t, os.Symlink(versionLib, sonameLink))
require.NoError(t, os.Symlink(sonameLink, soLink))
lut := newLdcacheLocator(logger, testDir)
lut := newLdcacheLocator(
WithLogger(logger),
WithRoot(testDir),
)
testCases := []struct {
description string
@ -94,7 +97,6 @@ func TestLDCacheLocator(t *testing.T) {
require.EqualValues(t, tc.expected, cleanedCandidates)
})
}
}
func TestLibraryLocator(t *testing.T) {
@ -125,8 +127,10 @@ func TestLibraryLocator(t *testing.T) {
require.NoError(t, os.Symlink(libTarget1, source1))
require.NoError(t, os.Symlink(source1, source2))
lut, err := NewLibraryLocator(logger, testDir)
require.NoError(t, err)
lut := NewLibraryLocator(
WithLogger(logger),
WithRoot(testDir),
)
testCases := []struct {
description string