Return nvcdi.spec.Interface from GetSpec

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar
2023-02-20 14:29:52 +02:00
parent 2e95e04359
commit 6d6cd56196
4 changed files with 41 additions and 4 deletions

View File

@@ -17,12 +17,17 @@
package nvcdi
import (
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
"github.com/sirupsen/logrus"
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device"
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/info"
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
)
type wrapper struct {
Interface
}
type nvcdilib struct {
logger *logrus.Logger
nvmllib nvml.Interface
@@ -60,6 +65,7 @@ func New(opts ...Option) Interface {
l.infolib = info.New()
}
var lib Interface
switch l.resolveMode() {
case ModeNvml:
if l.nvmllib == nil {
@@ -69,13 +75,30 @@ func New(opts ...Option) Interface {
l.devicelib = device.New(device.WithNvml(l.nvmllib))
}
return (*nvmllib)(l)
lib = (*nvmllib)(l)
case ModeWsl:
return (*wsllib)(l)
lib = (*wsllib)(l)
default:
// TODO: We would like to return an error here instead of panicking
panic("Unknown mode")
}
// TODO: We want an error here.
return nil
return &wrapper{Interface: lib}
}
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
func (l *wrapper) GetSpec() (spec.Interface, error) {
deviceSpecs, err := l.GetAllDeviceSpecs()
if err != nil {
return nil, err
}
edits, err := l.GetCommonEdits()
if err != nil {
return nil, err
}
return spec.New(deviceSpecs, *edits.ContainerEdits)
}
// resolveMode resolves the mode for CDI spec generation based on the current system.