Skip to content

Commit eb48d2d

Browse files
committed
Enable CDI in runtime if CDI_ENABLED is set
This change also enables CDI in the configured runtime when the toolkit is installed with CDI enabled. Signed-off-by: Evan Lezar <[email protected]>
1 parent b71bb87 commit eb48d2d

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (m command) build() *cli.Command {
163163
},
164164
&cli.BoolFlag{
165165
Name: "cdi.enabled",
166-
Aliases: []string{"cdi.enable"},
166+
Aliases: []string{"cdi.enable", "enable-cdi"},
167167
Usage: "Enable CDI in the configured runtime",
168168
Destination: &config.cdi.enabled,
169169
},

tools/container/nvidia-toolkit/run.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@ func main() {
129129
log.Infof("Completed %v", c.Name)
130130
}
131131

132-
func validateFlags(_ *cli.Context, o *options) error {
132+
func validateFlags(c *cli.Context, o *options) error {
133133
if filepath.Base(o.pidFile) != toolkitPidFilename {
134134
return fmt.Errorf("invalid toolkit.pid path %v", o.pidFile)
135135
}
136136
if err := toolkit.ValidateOptions(&o.toolkitOptions, o.toolkitRoot()); err != nil {
137137
return err
138138
}
139-
if err := runtime.ValidateOptions(&o.runtimeOptions, o.runtime, o.toolkitRoot()); err != nil {
139+
if err := runtime.ValidateOptions(c, &o.runtimeOptions, o.runtime, o.toolkitRoot(), &o.toolkitOptions); err != nil {
140140
return err
141141
}
142142
return nil

tools/container/runtime/runtime.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime/containerd"
2626
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime/crio"
2727
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/runtime/docker"
28+
"github.com/NVIDIA/nvidia-container-toolkit/tools/container/toolkit"
2829
)
2930

3031
const (
@@ -104,10 +105,14 @@ func Flags(opts *Options) []cli.Flag {
104105
}
105106

106107
// ValidateOptions checks whether the specified options are valid
107-
func ValidateOptions(opts *Options, runtime string, toolkitRoot string) error {
108+
func ValidateOptions(c *cli.Context, opts *Options, runtime string, toolkitRoot string, to *toolkit.Options) error {
108109
// We set this option here to ensure that it is available in future calls.
109110
opts.RuntimeDir = toolkitRoot
110111

112+
if !c.IsSet("enable-cdi-in-runtime") {
113+
opts.EnableCDI = to.CDI.Enabled
114+
}
115+
111116
// Apply the runtime-specific config changes.
112117
switch runtime {
113118
case containerd.Name:

tools/container/toolkit/toolkit.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ const (
4848
toolkitPidFilename = "toolkit.pid"
4949
)
5050

51+
type cdiOptions struct {
52+
Enabled bool
53+
outputDir string
54+
kind string
55+
vendor string
56+
class string
57+
}
58+
5159
type Options struct {
5260
DriverRoot string
5361
DevRoot string
@@ -67,11 +75,8 @@ type Options struct {
6775

6876
ContainerCLIDebug string
6977

70-
cdiEnabled bool
71-
cdiOutputDir string
72-
cdiKind string
73-
cdiVendor string
74-
cdiClass string
78+
// CDI stores the CDI options for the toolkit.
79+
CDI cdiOptions
7580

7681
createDeviceNodes cli.StringSlice
7782

@@ -174,21 +179,21 @@ func Flags(opts *Options) []cli.Flag {
174179
Name: "cdi-enabled",
175180
Aliases: []string{"enable-cdi"},
176181
Usage: "enable the generation of a CDI specification",
177-
Destination: &opts.cdiEnabled,
182+
Destination: &opts.CDI.Enabled,
178183
EnvVars: []string{"CDI_ENABLED", "ENABLE_CDI"},
179184
},
180185
&cli.StringFlag{
181186
Name: "cdi-output-dir",
182187
Usage: "the directory where the CDI output files are to be written. If this is set to '', no CDI specification is generated.",
183188
Value: "/var/run/cdi",
184-
Destination: &opts.cdiOutputDir,
189+
Destination: &opts.CDI.outputDir,
185190
EnvVars: []string{"CDI_OUTPUT_DIR"},
186191
},
187192
&cli.StringFlag{
188193
Name: "cdi-kind",
189194
Usage: "the vendor string to use for the generated CDI specification",
190195
Value: "management.nvidia.com/gpu",
191-
Destination: &opts.cdiKind,
196+
Destination: &opts.CDI.kind,
192197
EnvVars: []string{"CDI_KIND"},
193198
},
194199
&cli.BoolFlag{
@@ -221,19 +226,19 @@ func ValidateOptions(opts *Options, toolkitRoot string) error {
221226
return fmt.Errorf("invalid --toolkit-root option: %v", toolkitRoot)
222227
}
223228

224-
vendor, class := parser.ParseQualifier(opts.cdiKind)
229+
vendor, class := parser.ParseQualifier(opts.CDI.kind)
225230
if err := parser.ValidateVendorName(vendor); err != nil {
226231
return fmt.Errorf("invalid CDI vendor name: %v", err)
227232
}
228233
if err := parser.ValidateClassName(class); err != nil {
229234
return fmt.Errorf("invalid CDI class name: %v", err)
230235
}
231-
opts.cdiVendor = vendor
232-
opts.cdiClass = class
236+
opts.CDI.vendor = vendor
237+
opts.CDI.class = class
233238

234-
if opts.cdiEnabled && opts.cdiOutputDir == "" {
239+
if opts.CDI.Enabled && opts.CDI.outputDir == "" {
235240
log.Warning("Skipping CDI spec generation (no output directory specified)")
236-
opts.cdiEnabled = false
241+
opts.CDI.Enabled = false
237242
}
238243

239244
isDisabled := false
@@ -246,7 +251,7 @@ func ValidateOptions(opts *Options, toolkitRoot string) error {
246251
break
247252
}
248253
}
249-
if !opts.cdiEnabled && !isDisabled {
254+
if !opts.CDI.Enabled && !isDisabled {
250255
log.Info("disabling device node creation since --cdi-enabled=false")
251256
isDisabled = true
252257
}
@@ -761,7 +766,7 @@ func createDeviceNodes(opts *Options) error {
761766

762767
// generateCDISpec generates a CDI spec for use in management containers
763768
func generateCDISpec(opts *Options, nvidiaCDIHookPath string) error {
764-
if !opts.cdiEnabled {
769+
if !opts.CDI.Enabled {
765770
return nil
766771
}
767772
log.Info("Generating CDI spec for management containers")
@@ -770,8 +775,8 @@ func generateCDISpec(opts *Options, nvidiaCDIHookPath string) error {
770775
nvcdi.WithDriverRoot(opts.DriverRootCtrPath),
771776
nvcdi.WithDevRoot(opts.DevRootCtrPath),
772777
nvcdi.WithNVIDIACDIHookPath(nvidiaCDIHookPath),
773-
nvcdi.WithVendor(opts.cdiVendor),
774-
nvcdi.WithClass(opts.cdiClass),
778+
nvcdi.WithVendor(opts.CDI.vendor),
779+
nvcdi.WithClass(opts.CDI.class),
775780
)
776781
if err != nil {
777782
return fmt.Errorf("failed to create CDI library for management containers: %v", err)
@@ -796,7 +801,7 @@ func generateCDISpec(opts *Options, nvidiaCDIHookPath string) error {
796801
if err != nil {
797802
return fmt.Errorf("failed to generate CDI name for management containers: %v", err)
798803
}
799-
err = spec.Save(filepath.Join(opts.cdiOutputDir, name))
804+
err = spec.Save(filepath.Join(opts.CDI.outputDir, name))
800805
if err != nil {
801806
return fmt.Errorf("failed to save CDI spec for management containers: %v", err)
802807
}

0 commit comments

Comments
 (0)