Skip to content

Commit

Permalink
Add option in toolkit container to enable CDI in runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Desiniotis <[email protected]>
  • Loading branch information
cdesiniotis committed Dec 18, 2024
1 parent 1d24179 commit 0aaafb4
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
19 changes: 12 additions & 7 deletions tools/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ const (

// Options defines the shared options for the CLIs to configure containers runtimes.
type Options struct {
Config string
Socket string
RuntimeName string
RuntimeDir string
SetAsDefault bool
RestartMode string
HostRootMount string
Config string
Socket string
RuntimeName string
RuntimeDir string
SetAsDefault bool
RestartMode string
HostRootMount string
RuntimeEnableCDI bool
}

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

if o.RuntimeEnableCDI {
cfg.EnableCDI()
}

return nil
}

Expand Down
45 changes: 45 additions & 0 deletions tools/container/runtime/containerd/config_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,51 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) {
}
}

func TestUpdateV1EnableCDI(t *testing.T) {
logger, _ := testlog.NewNullLogger()
const runtimeDir = "/test/runtime/dir"

testCases := []struct {
runtimeEnableCDI bool
expectedEnableCDIValue interface{}
}{
{},
{
runtimeEnableCDI: false,
expectedEnableCDIValue: nil,
},
{
runtimeEnableCDI: true,
expectedEnableCDIValue: true,
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := &container.Options{
RuntimeName: "nvidia",
RuntimeDir: runtimeDir,
RuntimeEnableCDI: tc.runtimeEnableCDI,
}

cfg, err := toml.Empty.Load()
require.NoError(t, err, "%d: %v", i, tc)

v1 := &containerd.ConfigV1{
Logger: logger,
Tree: cfg,
RuntimeType: runtimeType,
}

err = o.UpdateConfig(v1)
require.NoError(t, err, "%d: %v", i, tc)

enableCDIValue := v1.GetPath([]string{"plugins", "cri", "containerd", "enable_cdi"})
require.EqualValues(t, tc.expectedEnableCDIValue, enableCDIValue, "%d: %v", i, tc)
})
}
}

func TestRevertV1Config(t *testing.T) {
testCases := []struct {
config map[string]interface {
Expand Down
46 changes: 46 additions & 0 deletions tools/container/runtime/containerd/config_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,52 @@ func TestUpdateV2ConfigWithRuncPresent(t *testing.T) {
}
}

func TestUpdateV2ConfigEnableCDI(t *testing.T) {
logger, _ := testlog.NewNullLogger()
const runtimeDir = "/test/runtime/dir"

testCases := []struct {
runtimeEnableCDI bool
expectedEnableCDIValue interface{}
}{
{},
{
runtimeEnableCDI: false,
expectedEnableCDIValue: nil,
},
{
runtimeEnableCDI: true,
expectedEnableCDIValue: true,
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
o := &container.Options{
RuntimeName: "nvidia",
RuntimeDir: runtimeDir,
SetAsDefault: false,
RuntimeEnableCDI: tc.runtimeEnableCDI,
}

cfg, err := toml.LoadMap(map[string]interface{}{})
require.NoError(t, err)

v2 := &containerd.Config{
Logger: logger,
Tree: cfg,
RuntimeType: runtimeType,
}

err = o.UpdateConfig(v2)
require.NoError(t, err)

enableCDIValue := cfg.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "enable_cdi"})
require.EqualValues(t, tc.expectedEnableCDIValue, enableCDIValue)
})
}
}

func TestRevertV2Config(t *testing.T) {
testCases := []struct {
config map[string]interface {
Expand Down
4 changes: 4 additions & 0 deletions tools/container/runtime/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func Setup(c *cli.Context, o *container.Options) error {
return fmt.Errorf("unable to configure docker: %v", err)
}

if o.RuntimeEnableCDI {
cfg.Set("features", map[string]bool{"cdi": true})
}

err = RestartDocker(o)
if err != nil {
return fmt.Errorf("unable to restart docker: %v", err)
Expand Down
15 changes: 13 additions & 2 deletions tools/container/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
const (
defaultSetAsDefault = true
// defaultRuntimeName specifies the NVIDIA runtime to be use as the default runtime if setting the default runtime is enabled
defaultRuntimeName = "nvidia"
defaultHostRootMount = "/host"
defaultRuntimeName = "nvidia"
defaultHostRootMount = "/host"
defaultRuntimeEnableCDI = false

runtimeSpecificDefault = "RUNTIME_SPECIFIC_DEFAULT"
)
Expand Down Expand Up @@ -89,6 +90,13 @@ func Flags(opts *Options) []cli.Flag {
EnvVars: []string{"NVIDIA_RUNTIME_SET_AS_DEFAULT", "CONTAINERD_SET_AS_DEFAULT", "DOCKER_SET_AS_DEFAULT"},
Hidden: true,
},
&cli.BoolFlag{
Name: "runtime-enable-cdi",
Usage: "Enable CDI in the configured runtime",
Value: defaultRuntimeEnableCDI,
Destination: &opts.RuntimeEnableCDI,
EnvVars: []string{"RUNTIME_ENABLE_CDI"},
},
}

flags = append(flags, containerd.Flags(&opts.containerdOptions)...)
Expand Down Expand Up @@ -124,6 +132,9 @@ func ValidateOptions(opts *Options, runtime string, toolkitRoot string) error {
if opts.RestartMode == runtimeSpecificDefault {
opts.RestartMode = crio.DefaultRestartMode
}
if opts.RuntimeEnableCDI {
opts.RuntimeEnableCDI = false
}
case docker.Name:
if opts.Config == runtimeSpecificDefault {
opts.Config = docker.DefaultConfig
Expand Down

0 comments on commit 0aaafb4

Please sign in to comment.