From 9b64d74f6ad4eaf84c9594c75d3d9b8f6885bf76 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Tue, 18 Jul 2023 11:57:26 +0200 Subject: [PATCH 1/2] Use functional options when constructing Symlink locator Signed-off-by: Evan Lezar --- .../hook/create-symlinks/create-symlinks.go | 5 ++++- internal/discover/csv.go | 5 ++++- internal/discover/symlinks.go | 5 ++++- internal/lookup/library.go | 2 +- internal/lookup/symlinks.go | 11 ++++------- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go b/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go index 3b253ed7..3aa5dc9f 100644 --- a/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go +++ b/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go @@ -101,7 +101,10 @@ func (m command) run(c *cli.Context, cfg *config) error { 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 for _, file := range csvFiles { diff --git a/internal/discover/csv.go b/internal/discover/csv.go index 7b747b1c..f5ffe61c 100644 --- a/internal/discover/csv.go +++ b/internal/discover/csv.go @@ -33,7 +33,10 @@ func NewFromCSVFiles(logger logger.Interface, files []string, driverRoot string) return None{}, nil } - symlinkLocator := lookup.NewSymlinkLocator(logger, driverRoot) + symlinkLocator := lookup.NewSymlinkLocator( + lookup.WithLogger(logger), + lookup.WithRoot(driverRoot), + ) locators := map[csv.MountSpecType]lookup.Locator{ csv.MountSpecDev: lookup.NewCharDeviceLocator(lookup.WithLogger(logger), lookup.WithRoot(driverRoot)), csv.MountSpecDir: lookup.NewDirectoryLocator(logger, driverRoot), diff --git a/internal/discover/symlinks.go b/internal/discover/symlinks.go index b1afadef..1ed8adda 100644 --- a/internal/discover/symlinks.go +++ b/internal/discover/symlinks.go @@ -113,7 +113,10 @@ func (d symlinkHook) getSpecificLinks() ([]string, error) { } 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 for _, file := range d.csvFiles { diff --git a/internal/lookup/library.go b/internal/lookup/library.go index fabf53fe..0b5b7937 100644 --- a/internal/lookup/library.go +++ b/internal/lookup/library.go @@ -41,7 +41,7 @@ func NewLibraryLocator(logger logger.Interface, root string) (Locator, error) { l := library{ logger: logger, - symlink: NewSymlinkLocator(logger, root), + symlink: NewSymlinkLocator(WithLogger(logger), WithRoot(root)), cache: cache, } diff --git a/internal/lookup/symlinks.go b/internal/lookup/symlinks.go index 27fd9096..002783cb 100644 --- a/internal/lookup/symlinks.go +++ b/internal/lookup/symlinks.go @@ -20,7 +20,6 @@ import ( "fmt" "path/filepath" - "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "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. -// A logger can also be specified. -func NewSymlinkChainLocator(logger logger.Interface, root string) Locator { - f := newFileLocator(WithLogger(logger), WithRoot(root)) +func NewSymlinkChainLocator(opts ...Option) Locator { + f := newFileLocator(opts...) l := symlinkChain{ 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. -// A logger can also be specified. -func NewSymlinkLocator(logger logger.Interface, root string) Locator { - f := newFileLocator(WithLogger(logger), WithRoot(root)) +func NewSymlinkLocator(opts ...Option) Locator { + f := newFileLocator(opts...) l := symlink{ file: *f, } From ce7d5f7a5170b92a7cf5228698228942636d5883 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Tue, 18 Jul 2023 15:36:03 +0200 Subject: [PATCH 2/2] Use functional options when constructing direcory locator Signed-off-by: Evan Lezar --- internal/discover/csv.go | 2 +- internal/discover/gds.go | 2 +- internal/lookup/dir.go | 14 ++++++-------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/internal/discover/csv.go b/internal/discover/csv.go index f5ffe61c..2d3b4018 100644 --- a/internal/discover/csv.go +++ b/internal/discover/csv.go @@ -39,7 +39,7 @@ func NewFromCSVFiles(logger logger.Interface, files []string, driverRoot string) ) locators := map[csv.MountSpecType]lookup.Locator{ 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 csv.MountSpecLib: symlinkLocator, csv.MountSpecSym: symlinkLocator, diff --git a/internal/discover/gds.go b/internal/discover/gds.go index 01360d82..c0307f26 100644 --- a/internal/discover/gds.go +++ b/internal/discover/gds.go @@ -38,7 +38,7 @@ func NewGDSDiscoverer(logger logger.Interface, root string) (Discover, error) { udev := NewMounts( logger, - lookup.NewDirectoryLocator(logger, root), + lookup.NewDirectoryLocator(lookup.WithLogger(logger), lookup.WithRoot(root)), root, []string{"/run/udev"}, ) diff --git a/internal/lookup/dir.go b/internal/lookup/dir.go index cf6eeb0b..edf42b56 100644 --- a/internal/lookup/dir.go +++ b/internal/lookup/dir.go @@ -19,17 +19,15 @@ package lookup import ( "fmt" "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 -// is also specified. -func NewDirectoryLocator(logger logger.Interface, root string) Locator { +// NewDirectoryLocator creates a Locator that can be used to find directories at the specified root. +func NewDirectoryLocator(opts ...Option) Locator { return NewFileLocator( - WithLogger(logger), - WithRoot(root), - WithFilter(assertDirectory), + append( + opts, + WithFilter(assertDirectory), + )..., ) }