Skip to content

Commit eb7430d

Browse files
[no-relnote] devel commit
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent 1f0719a commit eb7430d

File tree

11 files changed

+116
-302
lines changed

11 files changed

+116
-302
lines changed

cmd/nvidia-ctk/cdi/generate/generate.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type options struct {
6262
configSearchPaths []string
6363
librarySearchPaths []string
6464
disabledHooks []string
65-
enableChmodHook bool
65+
enabledHooks []string
6666

6767
csv struct {
6868
files []string
@@ -215,11 +215,12 @@ func (m command) build() *cli.Command {
215215
Destination: &opts.disabledHooks,
216216
Sources: cli.EnvVars("NVIDIA_CTK_CDI_GENERATE_DISABLED_HOOKS"),
217217
},
218-
&cli.BoolFlag{
219-
Name: "enable-chmod-hook",
220-
Usage: "Enable the chmod hook for device folder permissions. This hook is disabled by default.",
221-
Destination: &opts.enableChmodHook,
222-
Sources: cli.EnvVars("NVIDIA_CTK_CDI_GENERATE_ENABLE_CHMOD_HOOK"),
218+
&cli.StringSliceFlag{
219+
Name: "enable-hook",
220+
Aliases: []string{"enable-hooks", "force-hook", "force-hooks"},
221+
Usage: "Explicitly enable a hook in the generated CDI specification. This can be specified multiple times.",
222+
Destination: &opts.enabledHooks,
223+
Sources: cli.EnvVars("NVIDIA_CTK_CDI_GENERATE_ENABLED_HOOKS", "NVIDIA_CTK_CDI_GENERATE_FORCE_HOOKS"),
223224
},
224225
},
225226
}
@@ -299,6 +300,33 @@ func formatFromFilename(filename string) string {
299300
}
300301

301302
func (m command) generateSpec(opts *options) (spec.Interface, error) {
303+
// Load hooks configuration from config file if not specified via CLI
304+
if m.config != nil {
305+
if len(opts.disabledHooks) == 0 {
306+
if disabledHooks := m.config.Get("nvidia-container-runtime-hook.disabled"); disabledHooks != nil {
307+
if hooks, ok := disabledHooks.([]interface{}); ok {
308+
for _, hook := range hooks {
309+
if h, ok := hook.(string); ok {
310+
opts.disabledHooks = append(opts.disabledHooks, h)
311+
}
312+
}
313+
}
314+
}
315+
}
316+
317+
if len(opts.enabledHooks) == 0 {
318+
if enabledHooks := m.config.Get("nvidia-container-runtime-hook.enabled"); enabledHooks != nil {
319+
if hooks, ok := enabledHooks.([]interface{}); ok {
320+
for _, hook := range hooks {
321+
if h, ok := hook.(string); ok {
322+
opts.enabledHooks = append(opts.enabledHooks, h)
323+
}
324+
}
325+
}
326+
}
327+
}
328+
}
329+
302330
var deviceNamers []nvcdi.DeviceNamer
303331
for _, strategy := range opts.deviceNameStrategies {
304332
deviceNamer, err := nvcdi.NewDeviceNamer(strategy)
@@ -328,8 +356,8 @@ func (m command) generateSpec(opts *options) (spec.Interface, error) {
328356
cdiOptions = append(cdiOptions, nvcdi.WithDisabledHook(hook))
329357
}
330358

331-
if opts.enableChmodHook {
332-
cdiOptions = append(cdiOptions, nvcdi.WithEnableChmodHook(true))
359+
for _, hook := range opts.enabledHooks {
360+
cdiOptions = append(cdiOptions, nvcdi.WithEnabledHook(hook))
333361
}
334362

335363
cdilib, err := nvcdi.New(cdiOptions...)

cmd/nvidia-ctk/cdi/generate/generate_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ containerEdits:
328328
{
329329
description: "enableChmodHook",
330330
options: options{
331-
format: "yaml",
332-
mode: "nvml",
333-
vendor: "example.com",
334-
class: "device",
335-
driverRoot: driverRoot,
336-
enableChmodHook: true,
331+
format: "yaml",
332+
mode: "nvml",
333+
vendor: "example.com",
334+
class: "device",
335+
driverRoot: driverRoot,
336+
enabledHooks: []string{"enable-cuda-compat"},
337337
},
338338
expectedOptions: options{
339339
format: "yaml",
@@ -342,7 +342,7 @@ containerEdits:
342342
class: "device",
343343
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
344344
driverRoot: driverRoot,
345-
enableChmodHook: true,
345+
enabledHooks: []string{"enable-cuda-compat"},
346346
},
347347
expectedSpec: `---
348348
cdiVersion: 0.5.0

internal/config/features.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ type features struct {
4242
// possibly bypassing other checks by an orchestration system such as
4343
// kubernetes.
4444
IgnoreImexChannelRequests *feature `toml:"ignore-imex-channel-requests,omitempty"`
45-
// EnableChmodHook allows the chmod hook to be injected for device folder permissions.
46-
// This hook was originally added as a workaround for a specific crun issue with device
47-
// nodes in subdirectories of /dev (e.g., /dev/dri/*, /dev/nvidia-caps/*).
48-
// Since this issue has been resolved in newer versions of crun, this hook is disabled
49-
// by default. Users who still require this functionality can explicitly enable it.
50-
EnableChmodHook *feature `toml:"enable-chmod-hook,omitempty"`
5145
}
5246

5347
type feature bool

internal/config/features_test.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

internal/config/hook.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ type RuntimeHookConfig struct {
2323
Path string `toml:"path"`
2424
// SkipModeDetection disables the mode check for the runtime hook.
2525
SkipModeDetection bool `toml:"skip-mode-detection"`
26+
// DisabledHooks specifies the hooks that are disabled for the NVIDIA
27+
// Container Runtime hook.
28+
DisabledHooks []string `toml:"disabled,omitempty"`
29+
// EnabledHooks specifies the hooks that are enabled for the NVIDIA
30+
// Container Runtime hook.
31+
// Specify a list of explicitly enabled hooks. If a hook is specified as
32+
// both enabled and disabled, it will be enabled.
33+
EnabledHooks []string `toml:"enabled,omitempty"`
2634
}

internal/discover/hooks.go

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ const (
5050
defaultNvidiaCDIHookPath = "/usr/bin/nvidia-cdi-hook"
5151
)
5252

53+
// defaultDisabledHooks defines hooks that are disabled by default.
54+
// These hooks can be explicitly enabled using the WithEnabledHooks option.
55+
var defaultDisabledHooks = map[HookName]bool{
56+
// ChmodHook is disabled by default as it was a workaround for older
57+
// versions of crun that has since been fixed.
58+
ChmodHook: true,
59+
}
60+
5361
var _ Discover = (*Hook)(nil)
5462

5563
// Devices returns an empty list of devices for a Hook discoverer.
@@ -81,8 +89,9 @@ type Option func(*cdiHookCreator)
8189

8290
type cdiHookCreator struct {
8391
nvidiaCDIHookPath string
84-
disabledHooks map[HookName]bool
85-
enableChmodHook bool
92+
// hookStates tracks the enabled/disabled state of each hook
93+
// If not present in the map, the hook uses its default state
94+
hookStates map[HookName]bool
8695

8796
fixedArgs []string
8897
debugLogging bool
@@ -101,12 +110,12 @@ type HookCreator interface {
101110
Create(HookName, ...string) *Hook
102111
}
103112

104-
// WithDisabledHooks sets the set of hooks that are disabled for the CDI hook creator.
113+
// WithDisabledHooks explicitly disables the specified hooks.
105114
// This can be specified multiple times.
106115
func WithDisabledHooks(hooks ...HookName) Option {
107116
return func(c *cdiHookCreator) {
108117
for _, hook := range hooks {
109-
c.disabledHooks[hook] = true
118+
c.hookStates[hook] = false
110119
}
111120
}
112121
}
@@ -118,25 +127,27 @@ func WithNVIDIACDIHookPath(nvidiaCDIHookPath string) Option {
118127
}
119128
}
120129

121-
// WithEnableChmodHook allows the chmod hook to be enabled.
122-
// By default, the chmod hook is disabled as it was a workaround for older
123-
// versions of crun that has since been fixed.
124-
func WithEnableChmodHook(enabled bool) Option {
130+
// WithEnabledHooks explicitly enables the specified hooks.
131+
// This is useful for enabling hooks that are disabled by default.
132+
func WithEnabledHooks(hooks ...HookName) Option {
125133
return func(c *cdiHookCreator) {
126-
c.enableChmodHook = enabled
134+
for _, hook := range hooks {
135+
c.hookStates[hook] = true
136+
}
127137
}
128138
}
129139

130140
func NewHookCreator(opts ...Option) HookCreator {
131141
cdiHookCreator := &cdiHookCreator{
132142
nvidiaCDIHookPath: defaultNvidiaCDIHookPath,
133-
disabledHooks: make(map[HookName]bool),
143+
hookStates: make(map[HookName]bool),
134144
}
135145
for _, opt := range opts {
136146
opt(cdiHookCreator)
137147
}
138148

139-
if cdiHookCreator.disabledHooks[AllHooks] {
149+
// Check if all hooks are disabled
150+
if state, exists := cdiHookCreator.hookStates[AllHooks]; exists && !state {
140151
return &allDisabledHookCreator{}
141152
}
142153

@@ -162,24 +173,31 @@ func (c cdiHookCreator) Create(name HookName, args ...string) *Hook {
162173

163174
// isDisabled checks if the specified hook name is disabled.
164175
func (c cdiHookCreator) isDisabled(name HookName, args ...string) bool {
165-
if c.disabledHooks[name] {
166-
return true
167-
}
168-
169-
switch name {
170-
case CreateSymlinksHook:
171-
if len(args) == 0 {
176+
// Check if we have an explicit state for this hook
177+
if state, exists := c.hookStates[name]; exists {
178+
// If explicitly disabled, return true
179+
if !state {
172180
return true
173181
}
174-
case ChmodHook:
175-
// ChmodHook is disabled by default unless explicitly enabled
176-
if !c.enableChmodHook {
177-
return true
178-
}
179-
if len(args) == 0 {
180-
return true
182+
// If explicitly enabled, check only for empty args
183+
if name == CreateSymlinksHook || name == ChmodHook {
184+
return len(args) == 0
181185
}
186+
return false
182187
}
188+
189+
// No explicit state, check default behavior
190+
// First check if it's disabled by default
191+
if defaultDisabledHooks[name] {
192+
return true
193+
}
194+
195+
// Apply default logic for hooks that are enabled by default
196+
if name == CreateSymlinksHook {
197+
// Skip if no symlinks to create
198+
return len(args) == 0
199+
}
200+
183201
return false
184202
}
185203

0 commit comments

Comments
 (0)