Skip to content

Commit c57bdf3

Browse files
committed
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 <[email protected]>
1 parent cb7605e commit c57bdf3

File tree

8 files changed

+45
-11
lines changed

8 files changed

+45
-11
lines changed

cmd/nvidia-ctk-installer/container/container.go

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ const (
3838
type Options struct {
3939
Config string
4040
Socket string
41+
// ExecutablePath specifies the path to the container runtime executable.
42+
// This is used to extract the current config, for example.
43+
// If a HostRootMount is specified, this path is relative to the host root
44+
// mount.
45+
ExecutablePath string
4146
// EnabledCDI indicates whether CDI should be enabled.
4247
EnableCDI bool
4348
RuntimeName string

cmd/nvidia-ctk-installer/container/runtime/containerd/containerd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func getRuntimeConfig(o *container.Options, co *Options) (engine.Interface, erro
173173
containerd.WithPath(o.Config),
174174
containerd.WithConfigSource(
175175
toml.LoadFirst(
176-
containerd.CommandLineSource(o.HostRootMount),
176+
containerd.CommandLineSource(o.HostRootMount, o.ExecutablePath),
177177
toml.FromFile(o.Config),
178178
),
179179
),

cmd/nvidia-ctk-installer/container/runtime/crio/crio.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func getRuntimeConfig(o *container.Options) (engine.Interface, error) {
202202
crio.WithPath(o.Config),
203203
crio.WithConfigSource(
204204
toml.LoadFirst(
205-
crio.CommandLineSource(o.HostRootMount),
205+
crio.CommandLineSource(o.HostRootMount, o.ExecutablePath),
206206
toml.FromFile(o.Config),
207207
),
208208
),

cmd/nvidia-ctk-installer/container/runtime/runtime.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/crio"
2727
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/runtime/docker"
2828
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/container/toolkit"
29+
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2930
)
3031

3132
const (
@@ -53,6 +54,12 @@ func Flags(opts *Options) []cli.Flag {
5354
Destination: &opts.Config,
5455
EnvVars: []string{"RUNTIME_CONFIG", "CONTAINERD_CONFIG", "DOCKER_CONFIG"},
5556
},
57+
&cli.StringFlag{
58+
Name: "executable-path",
59+
Usage: "The path to the runtime executable. This is used to extract the current config",
60+
Destination: &opts.ExecutablePath,
61+
EnvVars: []string{"RUNTIME_EXECUTABLE_PATH"},
62+
},
5663
&cli.StringFlag{
5764
Name: "socket",
5865
Usage: "Path to the runtime socket file",
@@ -104,15 +111,20 @@ func Flags(opts *Options) []cli.Flag {
104111
return flags
105112
}
106113

107-
// ValidateOptions checks whether the specified options are valid
108-
func ValidateOptions(c *cli.Context, opts *Options, runtime string, toolkitRoot string, to *toolkit.Options) error {
114+
// Validate checks whether the specified options are valid
115+
func (opts *Options) Validate(logger logger.Interface, c *cli.Context, runtime string, toolkitRoot string, to *toolkit.Options) error {
109116
// We set this option here to ensure that it is available in future calls.
110117
opts.RuntimeDir = toolkitRoot
111118

112119
if !c.IsSet("enable-cdi-in-runtime") {
113120
opts.EnableCDI = to.CDI.Enabled
114121
}
115122

123+
if opts.ExecutablePath != "" && opts.RuntimeName == docker.Name {
124+
logger.Warningf("Ignoring executable-path=%q flag for %v", opts.ExecutablePath, opts.RuntimeName)
125+
opts.ExecutablePath = ""
126+
}
127+
116128
// Apply the runtime-specific config changes.
117129
switch runtime {
118130
case containerd.Name:

cmd/nvidia-ctk-installer/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (a *app) validateFlags(c *cli.Context, o *options) error {
167167
if err := a.toolkit.ValidateOptions(&o.toolkitOptions); err != nil {
168168
return err
169169
}
170-
if err := runtime.ValidateOptions(c, &o.runtimeOptions, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil {
170+
if err := o.runtimeOptions.Validate(a.logger, c, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil {
171171
return err
172172
}
173173
return nil

cmd/nvidia-ctk/runtime/configure/configure.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type config struct {
6868
dryRun bool
6969
runtime string
7070
configFilePath string
71+
executablePath string
7172
configSource string
7273
mode string
7374
hookFilePath string
@@ -118,6 +119,11 @@ func (m command) build() *cli.Command {
118119
Usage: "path to the config file for the target runtime",
119120
Destination: &config.configFilePath,
120121
},
122+
&cli.StringFlag{
123+
Name: "executable-path",
124+
Usage: "The path to the runtime executable. This is used to extract the current config",
125+
Destination: &config.executablePath,
126+
},
121127
&cli.StringFlag{
122128
Name: "config-mode",
123129
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 {
206212
config.cdi.enabled = false
207213
}
208214

215+
if config.executablePath != "" && config.runtime == "docker" {
216+
m.logger.Warningf("Ignoring executable-path=%q flag for %v", config.executablePath, config.runtime)
217+
config.executablePath = ""
218+
}
219+
209220
switch config.configSource {
210221
case configSourceCommand:
211222
if config.runtime == "docker" {
@@ -323,9 +334,9 @@ func (c *config) resolveConfigSource() (toml.Loader, error) {
323334
func (c *config) getCommandConfigSource() toml.Loader {
324335
switch c.runtime {
325336
case "containerd":
326-
return containerd.CommandLineSource("")
337+
return containerd.CommandLineSource("", c.executablePath)
327338
case "crio":
328-
return crio.CommandLineSource("")
339+
return crio.CommandLineSource("", c.executablePath)
329340
}
330341
return toml.Empty
331342
}

pkg/config/engine/containerd/containerd.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {
162162
}
163163

164164
// CommandLineSource returns the CLI-based containerd config loader
165-
func CommandLineSource(hostRoot string) toml.Loader {
166-
return toml.FromCommandLine(chrootIfRequired(hostRoot, "containerd", "config", "dump")...)
165+
func CommandLineSource(hostRoot string, executablePath string) toml.Loader {
166+
if executablePath == "" {
167+
executablePath = "containerd"
168+
}
169+
return toml.FromCommandLine(chrootIfRequired(hostRoot, executablePath, "config", "dump")...)
167170
}
168171

169172
func chrootIfRequired(hostRoot string, commandLine ...string) []string {

pkg/config/engine/crio/crio.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ func (c *Config) GetRuntimeConfig(name string) (engine.RuntimeConfig, error) {
157157
func (c *Config) EnableCDI() {}
158158

159159
// CommandLineSource returns the CLI-based crio config loader
160-
func CommandLineSource(hostRoot string) toml.Loader {
160+
func CommandLineSource(hostRoot string, executablePath string) toml.Loader {
161+
if executablePath == "" {
162+
executablePath = "crio"
163+
}
161164
return toml.LoadFirst(
162-
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio", "status", "config")...),
165+
toml.FromCommandLine(chrootIfRequired(hostRoot, executablePath, "status", "config")...),
163166
toml.FromCommandLine(chrootIfRequired(hostRoot, "crio-status", "config")...),
164167
)
165168
}

0 commit comments

Comments
 (0)