Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/nvidia-device-plugin/plugin-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
"github.com/NVIDIA/go-nvml/pkg/nvml"
"k8s.io/klog/v2"

spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
"github.com/NVIDIA/k8s-device-plugin/internal/cdi"
Expand All @@ -45,6 +46,9 @@ func GetPlugins(ctx context.Context, infolib info.Interface, nvmllib nvml.Interf
return nil, fmt.Errorf("error querying IMEX channels: %w", err)
}

resolvedStrategy := resolveStrategy(*config.Flags.DeviceDiscoveryStrategy, infolib)
klog.Infof("Using device discovery strategy: %s", resolvedStrategy)

cdiHandler, err := cdi.New(infolib, nvmllib, devicelib,
cdi.WithDeviceListStrategies(deviceListStrategies),
cdi.WithDriverRoot(string(driverRoot)),
Expand All @@ -59,6 +63,7 @@ func GetPlugins(ctx context.Context, infolib info.Interface, nvmllib nvml.Interf
cdi.WithMofedEnabled(*config.Flags.MOFEDEnabled),
cdi.WithImexChannels(imexChannels),
cdi.WithFeatureFlags(o.cdiFeatureFlags.Value()...),
cdi.WithDeviceDiscoveryStrategy(resolvedStrategy),
)
if err != nil {
return nil, fmt.Errorf("unable to create cdi handler: %v", err)
Expand All @@ -70,6 +75,7 @@ func GetPlugins(ctx context.Context, infolib info.Interface, nvmllib nvml.Interf
plugin.WithDeviceListStrategies(deviceListStrategies),
plugin.WithFailOnInitError(*config.Flags.FailOnInitError),
plugin.WithImexChannels(imexChannels),
plugin.WithDeviceDiscoveryStrategy(resolvedStrategy),
)
if err != nil {
return nil, fmt.Errorf("unable to create plugins: %w", err)
Expand All @@ -81,3 +87,23 @@ func GetPlugins(ctx context.Context, infolib info.Interface, nvmllib nvml.Interf

return plugins, nil
}

// resolveStrategy resolves an "auto" device discovery strategy to a concrete
// value based on the detected platform. Non-auto values are returned unchanged.
func resolveStrategy(strategy string, infolib info.Interface) string {
if strategy != "" && strategy != "auto" {
klog.Infof("Using requested device discovery strategy: %s", strategy)
return strategy
}

platform := infolib.ResolvePlatform()
klog.Infof("Detected platform: %s", platform)
switch platform {
case info.PlatformNVML, info.PlatformWSL:
return "nvml"
case info.PlatformTegra:
return "tegra"
}
klog.Warning("Unsupported platform detected; defaulting to nvml")
return "nvml"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/NVIDIA/go-gpuallocator v0.6.0
github.com/NVIDIA/go-nvlib v0.9.1-0.20251202135446-d0f42ba016dd
github.com/NVIDIA/go-nvml v0.13.0-1.0.20260212130905-92cf8c963449
github.com/NVIDIA/nvidia-container-toolkit v1.19.0
github.com/NVIDIA/nvidia-container-toolkit v1.19.1-0.20260410190841-2c0c91d0ebaa
github.com/fsnotify/fsnotify v1.9.0
github.com/google/renameio v1.0.1
github.com/google/uuid v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/NVIDIA/go-nvlib v0.9.1-0.20251202135446-d0f42ba016dd h1:MC1w/VYuo9Zt0
github.com/NVIDIA/go-nvlib v0.9.1-0.20251202135446-d0f42ba016dd/go.mod h1:7mzx9FSdO9fXWP9NKuZmWkCwhkEcSWQFe2tmFwtLb9c=
github.com/NVIDIA/go-nvml v0.13.0-1.0.20260212130905-92cf8c963449 h1:UrArFAaPhj9av2yzEN35CvzWw68BeQjp2MaQFUIoJSU=
github.com/NVIDIA/go-nvml v0.13.0-1.0.20260212130905-92cf8c963449/go.mod h1:ahi2psRYoa+wYUBIrZPRO+wJs9lcvMhxSSkjjvsJJNQ=
github.com/NVIDIA/nvidia-container-toolkit v1.19.0 h1:iuttW93jclh0+y3RdBw8DR7v6Gfv6h+HjjY87cTyn9M=
github.com/NVIDIA/nvidia-container-toolkit v1.19.0/go.mod h1:4aDFESAMdSDUGqFD6q3CBRaScL1ZNbMVDifD67cPxUw=
github.com/NVIDIA/nvidia-container-toolkit v1.19.1-0.20260410190841-2c0c91d0ebaa h1:/NWUzIuzA33H1hm9ze8RXdlKAsA1LVccZgdnt93MrQg=
github.com/NVIDIA/nvidia-container-toolkit v1.19.1-0.20260410190841-2c0c91d0ebaa/go.mod h1:4aDFESAMdSDUGqFD6q3CBRaScL1ZNbMVDifD67cPxUw=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
Expand Down
56 changes: 44 additions & 12 deletions internal/cdi/cdi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cdi

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -55,7 +56,8 @@ type cdiHandler struct {
vendor string
deviceIDStrategy string

deviceListStrategies spec.DeviceListStrategies
deviceListStrategies spec.DeviceListStrategies
deviceDiscoveryStrategy string

// nvcdiFeatureFlags allows finer control over CDI spec generation.
nvcdiFeatureFlags []string
Expand Down Expand Up @@ -86,8 +88,11 @@ func New(infolib info.Interface, nvmllib nvml.Interface, devicelib device.Interf
if !c.deviceListStrategies.AnyCDIEnabled() {
return &null{}, nil
}
if c.deviceDiscoveryStrategy == "" {
return nil, fmt.Errorf("device discovery strategy not set")
}
hasNVML, _ := infolib.HasNvml()
if !hasNVML {
if !hasNVML && c.deviceDiscoveryStrategy != "tegra" {
klog.Warning("No valid resources detected, creating a null CDI handler")
return &null{}, nil
}
Expand Down Expand Up @@ -128,6 +133,13 @@ func New(infolib info.Interface, nvmllib nvml.Interface, devicelib device.Interf
nvcdi.WithVendor(c.vendor),
}

// On Tegra (CSV mode), the default CSV files live under the driver root.
// Inject driver-root-aware paths explicitly so the nvcdi library does not
// fall back to the hardcoded absolute paths returned by csv.DefaultFileList().
if c.deviceDiscoveryStrategy == "tegra" {
commonOptions = append(commonOptions, nvcdi.WithCSVFiles(csvFilesForRoot(c.driverRoot)))
}

c.cdilibs = make(map[string]nvcdi.SpecGenerator)

c.cdilibs["gpu"], err = nvcdi.New(
Expand Down Expand Up @@ -177,16 +189,6 @@ func (cdi *cdiHandler) CreateSpecFile() error {
for class, cdilib := range cdi.cdilibs {
cdi.logger.Infof("Generating CDI spec for resource: %s/%s", cdi.vendor, class)

if class == "gpu" {
ret := cdi.nvmllib.Init()
if ret != nvml.SUCCESS {
return fmt.Errorf("failed to initialize NVML: %v", ret)
}
defer func() {
_ = cdi.nvmllib.Shutdown()
}()
}
Comment thread
cdesiniotis marked this conversation as resolved.

spec, err := cdilib.GetSpec()
if err != nil {
return fmt.Errorf("failed to get CDI spec: %v", err)
Expand Down Expand Up @@ -268,3 +270,33 @@ func (cdi *cdiHandler) AdditionalDevices() []string {
}
return devices
}

// defaultCSVMountSpecPath mirrors the constant defined in the nvidia-container-toolkit's
// internal/platform-support/tegra/csv package.
const defaultCSVMountSpecPath = "/etc/nvidia-container-runtime/host-files-for-container.d"

var defaultCSVFileNames = []string{"devices.csv", "drivers.csv", "l4t.csv"}

// csvFilesForRoot returns the Tegra CSV file paths to use for CDI spec generation.
// It checks for the CSV directory at the driver root first, then falls back to the
// absolute path. Returns nil if the directory is not found at either location,
// allowing nvcdi to fall back to csv.DefaultFileList().
func csvFilesForRoot(driverRoot string) []string {
roots := []string{driverRoot}
if driverRoot != "/" {
roots = append(roots, "/")
}
for _, root := range roots {
csvDir := filepath.Join(root, defaultCSVMountSpecPath)
stat, err := os.Stat(csvDir)
if err != nil || !stat.IsDir() {
continue
}
paths := make([]string, len(defaultCSVFileNames))
for i, f := range defaultCSVFileNames {
paths[i] = filepath.Join(csvDir, f)
}
return paths
}
return nil
}
68 changes: 68 additions & 0 deletions internal/cdi/csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
#
# 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 cdi

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestCSVFilesForRoot(t *testing.T) {
testCases := []struct {
description string
setup func(t *testing.T) string // returns driverRoot
expectedPaths func(driverRoot string) []string
}{
{
description: "directory exists at driver root",
setup: func(t *testing.T) string {
root := t.TempDir()
csvDir := filepath.Join(root, defaultCSVMountSpecPath)
require.NoError(t, os.MkdirAll(csvDir, 0755))
return root
},
expectedPaths: func(driverRoot string) []string {
csvDir := filepath.Join(driverRoot, defaultCSVMountSpecPath)
return []string{
filepath.Join(csvDir, "devices.csv"),
filepath.Join(csvDir, "drivers.csv"),
filepath.Join(csvDir, "l4t.csv"),
}
},
},
{
description: "directory does not exist at driver root or absolute path",
setup: func(t *testing.T) string {
return t.TempDir()
},
expectedPaths: func(_ string) []string {
return nil
},
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
driverRoot := tc.setup(t)
got := csvFilesForRoot(driverRoot)
require.Equal(t, tc.expectedPaths(driverRoot), got)
})
}
}
9 changes: 9 additions & 0 deletions internal/cdi/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ func WithImexChannels(imexChannels imex.Channels) Option {
c.imexChannels = imexChannels
}
}

// WithDeviceDiscoveryStrategy sets a pre-resolved device discovery strategy.
// When "tegra", the CDI handler uses CSV-based spec generation with
// driver-root-aware file paths.
func WithDeviceDiscoveryStrategy(strategy string) Option {
return func(c *cdiHandler) {
c.deviceDiscoveryStrategy = strategy
}
}
24 changes: 7 additions & 17 deletions internal/plugin/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type options struct {
cdiHandler cdi.Interface
config *spec.Config

deviceListStrategies spec.DeviceListStrategies
deviceListStrategies spec.DeviceListStrategies
deviceDiscoveryStrategy string

imexChannels imex.Channels
}
Expand All @@ -66,6 +67,10 @@ func New(ctx context.Context, infolib info.Interface, nvmllib nvml.Interface, de
o.cdiHandler = cdi.NewNullHandler()
}

if o.deviceDiscoveryStrategy == "" {
return nil, fmt.Errorf("device discovery strategy not set")
}

resourceManagers, err := o.getResourceManagers()
if err != nil {
return nil, fmt.Errorf("failed to construct resource managers: %w", err)
Expand All @@ -86,7 +91,7 @@ func New(ctx context.Context, infolib info.Interface, nvmllib nvml.Interface, de
// Each resource manager maps to a specific named extended resource and may
// include full GPUs or MIG devices.
func (o *options) getResourceManagers() ([]rm.ResourceManager, error) {
strategy := o.resolveStrategy(*o.config.Flags.DeviceDiscoveryStrategy)
strategy := o.deviceDiscoveryStrategy
switch strategy {
case "nvml":
ret := o.nvmllib.Init()
Expand Down Expand Up @@ -121,18 +126,3 @@ func (o *options) getResourceManagers() ([]rm.ResourceManager, error) {
return nil, nil
}
}

func (o *options) resolveStrategy(strategy string) string {
if strategy != "" && strategy != "auto" {
return strategy
}

platform := o.infolib.ResolvePlatform()
switch platform {
case info.PlatformNVML, info.PlatformWSL:
return "nvml"
case info.PlatformTegra:
return "tegra"
}
return strategy
}
8 changes: 8 additions & 0 deletions internal/plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ func WithImexChannels(imexChannels imex.Channels) Option {
m.imexChannels = imexChannels
}
}

// WithDeviceDiscoveryStrategy sets a pre-resolved device discovery strategy,
// bypassing the automatic platform-based resolution.
func WithDeviceDiscoveryStrategy(strategy string) Option {
return func(m *options) {
m.deviceDiscoveryStrategy = strategy
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ github.com/NVIDIA/go-nvlib/pkg/pciids
## explicit; go 1.20
github.com/NVIDIA/go-nvml/pkg/dl
github.com/NVIDIA/go-nvml/pkg/nvml
# github.com/NVIDIA/nvidia-container-toolkit v1.19.0
# github.com/NVIDIA/nvidia-container-toolkit v1.19.1-0.20260410190841-2c0c91d0ebaa
## explicit; go 1.25.0
github.com/NVIDIA/nvidia-container-toolkit/internal/config/image
github.com/NVIDIA/nvidia-container-toolkit/internal/devices
Expand Down
Loading