Move tegra-specifics to new package

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-06-22 15:49:27 +02:00
parent 01a7f7bb8e
commit 918bd03488
13 changed files with 72 additions and 100 deletions

View File

@ -23,8 +23,8 @@ import (
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"

View File

@ -22,11 +22,11 @@ import (
"path/filepath"
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
"github.com/urfave/cli/v2"
)

View File

@ -23,10 +23,10 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
"github.com/NVIDIA/nvidia-container-toolkit/internal/cuda"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/modifier/cdi"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/requirements"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi"
)

View File

@ -14,23 +14,24 @@
# limitations under the License.
**/
package discover
package tegra
import (
"fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
)
// NewFromCSVFiles creates a discoverer for the specified CSV files. A logger is also supplied.
// newDiscovererFromCSVFiles creates a discoverer for the specified CSV files. A logger is also supplied.
// The constructed discoverer is comprised of a list, with each element in the list being associated with a
// single CSV files.
func NewFromCSVFiles(logger logger.Interface, files []string, driverRoot string) (Discover, error) {
func newDiscovererFromCSVFiles(logger logger.Interface, files []string, driverRoot string) (discover.Discover, error) {
if len(files) == 0 {
logger.Warningf("No CSV files specified")
return None{}, nil
return discover.None{}, nil
}
symlinkLocator := lookup.NewSymlinkLocator(
@ -74,12 +75,12 @@ func loadCSVFile(logger logger.Interface, filename string) ([]*csv.MountSpec, er
// newFromMountSpecs creates a discoverer for the CSV file. A logger is also supplied.
// A list of csvDiscoverers is returned, with each being associated with a single MountSpecType.
func newFromMountSpecs(logger logger.Interface, locators map[csv.MountSpecType]lookup.Locator, driverRoot string, targets []*csv.MountSpec) (Discover, error) {
func newFromMountSpecs(logger logger.Interface, locators map[csv.MountSpecType]lookup.Locator, driverRoot string, targets []*csv.MountSpec) (discover.Discover, error) {
if len(targets) == 0 {
return &None{}, nil
return &discover.None{}, nil
}
var discoverers []Discover
var discoverers []discover.Discover
var mountSpecTypes []csv.MountSpecType
candidatesByType := make(map[csv.MountSpecType][]string)
for _, t := range targets {
@ -95,16 +96,16 @@ func newFromMountSpecs(logger logger.Interface, locators map[csv.MountSpecType]l
return nil, fmt.Errorf("no locator defined for '%v'", t)
}
var m Discover
var m discover.Discover
switch t {
case csv.MountSpecDev:
m = NewDeviceDiscoverer(logger, locator, driverRoot, candidatesByType[t])
m = discover.NewDeviceDiscoverer(logger, locator, driverRoot, candidatesByType[t])
default:
m = NewMounts(logger, locator, driverRoot, candidatesByType[t])
m = discover.NewMounts(logger, locator, driverRoot, candidatesByType[t])
}
discoverers = append(discoverers, m)
}
return &list{discoverers: discoverers}, nil
return discover.Merge(discoverers...), nil
}

View File

@ -14,14 +14,15 @@
# limitations under the License.
**/
package discover
package tegra
import (
"fmt"
"testing"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
testlog "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
)
@ -30,20 +31,32 @@ func TestNewFromMountSpec(t *testing.T) {
logger, _ := testlog.NewNullLogger()
locators := map[csv.MountSpecType]lookup.Locator{
"dev": &lookup.LocatorMock{},
"lib": &lookup.LocatorMock{},
"dev": &lookup.LocatorMock{
LocateFunc: func(pattern string) ([]string, error) {
return []string{"/dev/" + pattern}, nil
},
},
"lib": &lookup.LocatorMock{
LocateFunc: func(pattern string) ([]string, error) {
return []string{"/lib/" + pattern}, nil
},
},
}
testCases := []struct {
description string
root string
targets []*csv.MountSpec
expectedError error
expectedDiscoverer Discover
description string
root string
targets []*csv.MountSpec
expectedError error
expectedDevices []discover.Device
expectedMounts []discover.Mount
expectedHooks []discover.Hook
}{
{
description: "empty targets returns None discoverer list",
expectedDiscoverer: &None{},
description: "empty targets returns None discoverer list",
expectedDevices: []discover.Device{},
expectedMounts: []discover.Mount{},
expectedHooks: []discover.Hook{},
},
{
description: "unexpected locator returns error",
@ -71,59 +84,12 @@ func TestNewFromMountSpec(t *testing.T) {
Path: "dev1",
},
},
expectedDiscoverer: &list{
discoverers: []Discover{
(*charDevices)(
&mounts{
logger: logger,
lookup: locators["dev"],
root: "/",
required: []string{"dev0", "dev1"},
},
),
&mounts{
logger: logger,
lookup: locators["lib"],
root: "/",
required: []string{"lib0"},
},
},
expectedDevices: []discover.Device{
{Path: "/dev/dev0", HostPath: "/dev/dev0"},
{Path: "/dev/dev1", HostPath: "/dev/dev1"},
},
},
{
description: "sets root",
targets: []*csv.MountSpec{
{
Type: "dev",
Path: "dev0",
},
{
Type: "lib",
Path: "lib0",
},
{
Type: "dev",
Path: "dev1",
},
},
root: "/some/root",
expectedDiscoverer: &list{
discoverers: []Discover{
(*charDevices)(
&mounts{
logger: logger,
lookup: locators["dev"],
root: "/some/root",
required: []string{"dev0", "dev1"},
},
),
&mounts{
logger: logger,
lookup: locators["lib"],
root: "/some/root",
required: []string{"lib0"},
},
},
expectedMounts: []discover.Mount{
{Path: "/lib/lib0", HostPath: "/lib/lib0", Options: []string{"ro", "nosuid", "nodev", "bind"}},
},
},
}
@ -136,7 +102,18 @@ func TestNewFromMountSpec(t *testing.T) {
return
}
require.NoError(t, err)
require.EqualValues(t, tc.expectedDiscoverer, discoverer)
devices, err := discoverer.Devices()
require.NoError(t, err)
require.EqualValues(t, tc.expectedDevices, devices)
mounts, err := discoverer.Mounts()
require.NoError(t, err)
require.EqualValues(t, tc.expectedMounts, mounts)
hooks, err := discoverer.Hooks()
require.NoError(t, err)
require.EqualValues(t, tc.expectedHooks, hooks)
})
}
}

View File

@ -14,30 +14,31 @@
# limitations under the License.
**/
package discover
package tegra
import (
"fmt"
"path/filepath"
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
)
type symlinkHook struct {
None
discover.None
logger logger.Interface
driverRoot string
nvidiaCTKPath string
csvFiles []string
mountsFrom Discover
mountsFrom discover.Discover
}
// NewCreateSymlinksHook creates a discoverer for a hook that creates required symlinks in the container
func NewCreateSymlinksHook(logger logger.Interface, csvFiles []string, mounts Discover, nvidiaCTKPath string) (Discover, error) {
// createCSVSymlinkHooks creates a discoverer for a hook that creates required symlinks in the container
func createCSVSymlinkHooks(logger logger.Interface, csvFiles []string, mounts discover.Discover, nvidiaCTKPath string) (discover.Discover, error) {
d := symlinkHook{
logger: logger,
nvidiaCTKPath: nvidiaCTKPath,
@ -49,25 +50,18 @@ func NewCreateSymlinksHook(logger logger.Interface, csvFiles []string, mounts Di
}
// Hooks returns a hook to create the symlinks from the required CSV files
func (d symlinkHook) Hooks() ([]Hook, error) {
func (d symlinkHook) Hooks() ([]discover.Hook, error) {
specificLinks, err := d.getSpecificLinks()
if err != nil {
return nil, fmt.Errorf("failed to determine specific links: %v", err)
}
csvSymlinks := d.getCSVFileSymlinks()
var args []string
for _, link := range append(csvSymlinks, specificLinks...) {
args = append(args, "--link", link)
}
hook := CreateNvidiaCTKHook(
return discover.CreateCreateSymlinkHook(
d.nvidiaCTKPath,
"create-symlinks",
args...,
)
return []Hook{hook}, nil
append(csvSymlinks, specificLinks...),
).Hooks()
}
// getSpecificLinks returns the required specic links that need to be created

View File

@ -41,12 +41,12 @@ func New(opts ...Option) (discover.Discover, error) {
opt(o)
}
csvDiscoverer, err := discover.NewFromCSVFiles(o.logger, o.csvFiles, o.driverRoot)
csvDiscoverer, err := newDiscovererFromCSVFiles(o.logger, o.csvFiles, o.driverRoot)
if err != nil {
return nil, fmt.Errorf("failed to create CSV discoverer: %v", err)
}
createSymlinksHook, err := discover.NewCreateSymlinksHook(o.logger, o.csvFiles, csvDiscoverer, o.nvidiaCTKPath)
createSymlinksHook, err := createCSVSymlinkHooks(o.logger, o.csvFiles, csvDiscoverer, o.nvidiaCTKPath)
if err != nil {
return nil, fmt.Errorf("failed to create symlink hook discoverer: %v", err)
}

View File

@ -20,8 +20,8 @@ import (
"fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/tegra"
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
"github.com/container-orchestrated-devices/container-device-interface/specs-go"

View File

@ -19,8 +19,8 @@ package nvcdi
import (
"fmt"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover/csv"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device"