Skip to content

Commit

Permalink
add default runtime binary path to runtimes field of toolkit config toml
Browse files Browse the repository at this point in the history
Signed-off-by: Tariq Ibrahim <[email protected]>
  • Loading branch information
tariq1890 committed Sep 18, 2024
1 parent 1cd6998 commit bdd4f15
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/config/engine/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ type Interface interface {
Set(string, interface{})
RemoveRuntime(string) error
Save(string) (int64, error)
GetRuntime(name string) (interface{}, error)
}
12 changes: 12 additions & 0 deletions pkg/config/engine/containerd/config_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ func (c *ConfigV1) RemoveRuntime(name string) error {
return nil
}

func (c *ConfigV1) GetRuntime(name string) (interface{}, error) {
if c == nil || c.Tree == nil {
return nil, fmt.Errorf("config is nil")
}
config := *c.Tree
runtimeData, ok := config.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name}).(*toml.Tree)
if !ok {
return nil, fmt.Errorf("invalid toml object")
}
return runtimeData, nil
}

// SetOption sets the specified containerd option.
func (c *ConfigV1) Set(key string, value interface{}) {
config := *c.Tree
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/engine/containerd/config_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,16 @@ func (c *Config) RemoveRuntime(name string) error {
*c.Tree = config
return nil
}

// GetRuntime returns the TOML object of the runtime passed as input
func (c *Config) GetRuntime(name string) (interface{}, error) {
if c == nil || c.Tree == nil {
return nil, fmt.Errorf("config is nil")
}
config := *c.Tree
runtimeData, ok := config.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name}).(*toml.Tree)
if !ok {
return nil, fmt.Errorf("invalid toml object")
}
return runtimeData, nil
}
13 changes: 13 additions & 0 deletions pkg/config/engine/crio/crio.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,16 @@ func (c *Config) RemoveRuntime(name string) error {
*c.Tree = config
return nil
}

// GetRuntime returns the TOML object of the runtime passed as input
func (c *Config) GetRuntime(name string) (interface{}, error) {
if c == nil || c.Tree == nil {
return nil, fmt.Errorf("config is nil")
}
config := *c.Tree
runtimeData, ok := config.GetPath([]string{"crio", "runtime", "runtimes", name}).(*toml.Tree)
if !ok {
return nil, fmt.Errorf("invalid toml object")
}
return runtimeData, nil
}
22 changes: 22 additions & 0 deletions pkg/config/engine/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,25 @@ func (c Config) Save(path string) (int64, error) {
n, err := config.Raw(path).Write(output)
return int64(n), err
}

// GetRuntime returns the TOML object of the runtime passed as input
func (c *Config) GetRuntime(name string) (interface{}, error) {
if c == nil {
return nil, fmt.Errorf("config is nil")
}

config := *c

var runtimes map[string]interface{}
if _, exists := config["runtimes"]; exists {
runtimes = config["runtimes"].(map[string]interface{})
} else {
return nil, fmt.Errorf("config is nil")
}

if r, ok := runtimes[name]; ok {
return r, nil
} else {
return nil, fmt.Errorf("runtime %s not found", name)
}
}
89 changes: 87 additions & 2 deletions tools/container/toolkit/toolkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import (

"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/system/nvdevices"
pkgconfig "github.com/NVIDIA/nvidia-container-toolkit/pkg/config"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/crio"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi"
transformroot "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform/root"
)
Expand All @@ -40,6 +43,8 @@ const (
// DefaultNvidiaDriverRoot specifies the default NVIDIA driver run directory
DefaultNvidiaDriverRoot = "/run/nvidia/driver"

DefaultHostRootMount = "/host"

nvidiaContainerCliSource = "/usr/bin/nvidia-container-cli"
nvidiaContainerRuntimeHookSource = "/usr/bin/nvidia-container-runtime-hook"

Expand All @@ -54,6 +59,7 @@ type options struct {
DevRoot string
DriverRootCtrPath string
DevRootCtrPath string
HostRootMount string

ContainerRuntimeMode string
ContainerRuntimeDebug string
Expand Down Expand Up @@ -149,6 +155,13 @@ func main() {
Destination: &opts.DevRootCtrPath,
EnvVars: []string{"DEV_ROOT_CTR_PATH"},
},
&cli.StringFlag{
Name: "host-root",
Usage: "Specify the path to the host root to be used when restarting containerd using systemd",
Value: DefaultHostRootMount,
Destination: &opts.HostRootMount,
EnvVars: []string{"HOST_ROOT_MOUNT"},
},
&cli.StringFlag{
Name: "nvidia-container-runtime.debug",
Aliases: []string{"nvidia-container-runtime-debug"},
Expand Down Expand Up @@ -396,7 +409,65 @@ func Install(cli *cli.Context, opts *options) error {
log.Errorf("Ignoring error: %v", fmt.Errorf("error installing NVIDIA Container CDI Hook CLI: %v", err))
}

err = installToolkitConfig(cli, toolkitConfigPath, nvidiaContainerCliExecutable, nvidiaCTKPath, nvidiaContainerRuntimeHookPath, opts)
var runtimeBinaryPaths []string

if r, ok := os.LookupEnv("RUNTIME"); ok {

switch r {
case "containerd":
var cliArgs []string
if opts.HostRootMount != "" {
cliArgs = append(cliArgs, "chroot", opts.HostRootMount)
}
cliArgs = append(cliArgs, "containerd", "config", "dump")

cfg, err := containerd.New(
containerd.WithReference(pkgconfig.FromCLI(cliArgs)),
)
if err != nil {
return fmt.Errorf("unable to load containerd config: %w", err)
}

containerdDefaultRuntime := cfg.DefaultRuntime()
if containerdDefaultRuntime != "" {
runtimeData, err := cfg.GetRuntime(containerdDefaultRuntime)
if err == nil {
runtimeTOML := runtimeData.(*toml.Tree)
if binaryPath, ok := runtimeTOML.GetPath([]string{"options", "BinaryName"}).(string); ok && binaryPath != "" {
runtimeBinaryPaths = append(runtimeBinaryPaths, binaryPath)
}
}
}

case "crio":
var cliArgs []string
if opts.HostRootMount != "" {
cliArgs = append(cliArgs, "chroot", opts.HostRootMount)
}
cliArgs = append(cliArgs, "crio", "status", "config")

cfg, err := crio.New(
crio.WithReference(pkgconfig.FromCLI(cliArgs)),
)
if err != nil {
return fmt.Errorf("unable to load crio config: %w", err)
}

crioDefaultRuntime := cfg.DefaultRuntime()
if crioDefaultRuntime != "" {
runtimeData, err := cfg.GetRuntime(crioDefaultRuntime)
if err == nil {
runtimeTOML := runtimeData.(*toml.Tree)
if binaryPath, ok := runtimeTOML.GetPath([]string{"runtime_path"}).(string); ok && binaryPath != "" {
runtimeBinaryPaths = append(runtimeBinaryPaths, binaryPath)
}
}
}
}
}

err = installToolkitConfig(cli, toolkitConfigPath, nvidiaContainerCliExecutable, nvidiaCTKPath,
nvidiaContainerRuntimeHookPath, runtimeBinaryPaths, opts)
if err != nil && !opts.ignoreErrors {
return fmt.Errorf("error installing NVIDIA container toolkit config: %v", err)
} else if err != nil {
Expand Down Expand Up @@ -470,7 +541,8 @@ func installLibrary(libName string, toolkitRoot string) error {

// installToolkitConfig installs the config file for the NVIDIA container toolkit ensuring
// that the settings are updated to match the desired install and nvidia driver directories.
func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContainerCliExecutablePath string, nvidiaCTKPath string, nvidaContainerRuntimeHookPath string, opts *options) error {
func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContainerCliExecutablePath string, nvidiaCTKPath string,
nvidaContainerRuntimeHookPath string, runtimeBinaryPaths []string, opts *options) error {
log.Infof("Installing NVIDIA container toolkit config '%v'", toolkitConfigPath)

cfg, err := loadConfig(nvidiaContainerToolkitConfigSource)
Expand Down Expand Up @@ -545,6 +617,19 @@ func installToolkitConfig(c *cli.Context, toolkitConfigPath string, nvidiaContai
cfg.Set(key, value)
}

var ctkRuntimesArr []string
if len(runtimeBinaryPaths) > 0 {
ctkRuntimesArr = append(ctkRuntimesArr, runtimeBinaryPaths...)
}

log.Infof("Printing ctkRuntimes: %v", ctkRuntimesArr)

if cfgRuntimes, ok := cfg.Get("nvidia-container-runtime.runtimes").([]string); ok {
log.Infof("Cast to string slice successful. Printing runtimes: %v", cfgRuntimes)
} else {
log.Infof("Cast to string slice unsuccessful. Printing: %v", cfg.Get("nvidia-container-runtime.runtimes"))
}

if _, err := cfg.WriteTo(targetConfig); err != nil {
return fmt.Errorf("error writing config: %v", err)
}
Expand Down

0 comments on commit bdd4f15

Please sign in to comment.