Use functional options to construct driver root

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-02-14 14:29:05 +01:00
parent 1ddc859700
commit 93763d25f0
4 changed files with 59 additions and 13 deletions

View File

@ -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
}
}

View File

@ -32,22 +32,24 @@ type Driver struct {
librarySearchPaths []string librarySearchPaths []string
} }
// New creates a new Driver root at the specified path. // New creates a new Driver root using the specified options.
// TODO: Use functional options here. func New(opts ...Option) *Driver {
func New(logger logger.Interface, path string, librarySearchPaths []string) *Driver { d := &Driver{}
return &Driver{ for _, opt := range opts {
logger: logger, opt(d)
Root: path,
librarySearchPaths: normalizeSearchPaths(librarySearchPaths...),
} }
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 { func (r *Driver) Libraries() lookup.Locator {
return lookup.NewLibraryLocator( return lookup.NewLibraryLocator(
lookup.WithLogger(r.logger), lookup.WithLogger(r.logger),
lookup.WithRoot(r.Root), lookup.WithRoot(r.Root),
lookup.WithSearchPaths(r.librarySearchPaths...), lookup.WithSearchPaths(normalizeSearchPaths(r.librarySearchPaths...)...),
) )
} }

View File

@ -35,8 +35,10 @@ func NewGraphicsModifier(logger logger.Interface, cfg *config.Config, image imag
return nil, nil return nil, nil
} }
// TODO: We should not just pass `nil` as the search path here. driver := root.New(
driver := root.New(logger, cfg.NVIDIAContainerCLIConfig.Root, nil) root.WithLogger(logger),
root.WithDriverRoot(cfg.NVIDIAContainerCLIConfig.Root),
)
nvidiaCTKPath := cfg.NVIDIACTKConfig.Path nvidiaCTKPath := cfg.NVIDIACTKConfig.Path
mounts, err := discover.NewGraphicsMountsDiscoverer( mounts, err := discover.NewGraphicsMountsDiscoverer(

View File

@ -93,8 +93,11 @@ func New(opts ...Option) (Interface, error) {
l.infolib = info.New() l.infolib = info.New()
} }
// TODO: We need to improve the construction of this driver root. l.driver = root.New(
l.driver = root.New(l.logger, l.driverRoot, l.librarySearchPaths) root.WithLogger(l.logger),
root.WithDriverRoot(l.driverRoot),
root.WithLibrarySearchPaths(l.librarySearchPaths...),
)
var lib Interface var lib Interface
switch l.resolveMode() { switch l.resolveMode() {