Merge branch 'add-auto-discover-mode' into 'master'

Add auto discover mode and use this as the default

See merge request nvidia/container-toolkit/container-toolkit!124
This commit is contained in:
Evan Lezar 2022-04-07 14:29:44 +00:00
commit 99baea9d51
4 changed files with 79 additions and 3 deletions

View File

@ -18,6 +18,8 @@ package modifier
import ( import (
"fmt" "fmt"
"os"
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config" "github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover" "github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
@ -60,7 +62,8 @@ func NewExperimentalModifier(logger *logrus.Logger, cfg *config.Config, ociSpec
root := cfg.NVIDIAContainerCLIConfig.Root root := cfg.NVIDIAContainerCLIConfig.Root
var d discover.Discover var d discover.Discover
switch cfg.NVIDIAContainerRuntimeConfig.DiscoverMode {
switch resolveAutoDiscoverMode(logger, cfg.NVIDIAContainerRuntimeConfig.DiscoverMode) {
case "legacy": case "legacy":
legacyDiscoverer, err := discover.NewLegacyDiscoverer(logger, root) legacyDiscoverer, err := discover.NewLegacyDiscoverer(logger, root)
if err != nil { if err != nil {
@ -115,3 +118,47 @@ func (m experimental) Modify(spec *specs.Spec) error {
return specEdits.Modify(spec) return specEdits.Modify(spec)
} }
// resolveAutoDiscoverMode determines the correct discover mode for the specified platform if set to "auto"
func resolveAutoDiscoverMode(logger *logrus.Logger, mode string) (rmode string) {
if mode != "auto" {
return mode
}
defer func() {
logger.Infof("Auto-detected discover mode as '%v'", rmode)
}()
isTegra, reason := isTegraSystem()
logger.Debugf("Is Tegra-based system? %v: %v", isTegra, reason)
if isTegra {
return "csv"
}
return "legacy"
}
// isTegraSystem returns true if the system is detected as a Tegra-based system
func isTegraSystem() (bool, string) {
const tegraReleaseFile = "/etc/nv_tegra_release"
const tegraFamilyFile = "/sys/devices/soc0/family"
if info, err := os.Stat(tegraReleaseFile); err == nil && !info.IsDir() {
return true, fmt.Sprintf("%v found", tegraReleaseFile)
}
if info, err := os.Stat(tegraFamilyFile); err != nil || !info.IsDir() {
return false, fmt.Sprintf("%v not found", tegraFamilyFile)
}
contents, err := os.ReadFile(tegraFamilyFile)
if err != nil {
return false, fmt.Sprintf("could not read %v", tegraFamilyFile)
}
if strings.HasPrefix(strings.ToLower(string(contents)), "tegra") {
return true, fmt.Sprintf("%v has 'tegra' prefix", tegraFamilyFile)
}
return false, fmt.Sprintf("%v has no 'tegra' prefix", tegraFamilyFile)
}

View File

@ -320,3 +320,32 @@ func TestExperimentalModifier(t *testing.T) {
}) })
} }
} }
func TestResolveDiscoverMode(t *testing.T) {
logger, _ := testlog.NewNullLogger()
testCases := []struct {
description string
mode string
expectedMode string
}{
{
description: "non-auto resolves to input",
mode: "not-auto",
expectedMode: "not-auto",
},
// TODO: The following test is brittle in that it will break on Tegra-based systems.
// {
// description: "auto resolves to legacy",
// mode: "auto",
// expectedMode: "legacy",
// },
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
mode := resolveAutoDiscoverMode(logger, tc.mode)
require.EqualValues(t, tc.expectedMode, mode)
})
}
}

View File

@ -47,7 +47,7 @@ func getDefaultRuntimeConfig() *RuntimeConfig {
c := RuntimeConfig{ c := RuntimeConfig{
DebugFilePath: "/dev/null", DebugFilePath: "/dev/null",
Experimental: false, Experimental: false,
DiscoverMode: "legacy", DiscoverMode: "auto",
} }
return &c return &c

View File

@ -63,7 +63,7 @@ func TestGetConfig(t *testing.T) {
NVIDIAContainerRuntimeConfig: RuntimeConfig{ NVIDIAContainerRuntimeConfig: RuntimeConfig{
DebugFilePath: "/dev/null", DebugFilePath: "/dev/null",
Experimental: false, Experimental: false,
DiscoverMode: "legacy", DiscoverMode: "auto",
}, },
}, },
}, },