Skip to content

Commit

Permalink
netdevice: add nadutils to netResourcePool
Browse files Browse the repository at this point in the history
In order to be able to unit test functions that use nadutils, use the
newly created interface in the netResourcePool.

Signed-off-by: Adrian Moreno <[email protected]>
  • Loading branch information
amorenoz committed Nov 30, 2020
1 parent 3d3457b commit 78a6ebf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func (rf *resourceFactory) GetResourcePool(rc *types.ResourceConfig, filteredDev
case types.NetDeviceType:
if len(filteredDevice) > 0 {
if _, ok := filteredDevice[0].(types.PciNetDevice); ok {
rPool = netdevice.NewNetResourcePool(rc, apiDevices, devicePool)
nadUtils := rf.GetNadUtils()
rPool = netdevice.NewNetResourcePool(nadUtils, rc, apiDevices, devicePool)
} else {
err = fmt.Errorf("invalid device list for NetDeviceType")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/netdevice/netResourcePool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"strings"

nettypes "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
nadutils "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"

"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/resources"
Expand All @@ -30,17 +29,19 @@ import (
type netResourcePool struct {
*resources.ResourcePoolImpl
selectors *types.NetDeviceSelectors
nadutils types.NadUtils
}

var _ types.ResourcePool = &netResourcePool{}

// NewNetResourcePool returns an instance of resourcePool
func NewNetResourcePool(rc *types.ResourceConfig, apiDevices map[string]*pluginapi.Device, devicePool map[string]types.PciDevice) types.ResourcePool {
func NewNetResourcePool(nadutils types.NadUtils, rc *types.ResourceConfig, apiDevices map[string]*pluginapi.Device, devicePool map[string]types.PciDevice) types.ResourcePool {
rp := resources.NewResourcePool(rc, apiDevices, devicePool)
s, _ := rc.SelectorObj.(*types.NetDeviceSelectors)
return &netResourcePool{
ResourcePoolImpl: rp,
selectors: s,
nadutils: nadutils,
}
}

Expand Down Expand Up @@ -101,7 +102,7 @@ func (rp *netResourcePool) StoreDeviceInfoFile(resourceNamePrefix string) error
},
}
resource := fmt.Sprintf("%s/%s", resourceNamePrefix, rp.GetConfig().ResourceName)
if err := nadutils.SaveDeviceInfoForDP(resource, id, &devInfo); err != nil {
if err := rp.nadutils.SaveDeviceInfoFile(resource, id, &devInfo); err != nil {
return err
}
}
Expand All @@ -113,7 +114,7 @@ func (rp *netResourcePool) CleanDeviceInfoFile(resourceNamePrefix string) error
errors := make([]string, 0)
for id := range rp.GetDevicePool() {
resource := fmt.Sprintf("%s/%s", resourceNamePrefix, rp.GetConfig().ResourceName)
if err := nadutils.CleanDeviceInfoForDP(resource, id); err != nil {
if err := rp.nadutils.CleanDeviceInfoFile(resource, id); err != nil {
// Continue trying to clean.
errors = append(errors, err.Error())
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/netdevice/netResourcePool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package netdevice_test

import (
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/factory"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/netdevice"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types/mocks"
Expand All @@ -27,6 +28,8 @@ import (

var _ = Describe("NetResourcePool", func() {
Context("getting a new instance of the pool", func() {
rf := factory.NewResourceFactory("fake", "fake", true)
nadutils := rf.GetNadUtils()
rc := &types.ResourceConfig{
ResourceName: "fake",
ResourcePrefix: "fake",
Expand All @@ -35,14 +38,16 @@ var _ = Describe("NetResourcePool", func() {
devs := map[string]*v1beta1.Device{}
pcis := map[string]types.PciDevice{}

rp := netdevice.NewNetResourcePool(rc, devs, pcis)
rp := netdevice.NewNetResourcePool(nadutils, rc, devs, pcis)

It("should return a valid instance of the pool", func() {
Expect(rp).ToNot(BeNil())
})
})
Describe("getting DeviceSpecs", func() {
Context("for non-RDMA devices", func() {
rf := factory.NewResourceFactory("fake", "fake", true)
nadutils := rf.GetNadUtils()
rc := &types.ResourceConfig{
ResourceName: "fake",
ResourcePrefix: "fake",
Expand Down Expand Up @@ -78,7 +83,7 @@ var _ = Describe("NetResourcePool", func() {

pcis := map[string]types.PciDevice{"fake1": fake1, "fake2": fake2, "fake3": fake3}

rp := netdevice.NewNetResourcePool(rc, devs, pcis)
rp := netdevice.NewNetResourcePool(nadutils, rc, devs, pcis)

devIDs := []string{"fake1", "fake2"}

Expand All @@ -93,6 +98,8 @@ var _ = Describe("NetResourcePool", func() {
})
})
Context("for RDMA devices", func() {
rf := factory.NewResourceFactory("fake", "fake", true)
nadutils := rf.GetNadUtils()
rc := &types.ResourceConfig{
ResourceName: "fake",
ResourcePrefix: "fake",
Expand Down Expand Up @@ -123,7 +130,7 @@ var _ = Describe("NetResourcePool", func() {

pcis := map[string]types.PciDevice{"fake1": fake1, "fake2": fake2}

rp := netdevice.NewNetResourcePool(rc, devs, pcis)
rp := netdevice.NewNetResourcePool(nadutils, rc, devs, pcis)

devIDs := []string{"fake1", "fake2"}

Expand Down

0 comments on commit 78a6ebf

Please sign in to comment.