Merge pull request #1143 from elezar/add-device-ids-to-getspec

Add device IDs to nvcdi.GetSpec API
This commit is contained in:
Evan Lezar 2025-06-13 16:41:43 +02:00 committed by GitHub
commit cc7812470f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 25 additions and 28 deletions

View File

@ -144,28 +144,13 @@ func generateAutomaticCDISpec(logger logger.Interface, cfg *config.Config, devic
return nil, fmt.Errorf("failed to construct CDI library: %w", err) return nil, fmt.Errorf("failed to construct CDI library: %w", err)
} }
identifiers := []string{} var identifiers []string
for _, device := range devices { for _, device := range devices {
_, _, id := parser.ParseDevice(device) _, _, id := parser.ParseDevice(device)
identifiers = append(identifiers, id) identifiers = append(identifiers, id)
} }
deviceSpecs, err := cdilib.GetDeviceSpecsByID(identifiers...) return cdilib.GetSpec(identifiers...)
if err != nil {
return nil, fmt.Errorf("failed to get CDI device specs: %w", err)
}
commonEdits, err := cdilib.GetCommonEdits()
if err != nil {
return nil, fmt.Errorf("failed to get common CDI spec edits: %w", err)
}
return spec.New(
spec.WithDeviceSpecs(deviceSpecs),
spec.WithEdits(*commonEdits.ContainerEdits),
spec.WithVendor("runtime.nvidia.com"),
spec.WithClass("gpu"),
)
} }
type deduplicatedDeviceRequestor struct { type deduplicatedDeviceRequestor struct {

View File

@ -27,7 +27,7 @@ import (
// Interface defines the API for the nvcdi package // Interface defines the API for the nvcdi package
type Interface interface { type Interface interface {
GetSpec() (spec.Interface, error) GetSpec(...string) (spec.Interface, error)
GetCommonEdits() (*cdi.ContainerEdits, error) GetCommonEdits() (*cdi.ContainerEdits, error)
GetAllDeviceSpecs() ([]specs.Device, error) GetAllDeviceSpecs() ([]specs.Device, error)
GetGPUDeviceEdits(device.Device) (*cdi.ContainerEdits, error) GetGPUDeviceEdits(device.Device) (*cdi.ContainerEdits, error)

View File

@ -58,7 +58,7 @@ func (l *gdslib) GetCommonEdits() (*cdi.ContainerEdits, error) {
// GetSpec is unsppported for the gdslib specs. // GetSpec is unsppported for the gdslib specs.
// gdslib is typically wrapped by a spec that implements GetSpec. // gdslib is typically wrapped by a spec that implements GetSpec.
func (l *gdslib) GetSpec() (spec.Interface, error) { func (l *gdslib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("GetSpec is not supported") return nil, fmt.Errorf("GetSpec is not supported")
} }

View File

@ -34,7 +34,7 @@ type csvlib nvcdilib
var _ Interface = (*csvlib)(nil) var _ Interface = (*csvlib)(nil)
// GetSpec should not be called for wsllib // GetSpec should not be called for wsllib
func (l *csvlib) GetSpec() (spec.Interface, error) { func (l *csvlib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("unexpected call to csvlib.GetSpec()") return nil, fmt.Errorf("unexpected call to csvlib.GetSpec()")
} }

View File

@ -41,7 +41,7 @@ const (
) )
// GetSpec should not be called for imexlib. // GetSpec should not be called for imexlib.
func (l *imexlib) GetSpec() (spec.Interface, error) { func (l *imexlib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("unexpected call to imexlib.GetSpec()") return nil, fmt.Errorf("unexpected call to imexlib.GetSpec()")
} }

View File

@ -36,7 +36,7 @@ type nvmllib nvcdilib
var _ Interface = (*nvmllib)(nil) var _ Interface = (*nvmllib)(nil)
// GetSpec should not be called for nvmllib // GetSpec should not be called for nvmllib
func (l *nvmllib) GetSpec() (spec.Interface, error) { func (l *nvmllib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("unexpected call to nvmllib.GetSpec()") return nil, fmt.Errorf("unexpected call to nvmllib.GetSpec()")
} }

View File

@ -32,7 +32,7 @@ type wsllib nvcdilib
var _ Interface = (*wsllib)(nil) var _ Interface = (*wsllib)(nil)
// GetSpec should not be called for wsllib // GetSpec should not be called for wsllib
func (l *wsllib) GetSpec() (spec.Interface, error) { func (l *wsllib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("unexpected call to wsllib.GetSpec()") return nil, fmt.Errorf("unexpected call to wsllib.GetSpec()")
} }

View File

@ -180,7 +180,7 @@ func (m managementDiscoverer) nodeIsBlocked(path string) bool {
// GetSpec is unsppported for the managementlib specs. // GetSpec is unsppported for the managementlib specs.
// managementlib is typically wrapped by a spec that implements GetSpec. // managementlib is typically wrapped by a spec that implements GetSpec.
func (m *managementlib) GetSpec() (spec.Interface, error) { func (m *managementlib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("GetSpec is not supported") return nil, fmt.Errorf("GetSpec is not supported")
} }

View File

@ -58,7 +58,7 @@ func (l *mofedlib) GetCommonEdits() (*cdi.ContainerEdits, error) {
// GetSpec is unsppported for the mofedlib specs. // GetSpec is unsppported for the mofedlib specs.
// mofedlib is typically wrapped by a spec that implements GetSpec. // mofedlib is typically wrapped by a spec that implements GetSpec.
func (l *mofedlib) GetSpec() (spec.Interface, error) { func (l *mofedlib) GetSpec(...string) (spec.Interface, error) {
return nil, fmt.Errorf("GetSpec is not supported") return nil, fmt.Errorf("GetSpec is not supported")
} }

View File

@ -35,8 +35,11 @@ type wrapper struct {
} }
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface. // GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
func (l *wrapper) GetSpec() (spec.Interface, error) { func (l *wrapper) GetSpec(devices ...string) (spec.Interface, error) {
deviceSpecs, err := l.GetAllDeviceSpecs() if len(devices) == 0 {
devices = append(devices, "all")
}
deviceSpecs, err := l.GetDeviceSpecsByID(devices...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -55,6 +58,16 @@ func (l *wrapper) GetSpec() (spec.Interface, error) {
) )
} }
func (l *wrapper) GetDeviceSpecsByID(devices ...string) ([]specs.Device, error) {
for _, device := range devices {
if device != "all" {
continue
}
return l.GetAllDeviceSpecs()
}
return l.Interface.GetDeviceSpecsByID(devices...)
}
// GetAllDeviceSpecs returns the device specs for all available devices. // GetAllDeviceSpecs returns the device specs for all available devices.
func (l *wrapper) GetAllDeviceSpecs() ([]specs.Device, error) { func (l *wrapper) GetAllDeviceSpecs() ([]specs.Device, error) {
return l.Interface.GetAllDeviceSpecs() return l.Interface.GetAllDeviceSpecs()

View File

@ -1 +0,0 @@
{"ociVersion":"1.0.1-dev","process":{"terminal":true,"user":{"uid":0,"gid":0},"args":["sh"],"env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","TERM=xterm"],"cwd":"/","capabilities":{"bounding":["CAP_AUDIT_WRITE","CAP_KILL","CAP_NET_BIND_SERVICE"],"effective":["CAP_AUDIT_WRITE","CAP_KILL","CAP_NET_BIND_SERVICE"],"inheritable":["CAP_AUDIT_WRITE","CAP_KILL","CAP_NET_BIND_SERVICE"],"permitted":["CAP_AUDIT_WRITE","CAP_KILL","CAP_NET_BIND_SERVICE"],"ambient":["CAP_AUDIT_WRITE","CAP_KILL","CAP_NET_BIND_SERVICE"]},"rlimits":[{"type":"RLIMIT_NOFILE","hard":1024,"soft":1024}],"noNewPrivileges":true},"root":{"path":"rootfs","readonly":true},"hostname":"runc","mounts":[{"destination":"/proc","type":"proc","source":"proc"},{"destination":"/dev","type":"tmpfs","source":"tmpfs","options":["nosuid","strictatime","mode=755","size=65536k"]},{"destination":"/dev/pts","type":"devpts","source":"devpts","options":["nosuid","noexec","newinstance","ptmxmode=0666","mode=0620","gid=5"]},{"destination":"/dev/shm","type":"tmpfs","source":"shm","options":["nosuid","noexec","nodev","mode=1777","size=65536k"]},{"destination":"/dev/mqueue","type":"mqueue","source":"mqueue","options":["nosuid","noexec","nodev"]},{"destination":"/sys","type":"sysfs","source":"sysfs","options":["nosuid","noexec","nodev","ro"]},{"destination":"/sys/fs/cgroup","type":"cgroup","source":"cgroup","options":["nosuid","noexec","nodev","relatime","ro"]}],"hooks":{"prestart":[{"path":"nvidia-container-runtime-hook","args":["nvidia-container-runtime-hook","prestart"]}]},"linux":{"resources":{"devices":[{"allow":false,"access":"rwm"}]},"namespaces":[{"type":"pid"},{"type":"network"},{"type":"ipc"},{"type":"uts"},{"type":"mount"}],"maskedPaths":["/proc/kcore","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/sys/firmware","/proc/scsi"],"readonlyPaths":["/proc/asound","/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}}