Runtimes refactor using exec.LookPath

Signed-off-by: Vitor Savian <vitor.savian@suse.com>
pull/9404/head
Vitor Savian 2024-01-28 17:22:59 -03:00
parent 5eb278b838
commit f3b4effb32
3 changed files with 119 additions and 820 deletions

View File

@ -23,7 +23,10 @@ import (
"k8s.io/kubernetes/pkg/kubelet/util" "k8s.io/kubernetes/pkg/kubelet/util"
) )
const socketPrefix = "unix://" const (
socketPrefix = "unix://"
runtimesPath = "/usr/local/nvidia/toolkit:/opt/kwasm/bin:/usr/sbin:/usr/local/sbin:/usr/bin:/usr/local/bin"
)
func getContainerdArgs(cfg *config.Node) []string { func getContainerdArgs(cfg *config.Node) []string {
args := []string{ args := []string{
@ -53,7 +56,12 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
cfg.AgentConfig.Systemd = !isRunningInUserNS && controllers["cpuset"] && os.Getenv("INVOCATION_ID") != "" cfg.AgentConfig.Systemd = !isRunningInUserNS && controllers["cpuset"] && os.Getenv("INVOCATION_ID") != ""
} }
extraRuntimes := findContainerRuntimes(os.DirFS(string(os.PathSeparator))) // set the path to include the runtimes and then remove the aditional path entries
// that we added after finding the runtimes
originalPath := os.Getenv("PATH")
os.Setenv("PATH", runtimesPath)
extraRuntimes := findContainerRuntimes()
os.Setenv("PATH", originalPath)
// Verifies if the DefaultRuntime can be found // Verifies if the DefaultRuntime can be found
if _, ok := extraRuntimes[cfg.DefaultRuntime]; !ok && cfg.DefaultRuntime != "" { if _, ok := extraRuntimes[cfg.DefaultRuntime]; !ok && cfg.DefaultRuntime != "" {

View File

@ -6,7 +6,7 @@ package containerd
import ( import (
"errors" "errors"
"io/fs" "io/fs"
"path/filepath" "os/exec"
"github.com/k3s-io/k3s/pkg/agent/templates" "github.com/k3s-io/k3s/pkg/agent/templates"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -17,63 +17,40 @@ import (
type runtimeConfigs map[string]templates.ContainerdRuntimeConfig type runtimeConfigs map[string]templates.ContainerdRuntimeConfig
// searchForRuntimes searches for runtimes and add into foundRuntimes // searchForRuntimes searches for runtimes and add into foundRuntimes
// It checks install locations provided via potentitalRuntimes variable. // It checks the PATH for the executables
// The binaries are searched at the locations specivied by locationsToCheck. func searchForRuntimes(potentialRuntimes runtimeConfigs, foundRuntimes runtimeConfigs) {
// The given fs.FS should represent the filesystem root directory to search in.
func searchForRuntimes(root fs.FS, potentialRuntimes runtimeConfigs, locationsToCheck []string, foundRuntimes runtimeConfigs) {
// Check these locations in order. The GPU operator's installation should
// take precedence over the package manager's installation.
// Fill in the binary location with just the name of the binary, // Fill in the binary location with just the name of the binary,
// and check against each of the possible locations. If a match is found, // and check against each of the possible locations. If a match is found,
// set the location to the full path. // set the location to the full path.
for runtimeName, runtimeConfig := range potentialRuntimes { for runtimeName, runtimeConfig := range potentialRuntimes {
for _, location := range locationsToCheck { logrus.Debugf("Searching for %s container runtime", runtimeName)
binaryPath := filepath.Join(location, runtimeConfig.BinaryName) path, err := exec.LookPath(runtimeConfig.BinaryName)
logrus.Debugf("Searching for %s container runtime at /%s", runtimeName, binaryPath) if err != nil {
if info, err := fs.Stat(root, binaryPath); err == nil { if errors.Is(err, fs.ErrNotExist) {
if info.IsDir() { logrus.Debugf("%s container runtime not found in $PATH: %v", runtimeName, err)
logrus.Debugf("Found %s container runtime at /%s, but it is a directory. Skipping.", runtimeName, binaryPath)
continue
}
runtimeConfig.BinaryName = filepath.Join("/", binaryPath)
logrus.Infof("Found %s container runtime at %s", runtimeName, runtimeConfig.BinaryName)
foundRuntimes[runtimeName] = runtimeConfig
break
} else { } else {
if errors.Is(err, fs.ErrNotExist) { logrus.Debugf("Error searching for %s in $PATH: %v", runtimeName, err)
logrus.Debugf("%s container runtime not found at /%s", runtimeName, binaryPath)
} else {
logrus.Errorf("Error searching for %s container runtime at /%s: %v", runtimeName, binaryPath, err)
}
} }
continue
} }
logrus.Infof("Found %s container runtime at %s", runtimeName, path)
runtimeConfig.BinaryName = path
foundRuntimes[runtimeName] = runtimeConfig
} }
} }
// findContainerRuntimes is a function that searches for all the runtimes and // findContainerRuntimes is a function that searches for all the runtimes and
// return a list with all the runtimes that have been found // return a list with all the runtimes that have been found
func findContainerRuntimes(root fs.FS) runtimeConfigs { func findContainerRuntimes() runtimeConfigs {
foundRuntimes := runtimeConfigs{} foundRuntimes := runtimeConfigs{}
findCRunContainerRuntime(root, foundRuntimes) findCRunContainerRuntime(foundRuntimes)
findNvidiaContainerRuntimes(root, foundRuntimes) findNvidiaContainerRuntimes(foundRuntimes)
findWasiRuntimes(root, foundRuntimes) findWasiRuntimes(foundRuntimes)
return foundRuntimes return foundRuntimes
} }
// findCRunContainerRuntime finds if crun is available in the system and adds to foundRuntimes func findCRunContainerRuntime(foundRuntimes runtimeConfigs) {
func findCRunContainerRuntime(root fs.FS, foundRuntimes runtimeConfigs) {
// Check these locations in order.
locationsToCheck := []string{
"usr/sbin", // Path when installing via package manager
"usr/bin", // Path when installing via package manager
"usr/local/bin", // Path when installing manually
"usr/local/sbin", // Path when installing manually
}
// Fill in the binary location with just the name of the binary,
// and check against each of the possible locations. If a match is found,
// set the location to the full path.
potentialRuntimes := runtimeConfigs{ potentialRuntimes := runtimeConfigs{
"crun": { "crun": {
RuntimeType: "io.containerd.runc.v2", RuntimeType: "io.containerd.runc.v2",
@ -81,25 +58,10 @@ func findCRunContainerRuntime(root fs.FS, foundRuntimes runtimeConfigs) {
}, },
} }
searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes) searchForRuntimes(potentialRuntimes, foundRuntimes)
} }
// findNvidiaContainerRuntimes finds the nvidia runtimes that are are available on the system func findNvidiaContainerRuntimes(foundRuntimes runtimeConfigs) {
// and adds to foundRuntimes. It checks install locations used by the nvidia
// gpu operator and by system package managers. The gpu operator installation
// takes precedence over the system package manager installation.
// The given fs.FS should represent the filesystem root directory to search in.
func findNvidiaContainerRuntimes(root fs.FS, foundRuntimes runtimeConfigs) {
// Check these locations in order. The GPU operator's installation should
// take precedence over the package manager's installation.
locationsToCheck := []string{
"usr/local/nvidia/toolkit", // Path when installing via GPU Operator
"usr/bin", // Path when installing via package manager
}
// Fill in the binary location with just the name of the binary,
// and check against each of the possible locations. If a match is found,
// set the location to the full path.
potentialRuntimes := runtimeConfigs{ potentialRuntimes := runtimeConfigs{
"nvidia": { "nvidia": {
RuntimeType: "io.containerd.runc.v2", RuntimeType: "io.containerd.runc.v2",
@ -110,25 +72,11 @@ func findNvidiaContainerRuntimes(root fs.FS, foundRuntimes runtimeConfigs) {
BinaryName: "nvidia-container-runtime-experimental", BinaryName: "nvidia-container-runtime-experimental",
}, },
} }
searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes)
searchForRuntimes(potentialRuntimes, foundRuntimes)
} }
// findWasiRuntimes finds the WebAssembly (WASI) container runtimes that func findWasiRuntimes(foundRuntimes runtimeConfigs) {
// are available on the system and adds to foundRuntimes. It checks install locations used by the kwasm
// operator and by system package managers. The kwasm operator installation
// takes precedence over the system package manager installation.
// The given fs.FS should represent the filesystem root directory to search in.
func findWasiRuntimes(root fs.FS, foundRuntimes runtimeConfigs) {
// Check these locations in order.
locationsToCheck := []string{
"opt/kwasm/bin", // Path when installing via kwasm Operator
"usr/bin", // Path when installing via package manager
"usr/sbin", // Path when installing via package manager
}
// Fill in the binary location with just the name of the binary,
// and check against each of the possible locations. If a match is found,
// set the location to the full path.
potentialRuntimes := runtimeConfigs{ potentialRuntimes := runtimeConfigs{
"lunatic": { "lunatic": {
RuntimeType: "io.containerd.lunatic.v2", RuntimeType: "io.containerd.lunatic.v2",
@ -159,5 +107,6 @@ func findWasiRuntimes(root fs.FS, foundRuntimes runtimeConfigs) {
BinaryName: "containerd-shim-wasmtime-v1", BinaryName: "containerd-shim-wasmtime-v1",
}, },
} }
searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes)
searchForRuntimes(potentialRuntimes, foundRuntimes)
} }

View File

@ -4,17 +4,15 @@
package containerd package containerd
import ( import (
"io/fs" "os"
"path/filepath"
"reflect" "reflect"
"testing" "testing"
"testing/fstest"
) )
func Test_UnitFindContainerRuntimes(t *testing.T) { func Test_UnitFindContainerRuntimes(t *testing.T) {
executable := &fstest.MapFile{Mode: 0755}
type args struct { type args struct {
root fs.FS exec []string
} }
tests := []struct { tests := []struct {
@ -24,39 +22,102 @@ func Test_UnitFindContainerRuntimes(t *testing.T) {
}{ }{
{ {
name: "No runtimes", name: "No runtimes",
args: args{ args: args{},
root: fstest.MapFS{},
},
want: runtimeConfigs{}, want: runtimeConfigs{},
}, },
{ {
name: "Found crun, nvidia and wasm", name: "Found crun, nvidia and wasm",
args: args{ args: args{
root: fstest.MapFS{ exec: []string{
"usr/bin/nvidia-container-runtime": executable, "nvidia-container-runtime",
"usr/bin/crun": executable, "crun",
"opt/kwasm/bin/containerd-shim-lunatic-v1": executable, "containerd-shim-lunatic-v1",
}, },
}, },
want: runtimeConfigs{ want: runtimeConfigs{
"nvidia": { "nvidia": {
RuntimeType: "io.containerd.runc.v2", RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime", BinaryName: "/tmp/testExecutables/nvidia-container-runtime",
}, },
"crun": { "crun": {
RuntimeType: "io.containerd.runc.v2", RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/crun", BinaryName: "/tmp/testExecutables/crun",
}, },
"lunatic": { "lunatic": {
RuntimeType: "io.containerd.lunatic.v2", RuntimeType: "io.containerd.lunatic.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-lunatic-v1", BinaryName: "/tmp/testExecutables/containerd-shim-lunatic-v1",
},
},
},
{
name: "Found only wasm",
args: args{
exec: []string{
"containerd-shim-lunatic-v1",
"containerd-shim-wasmtime-v1",
"containerd-shim-lunatic-v1",
"containerd-shim-slight-v1",
"containerd-shim-spin-v1",
"containerd-shim-wws-v1",
"containerd-shim-wasmedge-v1",
"containerd-shim-wasmer-v1",
},
},
want: runtimeConfigs{
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-wasmtime-v1",
},
"lunatic": {
RuntimeType: "io.containerd.lunatic.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-lunatic-v1",
},
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-slight-v1",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-spin-v1",
},
"wws": {
RuntimeType: "io.containerd.wws.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-wws-v1",
},
"wasmedge": {
RuntimeType: "io.containerd.wasmedge.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-wasmedge-v1",
},
"wasmer": {
RuntimeType: "io.containerd.wasmer.v2",
BinaryName: "/tmp/testExecutables/containerd-shim-wasmer-v1",
}, },
}, },
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
foundRuntimes := findContainerRuntimes(tt.args.root) tempDirPath := filepath.Join(os.TempDir(), "testExecutables")
err := os.Mkdir(tempDirPath, 0755)
if err != nil {
t.Errorf("Error creating directory: %v", err)
}
defer os.RemoveAll(tempDirPath)
for _, execName := range tt.args.exec {
execPath := filepath.Join(tempDirPath, execName)
if err := createExec(execPath); err != nil {
t.Errorf("Failed to create executable %s: %v", execPath, err)
}
}
originalPath := os.Getenv("PATH")
os.Setenv("PATH", tempDirPath)
defer os.Setenv("PATH", originalPath)
foundRuntimes := findContainerRuntimes()
if !reflect.DeepEqual(foundRuntimes, tt.want) { if !reflect.DeepEqual(foundRuntimes, tt.want) {
t.Errorf("findContainerRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want) t.Errorf("findContainerRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want)
} }
@ -64,733 +125,14 @@ func Test_UnitFindContainerRuntimes(t *testing.T) {
} }
} }
func Test_UnitSearchContainerRuntimes(t *testing.T) { func createExec(path string) error {
executable := &fstest.MapFile{Mode: 0755} if err := os.WriteFile(path, []byte{}, 0755); err != nil {
locationsToCheck := []string{ return err
"usr/local/nvidia/toolkit", // Path for nvidia shim when installing via GPU Operator
"opt/kwasm/bin", // Path for wasm shim when installing via the kwasm operator
"usr/bin", // Path when installing via package manager
"usr/sbin", // Path when installing via package manager
} }
potentialRuntimes := runtimeConfigs{ if err := os.Chmod(path, 0755); err != nil {
"nvidia": { return err
RuntimeType: "io.containerd.runc.v2",
BinaryName: "nvidia-container-runtime",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "containerd-shim-spin-v1",
},
} }
type args struct { return nil
root fs.FS
potentialRuntimes runtimeConfigs
locationsToCheck []string
}
tests := []struct {
name string
args args
want runtimeConfigs
}{
{
name: "No runtimes",
args: args{
root: fstest.MapFS{},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{},
},
{
name: "Nvidia runtime in /usr/bin",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
},
},
{
name: "Two runtimes in separate directories",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"opt/kwasm/bin/containerd-shim-spin-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-spin-v1",
},
},
},
{
name: "Same runtime in two directories",
args: args{
root: fstest.MapFS{
"usr/bin/containerd-shim-spin-v1": executable,
"opt/kwasm/bin/containerd-shim-spin-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-spin-v1",
},
},
},
{
name: "Both runtimes in /usr/bin",
args: args{
root: fstest.MapFS{
"usr/bin/containerd-shim-spin-v1": executable,
"usr/bin/nvidia-container-runtime": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/usr/bin/containerd-shim-spin-v1",
},
},
},
{
name: "Both runtimes in both directories",
args: args{
root: fstest.MapFS{
"usr/local/nvidia/toolkit/nvidia-container-runtime": executable,
"usr/bin/nvidia-container-runtime": executable,
"usr/bin/containerd-shim-spin-v1": executable,
"opt/kwasm/bin/containerd-shim-spin-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-spin-v1",
},
},
},
{
name: "Both runtimes in /usr/bin and one duplicate in /usr/local/nvidia/toolkit",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"usr/bin/containerd-shim-spin-v1": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/usr/bin/containerd-shim-spin-v1",
},
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime",
},
},
},
{
name: "Runtime is a directory",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": &fstest.MapFile{
Mode: fs.ModeDir,
},
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{},
},
{
name: "Runtime in both directories, but one is a directory",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime": &fstest.MapFile{
Mode: fs.ModeDir,
},
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
foundRuntimes := runtimeConfigs{}
searchForRuntimes(tt.args.root, tt.args.potentialRuntimes, tt.args.locationsToCheck, foundRuntimes)
if !reflect.DeepEqual(foundRuntimes, tt.want) {
t.Errorf("findContainerRuntimes() = %+v\nWant = %+v", foundRuntimes, tt.want)
}
})
}
}
func Test_UnitSearchWasiRuntimes(t *testing.T) {
executable := &fstest.MapFile{Mode: 0755}
locationsToCheck := []string{
"usr/local/nvidia/toolkit", // Path for nvidia shim when installing via GPU Operator
"opt/kwasm/bin", // Path for wasm shim when installing via the kwasm operator
"usr/bin", // Path when installing via package manager
"usr/sbin", // Path when installing via package manager
}
potentialRuntimes := runtimeConfigs{
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "containerd-shim-wasmtime-v1",
},
"lunatic": {
RuntimeType: "io.containerd.lunatic.v2",
BinaryName: "containerd-shim-lunatic-v1",
},
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "containerd-shim-slight-v1",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "containerd-shim-spin-v1",
},
"wws": {
RuntimeType: "io.containerd.wws.v2",
BinaryName: "containerd-shim-wws-v1",
},
"wasmedge": {
RuntimeType: "io.containerd.wasmedge.v2",
BinaryName: "containerd-shim-wasmedge-v1",
},
"wasmer": {
RuntimeType: "io.containerd.wasmer.v2",
BinaryName: "containerd-shim-wasmer-v1",
},
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "nvidia-container-runtime",
},
}
type args struct {
root fs.FS
potentialRuntimes runtimeConfigs
locationsToCheck []string
}
tests := []struct {
name string
args args
want runtimeConfigs
}{
{
name: "No runtimes",
args: args{
root: fstest.MapFS{},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{},
},
{
name: "wasmtime runtime in /usr/sbin",
args: args{
root: fstest.MapFS{
"usr/sbin/containerd-shim-wasmtime-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "/usr/sbin/containerd-shim-wasmtime-v1",
},
},
},
{
name: "lunatic runtime in /opt/kwasm/bin/",
args: args{
root: fstest.MapFS{
"opt/kwasm/bin/containerd-shim-lunatic-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"lunatic": {
RuntimeType: "io.containerd.lunatic.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-lunatic-v1",
},
},
},
{
name: "Two runtimes in separate directories",
args: args{
root: fstest.MapFS{
"usr/bin/containerd-shim-wasmer-v1": executable,
"opt/kwasm/bin/containerd-shim-slight-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-slight-v1",
},
"wasmer": {
RuntimeType: "io.containerd.wasmer.v2",
BinaryName: "/usr/bin/containerd-shim-wasmer-v1",
},
},
},
{
name: "Same runtime in two directories",
args: args{
root: fstest.MapFS{
"usr/bin/containerd-shim-wasmedge-v1": executable,
"opt/kwasm/bin/containerd-shim-wasmedge-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"wasmedge": {
RuntimeType: "io.containerd.wasmedge.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-wasmedge-v1",
},
},
},
{
name: "All runtimes in /usr/bin",
args: args{
root: fstest.MapFS{
"usr/bin/containerd-shim-lunatic-v1": executable,
"usr/bin/containerd-shim-slight-v1": executable,
"usr/bin/containerd-shim-spin-v1": executable,
"usr/bin/containerd-shim-wws-v1": executable,
"usr/bin/containerd-shim-wasmedge-v1": executable,
"usr/bin/containerd-shim-wasmer-v1": executable,
"usr/bin/containerd-shim-wasmtime-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"lunatic": {
RuntimeType: "io.containerd.lunatic.v2",
BinaryName: "/usr/bin/containerd-shim-lunatic-v1",
},
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "/usr/bin/containerd-shim-slight-v1",
},
"spin": {
RuntimeType: "io.containerd.spin.v2",
BinaryName: "/usr/bin/containerd-shim-spin-v1",
},
"wws": {
RuntimeType: "io.containerd.wws.v2",
BinaryName: "/usr/bin/containerd-shim-wws-v1",
},
"wasmedge": {
RuntimeType: "io.containerd.wasmedge.v2",
BinaryName: "/usr/bin/containerd-shim-wasmedge-v1",
},
"wasmer": {
RuntimeType: "io.containerd.wasmer.v2",
BinaryName: "/usr/bin/containerd-shim-wasmer-v1",
},
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "/usr/bin/containerd-shim-wasmtime-v1",
},
},
},
{
name: "Both runtimes in both directories",
args: args{
root: fstest.MapFS{
"opt/kwasm/bin/containerd-shim-slight-v1": executable,
"opt/kwasm/bin/containerd-shim-wasmtime-v1": executable,
"usr/bin/containerd-shim-slight-v1": executable,
"usr/bin/containerd-shim-wasmtime-v1": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-slight-v1",
},
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-wasmtime-v1",
},
},
},
{
name: "Preserve already found runtimes",
args: args{
root: fstest.MapFS{
"opt/kwasm/bin/containerd-shim-wasmtime-v1": executable,
"usr/bin/nvidia-container-runtime": executable,
},
locationsToCheck: locationsToCheck,
potentialRuntimes: potentialRuntimes,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-wasmtime-v1",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
foundRuntimes := runtimeConfigs{}
searchForRuntimes(tt.args.root, tt.args.potentialRuntimes, tt.args.locationsToCheck, foundRuntimes)
if !reflect.DeepEqual(foundRuntimes, tt.want) {
t.Errorf("searchForRuntimes = %+v\nWant = %+v", foundRuntimes, tt.want)
}
})
}
}
func Test_UnitSearchNvidiaContainerRuntimes(t *testing.T) {
executable := &fstest.MapFile{Mode: 0755}
locationsToCheck := []string{
"usr/local/nvidia/toolkit", // Path for nvidia shim when installing via GPU Operator
"opt/kwasm/bin", // Path for wasm shim when installing via the kwasm operator
"usr/bin", // Path when installing via package manager
"usr/sbin", // Path when installing via package manager
}
potentialRuntimes := runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "nvidia-container-runtime",
},
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "nvidia-container-runtime-experimental",
},
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "containerd-shim-slight-v1",
},
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "containerd-shim-wasmtime-v1",
},
}
type args struct {
root fs.FS
potentialRuntimes runtimeConfigs
locationsToCheck []string
}
tests := []struct {
name string
args args
want runtimeConfigs
}{
{
name: "No runtimes",
args: args{
root: fstest.MapFS{},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{},
},
{
name: "Nvidia runtime in /usr/bin",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
},
},
{
name: "Experimental runtime in /usr/local/nvidia/toolkit",
args: args{
root: fstest.MapFS{
"usr/local/nvidia/toolkit/nvidia-container-runtime": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime",
},
},
},
{
name: "Two runtimes in separate directories",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime",
},
},
},
{
name: "Experimental runtime in /usr/bin",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime-experimental": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime-experimental",
},
},
},
{
name: "Same runtime in two directories",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime-experimental": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental",
},
},
},
{
name: "Both runtimes in /usr/bin",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime-experimental": executable,
"usr/bin/nvidia-container-runtime": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime-experimental",
},
},
},
{
name: "Both runtimes in both directories",
args: args{
root: fstest.MapFS{
"usr/local/nvidia/toolkit/nvidia-container-runtime": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable,
"usr/bin/nvidia-container-runtime": executable,
"usr/bin/nvidia-container-runtime-experimental": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime",
},
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental",
},
},
},
{
name: "Both runtimes in /usr/local/nvidia/toolkit",
args: args{
root: fstest.MapFS{
"usr/local/nvidia/toolkit/nvidia-container-runtime": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime",
},
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental",
},
},
},
{
name: "Both runtimes in /usr/bin and one duplicate in /usr/local/nvidia/toolkit",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"usr/bin/nvidia-container-runtime-experimental": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime-experimental": executable,
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
"nvidia-experimental": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/local/nvidia/toolkit/nvidia-container-runtime-experimental",
},
},
},
{
name: "Runtime is a directory",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": &fstest.MapFile{
Mode: fs.ModeDir,
},
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{},
},
{
name: "Runtime in both directories, but one is a directory",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime": &fstest.MapFile{
Mode: fs.ModeDir,
},
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
},
},
{
name: "Preserve already found runtimes",
args: args{
root: fstest.MapFS{
"usr/bin/nvidia-container-runtime": executable,
"opt/kwasm/bin/containerd-shim-wasmtime-v1": executable,
"opt/kwasm/bin/containerd-shim-slight-v1": executable,
"usr/local/nvidia/toolkit/nvidia-container-runtime": &fstest.MapFile{
Mode: fs.ModeDir,
},
},
potentialRuntimes: potentialRuntimes,
locationsToCheck: locationsToCheck,
},
want: runtimeConfigs{
"slight": {
RuntimeType: "io.containerd.slight.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-slight-v1",
},
"wasmtime": {
RuntimeType: "io.containerd.wasmtime.v2",
BinaryName: "/opt/kwasm/bin/containerd-shim-wasmtime-v1",
},
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "/usr/bin/nvidia-container-runtime",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
foundRuntimes := runtimeConfigs{}
searchForRuntimes(tt.args.root, tt.args.potentialRuntimes, tt.args.locationsToCheck, foundRuntimes)
if !reflect.DeepEqual(foundRuntimes, tt.want) {
t.Errorf("searchForRuntimes() = %+v\nWant = %+v", foundRuntimes, tt.want)
}
})
}
} }