From 066d8f30bc53e5a4697d2fae727b80c789b6e949 Mon Sep 17 00:00:00 2001 From: Christopher Desiniotis Date: Fri, 9 Jun 2023 17:27:31 -0700 Subject: [PATCH] Allow options to be passed when creating an instance of the nvpci interface Signed-off-by: Christopher Desiniotis --- pkg/nvmdev/nvmdev.go | 4 +++- pkg/nvpci/mock.go | 2 +- pkg/nvpci/nvpci.go | 32 ++++++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pkg/nvmdev/nvmdev.go b/pkg/nvmdev/nvmdev.go index fa2c3fc..24ec228 100644 --- a/pkg/nvmdev/nvmdev.go +++ b/pkg/nvmdev/nvmdev.go @@ -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) } diff --git a/pkg/nvpci/mock.go b/pkg/nvpci/mock.go index 0f3df3a..8f35fb6 100644 --- a/pkg/nvpci/mock.go +++ b/pkg/nvpci/mock.go @@ -44,7 +44,7 @@ func NewMockNvpci() (mock *MockNvpci, rerr error) { }() mock = &MockNvpci{ - NewFrom(rootDir).(*nvpci), + New(WithPCIDevicesRoot(rootDir)).(*nvpci), } return mock, nil diff --git a/pkg/nvpci/nvpci.go b/pkg/nvpci/nvpci.go index e6b23b6..1640590 100644 --- a/pkg/nvpci/nvpci.go +++ b/pkg/nvpci/nvpci.go @@ -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 } }