From 93763d25f041f0293905b9f7c0691bc871c1e7a9 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Wed, 14 Feb 2024 14:29:05 +0100 Subject: [PATCH] Use functional options to construct driver root Signed-off-by: Evan Lezar --- internal/lookup/root/options.go | 39 +++++++++++++++++++++++++++++++++ internal/lookup/root/root.go | 20 +++++++++-------- internal/modifier/graphics.go | 6 +++-- pkg/nvcdi/lib.go | 7 ++++-- 4 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 internal/lookup/root/options.go diff --git a/internal/lookup/root/options.go b/internal/lookup/root/options.go new file mode 100644 index 00000000..5774544e --- /dev/null +++ b/internal/lookup/root/options.go @@ -0,0 +1,39 @@ +/** +# Copyright 2024 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package root + +import "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" + +type Option func(*Driver) + +func WithLogger(logger logger.Interface) Option { + return func(d *Driver) { + d.logger = logger + } +} + +func WithDriverRoot(root string) Option { + return func(d *Driver) { + d.Root = root + } +} + +func WithLibrarySearchPaths(paths ...string) Option { + return func(d *Driver) { + d.librarySearchPaths = paths + } +} diff --git a/internal/lookup/root/root.go b/internal/lookup/root/root.go index f96e6b99..82ea7cb5 100644 --- a/internal/lookup/root/root.go +++ b/internal/lookup/root/root.go @@ -32,22 +32,24 @@ type Driver struct { librarySearchPaths []string } -// New creates a new Driver root at the specified path. -// TODO: Use functional options here. -func New(logger logger.Interface, path string, librarySearchPaths []string) *Driver { - return &Driver{ - logger: logger, - Root: path, - librarySearchPaths: normalizeSearchPaths(librarySearchPaths...), +// New creates a new Driver root using the specified options. +func New(opts ...Option) *Driver { + d := &Driver{} + for _, opt := range opts { + opt(d) } + if d.logger == nil { + d.logger = logger.New() + } + return d } -// Drivers returns a Locator for driver libraries. +// Libraries returns a Locator for driver libraries. func (r *Driver) Libraries() lookup.Locator { return lookup.NewLibraryLocator( lookup.WithLogger(r.logger), lookup.WithRoot(r.Root), - lookup.WithSearchPaths(r.librarySearchPaths...), + lookup.WithSearchPaths(normalizeSearchPaths(r.librarySearchPaths...)...), ) } diff --git a/internal/modifier/graphics.go b/internal/modifier/graphics.go index c0a98d1c..023c1d7b 100644 --- a/internal/modifier/graphics.go +++ b/internal/modifier/graphics.go @@ -35,8 +35,10 @@ func NewGraphicsModifier(logger logger.Interface, cfg *config.Config, image imag return nil, nil } - // TODO: We should not just pass `nil` as the search path here. - driver := root.New(logger, cfg.NVIDIAContainerCLIConfig.Root, nil) + driver := root.New( + root.WithLogger(logger), + root.WithDriverRoot(cfg.NVIDIAContainerCLIConfig.Root), + ) nvidiaCTKPath := cfg.NVIDIACTKConfig.Path mounts, err := discover.NewGraphicsMountsDiscoverer( diff --git a/pkg/nvcdi/lib.go b/pkg/nvcdi/lib.go index bd575249..b450956f 100644 --- a/pkg/nvcdi/lib.go +++ b/pkg/nvcdi/lib.go @@ -93,8 +93,11 @@ func New(opts ...Option) (Interface, error) { l.infolib = info.New() } - // TODO: We need to improve the construction of this driver root. - l.driver = root.New(l.logger, l.driverRoot, l.librarySearchPaths) + l.driver = root.New( + root.WithLogger(l.logger), + root.WithDriverRoot(l.driverRoot), + root.WithLibrarySearchPaths(l.librarySearchPaths...), + ) var lib Interface switch l.resolveMode() {