Skip to content

Commit 6ff48ea

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 6ff48ea

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-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+
func getPlaformOverride() (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+
}

0 commit comments

Comments
 (0)