Allow options to be passed when creating an instance of the nvpci interface

Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
This commit is contained in:
Christopher Desiniotis 2023-06-09 17:27:31 -07:00
parent 76018d282e
commit 066d8f30bc
3 changed files with 30 additions and 8 deletions

View File

@ -380,5 +380,7 @@ func (p *ParentDevice) GetAvailableMDEVInstances(mdevType string) (int, error) {
func newNvidiaPCIDeviceFromPath(devicePath string) (*nvpci.NvidiaPCIDevice, error) {
root := filepath.Dir(devicePath)
address := filepath.Base(devicePath)
return nvpci.NewFrom(root).GetGPUByPciBusID(address)
return nvpci.New(
nvpci.WithPCIDevicesRoot(root),
).GetGPUByPciBusID(address)
}

View File

@ -44,7 +44,7 @@ func NewMockNvpci() (mock *MockNvpci, rerr error) {
}()
mock = &MockNvpci{
NewFrom(rootDir).(*nvpci),
New(WithPCIDevicesRoot(rootDir)).(*nvpci),
}
return mock, nil

View File

@ -65,6 +65,7 @@ type ResourceInterface interface {
type nvpci struct {
pciDevicesRoot string
pcidbPath string
}
var _ Interface = (*nvpci)(nil)
@ -124,14 +125,33 @@ func (d *NvidiaPCIDevice) Reset() error {
}
// New interface that allows us to get a list of all NVIDIA PCI devices
func New() Interface {
return NewFrom(PCIDevicesRoot)
func New(opts ...Option) Interface {
n := &nvpci{}
for _, opt := range opts {
opt(n)
}
if n.pciDevicesRoot == "" {
n.pciDevicesRoot = PCIDevicesRoot
}
return n
}
// NewFrom interface allows us to get a list of all NVIDIA PCI devices at a specific root directory
func NewFrom(root string) Interface {
return &nvpci{
pciDevicesRoot: root,
// Option defines a function for passing options to the New() call
type Option func(*nvpci)
// WithPCIDevicesRoot provides an Option to set the root path
// for PCI devices on the system.
func WithPCIDevicesRoot(root string) Option {
return func(n *nvpci) {
n.pciDevicesRoot = root
}
}
// WithPCIDatabasePath provides an Option to set the path
// to the pciids database file.
func WithPCIDatabasePath(path string) Option {
return func(n *nvpci) {
n.pcidbPath = path
}
}