diff --git a/cmd/nvidia-ctk/cdi/generate/generate.go b/cmd/nvidia-ctk/cdi/generate/generate.go index 24ad57a4..8f6917d6 100644 --- a/cmd/nvidia-ctk/cdi/generate/generate.go +++ b/cmd/nvidia-ctk/cdi/generate/generate.go @@ -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" diff --git a/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go b/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go index 3aa5dc9f..16ef3ed5 100644 --- a/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go +++ b/cmd/nvidia-ctk/hook/create-symlinks/create-symlinks.go @@ -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" ) diff --git a/internal/modifier/csv.go b/internal/modifier/csv.go index db89af1b..56adcdc7 100644 --- a/internal/modifier/csv.go +++ b/internal/modifier/csv.go @@ -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" ) diff --git a/internal/discover/csv.go b/internal/platform-support/tegra/csv.go similarity index 80% rename from internal/discover/csv.go rename to internal/platform-support/tegra/csv.go index 2d3b4018..a4552c59 100644 --- a/internal/discover/csv.go +++ b/internal/platform-support/tegra/csv.go @@ -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 } diff --git a/internal/discover/csv/csv.go b/internal/platform-support/tegra/csv/csv.go similarity index 100% rename from internal/discover/csv/csv.go rename to internal/platform-support/tegra/csv/csv.go diff --git a/internal/discover/csv/csv_test.go b/internal/platform-support/tegra/csv/csv_test.go similarity index 100% rename from internal/discover/csv/csv_test.go rename to internal/platform-support/tegra/csv/csv_test.go diff --git a/internal/discover/csv/mount_spec.go b/internal/platform-support/tegra/csv/mount_spec.go similarity index 100% rename from internal/discover/csv/mount_spec.go rename to internal/platform-support/tegra/csv/mount_spec.go diff --git a/internal/discover/csv/mount_spec_test.go b/internal/platform-support/tegra/csv/mount_spec_test.go similarity index 100% rename from internal/discover/csv/mount_spec_test.go rename to internal/platform-support/tegra/csv/mount_spec_test.go diff --git a/internal/discover/csv_test.go b/internal/platform-support/tegra/csv_test.go similarity index 53% rename from internal/discover/csv_test.go rename to internal/platform-support/tegra/csv_test.go index f3c6dfeb..6889e200 100644 --- a/internal/discover/csv_test.go +++ b/internal/platform-support/tegra/csv_test.go @@ -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) }) } } diff --git a/internal/discover/symlinks.go b/internal/platform-support/tegra/symlinks.go similarity index 87% rename from internal/discover/symlinks.go rename to internal/platform-support/tegra/symlinks.go index 1ed8adda..607e96c6 100644 --- a/internal/discover/symlinks.go +++ b/internal/platform-support/tegra/symlinks.go @@ -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 diff --git a/internal/discover/tegra/tegra.go b/internal/platform-support/tegra/tegra.go similarity index 93% rename from internal/discover/tegra/tegra.go rename to internal/platform-support/tegra/tegra.go index 3091c500..bf75853b 100644 --- a/internal/discover/tegra/tegra.go +++ b/internal/platform-support/tegra/tegra.go @@ -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) } diff --git a/pkg/nvcdi/lib-csv.go b/pkg/nvcdi/lib-csv.go index 997724bf..244f473c 100644 --- a/pkg/nvcdi/lib-csv.go +++ b/pkg/nvcdi/lib-csv.go @@ -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" diff --git a/pkg/nvcdi/lib.go b/pkg/nvcdi/lib.go index ad5dd270..4c7febe0 100644 --- a/pkg/nvcdi/lib.go +++ b/pkg/nvcdi/lib.go @@ -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"