Skip to content

Commit e89be14

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

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ 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
39+
Config string
40+
Socket string
41+
// EnabledCDI indicates whether CDI should be enabled.
42+
EnableCDI bool
4143
RuntimeName string
4244
RuntimeDir string
4345
SetAsDefault bool
@@ -111,6 +113,10 @@ func (o Options) UpdateConfig(cfg engine.Interface) error {
111113
}
112114
}
113115

116+
if o.EnableCDI {
117+
cfg.EnableCDI()
118+
}
119+
114120
return nil
115121
}
116122

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+
enableCDI bool
419+
expectedEnableCDIValue interface{}
420+
}{
421+
{},
422+
{
423+
enableCDI: false,
424+
expectedEnableCDIValue: nil,
425+
},
426+
{
427+
enableCDI: true,
428+
expectedEnableCDIValue: true,
429+
},
430+
}
431+
432+
for _, tc := range testCases {
433+
t.Run(fmt.Sprintf("%v", tc.enableCDI), func(t *testing.T) {
434+
o := &container.Options{
435+
EnableCDI: tc.enableCDI,
436+
RuntimeName: "nvidia",
437+
RuntimeDir: runtimeDir,
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+
enableCDI bool
375+
expectedEnableCDIValue interface{}
376+
}{
377+
{},
378+
{
379+
enableCDI: false,
380+
expectedEnableCDIValue: nil,
381+
},
382+
{
383+
enableCDI: true,
384+
expectedEnableCDIValue: true,
385+
},
386+
}
387+
388+
for _, tc := range testCases {
389+
t.Run(fmt.Sprintf("%v", tc.enableCDI), func(t *testing.T) {
390+
o := &container.Options{
391+
EnableCDI: tc.enableCDI,
392+
RuntimeName: "nvidia",
393+
RuntimeDir: runtimeDir,
394+
SetAsDefault: false,
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/runtime.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func Flags(opts *Options) []cli.Flag {
6666
Destination: &opts.RestartMode,
6767
EnvVars: []string{"RUNTIME_RESTART_MODE"},
6868
},
69+
&cli.BoolFlag{
70+
Name: "enable-cdi-in-runtime",
71+
Usage: "Enable CDI in the configured runt ime",
72+
Destination: &opts.EnableCDI,
73+
EnvVars: []string{"RUNTIME_ENABLE_CDI"},
74+
},
6975
&cli.StringFlag{
7076
Name: "host-root",
7177
Usage: "Specify the path to the host root to be used when restarting the runtime using systemd",

0 commit comments

Comments
 (0)