mirror of
				https://github.com/NVIDIA/nvidia-container-toolkit
				synced 2025-06-26 18:18:24 +00:00 
			
		
		
		
	Allow container runtime executable path to be specified
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	
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 <elezar@nvidia.com>
This commit is contained in:
		
							parent
							
								
									cb7605e132
								
							
						
					
					
						commit
						c57bdf3391
					
				@ -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
 | 
			
		||||
 | 
			
		||||
@ -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),
 | 
			
		||||
			),
 | 
			
		||||
		),
 | 
			
		||||
 | 
			
		||||
@ -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),
 | 
			
		||||
			),
 | 
			
		||||
		),
 | 
			
		||||
 | 
			
		||||
@ -26,6 +26,7 @@ import (
 | 
			
		||||
	"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/crio"
 | 
			
		||||
	"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/docker"
 | 
			
		||||
	"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/toolkit"
 | 
			
		||||
	"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
@ -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(logger logger.Interface, 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 {
 | 
			
		||||
		logger.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:
 | 
			
		||||
 | 
			
		||||
@ -167,7 +167,7 @@ func (a *app) validateFlags(c *cli.Context, o *options) error {
 | 
			
		||||
	if err := a.toolkit.ValidateOptions(&o.toolkitOptions); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	if err := runtime.ValidateOptions(c, &o.runtimeOptions, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil {
 | 
			
		||||
	if err := o.runtimeOptions.Validate(a.logger, c, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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 {
 | 
			
		||||
 | 
			
		||||
@ -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")...),
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user