Skip to content
Open
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
21 changes: 17 additions & 4 deletions pkg/nvlib/info/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ func New(opts ...Option) Interface {
if o.devicelib == nil {
o.devicelib = device.New(o.nvmllib)
}
if o.platform == "" {
o.platform = PlatformAuto
}
if o.propertyExtractor == nil {
o.propertyExtractor = &propertyExtractor{
root: o.root,
Expand All @@ -70,9 +67,25 @@ func New(opts ...Option) Interface {
return &infolib{
PlatformResolver: &platformResolver{
logger: o.logger,
platform: o.platform,
platform: o.normalizePlatform(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

propertyExtractor: o.propertyExtractor,
},
PropertyExtractor: o.propertyExtractor,
}
}

func (o options) normalizePlatform() Platform {
if o.platform != "" && o.platform != PlatformAuto {
return o.platform
}

override, reason := getPlaformOverride()
if override != "" {
o.logger.Debugf("Using platform-override %q", override)
return Platform(override)
}

o.logger.Debugf("No platform-override detected: %v", reason)

return PlatformAuto
}
39 changes: 39 additions & 0 deletions pkg/nvlib/info/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package info

import (
"bufio"
"fmt"
"os"
"strings"
)

// Platform represents a supported plaform.
type Platform string

Expand Down Expand Up @@ -62,3 +69,35 @@ func (p platformResolver) ResolvePlatform() Platform {
return PlatformUnknown
}
}

// getPlatformOverride checks the system for a platform override file.
// This allows system administrators to force the detection of a specific
// platform.
//
// The first non-empty and non-comment line (starting with #) in the file is
// returned.
//
// Note that no checks are performed for a valid platform value.
//
// This function can be overridden for testing purposes.
var getPlaformOverride = func() (string, string) {
platformOverrideFile, err := os.Open("/etc/nvidia-container-toolkit/platform-override")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be pulled out to a constant.
Independently, it feels a bit weird to bake in a path referencing the nvidia-container-toolkit inside nvlib.

Copy link
Contributor

@klueska klueska Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead just have New() take the override as a string and leave the logic for including this file / how to parse it to the toolkit (possibly including helpers in nvlib for how to do the parsing, but not actually doing any parsing by default)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or instead of a raw string -- it takes the path to the override file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, the path to file should come from the entity importing go-nvlib

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of having this defined directly here (or at least the default) is that we want consistency across all our components that use this platform detection logic. These are currently:

  • The NVIDIA Container Toolkit (for the nvidia-container-runtime and nvidia-ctk cdi generate)
  • The GPU Device Plugin
  • GPU Feature Discovery

In the longer term, I could also see the DRA driver for GPUs also pulling this in if we wanted to support Tegra-based systems.

if os.IsNotExist(err) {
return "", "platform-override file does not exist"
}
if err != nil {
return "", fmt.Errorf("failed to open platform-override file: %w", err).Error()
}
defer platformOverrideFile.Close()

scanner := bufio.NewScanner(platformOverrideFile)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
return line, "read from platform-override file"
}

return "", "empty platform-override file"
}
20 changes: 20 additions & 0 deletions pkg/nvlib/info/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
func TestResolvePlatform(t *testing.T) {
testCases := []struct {
platform string
platformOverride string
hasTegraFiles bool
hasDXCore bool
hasNVML bool
Expand Down Expand Up @@ -82,10 +83,16 @@ func TestResolvePlatform(t *testing.T) {
hasDXCore: true,
expected: "not-auto",
},
{
platform: "auto",
platformOverride: "overridden",
expected: "overridden",
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
defer setGetPlatformOverrideForTest(tc.platformOverride)()
l := New(
WithPropertyExtractor(&PropertyExtractorMock{
HasDXCoreFunc: func() (bool, string) {
Expand All @@ -108,3 +115,16 @@ func TestResolvePlatform(t *testing.T) {
})
}
}

// setGetPlatformOverrideForTest overrides the distribution IDs that would normally be read from the /etc/os-release file.
func setGetPlatformOverrideForTest(override string) func() {
original := getPlaformOverride

getPlaformOverride = func() (string, string) {
return override, "overridden for test"
}

return func() {
getPlaformOverride = original
}
}