Skip to content

Commit d167812

Browse files
author
Evan Lezar
committed
Merge branch 'cherry-pick-enable-cdi' into 'release-1.14'
Add cdi.enabled option to runtime configure See merge request nvidia/container-toolkit/container-toolkit!539
2 parents ccff00b + 7ff2399 commit d167812

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* Added detection of libnvdxgdmal.so.1 on WSL2.
1010
* Fix bug in determining default nvidia-container-runtime.user config value on SUSE-based systems.
1111
* Add `crun` to the list of configured low-level runtimes.
12+
* Add `--cdi.enabled` option to `nvidia-ctk runtime configure` command to enable CDI in containerd.
13+
* Added support for `nvidia-ctk runtime configure --enable-cdi` for the `docker` runtime. Note that this requires Docker >= 25.
1214

1315
* [toolkit-container] Bump CUDA base image version to 12.3.1.
1416
* [libnvidia-container] Added detection of libnvdxgdmal.so.1 on WSL2.

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020
"fmt"
2121
"path/filepath"
2222

23+
"github.com/urfave/cli/v2"
24+
2325
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2426
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
2527
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/containerd"
2628
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/crio"
2729
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine/docker"
2830
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/ocihook"
29-
"github.com/urfave/cli/v2"
3031
)
3132

3233
const (
@@ -71,6 +72,11 @@ type config struct {
7172
hookPath string
7273
setAsDefault bool
7374
}
75+
76+
// cdi-specific options
77+
cdi struct {
78+
enabled bool
79+
}
7480
}
7581

7682
func (m command) build() *cli.Command {
@@ -141,6 +147,11 @@ func (m command) build() *cli.Command {
141147
Usage: "set the NVIDIA runtime as the default runtime",
142148
Destination: &config.nvidiaRuntime.setAsDefault,
143149
},
150+
&cli.BoolFlag{
151+
Name: "cdi.enabled",
152+
Usage: "Enable CDI in the configured runtime",
153+
Destination: &config.cdi.enabled,
154+
},
144155
}
145156

146157
return &configure
@@ -175,6 +186,13 @@ func (m command) validateFlags(c *cli.Context, config *config) error {
175186
}
176187
}
177188

189+
if config.runtime != "containerd" && config.runtime != "docker" {
190+
if config.cdi.enabled {
191+
m.logger.Warningf("Ignoring cdi.enabled flag for %v", config.runtime)
192+
}
193+
config.cdi.enabled = false
194+
}
195+
178196
return nil
179197
}
180198

@@ -227,6 +245,11 @@ func (m command) configureConfigFile(c *cli.Context, config *config) error {
227245
return fmt.Errorf("unable to update config: %v", err)
228246
}
229247

248+
err = enableCDI(config, cfg)
249+
if err != nil {
250+
return fmt.Errorf("failed to enable CDI in %s: %w", config.runtime, err)
251+
}
252+
230253
outputPath := config.getOuputConfigPath()
231254
n, err := cfg.Save(outputPath)
232255
if err != nil {
@@ -277,3 +300,17 @@ func (m *command) configureOCIHook(c *cli.Context, config *config) error {
277300
}
278301
return nil
279302
}
303+
304+
// enableCDI enables the use of CDI in the corresponding container engine
305+
func enableCDI(config *config, cfg engine.Interface) error {
306+
if !config.cdi.enabled {
307+
return nil
308+
}
309+
switch config.runtime {
310+
case "containerd":
311+
return cfg.Set("enable_cdi", true)
312+
case "docker":
313+
return cfg.Set("experimental", true)
314+
}
315+
return fmt.Errorf("enabling CDI in %s is not supported", config.runtime)
316+
}

pkg/config/engine/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package engine
2020
type Interface interface {
2121
DefaultRuntime() string
2222
AddRuntime(string, string, bool) error
23+
Set(string, interface{}) error
2324
RemoveRuntime(string) error
2425
Save(string) (int64, error)
2526
}

pkg/config/engine/containerd/config_v1.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ func (c *ConfigV1) RemoveRuntime(name string) error {
134134
return nil
135135
}
136136

137+
// SetOption sets the specified containerd option.
138+
func (c *ConfigV1) Set(key string, value interface{}) error {
139+
config := *c.Tree
140+
config.SetPath([]string{"plugins", "cri", "containerd", key}, value)
141+
*c.Tree = config
142+
return nil
143+
}
144+
137145
// Save wrotes the config to a file
138146
func (c ConfigV1) Save(path string) (int64, error) {
139147
return (Config)(c).Save(path)

pkg/config/engine/containerd/config_v2.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ func (c *Config) getRuntimeAnnotations(path []string) ([]string, error) {
9090
return annotations, nil
9191
}
9292

93+
// Set sets the specified containerd option.
94+
func (c *Config) Set(key string, value interface{}) error {
95+
config := *c.Tree
96+
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", key}, value)
97+
*c.Tree = config
98+
return nil
99+
}
100+
93101
// DefaultRuntime returns the default runtime for the cri-o config
94102
func (c Config) DefaultRuntime() string {
95103
if runtime, ok := c.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}).(string); ok {

pkg/config/engine/crio/crio.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func (c *Config) RemoveRuntime(name string) error {
9999
return nil
100100
}
101101

102+
// Set is not supported for cri-o configs.
103+
func (c *Config) Set(key string, value interface{}) error {
104+
return fmt.Errorf("Set is not supported for crio configs")
105+
}
106+
102107
// Save writes the config to the specified path
103108
func (c Config) Save(path string) (int64, error) {
104109
config := (toml.Tree)(c)

pkg/config/engine/docker/docker.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ func (c *Config) RemoveRuntime(name string) error {
114114
return nil
115115
}
116116

117+
// Set sets the specified docker option
118+
func (c *Config) Set(key string, value interface{}) error {
119+
(*c)[key] = value
120+
return nil
121+
}
122+
117123
// Save writes the config to the specified path
118124
func (c Config) Save(path string) (int64, error) {
119125
output, err := json.MarshalIndent(c, "", " ")

0 commit comments

Comments
 (0)