Skip to content

Commit

Permalink
Runtimes refactor using exec.LookPath
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Savian <[email protected]>
  • Loading branch information
vitorsavian committed Feb 7, 2024
1 parent f9ee66f commit e9cec46
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 793 deletions.
12 changes: 10 additions & 2 deletions pkg/agent/containerd/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
"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 {
args := []string{
Expand Down Expand Up @@ -53,7 +56,12 @@ func setupContainerdConfig(ctx context.Context, cfg *config.Node) error {
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
if _, ok := extraRuntimes[cfg.DefaultRuntime]; !ok && cfg.DefaultRuntime != "" {
Expand Down
103 changes: 26 additions & 77 deletions pkg/agent/containerd/runtimes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package containerd
import (
"errors"
"io/fs"
"path/filepath"
"os/exec"

"github.com/k3s-io/k3s/pkg/agent/templates"
"github.com/sirupsen/logrus"
Expand All @@ -17,89 +17,51 @@ import (
type runtimeConfigs map[string]templates.ContainerdRuntimeConfig

// searchForRuntimes searches for runtimes and add into foundRuntimes
// It checks install locations provided via potentitalRuntimes variable.
// The binaries are searched at the locations specivied by locationsToCheck.
// 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.

// It checks the PATH for the executables
func searchForRuntimes(potentialRuntimes runtimeConfigs, foundRuntimes runtimeConfigs) {
// 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.
for runtimeName, runtimeConfig := range potentialRuntimes {
for _, location := range locationsToCheck {
binaryPath := filepath.Join(location, runtimeConfig.BinaryName)
logrus.Debugf("Searching for %s container runtime at /%s", runtimeName, binaryPath)
if info, err := fs.Stat(root, binaryPath); err == nil {
if info.IsDir() {
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
logrus.Debugf("Searching for %s container runtime", runtimeName)
path, err := exec.LookPath(runtimeConfig.BinaryName)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
logrus.Debugf("%s container runtime not found in $PATH: %v", runtimeName, err)
} else {
if errors.Is(err, fs.ErrNotExist) {
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)
}
logrus.Debugf("Error searching for %s in $PATH: %v", runtimeName, 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
// return a list with all the runtimes that have been found
func findContainerRuntimes(root fs.FS) runtimeConfigs {
func findContainerRuntimes() runtimeConfigs {
foundRuntimes := runtimeConfigs{}
findCRunContainerRuntime(root, foundRuntimes)
findNvidiaContainerRuntimes(root, foundRuntimes)
findWasiRuntimes(root, foundRuntimes)
findCRunContainerRuntime(foundRuntimes)
findNvidiaContainerRuntimes(foundRuntimes)
findWasiRuntimes(foundRuntimes)
return foundRuntimes
}

// findCRunContainerRuntime finds if crun is available in the system and adds to foundRuntimes
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.
func findCRunContainerRuntime(foundRuntimes runtimeConfigs) {
potentialRuntimes := runtimeConfigs{
"crun": {
RuntimeType: "io.containerd.runc.v2",
BinaryName: "crun",
},
}

searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes)
searchForRuntimes(potentialRuntimes, foundRuntimes)
}

// findNvidiaContainerRuntimes finds the nvidia runtimes that are are available on the system
// 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.
func findNvidiaContainerRuntimes(foundRuntimes runtimeConfigs) {
potentialRuntimes := runtimeConfigs{
"nvidia": {
RuntimeType: "io.containerd.runc.v2",
Expand All @@ -110,25 +72,11 @@ func findNvidiaContainerRuntimes(root fs.FS, foundRuntimes runtimeConfigs) {
BinaryName: "nvidia-container-runtime-experimental",
},
}
searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes)
}

// findWasiRuntimes finds the WebAssembly (WASI) container runtimes that
// 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
}
searchForRuntimes(potentialRuntimes, foundRuntimes)
}

// 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.
func findWasiRuntimes(foundRuntimes runtimeConfigs) {
potentialRuntimes := runtimeConfigs{
"lunatic": {
RuntimeType: "io.containerd.lunatic.v2",
Expand Down Expand Up @@ -159,5 +107,6 @@ func findWasiRuntimes(root fs.FS, foundRuntimes runtimeConfigs) {
BinaryName: "containerd-shim-wasmtime-v1",
},
}
searchForRuntimes(root, potentialRuntimes, locationsToCheck, foundRuntimes)

searchForRuntimes(potentialRuntimes, foundRuntimes)
}
Loading

0 comments on commit e9cec46

Please sign in to comment.