Merge branch 'lookup-functional-options' into 'main'

Use functional options when creating Symlink and Directory locators

See merge request nvidia/container-toolkit/container-toolkit!452
This commit is contained in:
Evan Lezar 2023-07-18 13:39:23 +00:00
commit 32ec10485e
7 changed files with 25 additions and 21 deletions

View File

@ -101,7 +101,10 @@ func (m command) run(c *cli.Context, cfg *config) error {
csvFiles := cfg.filenames.Value() csvFiles := cfg.filenames.Value()
chainLocator := lookup.NewSymlinkChainLocator(m.logger, cfg.hostRoot) chainLocator := lookup.NewSymlinkChainLocator(
lookup.WithLogger(m.logger),
lookup.WithRoot(cfg.hostRoot),
)
var candidates []string var candidates []string
for _, file := range csvFiles { for _, file := range csvFiles {

View File

@ -33,10 +33,13 @@ func NewFromCSVFiles(logger logger.Interface, files []string, driverRoot string)
return None{}, nil return None{}, nil
} }
symlinkLocator := lookup.NewSymlinkLocator(logger, driverRoot) symlinkLocator := lookup.NewSymlinkLocator(
lookup.WithLogger(logger),
lookup.WithRoot(driverRoot),
)
locators := map[csv.MountSpecType]lookup.Locator{ locators := map[csv.MountSpecType]lookup.Locator{
csv.MountSpecDev: lookup.NewCharDeviceLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)), csv.MountSpecDev: lookup.NewCharDeviceLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)),
csv.MountSpecDir: lookup.NewDirectoryLocator(logger, driverRoot), csv.MountSpecDir: lookup.NewDirectoryLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)),
// Libraries and symlinks are handled in the same way // Libraries and symlinks are handled in the same way
csv.MountSpecLib: symlinkLocator, csv.MountSpecLib: symlinkLocator,
csv.MountSpecSym: symlinkLocator, csv.MountSpecSym: symlinkLocator,

View File

@ -38,7 +38,7 @@ func NewGDSDiscoverer(logger logger.Interface, root string) (Discover, error) {
udev := NewMounts( udev := NewMounts(
logger, logger,
lookup.NewDirectoryLocator(logger, root), lookup.NewDirectoryLocator(lookup.WithLogger(logger), lookup.WithRoot(root)),
root, root,
[]string{"/run/udev"}, []string{"/run/udev"},
) )

View File

@ -113,7 +113,10 @@ func (d symlinkHook) getSpecificLinks() ([]string, error) {
} }
func (d symlinkHook) getCSVFileSymlinks() []string { func (d symlinkHook) getCSVFileSymlinks() []string {
chainLocator := lookup.NewSymlinkChainLocator(d.logger, d.driverRoot) chainLocator := lookup.NewSymlinkChainLocator(
lookup.WithLogger(d.logger),
lookup.WithRoot(d.driverRoot),
)
var candidates []string var candidates []string
for _, file := range d.csvFiles { for _, file := range d.csvFiles {

View File

@ -19,17 +19,15 @@ package lookup
import ( import (
"fmt" "fmt"
"os" "os"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
) )
// NewDirectoryLocator creates a Locator that can be used to find directories at the specified root. A logger // NewDirectoryLocator creates a Locator that can be used to find directories at the specified root.
// is also specified. func NewDirectoryLocator(opts ...Option) Locator {
func NewDirectoryLocator(logger logger.Interface, root string) Locator {
return NewFileLocator( return NewFileLocator(
WithLogger(logger), append(
WithRoot(root), opts,
WithFilter(assertDirectory), WithFilter(assertDirectory),
)...,
) )
} }

View File

@ -41,7 +41,7 @@ func NewLibraryLocator(logger logger.Interface, root string) (Locator, error) {
l := library{ l := library{
logger: logger, logger: logger,
symlink: NewSymlinkLocator(logger, root), symlink: NewSymlinkLocator(WithLogger(logger), WithRoot(root)),
cache: cache, cache: cache,
} }

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"path/filepath" "path/filepath"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
) )
@ -33,9 +32,8 @@ type symlink struct {
} }
// NewSymlinkChainLocator creats a locator that can be used for locating files through symlinks. // NewSymlinkChainLocator creats a locator that can be used for locating files through symlinks.
// A logger can also be specified. func NewSymlinkChainLocator(opts ...Option) Locator {
func NewSymlinkChainLocator(logger logger.Interface, root string) Locator { f := newFileLocator(opts...)
f := newFileLocator(WithLogger(logger), WithRoot(root))
l := symlinkChain{ l := symlinkChain{
file: *f, file: *f,
} }
@ -44,9 +42,8 @@ func NewSymlinkChainLocator(logger logger.Interface, root string) Locator {
} }
// NewSymlinkLocator creats a locator that can be used for locating files through symlinks. // NewSymlinkLocator creats a locator that can be used for locating files through symlinks.
// A logger can also be specified. func NewSymlinkLocator(opts ...Option) Locator {
func NewSymlinkLocator(logger logger.Interface, root string) Locator { f := newFileLocator(opts...)
f := newFileLocator(WithLogger(logger), WithRoot(root))
l := symlink{ l := symlink{
file: *f, file: *f,
} }