Skip to content

Commit 0aaafb4

Browse files
committed
Add option in toolkit container to enable CDI in runtime
Signed-off-by: Christopher Desiniotis <[email protected]>
1 parent 1d24179 commit 0aaafb4

File tree

5 files changed

+120
-9
lines changed

5 files changed

+120
-9
lines changed

tools/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

tools/container/runtime/containerd/config_v1_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,51 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) {
415415
}
416416
}
417417

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

tools/container/runtime/containerd/config_v2_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,52 @@ func TestUpdateV2ConfigWithRuncPresent(t *testing.T) {
369369
}
370370
}
371371

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

tools/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)

tools/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)