Skip to content

Commit

Permalink
Merge pull request #586 from ykulazhenkov/host-pkg-layout
Browse files Browse the repository at this point in the history
Change layout of the pkg/host to simplify testing
  • Loading branch information
zeeke authored Jan 18, 2024
2 parents a9a61c9 + bdb6d90 commit 527c473
Show file tree
Hide file tree
Showing 19 changed files with 1,567 additions and 595 deletions.
9 changes: 5 additions & 4 deletions pkg/helper/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/store"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
mlx "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vendors/mellanox"
)
Expand All @@ -12,21 +13,21 @@ import (
type HostHelpersInterface interface {
utils.CmdInterface
host.HostManagerInterface
host.StoreManagerInterface
store.ManagerInterface
mlx.MellanoxInterface
}

type hostHelpers struct {
utils.CmdInterface
host.HostManagerInterface
host.StoreManagerInterface
store.ManagerInterface
mlx.MellanoxInterface
}

// Use for unit tests
func NewHostHelpers(utilsHelper utils.CmdInterface,
hostManager host.HostManagerInterface,
storeManager host.StoreManagerInterface,
storeManager store.ManagerInterface,
mlxHelper mlx.MellanoxInterface) HostHelpersInterface {
return &hostHelpers{utilsHelper, hostManager, storeManager, mlxHelper}
}
Expand All @@ -35,7 +36,7 @@ func NewDefaultHostHelpers() (HostHelpersInterface, error) {
utilsHelper := utils.New()
mlxHelper := mlx.New(utilsHelper)
hostManager := host.NewHostManager(utilsHelper)
storeManager, err := host.NewStoreManager()
storeManager, err := store.NewManager()
if err != nil {
log.Log.Error(err, "failed to create store manager")
return nil, err
Expand Down
331 changes: 166 additions & 165 deletions pkg/helper/mock/mock_helper.go

Large diffs are not rendered by default.

55 changes: 0 additions & 55 deletions pkg/host/interface.go

This file was deleted.

19 changes: 19 additions & 0 deletions pkg/host/internal/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package internal

import (
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
)

const (
HostPathFromDaemon = consts.Host
RedhatReleaseFile = "/etc/redhat-release"
RhelRDMAConditionFile = "/usr/libexec/rdma-init-kernel"
RhelRDMAServiceName = "rdma"
RhelPackageManager = "yum"

UbuntuRDMAConditionFile = "/usr/sbin/rdma-ndd"
UbuntuRDMAServiceName = "rdma-ndd"
UbuntuPackageManager = "apt-get"

GenericOSReleaseFile = "/etc/os-release"
)
90 changes: 15 additions & 75 deletions pkg/host/kernel.go → pkg/host/internal/kernel/kernel.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package host
package kernel

import (
"errors"
Expand All @@ -11,77 +11,17 @@ import (

sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
)

type KernelInterface interface {
// TryEnableTun load the tun kernel module
TryEnableTun()
// TryEnableVhostNet load the vhost-net kernel module
TryEnableVhostNet()
// TryEnableRdma tries to enable RDMA on the machine base on the operating system
// if the package doesn't exist it will also will try to install it
// supported operating systems are RHEL RHCOS and ubuntu
TryEnableRdma() (bool, error)
// TriggerUdevEvent triggers a udev event
TriggerUdevEvent() error
// GetCurrentKernelArgs reads the /proc/cmdline to check the current kernel arguments
GetCurrentKernelArgs() (string, error)
// IsKernelArgsSet check is the requested kernel arguments are set
IsKernelArgsSet(string, string) bool
// Unbind unbinds a virtual function from is current driver
Unbind(string) error
// BindDpdkDriver binds the virtual function to a DPDK driver
BindDpdkDriver(string, string) error
// BindDefaultDriver binds the virtual function to is default driver
BindDefaultDriver(string) error
// BindDriverByBusAndDevice binds device to the provided driver
// bus - the bus path in the sysfs, e.g. "pci" or "vdpa"
// device - the name of the device on the bus, e.g. 0000:85:1e.5 for PCI or vpda1 for VDPA
// driver - the name of the driver, e.g. vfio-pci or vhost_vdpa.
BindDriverByBusAndDevice(bus, device, driver string) error
// HasDriver returns try if the virtual function is bind to a driver
HasDriver(string) (bool, string)
// RebindVfToDefaultDriver rebinds the virtual function to is default driver
RebindVfToDefaultDriver(string) error
// UnbindDriverIfNeeded unbinds the virtual function from a driver if needed
UnbindDriverIfNeeded(string, bool) error
// UnbindDriverByBusAndDevice unbind device identified by bus and device ID from the driver
// bus - the bus path in the sysfs, e.g. "pci" or "vdpa"
// device - the name of the device on the bus, e.g. 0000:85:1e.5 for PCI or vpda1 for VDPA
UnbindDriverByBusAndDevice(bus, device string) error
// LoadKernelModule loads a kernel module to the host
LoadKernelModule(name string, args ...string) error
// IsKernelModuleLoaded returns try if the requested kernel module is loaded
IsKernelModuleLoaded(string) (bool, error)
// ReloadDriver reloads a requested driver
ReloadDriver(string) error
// IsKernelLockdownMode returns true if the kernel is in lockdown mode
IsKernelLockdownMode() bool
// IsRHELSystem returns try if the system is a RHEL base
IsRHELSystem() (bool, error)
// IsUbuntuSystem returns try if the system is an ubuntu base
IsUbuntuSystem() (bool, error)
// IsCoreOS returns true if the system is a CoreOS or RHCOS base
IsCoreOS() (bool, error)
// RdmaIsLoaded returns try if RDMA kernel modules are loaded
RdmaIsLoaded() (bool, error)
// EnableRDMA enable RDMA on the system
EnableRDMA(string, string, string) (bool, error)
// InstallRDMA install RDMA packages on the system
InstallRDMA(string) error
// EnableRDMAOnRHELMachine enable RDMA on a RHEL base system
EnableRDMAOnRHELMachine() (bool, error)
// GetOSPrettyName returns OS name
GetOSPrettyName() (string, error)
}

type kernel struct {
utilsHelper utils.CmdInterface
}

func newKernelInterface(utilsHelper utils.CmdInterface) KernelInterface {
func New(utilsHelper utils.CmdInterface) types.KernelInterface {
return &kernel{utilsHelper: utilsHelper}
}

Expand Down Expand Up @@ -384,7 +324,7 @@ func (k *kernel) EnableRDMAOnRHELMachine() (bool, error) {

// RHEL
log.Log.Info("EnableRDMAOnRHELMachine(): enabling RDMA on RHEL machine")
isRDMAEnable, err := k.EnableRDMA(rhelRDMAConditionFile, rhelRDMAServiceName, rhelPackageManager)
isRDMAEnable, err := k.EnableRDMA(internal.RhelRDMAConditionFile, internal.RhelRDMAServiceName, internal.RhelPackageManager)
if err != nil {
log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to enable RDMA on RHEL machine")
return false, err
Expand Down Expand Up @@ -413,7 +353,7 @@ func (k *kernel) EnableRDMAOnRHELMachine() (bool, error) {

func (k *kernel) EnableRDMAOnUbuntuMachine() (bool, error) {
log.Log.Info("EnableRDMAOnUbuntuMachine(): enabling RDMA on RHEL machine")
isRDMAEnable, err := k.EnableRDMA(ubuntuRDMAConditionFile, ubuntuRDMAServiceName, ubuntuPackageManager)
isRDMAEnable, err := k.EnableRDMA(internal.UbuntuRDMAConditionFile, internal.UbuntuRDMAServiceName, internal.UbuntuPackageManager)
if err != nil {
log.Log.Error(err, "EnableRDMAOnUbuntuMachine(): failed to enable RDMA on Ubuntu machine")
return false, err
Expand Down Expand Up @@ -442,9 +382,9 @@ func (k *kernel) EnableRDMAOnUbuntuMachine() (bool, error) {

func (k *kernel) IsRHELSystem() (bool, error) {
log.Log.Info("IsRHELSystem(): checking for RHEL machine")
path := redhatReleaseFile
path := internal.RedhatReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(hostPathFromDaemon, path)
path = filepath.Join(internal.HostPathFromDaemon, path)
}
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
Expand All @@ -461,9 +401,9 @@ func (k *kernel) IsRHELSystem() (bool, error) {

func (k *kernel) IsCoreOS() (bool, error) {
log.Log.Info("IsCoreOS(): checking for CoreOS machine")
path := redhatReleaseFile
path := internal.RedhatReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(hostPathFromDaemon, path)
path = filepath.Join(internal.HostPathFromDaemon, path)
}

data, err := os.ReadFile(path)
Expand All @@ -481,9 +421,9 @@ func (k *kernel) IsCoreOS() (bool, error) {

func (k *kernel) IsUbuntuSystem() (bool, error) {
log.Log.Info("IsUbuntuSystem(): checking for Ubuntu machine")
path := genericOSReleaseFile
path := internal.GenericOSReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(hostPathFromDaemon, path)
path = filepath.Join(internal.HostPathFromDaemon, path)
}

if _, err := os.Stat(path); err != nil {
Expand Down Expand Up @@ -530,7 +470,7 @@ func (k *kernel) RdmaIsLoaded() (bool, error) {
func (k *kernel) EnableRDMA(conditionFilePath, serviceName, packageManager string) (bool, error) {
path := conditionFilePath
if !vars.UsingSystemdMode {
path = filepath.Join(hostPathFromDaemon, path)
path = filepath.Join(internal.HostPathFromDaemon, path)
}
log.Log.Info("EnableRDMA(): checking for service file", "path", path)

Expand Down Expand Up @@ -604,9 +544,9 @@ func (k *kernel) ReloadDriver(driverName string) error {
}

func (k *kernel) GetOSPrettyName() (string, error) {
path := genericOSReleaseFile
path := internal.GenericOSReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(hostPathFromDaemon, path)
path = filepath.Join(internal.HostPathFromDaemon, path)
}

log.Log.Info("GetOSPrettyName(): getting os name from os-release file")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package host
package kernel

import (
"os"
Expand All @@ -8,6 +8,7 @@ import (
. "github.com/onsi/gomega"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem"
)
Expand All @@ -21,7 +22,7 @@ func assertFileContentsEquals(path, expectedContent string) {
var _ = Describe("Kernel", func() {
Context("Drivers", func() {
var (
k KernelInterface
k types.KernelInterface
)
configureFS := func(f *fakefilesystem.FS) {
var (
Expand All @@ -33,7 +34,7 @@ var _ = Describe("Kernel", func() {
DeferCleanup(cleanFakeFs)
}
BeforeEach(func() {
k = newKernelInterface(nil)
k = New(nil)
})
Context("Unbind, UnbindDriverByBusAndDevice", func() {
It("unknown device", func() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package host
package kernel

import (
"testing"
Expand All @@ -11,11 +11,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

func TestHostManager(t *testing.T) {
func TestKernel(t *testing.T) {
log.SetLogger(zap.New(
zap.WriteTo(GinkgoWriter),
zap.Level(zapcore.Level(-2)),
zap.UseDevMode(true)))
RegisterFailHandler(Fail)
RunSpecs(t, "Package Host Suite")
RunSpecs(t, "Package Kernel Suite")
}
28 changes: 3 additions & 25 deletions pkg/host/network.go → pkg/host/internal/network/network.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package host
package network

import (
"fmt"
Expand All @@ -14,38 +14,16 @@ import (
dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils"

"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
)

type NetworkInterface interface {
// TryToGetVirtualInterfaceName tries to find the virtio interface name base on pci address
// used for virtual environment where we pass SR-IOV virtual function into the system
// supported platform openstack
TryToGetVirtualInterfaceName(string) string
// TryGetInterfaceName tries to find the SR-IOV virtual interface name base on pci address
TryGetInterfaceName(string) string
// GetPhysSwitchID returns the physical switch ID for a specific pci address
GetPhysSwitchID(string) (string, error)
// GetPhysPortName returns the physical port name for a specific pci address
GetPhysPortName(string) (string, error)
// IsSwitchdev returns true of the pci address is on switchdev mode
IsSwitchdev(string) bool
// GetNetdevMTU returns the interface MTU for devices attached to kernel drivers
GetNetdevMTU(string) int
// SetNetdevMTU sets the MTU for a request interface
SetNetdevMTU(string, int) error
// GetNetDevMac returns the network interface mac address
GetNetDevMac(string) string
// GetNetDevLinkSpeed returns the network interface link speed
GetNetDevLinkSpeed(string) string
}

type network struct {
utilsHelper utils.CmdInterface
}

func newNetworkInterface(utilsHelper utils.CmdInterface) NetworkInterface {
func New(utilsHelper utils.CmdInterface) types.NetworkInterface {
return &network{utilsHelper: utilsHelper}
}

Expand Down
Loading

0 comments on commit 527c473

Please sign in to comment.