Merge pull request #427 from elezar/add-functional-options-to-driver-root

Use functional options to construct driver root
This commit is contained in:
Evan Lezar 2024-03-25 20:17:54 +02:00 committed by GitHub
commit bdfa525a75
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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
}
// 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...)...),
)
}

View File

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

View File

@ -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() {