Skip to content

Commit c7e09c9

Browse files
Consolidate HookName functionality on internal/discover pkg
Signed-off-by: Carlos Eduardo Arango Gutierrez <[email protected]>
1 parent bb3a54f commit c7e09c9

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

internal/discover/hooks.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ func (h *Hook) Hooks() ([]Hook, error) {
4545
return []Hook{*h}, nil
4646
}
4747

48+
type HookName string
49+
50+
// DisabledHooks allows individual hooks to be disabled.
51+
type DisabledHooks map[HookName]bool
52+
53+
const (
54+
// HookEnableCudaCompat refers to the hook used to enable CUDA Forward Compatibility.
55+
// This was added with v1.17.5 of the NVIDIA Container Toolkit.
56+
HookEnableCudaCompat = HookName("enable-cuda-compat")
57+
// directory path to be mounted into a container.
58+
HookCreateSymlinks = HookName("create-symlinks")
59+
// HookUpdateLDCache refers to the hook used to Update the dynamic linker
60+
// cache inside the directory path to be mounted into a container.
61+
HookUpdateLDCache = HookName("update-ldcache")
62+
)
63+
64+
// AllHooks maintains a future-proof list of all defined hooks.
65+
var AllHooks = []HookName{
66+
HookEnableCudaCompat,
67+
HookCreateSymlinks,
68+
HookUpdateLDCache,
69+
}
70+
4871
// Option is a function that configures the nvcdilib
4972
type Option func(*CDIHook)
5073

@@ -54,7 +77,7 @@ type CDIHook struct {
5477
}
5578

5679
type HookCreator interface {
57-
Create(string, ...string) *Hook
80+
Create(HookName, ...string) *Hook
5881
}
5982

6083
func NewHookCreator(nvidiaCDIHookPath string, debugLogging bool) HookCreator {
@@ -66,7 +89,7 @@ func NewHookCreator(nvidiaCDIHookPath string, debugLogging bool) HookCreator {
6689
return CDIHook
6790
}
6891

69-
func (c CDIHook) Create(name string, args ...string) *Hook {
92+
func (c CDIHook) Create(name HookName, args ...string) *Hook {
7093
if name == "create-symlinks" {
7194
if len(args) == 0 {
7295
return nil

internal/discover/ldconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func createLDCacheUpdateHook(hookCreator HookCreator, ldconfig string, libraries
7272
args = append(args, "--folder", f)
7373
}
7474

75-
return hookCreator.Create("update-ldcache", args...)
75+
return hookCreator.Create(HookUpdateLDCache, args...)
7676
}
7777

7878
// getLibraryPaths extracts the library dirs from the specified mounts

pkg/nvcdi/api.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"tags.cncf.io/container-device-interface/pkg/cdi"
2222
"tags.cncf.io/container-device-interface/specs-go"
2323

24+
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2425
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
2526
)
2627

@@ -36,14 +37,14 @@ type Interface interface {
3637
GetDeviceSpecsByID(...string) ([]specs.Device, error)
3738
}
3839

39-
// A HookName refers to one of the predefined set of CDI hooks that may be
40-
// included in the generated CDI specification.
41-
type HookName string
40+
// HookName is an alias for the discover.HookName type.
41+
type HookName = discover.HookName
4242

43+
// Aliases for the discover.HookName constants.
4344
const (
44-
// HookEnableCudaCompat refers to the hook used to enable CUDA Forward Compatibility.
45-
// This was added with v1.17.5 of the NVIDIA Container Toolkit.
46-
HookEnableCudaCompat = HookName("enable-cuda-compat")
45+
HookEnableCudaCompat = discover.HookEnableCudaCompat
46+
HookCreateSymlinks = discover.HookCreateSymlinks
47+
HookUpdateLDCache = discover.HookUpdateLDCache
4748
)
4849

4950
// A FeatureFlag refers to a specific feature that can be toggled in the CDI api.

pkg/nvcdi/driver-wsl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (m nvidiaSMISimlinkHook) Hooks() ([]discover.Hook, error) {
135135
}
136136
link := "/usr/bin/nvidia-smi"
137137
links := []string{fmt.Sprintf("%s::%s", target, link)}
138-
symlinkHook := m.hookCreator.Create("create-symlinks", links...)
138+
symlinkHook := m.hookCreator.Create(HookCreateSymlinks, links...)
139139

140140
return symlinkHook.Hooks()
141141
}

pkg/nvcdi/hooks.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
package nvcdi
1818

19-
// disabledHooks allows individual hooks to be disabled.
20-
type disabledHooks map[HookName]bool
21-
2219
// HookIsSupported checks whether a hook of the specified name is supported.
2320
// Hooks must be explicitly disabled, meaning that if no disabled hooks are
2421
// all hooks are supported.

pkg/nvcdi/lib.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,13 @@ type nvcdilib struct {
5858

5959
featureFlags map[FeatureFlag]bool
6060

61-
disabledHooks disabledHooks
61+
disabledHooks []discover.HookName
6262
hookCreator discover.HookCreator
6363
}
6464

6565
// New creates a new nvcdi library
6666
func New(opts ...Option) (Interface, error) {
67-
l := &nvcdilib{
68-
disabledHooks: make(disabledHooks),
69-
featureFlags: make(map[FeatureFlag]bool),
70-
}
67+
l := &nvcdilib{}
7168
for _, opt := range opts {
7269
opt(l)
7370
}
@@ -136,7 +133,7 @@ func New(opts ...Option) (Interface, error) {
136133
l.vendor = "management.nvidia.com"
137134
}
138135
// Management containers in general do not require CUDA Forward compatibility.
139-
l.disabledHooks[HookEnableCudaCompat] = true
136+
l.disabledHooks = append(l.disabledHooks, discover.HookEnableCudaCompat)
140137
lib = (*managementlib)(l)
141138
case ModeNvml:
142139
lib = (*nvmllib)(l)

pkg/nvcdi/options.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
2222
"github.com/NVIDIA/go-nvml/pkg/nvml"
2323

24+
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2425
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2526
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
2627
)
@@ -158,12 +159,9 @@ func WithLibrarySearchPaths(paths []string) Option {
158159

159160
// WithDisabledHook allows specific hooks to the disabled.
160161
// This option can be specified multiple times for each hook.
161-
func WithDisabledHook(hook HookName) Option {
162+
func WithDisabledHook[T string | HookName](hook T) Option {
162163
return func(o *nvcdilib) {
163-
if o.disabledHooks == nil {
164-
o.disabledHooks = make(map[HookName]bool)
165-
}
166-
o.disabledHooks[hook] = true
164+
o.disabledHooks = append(o.disabledHooks, discover.HookName(hook))
167165
}
168166
}
169167

0 commit comments

Comments
 (0)