Skip to content

Commit 67d6718

Browse files
cdesiniotiselezar
authored andcommitted
Add option in toolkit container to enable CDI in runtime
Signed-off-by: Christopher Desiniotis <[email protected]>
1 parent b780604 commit 67d6718

File tree

5 files changed

+121
-9
lines changed

5 files changed

+121
-9
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ const (
3636

3737
// Options defines the shared options for the CLIs to configure containers runtimes.
3838
type Options struct {
39-
Config string
40-
Socket string
41-
RuntimeName string
42-
RuntimeDir string
43-
SetAsDefault bool
44-
RestartMode string
45-
HostRootMount string
39+
Config string
40+
Socket string
41+
RuntimeName string
42+
RuntimeDir string
43+
SetAsDefault bool
44+
RestartMode string
45+
HostRootMount string
46+
RuntimeEnableCDI bool
4647
}
4748

4849
// ParseArgs parses the command line arguments to the CLI
@@ -111,6 +112,10 @@ func (o Options) UpdateConfig(cfg engine.Interface) error {
111112
}
112113
}
113114

115+
if o.RuntimeEnableCDI {
116+
cfg.EnableCDI()
117+
}
118+
114119
return nil
115120
}
116121

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,51 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) {
410410
}
411411
}
412412

413+
func TestUpdateV1EnableCDI(t *testing.T) {
414+
logger, _ := testlog.NewNullLogger()
415+
const runtimeDir = "/test/runtime/dir"
416+
417+
testCases := []struct {
418+
runtimeEnableCDI bool
419+
expectedEnableCDIValue interface{}
420+
}{
421+
{},
422+
{
423+
runtimeEnableCDI: false,
424+
expectedEnableCDIValue: nil,
425+
},
426+
{
427+
runtimeEnableCDI: true,
428+
expectedEnableCDIValue: true,
429+
},
430+
}
431+
432+
for _, tc := range testCases {
433+
t.Run(fmt.Sprintf("%v", tc.runtimeEnableCDI), func(t *testing.T) {
434+
o := &container.Options{
435+
RuntimeName: "nvidia",
436+
RuntimeDir: runtimeDir,
437+
RuntimeEnableCDI: tc.runtimeEnableCDI,
438+
}
439+
440+
cfg, err := toml.Empty.Load()
441+
require.NoError(t, err)
442+
443+
v1 := &containerd.ConfigV1{
444+
Logger: logger,
445+
Tree: cfg,
446+
RuntimeType: runtimeType,
447+
}
448+
449+
err = o.UpdateConfig(v1)
450+
require.NoError(t, err)
451+
452+
enableCDIValue := v1.GetPath([]string{"plugins", "cri", "containerd", "enable_cdi"})
453+
require.EqualValues(t, tc.expectedEnableCDIValue, enableCDIValue)
454+
})
455+
}
456+
}
457+
413458
func TestRevertV1Config(t *testing.T) {
414459
logger, _ := testlog.NewNullLogger()
415460
testCases := []struct {

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,53 @@ func TestUpdateV2ConfigWithRuncPresent(t *testing.T) {
366366
}
367367
}
368368

369+
func TestUpdateV2ConfigEnableCDI(t *testing.T) {
370+
logger, _ := testlog.NewNullLogger()
371+
const runtimeDir = "/test/runtime/dir"
372+
373+
testCases := []struct {
374+
runtimeEnableCDI bool
375+
expectedEnableCDIValue interface{}
376+
}{
377+
{},
378+
{
379+
runtimeEnableCDI: false,
380+
expectedEnableCDIValue: nil,
381+
},
382+
{
383+
runtimeEnableCDI: true,
384+
expectedEnableCDIValue: true,
385+
},
386+
}
387+
388+
for _, tc := range testCases {
389+
t.Run(fmt.Sprintf("%v", tc.runtimeEnableCDI), func(t *testing.T) {
390+
o := &container.Options{
391+
RuntimeName: "nvidia",
392+
RuntimeDir: runtimeDir,
393+
SetAsDefault: false,
394+
RuntimeEnableCDI: tc.runtimeEnableCDI,
395+
}
396+
397+
cfg, err := toml.LoadMap(map[string]interface{}{})
398+
require.NoError(t, err)
399+
400+
v2 := &containerd.Config{
401+
Logger: logger,
402+
Tree: cfg,
403+
RuntimeType: runtimeType,
404+
CRIRuntimePluginName: "io.containerd.grpc.v1.cri",
405+
}
406+
407+
err = o.UpdateConfig(v2)
408+
require.NoError(t, err)
409+
410+
enableCDIValue := cfg.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "enable_cdi"})
411+
require.EqualValues(t, tc.expectedEnableCDIValue, enableCDIValue)
412+
})
413+
}
414+
}
415+
369416
func TestRevertV2Config(t *testing.T) {
370417
logger, _ := testlog.NewNullLogger()
371418

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func Setup(c *cli.Context, o *container.Options) error {
5555
return fmt.Errorf("unable to configure docker: %v", err)
5656
}
5757

58+
if o.RuntimeEnableCDI {
59+
cfg.Set("features", map[string]bool{"cdi": true})
60+
}
61+
5862
err = RestartDocker(o)
5963
if err != nil {
6064
return fmt.Errorf("unable to restart docker: %v", err)

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import (
3030
const (
3131
defaultSetAsDefault = true
3232
// defaultRuntimeName specifies the NVIDIA runtime to be use as the default runtime if setting the default runtime is enabled
33-
defaultRuntimeName = "nvidia"
34-
defaultHostRootMount = "/host"
33+
defaultRuntimeName = "nvidia"
34+
defaultHostRootMount = "/host"
35+
defaultRuntimeEnableCDI = false
3536

3637
runtimeSpecificDefault = "RUNTIME_SPECIFIC_DEFAULT"
3738
)
@@ -89,6 +90,13 @@ func Flags(opts *Options) []cli.Flag {
8990
EnvVars: []string{"NVIDIA_RUNTIME_SET_AS_DEFAULT", "CONTAINERD_SET_AS_DEFAULT", "DOCKER_SET_AS_DEFAULT"},
9091
Hidden: true,
9192
},
93+
&cli.BoolFlag{
94+
Name: "runtime-enable-cdi",
95+
Usage: "Enable CDI in the configured runtime",
96+
Value: defaultRuntimeEnableCDI,
97+
Destination: &opts.RuntimeEnableCDI,
98+
EnvVars: []string{"RUNTIME_ENABLE_CDI"},
99+
},
92100
}
93101

94102
flags = append(flags, containerd.Flags(&opts.containerdOptions)...)
@@ -124,6 +132,9 @@ func ValidateOptions(opts *Options, runtime string, toolkitRoot string) error {
124132
if opts.RestartMode == runtimeSpecificDefault {
125133
opts.RestartMode = crio.DefaultRestartMode
126134
}
135+
if opts.RuntimeEnableCDI {
136+
opts.RuntimeEnableCDI = false
137+
}
127138
case docker.Name:
128139
if opts.Config == runtimeSpecificDefault {
129140
opts.Config = docker.DefaultConfig

0 commit comments

Comments
 (0)