Skip to content

Commit

Permalink
sriov pkg: add DisablePlugins and GetTotalVFs methods (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenLevin authored Nov 27, 2024
1 parent 3d240e1 commit de3cbb2
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 4 deletions.
13 changes: 13 additions & 0 deletions pkg/sriov/networknodestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ func (builder *NetworkNodeStateBuilder) GetPciAddress(sriovInterfaceName string)
return interf.PciAddress, nil
}

// GetTotalVFs returns total VFs under the given interface.
func (builder *NetworkNodeStateBuilder) GetTotalVFs(sriovInterfaceName string) (int, error) {
glog.V(100).Infof("Getting totalvfs under interface %s from SriovNetworkNodeState %s",
sriovInterfaceName, builder.nodeName)

interf, err := builder.findInterfaceByName(sriovInterfaceName)
if err != nil {
return 0, err
}

return interf.TotalVfs, nil
}

func (builder *NetworkNodeStateBuilder) findInterfaceByName(sriovInterfaceName string) (*srIovV1.InterfaceExt, error) {
if valid, err := builder.validate(); !valid {
return nil, err
Expand Down
42 changes: 38 additions & 4 deletions pkg/sriov/networknodestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,16 @@ func TestNetworkNodeStateWaitUntilSyncStatus(t *testing.T) {
func TestNetworkNodeStateGetNumVFs(t *testing.T) {
testCases := []struct {
netInterface srIovV1.InterfaceExts
upInterface srIovV1.InterfaceExts
}{
{
netInterface: []srIovV1.InterfaceExt{
{Name: "eth1", LinkSpeed: "1000Mb", TotalVfs: 5},
{Name: "eth1", LinkSpeed: "1000Mb", NumVfs: 5},
},
},
{
netInterface: []srIovV1.InterfaceExt{
{Name: "eth1", LinkSpeed: "1000Mb", TotalVfs: 5},
{Name: "eth1", LinkSpeed: "1000Mb", TotalVfs: 10},
{Name: "eth1", LinkSpeed: "1000Mb", NumVfs: 5},
{Name: "eth2", LinkSpeed: "1000Mb", NumVfs: 10},
},
},
}
Expand All @@ -328,6 +327,41 @@ func TestNetworkNodeStateGetNumVFs(t *testing.T) {
}
}

func TestNetworkNodeStateGetTotalVFs(t *testing.T) {
testCases := []struct {
netInterface srIovV1.InterfaceExts
}{
{
netInterface: []srIovV1.InterfaceExt{
{Name: "eth1", LinkSpeed: "1000Mb", TotalVfs: 5},
},
},
{
netInterface: []srIovV1.InterfaceExt{
{Name: "eth1", LinkSpeed: "1000Mb", TotalVfs: 5},
{Name: "eth2", LinkSpeed: "1000Mb", TotalVfs: 10},
},
},
}

for _, testCase := range testCases {
networkNodeState := buildNodeNetworkStateWithNics(testCase.netInterface)
testSettings := clients.GetTestClients(clients.TestClientParams{
K8sMockObjects: []runtime.Object{networkNodeState},
SchemeAttachers: testSchemes,
})
networkNodeStateBuilder := NewNetworkNodeStateBuilder(testSettings, defaultNodeName, defaultNodeNsName)
err := networkNodeStateBuilder.Discover()
assert.Nil(t, err)

for _, netInterface := range testCase.netInterface {
totalVFNum, err := networkNodeStateBuilder.GetTotalVFs(netInterface.Name)
assert.Nil(t, err)
assert.Equal(t, netInterface.TotalVfs, totalVFNum)
}
}
}

func TestNetworkNodeStateGetDriverName(t *testing.T) {
testCases := []struct {
netInterface srIovV1.InterfaceExts
Expand Down
47 changes: 47 additions & 0 deletions pkg/sriov/operatorconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ import (
"github.com/openshift-kni/eco-goinfra/pkg/clients"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/strings/slices"
)

const (
sriovOperatorConfigName = "default"
)

var (
// allowedDisablePlugins represents all allowed plugins that could be disabled.
allowedDisablePlugins = []string{"mellanox"}
)

// OperatorConfigBuilder provides a struct for SriovOperatorConfig object from the cluster and
// a SriovOperatorConfig definition.
type OperatorConfigBuilder struct {
Expand Down Expand Up @@ -237,6 +243,47 @@ func (builder *OperatorConfigBuilder) WithConfigDaemonNodeSelector(
return builder
}

// WithDisablePlugins configures disablePlugins in the SriovOperatorConfig.
func (builder *OperatorConfigBuilder) WithDisablePlugins(plugins []string) *OperatorConfigBuilder {
if valid, _ := builder.validate(); !valid {
return builder
}

glog.V(100).Infof("Configuring disablePlugins %v in SriovOperatorConfig object %s",
plugins, builder.Definition.Name,
)

var pluginSlice srIovV1.PluginNameSlice

for _, plugin := range plugins {
if !slices.Contains(allowedDisablePlugins, plugin) {
glog.V(100).Infof("error to add plugin %s, allowed modes are %v", plugin, allowedDisablePlugins)

builder.errorMsg = "invalid plugin parameter"

return builder
}

pluginSlice = append(pluginSlice, srIovV1.PluginNameValue(plugin))
}

builder.Definition.Spec.DisablePlugins = pluginSlice

return builder
}

// RemoveDisablePlugins deletes disablePlugins in the SriovOperatorConfig.
func (builder *OperatorConfigBuilder) RemoveDisablePlugins() *OperatorConfigBuilder {
if valid, _ := builder.validate(); !valid {
return builder
}

glog.V(100).Infof("Removing disablePlugins in SriovOperatorConfig object %s", builder.Definition.Name)
builder.Definition.Spec.DisablePlugins = nil

return builder
}

// Update renovates the existing SriovOperatorConfig object with the new definition in builder.
func (builder *OperatorConfigBuilder) Update() (*OperatorConfigBuilder, error) {
if valid, err := builder.validate(); !valid {
Expand Down
52 changes: 52 additions & 0 deletions pkg/sriov/operatorconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,58 @@ func TestOperatorConfigWithConfigDaemonNodeSelector(t *testing.T) {
}
}

func TestOperatorConfigWithDisablePlugins(t *testing.T) {
testCases := []struct {
plugins []string
expectedErrorText string
}{
{
plugins: []string{"mellanox"},
expectedErrorText: "",
},
{
plugins: []string{"test"},
expectedErrorText: "invalid plugin parameter",
},
}

for _, testCase := range testCases {
testSettings := buildTestClientWithDummyPolicyObject()
operatorConfigBuilder := NewOperatorConfigBuilder(testSettings, "testnamespace").
WithDisablePlugins(testCase.plugins)

assert.Equal(t, testCase.expectedErrorText, operatorConfigBuilder.errorMsg)

if testCase.expectedErrorText == "" {
var pluginSlice srIovV1.PluginNameSlice
for _, plugin := range testCase.plugins {
pluginSlice = append(pluginSlice, srIovV1.PluginNameValue(plugin))
}

assert.Equal(t, pluginSlice,
operatorConfigBuilder.Definition.Spec.DisablePlugins)
}
}
}

func TestOperatorConfigRemoveDisablePlugins(t *testing.T) {
testCases := []struct {
testOperatorConfig *OperatorConfigBuilder
}{
{
testOperatorConfig: NewOperatorConfigBuilder(
buildTestClientWithDummyOperatorConfigObject(),
defaultOperatorConfigNsName).WithDisablePlugins([]string{"mellanox"}),
},
}
for _, testCase := range testCases {
operatorConfigBuilder, _ := testCase.testOperatorConfig.Create()

operatorConfigBuilder.RemoveDisablePlugins()
assert.Nil(t, operatorConfigBuilder.Definition.Spec.DisablePlugins)
}
}

func TestOperatorConfigUpdate(t *testing.T) {
testCases := []struct {
testOperatorConfig *OperatorConfigBuilder
Expand Down

0 comments on commit de3cbb2

Please sign in to comment.