Skip to content

Commit d7bc09a

Browse files
committed
Add support for a platform override file
This change adds support for reading the detected platform (if set to `auto`) from a platform override file. This allows system administrators to explicitly select a detected platform for tooling such as the nvidia-container-toolkit, the k8s-device-plugin, and k8s-dra-driver-gpu. Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent eda6327 commit d7bc09a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pkg/nvlib/info/resolver.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
package info
1818

19+
import (
20+
"bufio"
21+
"fmt"
22+
"os"
23+
"strings"
24+
)
25+
1926
// Platform represents a supported plaform.
2027
type Platform string
2128

@@ -39,6 +46,13 @@ func (p platformResolver) ResolvePlatform() Platform {
3946
return p.platform
4047
}
4148

49+
override, reason := getPlaformOverride()
50+
if override != "" {
51+
p.logger.Debugf("Using platform-override %q", override)
52+
return Platform(override)
53+
}
54+
p.logger.Debugf("No platform-override detected: %v", reason)
55+
4256
hasDXCore, reason := p.propertyExtractor.HasDXCore()
4357
p.logger.Debugf("Is WSL-based system? %v: %v", hasDXCore, reason)
4458

@@ -62,3 +76,33 @@ func (p platformResolver) ResolvePlatform() Platform {
6276
return PlatformUnknown
6377
}
6478
}
79+
80+
// getPlatformOverride checks the system for a platform override file.
81+
// This allows system administrators to force the detection of a specific
82+
// platform.
83+
//
84+
// The first non-empty and non-comment line (starting with #) in the file is
85+
// returned.
86+
//
87+
// Note that no checks are performed for a valid platform value.
88+
var getPlaformOverride = func() (string, string) {
89+
platformOverrideFile, err := os.Open("/etc/nvidia-container-toolkit/platform-override")
90+
if os.IsNotExist(err) {
91+
return "", "platform-override file does not exist"
92+
}
93+
if err != nil {
94+
return "", fmt.Errorf("failed to open platform-override file: %w", err).Error()
95+
}
96+
defer platformOverrideFile.Close()
97+
98+
scanner := bufio.NewScanner(platformOverrideFile)
99+
for scanner.Scan() {
100+
line := strings.TrimSpace(scanner.Text())
101+
if line == "" || strings.HasPrefix(line, "#") {
102+
continue
103+
}
104+
return line, "read from platform-override file"
105+
}
106+
107+
return "", "empty platform-override file"
108+
}

pkg/nvlib/info/resolver_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
func TestResolvePlatform(t *testing.T) {
2727
testCases := []struct {
2828
platform string
29+
platformOverride string
2930
hasTegraFiles bool
3031
hasDXCore bool
3132
hasNVML bool
@@ -82,10 +83,16 @@ func TestResolvePlatform(t *testing.T) {
8283
hasDXCore: true,
8384
expected: "not-auto",
8485
},
86+
{
87+
platform: "auto",
88+
platformOverride: "overridden",
89+
expected: "overridden",
90+
},
8591
}
8692

8793
for i, tc := range testCases {
8894
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
95+
defer setGetPlatformOverrideForTest(tc.platformOverride)()
8996
l := New(
9097
WithPropertyExtractor(&PropertyExtractorMock{
9198
HasDXCoreFunc: func() (bool, string) {
@@ -108,3 +115,16 @@ func TestResolvePlatform(t *testing.T) {
108115
})
109116
}
110117
}
118+
119+
// setGetPlatformOverrideForTest overrides the distribution IDs that would normally be read from the /etc/os-release file.
120+
func setGetPlatformOverrideForTest(override string) func() {
121+
original := getPlaformOverride
122+
123+
getPlaformOverride = func() (string, string) {
124+
return override, "overridden for test"
125+
}
126+
127+
return func() {
128+
getPlaformOverride = original
129+
}
130+
}

0 commit comments

Comments
 (0)