diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index c1239573e8..2a60744bca 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -181,7 +181,7 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { } if !vars.UsingSystemdMode { - log.Log.V(0).Info("Run(): daemon running in systemd mode") + log.Log.V(0).Info("Run(): daemon running in daemon mode") dn.HostHelpers.TryEnableRdma() dn.HostHelpers.TryEnableTun() dn.HostHelpers.TryEnableVhostNet() @@ -189,7 +189,10 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { if err != nil { log.Log.Error(err, "failed to remove all the systemd sriov files") } + } else { + log.Log.V(0).Info("Run(): daemon running in systemd mode") } + // Only watch own SriovNetworkNodeState CR defer utilruntime.HandleCrash() defer dn.workqueue.ShutDown() diff --git a/pkg/daemon/writer.go b/pkg/daemon/writer.go index 4a82092e67..ffa80aae0f 100644 --- a/pkg/daemon/writer.go +++ b/pkg/daemon/writer.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" "os" "path/filepath" "time" @@ -19,6 +18,7 @@ import ( snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" ) diff --git a/pkg/host/driver.go b/pkg/host/driver.go index 9e7fb91210..d78f47a569 100644 --- a/pkg/host/driver.go +++ b/pkg/host/driver.go @@ -15,7 +15,7 @@ import ( ) // Unbind unbind driver for one device -func (h *HostManager) Unbind(pciAddr string) error { +func (h *hostManager) Unbind(pciAddr string) error { log.Log.V(2).Info("Unbind(): unbind device driver for device", "device", pciAddr) yes, driver := h.HasDriver(pciAddr) if !yes { @@ -33,7 +33,7 @@ func (h *HostManager) Unbind(pciAddr string) error { // BindDpdkDriver bind dpdk driver for one device // Bind the device given by "pciAddr" to the driver "driver" -func (h *HostManager) BindDpdkDriver(pciAddr, driver string) error { +func (h *hostManager) BindDpdkDriver(pciAddr, driver string) error { log.Log.V(2).Info("BindDpdkDriver(): bind device to driver", "device", pciAddr, "driver", driver) @@ -80,7 +80,7 @@ func (h *HostManager) BindDpdkDriver(pciAddr, driver string) error { // BindDefaultDriver bind driver for one device // Bind the device given by "pciAddr" to the default driver -func (h *HostManager) BindDefaultDriver(pciAddr string) error { +func (h *hostManager) BindDefaultDriver(pciAddr string) error { log.Log.V(2).Info("BindDefaultDriver(): bind device to default driver", "device", pciAddr) if yes, d := h.HasDriver(pciAddr); yes { @@ -114,7 +114,7 @@ func (h *HostManager) BindDefaultDriver(pciAddr string) error { // Workaround function to handle a case where the vf default driver is stuck and not able to create the vf kernel interface. // This function unbind the VF from the default driver and try to bind it again // bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2045087 -func (h *HostManager) RebindVfToDefaultDriver(vfAddr string) error { +func (h *hostManager) RebindVfToDefaultDriver(vfAddr string) error { log.Log.Info("RebindVfToDefaultDriver()", "vf", vfAddr) if err := h.Unbind(vfAddr); err != nil { return err @@ -128,7 +128,7 @@ func (h *HostManager) RebindVfToDefaultDriver(vfAddr string) error { return nil } -func (h *HostManager) UnbindDriverIfNeeded(vfAddr string, isRdma bool) error { +func (h *hostManager) UnbindDriverIfNeeded(vfAddr string, isRdma bool) error { if isRdma { log.Log.Info("UnbindDriverIfNeeded(): unbinding driver", "device", vfAddr) if err := h.Unbind(vfAddr); err != nil { @@ -139,7 +139,7 @@ func (h *HostManager) UnbindDriverIfNeeded(vfAddr string, isRdma bool) error { return nil } -func (h *HostManager) HasDriver(pciAddr string) (bool, string) { +func (h *hostManager) HasDriver(pciAddr string) (bool, string) { driver, err := dputils.GetDriverName(pciAddr) if err != nil { log.Log.V(2).Info("HasDriver(): device driver is empty for device", "device", pciAddr) diff --git a/pkg/host/host.go b/pkg/host/host.go index 1360c7d998..e6b3d8c0e0 100644 --- a/pkg/host/host.go +++ b/pkg/host/host.go @@ -119,17 +119,17 @@ type HostManagerInterface interface { GetOSPrettyName() (string, error) } -type HostManager struct { - utilsHelper utils.UtilsInterface +type hostManager struct { + utilsHelper utils.CmdInterface } -func NewHostManager(utilsInterface utils.UtilsInterface) HostManagerInterface { - return &HostManager{ +func NewHostManager(utilsInterface utils.CmdInterface) HostManagerInterface { + return &hostManager{ utilsHelper: utilsInterface, } } -func (h *HostManager) LoadKernelModule(name string, args ...string) error { +func (h *hostManager) LoadKernelModule(name string, args ...string) error { log.Log.Info("LoadKernelModule(): try to load kernel module", "name", name, "args", args) chrootDefinition := utils.GetChrootExtension() cmdArgs := strings.Join(args, " ") @@ -152,7 +152,7 @@ func (h *HostManager) LoadKernelModule(name string, args ...string) error { return nil } -func (h *HostManager) IsKernelModuleLoaded(kernelModuleName string) (bool, error) { +func (h *hostManager) IsKernelModuleLoaded(kernelModuleName string) (bool, error) { log.Log.Info("IsKernelModuleLoaded(): check if kernel module is loaded", "name", kernelModuleName) chrootDefinition := utils.GetChrootExtension() @@ -176,19 +176,19 @@ func (h *HostManager) IsKernelModuleLoaded(kernelModuleName string) (bool, error return false, nil } -func (h *HostManager) TryEnableTun() { +func (h *hostManager) TryEnableTun() { if err := h.LoadKernelModule("tun"); err != nil { log.Log.Error(err, "tryEnableTun(): TUN kernel module not loaded") } } -func (h *HostManager) TryEnableVhostNet() { +func (h *hostManager) TryEnableVhostNet() { if err := h.LoadKernelModule("vhost_net"); err != nil { log.Log.Error(err, "tryEnableVhostNet(): VHOST_NET kernel module not loaded") } } -func (h *HostManager) TryEnableRdma() (bool, error) { +func (h *hostManager) TryEnableRdma() (bool, error) { log.Log.V(2).Info("tryEnableRdma()") chrootDefinition := utils.GetChrootExtension() @@ -241,7 +241,7 @@ func (h *HostManager) TryEnableRdma() (bool, error) { return false, fmt.Errorf("unable to load RDMA unsupported OS: %s", osName) } -func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { +func (h *hostManager) EnableRDMAOnRHELMachine() (bool, error) { log.Log.Info("EnableRDMAOnRHELMachine()") isCoreOsSystem, err := h.IsCoreOS() if err != nil { @@ -289,7 +289,7 @@ func (h *HostManager) EnableRDMAOnRHELMachine() (bool, error) { return true, nil } -func (h *HostManager) EnableRDMAOnUbuntuMachine() (bool, error) { +func (h *hostManager) EnableRDMAOnUbuntuMachine() (bool, error) { log.Log.Info("EnableRDMAOnUbuntuMachine(): enabling RDMA on RHEL machine") isRDMAEnable, err := h.EnableRDMA(ubuntuRDMAConditionFile, ubuntuRDMAServiceName, ubuntuPackageManager) if err != nil { @@ -318,7 +318,7 @@ func (h *HostManager) EnableRDMAOnUbuntuMachine() (bool, error) { return true, nil } -func (h *HostManager) IsRHELSystem() (bool, error) { +func (h *hostManager) IsRHELSystem() (bool, error) { log.Log.Info("IsRHELSystem(): checking for RHEL machine") path := redhatReleaseFile if !vars.UsingSystemdMode { @@ -337,7 +337,7 @@ func (h *HostManager) IsRHELSystem() (bool, error) { return true, nil } -func (h *HostManager) IsCoreOS() (bool, error) { +func (h *hostManager) IsCoreOS() (bool, error) { log.Log.Info("IsCoreOS(): checking for CoreOS machine") path := redhatReleaseFile if !vars.UsingSystemdMode { @@ -357,7 +357,7 @@ func (h *HostManager) IsCoreOS() (bool, error) { return false, nil } -func (h *HostManager) IsUbuntuSystem() (bool, error) { +func (h *hostManager) IsUbuntuSystem() (bool, error) { log.Log.Info("IsUbuntuSystem(): checking for Ubuntu machine") path := genericOSReleaseFile if !vars.UsingSystemdMode { @@ -387,7 +387,7 @@ func (h *HostManager) IsUbuntuSystem() (bool, error) { return false, nil } -func (h *HostManager) RdmaIsLoaded() (bool, error) { +func (h *hostManager) RdmaIsLoaded() (bool, error) { log.Log.V(2).Info("RdmaIsLoaded()") chrootDefinition := utils.GetChrootExtension() @@ -405,7 +405,7 @@ func (h *HostManager) RdmaIsLoaded() (bool, error) { return true, nil } -func (h *HostManager) EnableRDMA(conditionFilePath, serviceName, packageManager string) (bool, error) { +func (h *hostManager) EnableRDMA(conditionFilePath, serviceName, packageManager string) (bool, error) { path := conditionFilePath if !vars.UsingSystemdMode { path = pathlib.Join(hostPathFromDaemon, path) @@ -438,7 +438,7 @@ func (h *HostManager) EnableRDMA(conditionFilePath, serviceName, packageManager return true, nil } -func (h *HostManager) InstallRDMA(packageManager string) error { +func (h *hostManager) InstallRDMA(packageManager string) error { log.Log.Info("InstallRDMA(): installing RDMA") chrootDefinition := utils.GetChrootExtension() @@ -451,7 +451,7 @@ func (h *HostManager) InstallRDMA(packageManager string) error { return nil } -func (h *HostManager) TriggerUdevEvent() error { +func (h *hostManager) TriggerUdevEvent() error { log.Log.Info("TriggerUdevEvent(): installing RDMA") err := h.ReloadDriver("mlx4_en") @@ -467,7 +467,7 @@ func (h *HostManager) TriggerUdevEvent() error { return nil } -func (h *HostManager) ReloadDriver(driverName string) error { +func (h *hostManager) ReloadDriver(driverName string) error { log.Log.Info("ReloadDriver(): reload driver", "name", driverName) chrootDefinition := utils.GetChrootExtension() @@ -481,7 +481,7 @@ func (h *HostManager) ReloadDriver(driverName string) error { return nil } -func (h *HostManager) GetOSPrettyName() (string, error) { +func (h *hostManager) GetOSPrettyName() (string, error) { path := genericOSReleaseFile if !vars.UsingSystemdMode { path = pathlib.Join(hostPathFromDaemon, path) @@ -504,7 +504,7 @@ func (h *HostManager) GetOSPrettyName() (string, error) { // IsKernelLockdownMode returns true when kernel lockdown mode is enabled // TODO: change this to return error -func (h *HostManager) IsKernelLockdownMode() bool { +func (h *hostManager) IsKernelLockdownMode() bool { path := utils.GetHostExtension() path = filepath.Join(path, "/sys/kernel/security/lockdown") diff --git a/pkg/host/kernel.go b/pkg/host/kernel.go index f0e802aac0..190835d796 100644 --- a/pkg/host/kernel.go +++ b/pkg/host/kernel.go @@ -11,7 +11,7 @@ import ( ) // GetCurrentKernelArgs This retrieves the kernel cmd line arguments -func (h *HostManager) GetCurrentKernelArgs() (string, error) { +func (h *hostManager) GetCurrentKernelArgs() (string, error) { path := consts.ProcKernelCmdLine if !vars.UsingSystemdMode { path = filepath.Join("/host", path) @@ -27,7 +27,7 @@ func (h *HostManager) GetCurrentKernelArgs() (string, error) { // IsKernelArgsSet This checks if the kernel cmd line is set properly. Please note that the same key could be repeated // several times in the kernel cmd line. We can only ensure that the kernel cmd line has the key/val kernel arg that we set. -func (h *HostManager) IsKernelArgsSet(cmdLine string, karg string) bool { +func (h *hostManager) IsKernelArgsSet(cmdLine string, karg string) bool { elements := strings.Fields(cmdLine) for _, element := range elements { if element == karg { diff --git a/pkg/host/network.go b/pkg/host/network.go index 8c0d5c643a..f4f942cbc6 100644 --- a/pkg/host/network.go +++ b/pkg/host/network.go @@ -23,7 +23,7 @@ import ( ) // TryToGetVirtualInterfaceName get the interface name of a virtio interface -func (h *HostManager) TryToGetVirtualInterfaceName(pciAddr string) string { +func (h *hostManager) TryToGetVirtualInterfaceName(pciAddr string) string { log.Log.Info("TryToGetVirtualInterfaceName() get interface name for device", "device", pciAddr) // To support different driver that is not virtio-pci like mlx @@ -55,7 +55,7 @@ func (h *HostManager) TryToGetVirtualInterfaceName(pciAddr string) string { return names[0] } -func (h *HostManager) TryGetInterfaceName(pciAddr string) string { +func (h *hostManager) TryGetInterfaceName(pciAddr string) string { names, err := dputils.GetNetNames(pciAddr) if err != nil || len(names) < 1 { return "" @@ -82,7 +82,7 @@ func (h *HostManager) TryGetInterfaceName(pciAddr string) string { return netDevName } -func (h *HostManager) GetNicSriovMode(pciAddress string) (string, error) { +func (h *hostManager) GetNicSriovMode(pciAddress string) (string, error) { log.Log.V(2).Info("GetNicSriovMode()", "device", pciAddress) devLink, err := netlink.DevLinkGetDeviceByName("pci", pciAddress) @@ -97,7 +97,7 @@ func (h *HostManager) GetNicSriovMode(pciAddress string) (string, error) { return devLink.Attrs.Eswitch.Mode, nil } -func (h *HostManager) GetPhysSwitchID(name string) (string, error) { +func (h *hostManager) GetPhysSwitchID(name string) (string, error) { swIDFile := filepath.Join(vars.FilesystemRoot, consts.SysClassNet, name, "phys_switch_id") physSwitchID, err := os.ReadFile(swIDFile) if err != nil { @@ -109,7 +109,7 @@ func (h *HostManager) GetPhysSwitchID(name string) (string, error) { return "", nil } -func (h *HostManager) GetPhysPortName(name string) (string, error) { +func (h *hostManager) GetPhysPortName(name string) (string, error) { devicePortNameFile := filepath.Join(vars.FilesystemRoot, consts.SysClassNet, name, "phys_port_name") physPortName, err := os.ReadFile(devicePortNameFile) if err != nil { @@ -121,7 +121,7 @@ func (h *HostManager) GetPhysPortName(name string) (string, error) { return "", nil } -func (h *HostManager) IsSwitchdev(name string) bool { +func (h *hostManager) IsSwitchdev(name string) bool { switchID, err := h.GetPhysSwitchID(name) if err != nil || switchID == "" { return false @@ -130,7 +130,7 @@ func (h *HostManager) IsSwitchdev(name string) bool { return true } -func (h *HostManager) GetNetdevMTU(pciAddr string) int { +func (h *hostManager) GetNetdevMTU(pciAddr string) int { log.Log.V(2).Info("GetNetdevMTU(): get MTU", "device", pciAddr) ifaceName := h.TryGetInterfaceName(pciAddr) if ifaceName == "" { @@ -151,7 +151,7 @@ func (h *HostManager) GetNetdevMTU(pciAddr string) int { return mtu } -func (h *HostManager) SetNetdevMTU(pciAddr string, mtu int) error { +func (h *hostManager) SetNetdevMTU(pciAddr string, mtu int) error { log.Log.V(2).Info("SetNetdevMTU(): set MTU", "device", pciAddr, "mtu", mtu) if mtu <= 0 { log.Log.V(2).Info("SetNetdevMTU(): refusing to set MTU", "mtu", mtu) @@ -178,7 +178,7 @@ func (h *HostManager) SetNetdevMTU(pciAddr string, mtu int) error { return nil } -func (h *HostManager) GetNetDevMac(ifaceName string) string { +func (h *hostManager) GetNetDevMac(ifaceName string) string { log.Log.V(2).Info("GetNetDevMac(): get Mac", "device", ifaceName) macFilePath := filepath.Join(vars.FilesystemRoot, consts.SysClassNet, ifaceName, "address") data, err := os.ReadFile(macFilePath) @@ -190,7 +190,7 @@ func (h *HostManager) GetNetDevMac(ifaceName string) string { return strings.TrimSpace(string(data)) } -func (h *HostManager) GetNetDevLinkSpeed(ifaceName string) string { +func (h *hostManager) GetNetDevLinkSpeed(ifaceName string) string { log.Log.V(2).Info("GetNetDevLinkSpeed(): get LinkSpeed", "device", ifaceName) speedFilePath := filepath.Join(vars.FilesystemRoot, consts.SysClassNet, ifaceName, "speed") data, err := os.ReadFile(speedFilePath) @@ -202,7 +202,7 @@ func (h *HostManager) GetNetDevLinkSpeed(ifaceName string) string { return fmt.Sprintf("%s Mb/s", strings.TrimSpace(string(data))) } -func (h *HostManager) GetLinkType(ifaceStatus sriovnetworkv1.InterfaceExt) string { +func (h *hostManager) GetLinkType(ifaceStatus sriovnetworkv1.InterfaceExt) string { log.Log.V(2).Info("GetLinkType()", "device", ifaceStatus.PciAddress) if ifaceStatus.Name != "" { link, err := netlink.LinkByName(ifaceStatus.Name) @@ -221,7 +221,7 @@ func (h *HostManager) GetLinkType(ifaceStatus sriovnetworkv1.InterfaceExt) strin return "" } -func (h *HostManager) DiscoverSriovDevices(storeManager StoreManagerInterface) ([]sriovnetworkv1.InterfaceExt, error) { +func (h *hostManager) DiscoverSriovDevices(storeManager StoreManagerInterface) ([]sriovnetworkv1.InterfaceExt, error) { log.Log.V(2).Info("DiscoverSriovDevices") pfList := []sriovnetworkv1.InterfaceExt{} diff --git a/pkg/host/service.go b/pkg/host/service.go index ef5de937a7..3d0a230062 100644 --- a/pkg/host/service.go +++ b/pkg/host/service.go @@ -54,7 +54,7 @@ type ScriptManifestFile struct { } // IsServiceExist check if service unit exist -func (h *HostManager) IsServiceExist(servicePath string) (bool, error) { +func (h *hostManager) IsServiceExist(servicePath string) (bool, error) { _, err := os.Stat(path.Join(consts.Chroot, servicePath)) if err != nil { if os.IsNotExist(err) { @@ -67,7 +67,7 @@ func (h *HostManager) IsServiceExist(servicePath string) (bool, error) { } // IsServiceEnabled check if service exist and enabled -func (h *HostManager) IsServiceEnabled(servicePath string) (bool, error) { +func (h *hostManager) IsServiceEnabled(servicePath string) (bool, error) { exist, err := h.IsServiceExist(servicePath) if err != nil || !exist { return false, err @@ -86,7 +86,7 @@ func (h *HostManager) IsServiceEnabled(servicePath string) (bool, error) { } // ReadService read service from given path -func (h *HostManager) ReadService(servicePath string) (*Service, error) { +func (h *hostManager) ReadService(servicePath string) (*Service, error) { data, err := os.ReadFile(path.Join(consts.Chroot, servicePath)) if err != nil { return nil, err @@ -100,7 +100,7 @@ func (h *HostManager) ReadService(servicePath string) (*Service, error) { } // EnableService creates service file and enables it with systemctl enable -func (h *HostManager) EnableService(service *Service) error { +func (h *hostManager) EnableService(service *Service) error { // Write service file err := os.WriteFile(path.Join(consts.Chroot, service.Path), []byte(service.Content), 0644) if err != nil { @@ -120,7 +120,7 @@ func (h *HostManager) EnableService(service *Service) error { } // CompareServices compare 2 service and return true if serviceA has all the fields of serviceB -func (h *HostManager) CompareServices(serviceA, serviceB *Service) (bool, error) { +func (h *hostManager) CompareServices(serviceA, serviceB *Service) (bool, error) { optsA, err := unit.Deserialize(strings.NewReader(serviceA.Content)) if err != nil { return false, err @@ -145,7 +145,7 @@ OUTER: } // RemoveFromService removes given fields from service -func (h *HostManager) RemoveFromService(service *Service, options ...*unit.UnitOption) (*Service, error) { +func (h *hostManager) RemoveFromService(service *Service, options ...*unit.UnitOption) (*Service, error) { opts, err := unit.Deserialize(strings.NewReader(service.Content)) if err != nil { return nil, err @@ -175,37 +175,8 @@ OUTER: }, nil } -// AppendToService appends given fields to service -func AppendToService(service *Service, options ...*unit.UnitOption) (*Service, error) { - serviceOptions, err := unit.Deserialize(strings.NewReader(service.Content)) - if err != nil { - return nil, err - } - -OUTER: - for _, appendOpt := range options { - for _, opt := range serviceOptions { - if opt.Match(appendOpt) { - continue OUTER - } - } - serviceOptions = append(serviceOptions, appendOpt) - } - - data, err := io.ReadAll(unit.Serialize(serviceOptions)) - if err != nil { - return nil, err - } - - return &Service{ - Name: service.Name, - Path: service.Path, - Content: string(data), - }, nil -} - // ReadServiceInjectionManifestFile reads service injection file -func (h *HostManager) ReadServiceInjectionManifestFile(path string) (*Service, error) { +func (h *hostManager) ReadServiceInjectionManifestFile(path string) (*Service, error) { data, err := os.ReadFile(path) if err != nil { return nil, err @@ -224,7 +195,7 @@ func (h *HostManager) ReadServiceInjectionManifestFile(path string) (*Service, e } // ReadServiceManifestFile reads service file -func (h *HostManager) ReadServiceManifestFile(path string) (*Service, error) { +func (h *hostManager) ReadServiceManifestFile(path string) (*Service, error) { data, err := os.ReadFile(path) if err != nil { return nil, err @@ -243,7 +214,7 @@ func (h *HostManager) ReadServiceManifestFile(path string) (*Service, error) { } // ReadScriptManifestFile reads script file -func (h *HostManager) ReadScriptManifestFile(path string) (*ScriptManifestFile, error) { +func (h *hostManager) ReadScriptManifestFile(path string) (*ScriptManifestFile, error) { data, err := os.ReadFile(path) if err != nil { return nil, err diff --git a/pkg/host/sriov.go b/pkg/host/sriov.go index 3e5a3bac2f..c1d219d19a 100644 --- a/pkg/host/sriov.go +++ b/pkg/host/sriov.go @@ -23,7 +23,7 @@ import ( mlx "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vendors/mellanox" ) -func (h *HostManager) SetSriovNumVfs(pciAddr string, numVfs int) error { +func (h *hostManager) SetSriovNumVfs(pciAddr string, numVfs int) error { log.Log.V(2).Info("SetSriovNumVfs(): set NumVfs", "device", pciAddr, "numVfs", numVfs) numVfsFilePath := filepath.Join(vars.FilesystemRoot, consts.SysBusPciDevices, pciAddr, consts.NumVfsFile) bs := []byte(strconv.Itoa(numVfs)) @@ -40,7 +40,7 @@ func (h *HostManager) SetSriovNumVfs(pciAddr string, numVfs int) error { return nil } -func (h *HostManager) ResetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) error { +func (h *hostManager) ResetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) error { log.Log.V(2).Info("ResetSriovDevice(): reset SRIOV device", "address", ifaceStatus.PciAddress) if err := h.SetSriovNumVfs(ifaceStatus.PciAddress, 0); err != nil { return err @@ -65,7 +65,7 @@ func (h *HostManager) ResetSriovDevice(ifaceStatus sriovnetworkv1.InterfaceExt) return nil } -func (h *HostManager) GetVfInfo(pciAddr string, devices []*ghw.PCIDevice) sriovnetworkv1.VirtualFunction { +func (h *hostManager) GetVfInfo(pciAddr string, devices []*ghw.PCIDevice) sriovnetworkv1.VirtualFunction { driver, err := dputils.GetDriverName(pciAddr) if err != nil { log.Log.Error(err, "getVfInfo(): unable to parse device driver", "device", pciAddr) @@ -99,7 +99,7 @@ func (h *HostManager) GetVfInfo(pciAddr string, devices []*ghw.PCIDevice) sriovn return vf } -func (h *HostManager) SetVfGUID(vfAddr string, pfLink netlink.Link) error { +func (h *hostManager) SetVfGUID(vfAddr string, pfLink netlink.Link) error { log.Log.Info("SetVfGUID()", "vf", vfAddr) vfID, err := dputils.GetVFID(vfAddr) if err != nil { @@ -120,7 +120,7 @@ func (h *HostManager) SetVfGUID(vfAddr string, pfLink netlink.Link) error { return nil } -func (h *HostManager) VFIsReady(pciAddr string) (netlink.Link, error) { +func (h *hostManager) VFIsReady(pciAddr string) (netlink.Link, error) { log.Log.Info("VFIsReady()", "device", pciAddr) var err error var vfLink netlink.Link @@ -138,7 +138,7 @@ func (h *HostManager) VFIsReady(pciAddr string) (netlink.Link, error) { return vfLink, nil } -func (h *HostManager) SetVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error { +func (h *hostManager) SetVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error { log.Log.Info("SetVfAdminMac()", "vf", vfAddr) vfID, err := dputils.GetVFID(vfAddr) @@ -154,7 +154,7 @@ func (h *HostManager) SetVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) return nil } -func (h *HostManager) ConfigSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.InterfaceExt) error { +func (h *hostManager) ConfigSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.InterfaceExt) error { log.Log.V(2).Info("configSriovDevice(): configure sriov device", "device", iface.PciAddress, "config", iface) var err error @@ -307,7 +307,7 @@ func (h *HostManager) ConfigSriovDevice(iface *sriovnetworkv1.Interface, ifaceSt return nil } -func (h *HostManager) ConfigSriovInterfaces(storeManager StoreManagerInterface, interfaces []sriovnetworkv1.Interface, ifaceStatuses []sriovnetworkv1.InterfaceExt, pfsToConfig map[string]bool) error { +func (h *hostManager) ConfigSriovInterfaces(storeManager StoreManagerInterface, interfaces []sriovnetworkv1.Interface, ifaceStatuses []sriovnetworkv1.InterfaceExt, pfsToConfig map[string]bool) error { if h.IsKernelLockdownMode() && mlx.HasMellanoxInterfacesInSpec(ifaceStatuses, interfaces) { log.Log.Error(nil, "cannot use mellanox devices when in kernel lockdown mode") return fmt.Errorf("cannot use mellanox devices when in kernel lockdown mode") @@ -396,7 +396,7 @@ func (h *HostManager) ConfigSriovInterfaces(storeManager StoreManagerInterface, return nil } -func (h *HostManager) ConfigSriovDeviceVirtual(iface *sriovnetworkv1.Interface) error { +func (h *hostManager) ConfigSriovDeviceVirtual(iface *sriovnetworkv1.Interface) error { log.Log.V(2).Info("ConfigSriovDeviceVirtual(): config interface", "address", iface.PciAddress, "config", iface) // Config VFs if iface.NumVfs > 0 { diff --git a/pkg/host/store.go b/pkg/host/store.go index 17eea9e564..1bced8c9dc 100644 --- a/pkg/host/store.go +++ b/pkg/host/store.go @@ -26,7 +26,7 @@ type StoreManagerInterface interface { WriteCheckpointFile(*sriovnetworkv1.SriovNetworkNodeState) error } -type StoreManager struct { +type storeManager struct { } // NewStoreManager: create the initial folders needed to store the info about the PF @@ -36,7 +36,7 @@ func NewStoreManager() (StoreManagerInterface, error) { return nil, err } - return &StoreManager{}, nil + return &storeManager{}, nil } // createOperatorConfigFolderIfNeeded: create the operator base folder on the host @@ -73,7 +73,7 @@ func createOperatorConfigFolderIfNeeded() error { } // ClearPCIAddressFolder: removes all the PFs storage information -func (s *StoreManager) ClearPCIAddressFolder() error { +func (s *storeManager) ClearPCIAddressFolder() error { hostExtension := utils.GetHostExtension() PfAppliedConfigUse := filepath.Join(hostExtension, consts.PfAppliedConfig) _, err := os.Stat(PfAppliedConfigUse) @@ -99,7 +99,7 @@ func (s *StoreManager) ClearPCIAddressFolder() error { // SaveLastPfAppliedStatus will save the PF object as a json into the /etc/sriov-operator/pci/ // this function must be called after running the chroot function -func (s *StoreManager) SaveLastPfAppliedStatus(PfInfo *sriovnetworkv1.Interface) error { +func (s *storeManager) SaveLastPfAppliedStatus(PfInfo *sriovnetworkv1.Interface) error { data, err := json.Marshal(PfInfo) if err != nil { log.Log.Error(err, "failed to marshal PF status", "status", *PfInfo) @@ -114,7 +114,7 @@ func (s *StoreManager) SaveLastPfAppliedStatus(PfInfo *sriovnetworkv1.Interface) // LoadPfsStatus convert the /etc/sriov-operator/pci/ json to pfstatus // returns false if the file doesn't exist. -func (s *StoreManager) LoadPfsStatus(pciAddress string) (*sriovnetworkv1.Interface, bool, error) { +func (s *storeManager) LoadPfsStatus(pciAddress string) (*sriovnetworkv1.Interface, bool, error) { hostExtension := utils.GetHostExtension() pathFile := filepath.Join(hostExtension, consts.PfAppliedConfig, pciAddress) pfStatus := &sriovnetworkv1.Interface{} @@ -136,7 +136,7 @@ func (s *StoreManager) LoadPfsStatus(pciAddress string) (*sriovnetworkv1.Interfa return pfStatus, true, nil } -func (s *StoreManager) GetCheckPointNodeState() (*sriovnetworkv1.SriovNetworkNodeState, error) { +func (s *storeManager) GetCheckPointNodeState() (*sriovnetworkv1.SriovNetworkNodeState, error) { log.Log.Info("getCheckPointNodeState()") configdir := filepath.Join(vars.Destdir, consts.CheckpointFileName) file, err := os.OpenFile(configdir, os.O_RDONLY, 0644) @@ -154,7 +154,7 @@ func (s *StoreManager) GetCheckPointNodeState() (*sriovnetworkv1.SriovNetworkNod return &sriovnetworkv1.InitialState, nil } -func (s *StoreManager) WriteCheckpointFile(ns *sriovnetworkv1.SriovNetworkNodeState) error { +func (s *storeManager) WriteCheckpointFile(ns *sriovnetworkv1.SriovNetworkNodeState) error { configdir := filepath.Join(vars.Destdir, consts.CheckpointFileName) file, err := os.OpenFile(configdir, os.O_RDWR|os.O_CREATE, 0644) if err != nil { diff --git a/pkg/host/udev.go b/pkg/host/udev.go index 984c128a47..1271b117d4 100644 --- a/pkg/host/udev.go +++ b/pkg/host/udev.go @@ -20,7 +20,7 @@ type config struct { Interfaces []sriovnetworkv1.Interface `json:"interfaces"` } -func (h *HostManager) PrepareNMUdevRule(supportedVfIds []string) error { +func (h *hostManager) PrepareNMUdevRule(supportedVfIds []string) error { log.Log.V(2).Info("PrepareNMUdevRule()") filePath := filepath.Join(vars.FilesystemRoot, consts.HostUdevRulesFolder, "10-nm-unmanaged.rules") @@ -46,7 +46,7 @@ func (h *HostManager) PrepareNMUdevRule(supportedVfIds []string) error { return nil } -func (h *HostManager) WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, pfsToSkip map[string]bool) (update bool, err error) { +func (h *hostManager) WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, pfsToSkip map[string]bool) (update bool, err error) { cfg := config{} for _, iface := range newState.Spec.Interfaces { for _, ifaceStatus := range newState.Status.Interfaces { @@ -138,7 +138,7 @@ func (h *HostManager) WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetwo return } -func (h *HostManager) AddUdevRule(pfPciAddress string) error { +func (h *hostManager) AddUdevRule(pfPciAddress string) error { log.Log.V(2).Info("AddUdevRule()", "device", pfPciAddress) pathFile := filepath.Join(vars.FilesystemRoot, consts.UdevRulesFolder) udevRuleContent := fmt.Sprintf(consts.NMUdevRule, strings.Join(vars.SupportedVfIds, "|"), pfPciAddress) @@ -160,7 +160,7 @@ func (h *HostManager) AddUdevRule(pfPciAddress string) error { return nil } -func (h *HostManager) RemoveUdevRule(pfPciAddress string) error { +func (h *hostManager) RemoveUdevRule(pfPciAddress string) error { pathFile := filepath.Join(vars.FilesystemRoot, consts.UdevRulesFolder) filePath := path.Join(pathFile, fmt.Sprintf("10-nm-disable-%s.rules", pfPciAddress)) err := os.Remove(filePath) diff --git a/pkg/plugins/k8s/k8s_plugin.go b/pkg/plugins/k8s/k8s_plugin.go index bf52b0e3ce..304212a3c6 100644 --- a/pkg/plugins/k8s/k8s_plugin.go +++ b/pkg/plugins/k8s/k8s_plugin.go @@ -2,6 +2,7 @@ package k8s import ( "fmt" + "io" "os" "path" "strings" @@ -493,10 +494,39 @@ func (p *K8sPlugin) updateSystemService(serviceObj *host.Service) error { if err != nil { return err } - updatedService, err := host.AppendToService(systemService, serviceOptions...) + updatedService, err := appendToService(systemService, serviceOptions...) if err != nil { return err } return p.hostHelper.EnableService(updatedService) } + +// appendToService appends given fields to service +func appendToService(service *host.Service, options ...*unit.UnitOption) (*host.Service, error) { + serviceOptions, err := unit.Deserialize(strings.NewReader(service.Content)) + if err != nil { + return nil, err + } + +OUTER: + for _, appendOpt := range options { + for _, opt := range serviceOptions { + if opt.Match(appendOpt) { + continue OUTER + } + } + serviceOptions = append(serviceOptions, appendOpt) + } + + data, err := io.ReadAll(unit.Serialize(serviceOptions)) + if err != nil { + return nil, err + } + + return &host.Service{ + Name: service.Name, + Path: service.Path, + Content: string(data), + }, nil +} diff --git a/pkg/plugins/plugin.go b/pkg/plugins/plugin.go index a64cb8ddb9..b9871720e9 100644 --- a/pkg/plugins/plugin.go +++ b/pkg/plugins/plugin.go @@ -22,21 +22,21 @@ type VendorPlugin interface { } type HostHelpersInterface interface { - utils.UtilsInterface + utils.CmdInterface host.HostManagerInterface host.StoreManagerInterface mlx.MellanoxInterface } type HostHelpers struct { - utils.UtilsInterface + utils.CmdInterface host.HostManagerInterface host.StoreManagerInterface mlx.MellanoxInterface } // Use for unit tests -func NewVendorPluginHelpers(utilsHelper utils.UtilsInterface, +func NewVendorPluginHelpers(utilsHelper utils.CmdInterface, hostManager host.HostManagerInterface, storeManager host.StoreManagerInterface, mlxHelper mlx.MellanoxInterface) *HostHelpers { @@ -44,7 +44,7 @@ func NewVendorPluginHelpers(utilsHelper utils.UtilsInterface, } func NewDefaultVendorPluginHelpers() (*HostHelpers, error) { - utilsHelper := utils.NewUtilsHelper() + utilsHelper := utils.New() mlxHelper := mlx.New(utilsHelper) hostManager := host.NewHostManager(utilsHelper) storeManager, err := host.NewStoreManager() diff --git a/pkg/utils/mock/mock_utils.go b/pkg/utils/mock/mock_utils.go index 1c4d7d5236..636c7f4a18 100644 --- a/pkg/utils/mock/mock_utils.go +++ b/pkg/utils/mock/mock_utils.go @@ -10,31 +10,31 @@ import ( gomock "github.com/golang/mock/gomock" ) -// MockUtilsInterface is a mock of UtilsInterface interface. -type MockUtilsInterface struct { +// MockCmdInterface is a mock of CmdInterface interface. +type MockCmdInterface struct { ctrl *gomock.Controller - recorder *MockUtilsInterfaceMockRecorder + recorder *MockCmdInterfaceMockRecorder } -// MockUtilsInterfaceMockRecorder is the mock recorder for MockUtilsInterface. -type MockUtilsInterfaceMockRecorder struct { - mock *MockUtilsInterface +// MockCmdInterfaceMockRecorder is the mock recorder for MockCmdInterface. +type MockCmdInterfaceMockRecorder struct { + mock *MockCmdInterface } -// NewMockUtilsInterface creates a new mock instance. -func NewMockUtilsInterface(ctrl *gomock.Controller) *MockUtilsInterface { - mock := &MockUtilsInterface{ctrl: ctrl} - mock.recorder = &MockUtilsInterfaceMockRecorder{mock} +// NewMockCmdInterface creates a new mock instance. +func NewMockCmdInterface(ctrl *gomock.Controller) *MockCmdInterface { + mock := &MockCmdInterface{ctrl: ctrl} + mock.recorder = &MockCmdInterfaceMockRecorder{mock} return mock } // EXPECT returns an object that allows the caller to indicate expected use. -func (m *MockUtilsInterface) EXPECT() *MockUtilsInterfaceMockRecorder { +func (m *MockCmdInterface) EXPECT() *MockCmdInterfaceMockRecorder { return m.recorder } // Chroot mocks base method. -func (m *MockUtilsInterface) Chroot(arg0 string) (func() error, error) { +func (m *MockCmdInterface) Chroot(arg0 string) (func() error, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Chroot", arg0) ret0, _ := ret[0].(func() error) @@ -43,13 +43,13 @@ func (m *MockUtilsInterface) Chroot(arg0 string) (func() error, error) { } // Chroot indicates an expected call of Chroot. -func (mr *MockUtilsInterfaceMockRecorder) Chroot(arg0 interface{}) *gomock.Call { +func (mr *MockCmdInterfaceMockRecorder) Chroot(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Chroot", reflect.TypeOf((*MockUtilsInterface)(nil).Chroot), arg0) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Chroot", reflect.TypeOf((*MockCmdInterface)(nil).Chroot), arg0) } // RunCommand mocks base method. -func (m *MockUtilsInterface) RunCommand(arg0 string, arg1 ...string) (string, string, error) { +func (m *MockCmdInterface) RunCommand(arg0 string, arg1 ...string) (string, string, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0} for _, a := range arg1 { @@ -63,8 +63,8 @@ func (m *MockUtilsInterface) RunCommand(arg0 string, arg1 ...string) (string, st } // RunCommand indicates an expected call of RunCommand. -func (mr *MockUtilsInterfaceMockRecorder) RunCommand(arg0 interface{}, arg1 ...interface{}) *gomock.Call { +func (mr *MockCmdInterfaceMockRecorder) RunCommand(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0}, arg1...) - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RunCommand", reflect.TypeOf((*MockUtilsInterface)(nil).RunCommand), varargs...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RunCommand", reflect.TypeOf((*MockCmdInterface)(nil).RunCommand), varargs...) } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 246bc885a7..3e78cf1c69 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -20,19 +20,19 @@ import ( ) //go:generate ../../bin/mockgen -destination mock/mock_utils.go -source utils.go -type UtilsInterface interface { +type CmdInterface interface { Chroot(string) (func() error, error) RunCommand(string, ...string) (string, string, error) } -type UtilsHelper struct { +type utilsHelper struct { } -func NewUtilsHelper() UtilsInterface { - return &UtilsHelper{} +func New() CmdInterface { + return &utilsHelper{} } -func (u *UtilsHelper) Chroot(path string) (func() error, error) { +func (u *utilsHelper) Chroot(path string) (func() error, error) { root, err := os.Open("/") if err != nil { return nil, err @@ -55,7 +55,7 @@ func (u *UtilsHelper) Chroot(path string) (func() error, error) { } // RunCommand runs a command -func (u *UtilsHelper) RunCommand(command string, args ...string) (string, string, error) { +func (u *utilsHelper) RunCommand(command string, args ...string) (string, string, error) { log.Log.Info("RunCommand()", "command", command, "args", args) var stdout, stderr bytes.Buffer diff --git a/pkg/vendors/mellanox/mellanox.go b/pkg/vendors/mellanox/mellanox.go index e5723518a6..c6631ed289 100644 --- a/pkg/vendors/mellanox/mellanox.go +++ b/pkg/vendors/mellanox/mellanox.go @@ -63,10 +63,10 @@ type MellanoxInterface interface { } type mellanoxHelper struct { - utils utils.UtilsInterface + utils utils.CmdInterface } -func New(utilsHelper utils.UtilsInterface) MellanoxInterface { +func New(utilsHelper utils.CmdInterface) MellanoxInterface { return &mellanoxHelper{ utils: utilsHelper, }