From 01e55461e8e2540970694d7e6e62a68ac2e7512b Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Thu, 3 Apr 2025 09:17:49 +0200 Subject: [PATCH 1/2] [no-relnote] Remove unused runtimeConfigOverideJSON variable Signed-off-by: Evan Lezar --- cmd/nvidia-ctk/runtime/configure/configure.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cmd/nvidia-ctk/runtime/configure/configure.go b/cmd/nvidia-ctk/runtime/configure/configure.go index aa8a496cb..09122ba00 100644 --- a/cmd/nvidia-ctk/runtime/configure/configure.go +++ b/cmd/nvidia-ctk/runtime/configure/configure.go @@ -72,8 +72,6 @@ type config struct { mode string hookFilePath string - runtimeConfigOverrideJSON string - nvidiaRuntime struct { name string path string @@ -208,11 +206,6 @@ func (m command) validateFlags(c *cli.Context, config *config) error { config.cdi.enabled = false } - if config.runtimeConfigOverrideJSON != "" && config.runtime != "containerd" { - m.logger.Warningf("Ignoring runtime-config-override flag for %v", config.runtime) - config.runtimeConfigOverrideJSON = "" - } - switch config.configSource { case configSourceCommand: if config.runtime == "docker" { From 8176ac40ee0ff7a254aa321a5dbf897e0481638a Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Wed, 2 Apr 2025 17:56:46 +0200 Subject: [PATCH 2/2] Allow container runtime executable path to be specified This change adds support for specifying the container runtime executable path. This can be used if, for example, there are two containerd or crio executables and a specific one must be used. Signed-off-by: Evan Lezar --- cmd/nvidia-ctk/runtime/configure/configure.go | 15 +++++++++++++-- pkg/config/engine/containerd/containerd.go | 7 +++++-- pkg/config/engine/crio/crio.go | 7 +++++-- tools/container/container.go | 5 +++++ tools/container/nvidia-toolkit/run.go | 2 +- tools/container/runtime/containerd/containerd.go | 2 +- tools/container/runtime/crio/crio.go | 2 +- tools/container/runtime/runtime.go | 16 ++++++++++++++-- 8 files changed, 45 insertions(+), 11 deletions(-) diff --git a/cmd/nvidia-ctk/runtime/configure/configure.go b/cmd/nvidia-ctk/runtime/configure/configure.go index 09122ba00..277f23d1d 100644 --- a/cmd/nvidia-ctk/runtime/configure/configure.go +++ b/cmd/nvidia-ctk/runtime/configure/configure.go @@ -68,6 +68,7 @@ type config struct { dryRun bool runtime string configFilePath string + executablePath string configSource string mode string hookFilePath string @@ -118,6 +119,11 @@ func (m command) build() *cli.Command { Usage: "path to the config file for the target runtime", Destination: &config.configFilePath, }, + &cli.StringFlag{ + Name: "executable-path", + Usage: "The path to the runtime executable. This is used to extract the current config", + Destination: &config.executablePath, + }, &cli.StringFlag{ Name: "config-mode", Usage: "the config mode for runtimes that support multiple configuration mechanisms", @@ -206,6 +212,11 @@ func (m command) validateFlags(c *cli.Context, config *config) error { config.cdi.enabled = false } + if config.executablePath != "" && config.runtime == "docker" { + m.logger.Warningf("Ignoring executable-path=%q flag for %v", config.executablePath, config.runtime) + config.executablePath = "" + } + switch config.configSource { case configSourceCommand: if config.runtime == "docker" { @@ -323,9 +334,9 @@ func (c *config) resolveConfigSource() (toml.Loader, error) { func (c *config) getCommandConfigSource() toml.Loader { switch c.runtime { case "containerd": - return containerd.CommandLineSource("") + return containerd.CommandLineSource("", c.executablePath) case "crio": - return crio.CommandLineSource("") + return crio.CommandLineSource("", c.executablePath) } return toml.Empty } diff --git a/pkg/config/engine/containerd/containerd.go b/pkg/config/engine/containerd/containerd.go index 8c41e947e..ca35c75db 100644 --- a/pkg/config/engine/containerd/containerd.go +++ b/pkg/config/engine/containerd/containerd.go @@ -162,8 +162,11 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) { } // CommandLineSource returns the CLI-based containerd config loader -func CommandLineSource(hostRoot string) toml.Loader { - return toml.FromCommandLine(chrootIfRequired(hostRoot, "containerd", "config", "dump")...) +func CommandLineSource(hostRoot string, executablePath string) toml.Loader { + if executablePath == "" { + executablePath = "containerd" + } + return toml.FromCommandLine(chrootIfRequired(hostRoot, executablePath, "config", "dump")...) } func chrootIfRequired(hostRoot string, commandLine ...string) []string { diff --git a/pkg/config/engine/crio/crio.go b/pkg/config/engine/crio/crio.go index c0cc60be7..473b8fa43 100644 --- a/pkg/config/engine/crio/crio.go +++ b/pkg/config/engine/crio/crio.go @@ -157,9 +157,12 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) { func (c *Config) EnableCDI() {} // CommandLineSource returns the CLI-based crio config loader -func CommandLineSource(hostRoot string) toml.Loader { +func CommandLineSource(hostRoot string, executablePath string) toml.Loader { + if executablePath == "" { + executablePath = "crio" + } return toml.LoadFirst( - toml.FromCommandLine(chrootIfRequired(hostRoot, "crio", "status", "config")...), + toml.FromCommandLine(chrootIfRequired(hostRoot, executablePath, "status", "config")...), toml.FromCommandLine(chrootIfRequired(hostRoot, "crio-status", "config")...), ) } diff --git a/tools/container/container.go b/tools/container/container.go index 4b694c4c8..ce0dd8ec6 100644 --- a/tools/container/container.go +++ b/tools/container/container.go @@ -38,6 +38,11 @@ const ( type Options struct { Config string Socket string + // ExecutablePath specifies the path to the container runtime executable. + // This is used to extract the current config, for example. + // If a HostRootMount is specified, this path is relative to the host root + // mount. + ExecutablePath string // EnabledCDI indicates whether CDI should be enabled. EnableCDI bool RuntimeName string diff --git a/tools/container/nvidia-toolkit/run.go b/tools/container/nvidia-toolkit/run.go index 6e1edaef3..e90a59da6 100644 --- a/tools/container/nvidia-toolkit/run.go +++ b/tools/container/nvidia-toolkit/run.go @@ -136,7 +136,7 @@ func validateFlags(c *cli.Context, o *options) error { if err := toolkit.ValidateOptions(&o.toolkitOptions, o.toolkitRoot()); err != nil { return err } - if err := runtime.ValidateOptions(c, &o.runtimeOptions, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil { + if err := o.runtimeOptions.Validate(c, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil { return err } return nil diff --git a/tools/container/runtime/containerd/containerd.go b/tools/container/runtime/containerd/containerd.go index 31fd2feaa..528c7f4f8 100644 --- a/tools/container/runtime/containerd/containerd.go +++ b/tools/container/runtime/containerd/containerd.go @@ -173,7 +173,7 @@ func getRuntimeConfig(o *container.Options, co *Options) (engine.Interface, erro containerd.WithPath(o.Config), containerd.WithConfigSource( toml.LoadFirst( - containerd.CommandLineSource(o.HostRootMount), + containerd.CommandLineSource(o.HostRootMount, o.ExecutablePath), toml.FromFile(o.Config), ), ), diff --git a/tools/container/runtime/crio/crio.go b/tools/container/runtime/crio/crio.go index e37fc4ba3..a5ee26d06 100644 --- a/tools/container/runtime/crio/crio.go +++ b/tools/container/runtime/crio/crio.go @@ -202,7 +202,7 @@ func getRuntimeConfig(o *container.Options) (engine.Interface, error) { crio.WithPath(o.Config), crio.WithConfigSource( toml.LoadFirst( - crio.CommandLineSource(o.HostRootMount), + crio.CommandLineSource(o.HostRootMount, o.ExecutablePath), toml.FromFile(o.Config), ), ), diff --git a/tools/container/runtime/runtime.go b/tools/container/runtime/runtime.go index c6aed501f..0042938c2 100644 --- a/tools/container/runtime/runtime.go +++ b/tools/container/runtime/runtime.go @@ -19,6 +19,7 @@ package runtime import ( "fmt" + log "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" "github.com/NVIDIA/nvidia-container-toolkit/tools/container" @@ -53,6 +54,12 @@ func Flags(opts *Options) []cli.Flag { Destination: &opts.Config, EnvVars: []string{"RUNTIME_CONFIG", "CONTAINERD_CONFIG", "DOCKER_CONFIG"}, }, + &cli.StringFlag{ + Name: "executable-path", + Usage: "The path to the runtime executable. This is used to extract the current config", + Destination: &opts.ExecutablePath, + EnvVars: []string{"RUNTIME_EXECUTABLE_PATH"}, + }, &cli.StringFlag{ Name: "socket", Usage: "Path to the runtime socket file", @@ -104,8 +111,8 @@ func Flags(opts *Options) []cli.Flag { return flags } -// ValidateOptions checks whether the specified options are valid -func ValidateOptions(c *cli.Context, opts *Options, runtime string, toolkitRoot string, to *toolkit.Options) error { +// Validate checks whether the specified options are valid +func (opts *Options) Validate(c *cli.Context, runtime string, toolkitRoot string, to *toolkit.Options) error { // We set this option here to ensure that it is available in future calls. opts.RuntimeDir = toolkitRoot @@ -113,6 +120,11 @@ func ValidateOptions(c *cli.Context, opts *Options, runtime string, toolkitRoot opts.EnableCDI = to.CDI.Enabled } + if opts.ExecutablePath != "" && opts.RuntimeName == docker.Name { + log.Warningf("Ignoring executable-path=%q flag for %v", opts.ExecutablePath, opts.RuntimeName) + opts.ExecutablePath = "" + } + // Apply the runtime-specific config changes. switch runtime { case containerd.Name: