Skip to content

Commit

Permalink
Use functional options to construct driver root
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Lezar <[email protected]>
  • Loading branch information
elezar committed Mar 25, 2024
1 parent 1ddc859 commit f14c609
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
39 changes: 39 additions & 0 deletions internal/lookup/root/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
# Copyright 2023 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
}
}
20 changes: 11 additions & 9 deletions internal/lookup/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)...),
)
}

Expand Down
6 changes: 4 additions & 2 deletions internal/modifier/graphics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions pkg/nvcdi/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit f14c609

Please sign in to comment.