From ae7e5e9ef21522508fcab1e1e4fc412d7c55365a Mon Sep 17 00:00:00 2001 From: Sebastian Sch Date: Sun, 26 Nov 2023 00:30:54 +0200 Subject: [PATCH] update all the needed packages with the new global imports and interfaces Signed-off-by: Sebastian Sch --- .gitignore | 3 +- api/v1/helper.go | 78 +++ api/v1/helper_test.go | 2 +- cmd/sriov-network-config-daemon/service.go | 92 +-- cmd/sriov-network-config-daemon/start.go | 107 +-- controllers/helper.go | 2 +- .../sriovnetworknodepolicy_controller.go | 2 +- .../sriovnetworknodepolicy_controller_test.go | 2 +- .../sriovnetworkpoolconfig_controller.go | 17 +- .../sriovnetworkpoolconfig_controller_test.go | 2 +- controllers/sriovoperatorconfig_controller.go | 38 +- controllers/suite_test.go | 36 +- main.go | 33 +- pkg/apply/merge.go | 2 +- pkg/daemon/daemon.go | 214 +++--- pkg/daemon/daemon_test.go | 625 +++++++++--------- pkg/daemon/event_recorder.go | 9 +- pkg/daemon/plugin.go | 24 +- pkg/daemon/writer.go | 101 ++- pkg/systemd/systemd.go | 21 +- pkg/webhook/mutate.go | 2 +- pkg/webhook/validate.go | 22 +- pkg/webhook/validate_test.go | 2 +- test/conformance/tests/test_sriov_operator.go | 2 +- 24 files changed, 775 insertions(+), 663 deletions(-) diff --git a/.gitignore b/.gitignore index e63284190e..6761d7ea12 100644 --- a/.gitignore +++ b/.gitignore @@ -82,4 +82,5 @@ tags # End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode #IDE (GoLand) specific .idea/ - +# test-environment files +registry-login.conf diff --git a/api/v1/helper.go b/api/v1/helper.go index c2725f7c2a..fb7e9614b5 100644 --- a/api/v1/helper.go +++ b/api/v1/helper.go @@ -20,6 +20,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" ) @@ -47,6 +49,8 @@ var log = logf.Log.WithName("sriovnetwork") // Vendor ID, Physical Function Device ID, Virtual Function Device ID var NicIDMap = []string{} +var InitialState SriovNetworkNodeState + // NetFilterType Represents the NetFilter tags to be used type NetFilterType int @@ -211,6 +215,80 @@ func GetVfDeviceID(deviceID string) string { return "" } +func IsSwitchdevModeSpec(spec SriovNetworkNodeStateSpec) bool { + for _, iface := range spec.Interfaces { + if iface.EswitchMode == ESwithModeSwitchDev { + return true + } + } + return false +} + +func FindInterface(interfaces Interfaces, name string) (iface Interface, err error) { + for _, i := range interfaces { + if i.Name == name { + return i, nil + } + } + return Interface{}, fmt.Errorf("unable to find interface: %v", name) +} + +func NeedToUpdateSriov(iface *Interface, ifaceStatus *InterfaceExt) bool { + if iface.Mtu > 0 { + mtu := iface.Mtu + if mtu != ifaceStatus.Mtu { + log.V(2).Info("NeedUpdate(): MTU needs update", "desired", mtu, "current", ifaceStatus.Mtu) + return true + } + } + + if iface.NumVfs != ifaceStatus.NumVfs { + log.V(2).Info("NeedUpdate(): NumVfs needs update", "desired", iface.NumVfs, "current", ifaceStatus.NumVfs) + return true + } + if iface.NumVfs > 0 { + for _, vf := range ifaceStatus.VFs { + ingroup := false + for _, group := range iface.VfGroups { + if IndexInRange(vf.VfID, group.VfRange) { + ingroup = true + if group.DeviceType != consts.DeviceTypeNetDevice { + if group.DeviceType != vf.Driver { + log.V(2).Info("NeedUpdate(): Driver needs update", + "desired", group.DeviceType, "current", vf.Driver) + return true + } + } else { + if StringInArray(vf.Driver, vars.DpdkDrivers) { + log.V(2).Info("NeedUpdate(): Driver needs update", + "desired", group.DeviceType, "current", vf.Driver) + return true + } + if vf.Mtu != 0 && group.Mtu != 0 && vf.Mtu != group.Mtu { + log.V(2).Info("NeedUpdate(): VF MTU needs update", + "vf", vf.VfID, "desired", group.Mtu, "current", vf.Mtu) + return true + } + + // this is needed to be sure the admin mac address is configured as expected + if iface.ExternallyManaged { + log.V(2).Info("NeedUpdate(): need to update the device as it's externally manage", + "device", ifaceStatus.PciAddress) + return true + } + } + break + } + } + if !ingroup && StringInArray(vf.Driver, vars.DpdkDrivers) { + // VF which has DPDK driver loaded but not in any group, needs to be reset to default driver. + return true + } + } + } + return false +} + type ByPriority []SriovNetworkNodePolicy func (a ByPriority) Len() int { diff --git a/api/v1/helper_test.go b/api/v1/helper_test.go index 674c64d2c6..2a1b6f527f 100644 --- a/api/v1/helper_test.go +++ b/api/v1/helper_test.go @@ -13,7 +13,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" ) var update = flag.Bool("updategolden", false, "update .golden files") diff --git a/cmd/sriov-network-config-daemon/service.go b/cmd/sriov-network-config-daemon/service.go index fc9cfc6d94..1ac6d7fcae 100644 --- a/cmd/sriov-network-config-daemon/service.go +++ b/cmd/sriov-network-config-daemon/service.go @@ -24,15 +24,15 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" + snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version" - - snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" ) var ( @@ -57,11 +57,16 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { setupLog.V(2).Info("sriov-config-service", "version", version.Version) setupLog.V(0).Info("Starting sriov-config-service") + + // Mark that we are running on host + vars.UsingSystemdMode = true + vars.InChroot = true + supportedNicIds, err := systemd.ReadSriovSupportedNics() if err != nil { setupLog.Error(err, "failed to read list of supported nic ids") sriovResult := &systemd.SriovResult{ - SyncStatus: "Failed", + SyncStatus: consts.SyncStatusFailed, LastSyncError: fmt.Sprintf("failed to read list of supported nic ids: %v", err), } err = systemd.WriteSriovResult(sriovResult) @@ -78,7 +83,7 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { if _, err := os.Stat(systemd.SriovSystemdConfigPath); !errors.Is(err, os.ErrNotExist) { setupLog.Error(err, "failed to read the sriov configuration file", "path", systemd.SriovSystemdConfigPath) sriovResult := &systemd.SriovResult{ - SyncStatus: "Failed", + SyncStatus: consts.SyncStatusFailed, LastSyncError: fmt.Sprintf("failed to read the sriov configuration file in path %s: %v", systemd.SriovSystemdConfigPath, err), } err = systemd.WriteSriovResult(sriovResult) @@ -91,67 +96,64 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { nodeStateSpec = &systemd.SriovConfig{ Spec: sriovv1.SriovNetworkNodeStateSpec{}, UnsupportedNics: false, - PlatformType: utils.Baremetal, + PlatformType: consts.Baremetal, } } setupLog.V(2).Info("sriov-config-service", "config", nodeStateSpec) - - storeManager, err := utils.NewStoreManager(true) + vendorHelpers, err := plugin.NewDefaultVendorPluginHelpers() if err != nil { - setupLog.Error(err, "failed to create store manager") - return err + setupLog.Error(err, "failed to create vendorHelpers") + return updateSriovResultErr("failed to create vendorHelpers") } - // Load kernel modules - hostManager := host.NewHostManager(true) - _, err = hostManager.TryEnableRdma() + platformHelper, err := platforms.NewDefaultPlatformHelper() + if err != nil { + setupLog.Error(err, "failed to create platformHelpers") + return updateSriovResultErr("failed to create platformHelpers") + } + + _, err = vendorHelpers.TryEnableRdma() if err != nil { setupLog.Error(err, "warning, failed to enable RDMA") } - hostManager.TryEnableTun() - hostManager.TryEnableVhostNet() + vendorHelpers.TryEnableTun() + vendorHelpers.TryEnableVhostNet() var configPlugin plugin.VendorPlugin var ifaceStatuses []sriovv1.InterfaceExt - if nodeStateSpec.PlatformType == utils.Baremetal { + if nodeStateSpec.PlatformType == consts.Baremetal { // Bare metal support - ifaceStatuses, err = utils.DiscoverSriovDevices(nodeStateSpec.UnsupportedNics, storeManager) + vars.DevMode = nodeStateSpec.UnsupportedNics + ifaceStatuses, err = vendorHelpers.DiscoverSriovDevices(vendorHelpers.StoreManagerInterface) if err != nil { setupLog.Error(err, "failed to discover sriov devices on the host") - return fmt.Errorf("sriov-config-service: failed to discover sriov devices on the host: %v", err) + return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to discover sriov devices on the host: %v", err)) } // Create the generic plugin - configPlugin, err = generic.NewGenericPlugin(true, hostManager, storeManager) + configPlugin, err = generic.NewGenericPlugin(vendorHelpers) if err != nil { setupLog.Error(err, "failed to create generic plugin") - return fmt.Errorf("sriov-config-service failed to create generic plugin %v", err) - } - } else if nodeStateSpec.PlatformType == utils.VirtualOpenStack { - // Openstack support - metaData, networkData, err := utils.GetOpenstackData(false) - if err != nil { - setupLog.Error(err, "failed to read OpenStack data") - return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err) + return updateSriovResultErr(fmt.Sprintf("sriov-config-service failed to create generic plugin %v", err)) } - - openStackDevicesInfo, err := utils.CreateOpenstackDevicesInfo(metaData, networkData) + } else if nodeStateSpec.PlatformType == consts.VirtualOpenStack { + err = platformHelper.CreateOpenstackDevicesInfo() if err != nil { setupLog.Error(err, "failed to read OpenStack data") - return fmt.Errorf("sriov-config-service failed to read OpenStack data: %v", err) + return updateSriovResultErr(fmt.Sprintf("sriov-config-service failed to read OpenStack data: %v", err)) } - ifaceStatuses, err = utils.DiscoverSriovDevicesVirtual(openStackDevicesInfo) + ifaceStatuses, err = platformHelper.DiscoverSriovDevicesVirtual() if err != nil { setupLog.Error(err, "failed to read OpenStack data") - return fmt.Errorf("sriov-config-service: failed to read OpenStack data: %v", err) + return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to read OpenStack data: %v", err)) } // Create the virtual plugin - configPlugin, err = virtual.NewVirtualPlugin(true) + configPlugin, err = virtual.NewVirtualPlugin(vendorHelpers) if err != nil { setupLog.Error(err, "failed to create virtual plugin") - return fmt.Errorf("sriov-config-service: failed to create virtual plugin %v", err) + return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to create virtual plugin %v", err)) } } @@ -163,18 +165,18 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { _, _, err = configPlugin.OnNodeStateChange(nodeState) if err != nil { setupLog.Error(err, "failed to run OnNodeStateChange to update the generic plugin status") - return fmt.Errorf("sriov-config-service: failed to run OnNodeStateChange to update the generic plugin status %v", err) + return updateSriovResultErr(fmt.Sprintf("sriov-config-service: failed to run OnNodeStateChange to update the generic plugin status %v", err)) } sriovResult := &systemd.SriovResult{ - SyncStatus: "Succeeded", + SyncStatus: consts.SyncStatusSucceeded, LastSyncError: "", } err = configPlugin.Apply() if err != nil { setupLog.Error(err, "failed to run apply node configuration") - sriovResult.SyncStatus = "Failed" + sriovResult.SyncStatus = consts.SyncStatusFailed sriovResult.LastSyncError = err.Error() } @@ -187,3 +189,17 @@ func runServiceCmd(cmd *cobra.Command, args []string) error { setupLog.V(0).Info("shutting down sriov-config-service") return nil } + +func updateSriovResultErr(errMsg string) error { + sriovResult := &systemd.SriovResult{ + SyncStatus: consts.SyncStatusFailed, + LastSyncError: errMsg, + } + + err := systemd.WriteSriovResult(sriovResult) + if err != nil { + log.Log.Error(err, "failed to write sriov result file", "content", *sriovResult) + return fmt.Errorf("sriov-config-service failed to write sriov result file with content %v error: %v", *sriovResult, err) + } + return nil +} diff --git a/cmd/sriov-network-config-daemon/start.go b/cmd/sriov-network-config-daemon/start.go index a7feffbde9..c0dc132547 100644 --- a/cmd/sriov-network-config-daemon/start.go +++ b/cmd/sriov-network-config-daemon/start.go @@ -24,8 +24,6 @@ import ( "strings" "time" - configv1 "github.com/openshift/api/config/v1" - mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" "github.com/spf13/cobra" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -35,12 +33,17 @@ import ( "k8s.io/client-go/util/connrotation" "sigs.k8s.io/controller-runtime/pkg/log" + configv1 "github.com/openshift/api/config/v1" + mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" + sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/daemon" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/version" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" + plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" ) var ( @@ -70,8 +73,14 @@ func runStartCmd(cmd *cobra.Command, args []string) error { snolog.InitLog() setupLog := log.Log.WithName("sriov-network-config-daemon") + // Mark that we are running inside a container + vars.UsingSystemdMode = false + if startOpts.systemd { + vars.UsingSystemdMode = true + } // To help debugging, immediately log version - setupLog.V(2).Info("sriov-network-config-daemon", "version", version.Version) + // TODO: we don't need this anymore as we don't update it + //setupLog.V(2).Info("sriov-network-config-daemon", "version", version.Version) if startOpts.nodeName == "" { name, ok := os.LookupEnv("NODE_NAME") @@ -80,6 +89,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error { } startOpts.nodeName = name } + vars.NodeName = startOpts.nodeName // This channel is used to ensure all spawned goroutines exit when we exit. stopCh := make(chan struct{}) @@ -102,7 +112,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error { var config *rest.Config var err error - if os.Getenv("CLUSTER_TYPE") == utils.ClusterTypeOpenshift { + if vars.ClusterType == consts.ClusterTypeOpenshift { kubeconfig, err := clientcmd.LoadFromFile("/host/etc/kubernetes/kubeconfig") if err != nil { setupLog.Error(err, "failed to load kubelet kubeconfig") @@ -110,7 +120,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error { clusterName := kubeconfig.Contexts[kubeconfig.CurrentContext].Cluster apiURL := kubeconfig.Clusters[clusterName].Server - url, err := url.Parse(apiURL) + urlPath, err := url.Parse(apiURL) if err != nil { setupLog.Error(err, "failed to parse api url from kubelet kubeconfig") } @@ -118,8 +128,14 @@ func runStartCmd(cmd *cobra.Command, args []string) error { // The kubernetes in-cluster functions don't let you override the apiserver // directly; gotta "pass" it via environment vars. setupLog.V(0).Info("overriding kubernetes api", "new-url", apiURL) - os.Setenv("KUBERNETES_SERVICE_HOST", url.Hostname()) - os.Setenv("KUBERNETES_SERVICE_PORT", url.Port()) + err = os.Setenv("KUBERNETES_SERVICE_HOST", urlPath.Hostname()) + if err != nil { + setupLog.Error(err, "failed to set KUBERNETES_SERVICE_HOST environment variable") + } + err = os.Setenv("KUBERNETES_SERVICE_PORT", urlPath.Port()) + if err != nil { + setupLog.Error(err, "failed to set KUBERNETES_SERVICE_PORT environment variable") + } } kubeconfig := os.Getenv("KUBECONFIG") @@ -134,57 +150,73 @@ func runStartCmd(cmd *cobra.Command, args []string) error { return err } + vars.Config = config + vars.Scheme = scheme.Scheme + closeAllConns, err := updateDialer(config) if err != nil { return err } - sriovnetworkv1.AddToScheme(scheme.Scheme) - mcfgv1.AddToScheme(scheme.Scheme) - configv1.Install(scheme.Scheme) + err = sriovnetworkv1.AddToScheme(scheme.Scheme) + if err != nil { + setupLog.Error(err, "failed to load sriov network CRDs to scheme") + return err + } + + err = mcfgv1.AddToScheme(scheme.Scheme) + if err != nil { + setupLog.Error(err, "failed to load machine config CRDs to scheme") + return err + } + + err = configv1.Install(scheme.Scheme) + if err != nil { + setupLog.Error(err, "failed to load openshift config CRDs to scheme") + return err + } snclient := snclientset.NewForConfigOrDie(config) kubeclient := kubernetes.NewForConfigOrDie(config) - openshiftContext, err := utils.NewOpenshiftContext(config, scheme.Scheme) + + vendorHelpers, err := plugin.NewDefaultVendorPluginHelpers() + if err != nil { + setupLog.Error(err, "failed to create vendorHelpers") + return err + } + + platformHelper, err := platforms.NewDefaultPlatformHelper() if err != nil { + setupLog.Error(err, "failed to create platformHelper") return err } config.Timeout = 5 * time.Second writerclient := snclientset.NewForConfigOrDie(config) - mode := os.Getenv("DEV_MODE") - devMode := false - if mode == "TRUE" { - devMode = true - setupLog.V(0).Info("dev mode enabled") - } - - eventRecorder := daemon.NewEventRecorder(writerclient, startOpts.nodeName, kubeclient) + eventRecorder := daemon.NewEventRecorder(writerclient, kubeclient) defer eventRecorder.Shutdown() setupLog.V(0).Info("starting node writer") - nodeWriter := daemon.NewNodeStateStatusWriter(writerclient, startOpts.nodeName, closeAllConns, eventRecorder, devMode) - - destdir := os.Getenv("DEST_DIR") - if destdir == "" { - destdir = "/host/tmp" - } - - platformType := utils.Baremetal + nodeWriter := daemon.NewNodeStateStatusWriter(writerclient, + closeAllConns, + eventRecorder, + vendorHelpers.StoreManagerInterface, + vendorHelpers.HostManagerInterface, + platformHelper.OpenstackInterface) nodeInfo, err := kubeclient.CoreV1().Nodes().Get(context.Background(), startOpts.nodeName, v1.GetOptions{}) if err == nil { - for key, pType := range utils.PlatformMap { + for key, pType := range vars.PlatformsMap { if strings.Contains(strings.ToLower(nodeInfo.Spec.ProviderID), strings.ToLower(key)) { - platformType = pType + vars.PlatformType = pType } } } else { setupLog.Error(err, "failed to fetch node state, exiting", "node-name", startOpts.nodeName) return err } - setupLog.Info("Running on", "platform", platformType.String()) + setupLog.Info("Running on", "platform", vars.PlatformType.String()) var namespace = os.Getenv("NAMESPACE") if err := sriovnetworkv1.InitNicIDMapFromConfigMap(kubeclient, namespace); err != nil { @@ -195,27 +227,24 @@ func runStartCmd(cmd *cobra.Command, args []string) error { eventRecorder.SendEvent("ConfigDaemonStart", "Config Daemon starting") // block the deamon process until nodeWriter finish first its run - err = nodeWriter.RunOnce(destdir, platformType) + err = nodeWriter.RunOnce() if err != nil { setupLog.Error(err, "failed to run writer") return err } - go nodeWriter.Run(stopCh, refreshCh, syncCh, platformType) + go nodeWriter.Run(stopCh, refreshCh, syncCh) setupLog.V(0).Info("Starting SriovNetworkConfigDaemon") err = daemon.New( - startOpts.nodeName, snclient, kubeclient, - openshiftContext, + vendorHelpers, + platformHelper, exitCh, stopCh, syncCh, refreshCh, - platformType, - startOpts.systemd, eventRecorder, - devMode, ).Run(stopCh, exitCh) if err != nil { setupLog.Error(err, "failed to run daemon") diff --git a/controllers/helper.go b/controllers/helper.go index 1da3e626f6..2fa620f674 100644 --- a/controllers/helper.go +++ b/controllers/helper.go @@ -22,7 +22,7 @@ import ( "os" "strings" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" ) var webhooks = map[string](string){ diff --git a/controllers/sriovnetworknodepolicy_controller.go b/controllers/sriovnetworknodepolicy_controller.go index 3ba1e41449..f784badeb6 100644 --- a/controllers/sriovnetworknodepolicy_controller.go +++ b/controllers/sriovnetworknodepolicy_controller.go @@ -51,7 +51,7 @@ import ( sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/apply" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" ) diff --git a/controllers/sriovnetworknodepolicy_controller_test.go b/controllers/sriovnetworknodepolicy_controller_test.go index bd4579d789..e3dc044634 100644 --- a/controllers/sriovnetworknodepolicy_controller_test.go +++ b/controllers/sriovnetworknodepolicy_controller_test.go @@ -17,7 +17,7 @@ import ( sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" v1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" ) func TestNodeSelectorMerge(t *testing.T) { diff --git a/controllers/sriovnetworkpoolconfig_controller.go b/controllers/sriovnetworkpoolconfig_controller.go index de8fb32b2e..1084f75fbf 100644 --- a/controllers/sriovnetworkpoolconfig_controller.go +++ b/controllers/sriovnetworkpoolconfig_controller.go @@ -16,16 +16,17 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) // SriovNetworkPoolConfigReconciler reconciles a SriovNetworkPoolConfig object type SriovNetworkPoolConfigReconciler struct { client.Client - Scheme *runtime.Scheme - OpenshiftContext *utils.OpenshiftContext + Scheme *runtime.Scheme + PlatformHelper platforms.PlatformHelperInterface } //+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovnetworkpoolconfigs,verbs=get;list;watch;create;update;patch;delete @@ -44,8 +45,8 @@ type SriovNetworkPoolConfigReconciler struct { func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { logger := log.FromContext(ctx).WithValues("sriovnetworkpoolconfig", req.NamespacedName) isHypershift := false - if r.OpenshiftContext.IsOpenshiftCluster() { - if r.OpenshiftContext.IsHypershift() { + if r.PlatformHelper.IsOpenshiftCluster() { + if r.PlatformHelper.IsHypershift() { isHypershift = true } logger = logger.WithValues("isHypershift", isHypershift) @@ -78,7 +79,7 @@ func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ct return reconcile.Result{}, err } } - if utils.ClusterType == utils.ClusterTypeOpenshift { + if vars.ClusterType == constants.ClusterTypeOpenshift { if !isHypershift { if err = r.syncOvsHardwareOffloadMachineConfigs(ctx, instance, false); err != nil { return reconcile.Result{}, err @@ -92,7 +93,7 @@ func (r *SriovNetworkPoolConfigReconciler) Reconcile(ctx context.Context, req ct if sriovnetworkv1.StringInArray(sriovnetworkv1.POOLCONFIGFINALIZERNAME, instance.ObjectMeta.Finalizers) { // our finalizer is present, so lets handle any external dependency logger.Info("delete SriovNetworkPoolConfig CR", "Namespace", instance.Namespace, "Name", instance.Name) - if utils.ClusterType == utils.ClusterTypeOpenshift && !isHypershift { + if vars.ClusterType == constants.ClusterTypeOpenshift && !isHypershift { if err = r.syncOvsHardwareOffloadMachineConfigs(ctx, instance, true); err != nil { // if fail to delete the external dependency here, return with error // so that it can be retried diff --git a/controllers/sriovnetworkpoolconfig_controller_test.go b/controllers/sriovnetworkpoolconfig_controller_test.go index 5b76d1d19c..84391a6bd2 100644 --- a/controllers/sriovnetworkpoolconfig_controller_test.go +++ b/controllers/sriovnetworkpoolconfig_controller_test.go @@ -13,7 +13,7 @@ import ( mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" ) var _ = Describe("Operator", func() { diff --git a/controllers/sriovoperatorconfig_controller.go b/controllers/sriovoperatorconfig_controller.go index 7e9c548fbd..231322f953 100644 --- a/controllers/sriovoperatorconfig_controller.go +++ b/controllers/sriovoperatorconfig_controller.go @@ -38,7 +38,9 @@ import ( sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" apply "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/apply" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + consts "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" utils "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) @@ -46,8 +48,8 @@ import ( // SriovOperatorConfigReconciler reconciles a SriovOperatorConfig object type SriovOperatorConfigReconciler struct { client.Client - Scheme *runtime.Scheme - OpenshiftContext *utils.OpenshiftContext + Scheme *runtime.Scheme + PlatformHelper platforms.PlatformHelperInterface } //+kubebuilder:rbac:groups=sriovnetwork.openshift.io,resources=sriovoperatorconfigs,verbs=get;list;watch;create;update;patch;delete @@ -74,7 +76,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. } defaultConfig := &sriovnetworkv1.SriovOperatorConfig{} err := r.Get(ctx, types.NamespacedName{ - Name: constants.DefaultConfigName, Namespace: namespace}, defaultConfig) + Name: consts.DefaultConfigName, Namespace: namespace}, defaultConfig) if err != nil { if apierrors.IsNotFound(err) { singleNode, err := utils.IsSingleNodeCluster(r.Client) @@ -84,7 +86,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. // Default Config object not found, create it. defaultConfig.SetNamespace(namespace) - defaultConfig.SetName(constants.DefaultConfigName) + defaultConfig.SetName(consts.DefaultConfigName) defaultConfig.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ EnableInjector: func() *bool { b := enableAdmissionController; return &b }(), EnableOperatorWebhook: func() *bool { b := enableAdmissionController; return &b }(), @@ -97,7 +99,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. err = r.Create(ctx, defaultConfig) if err != nil { logger.Error(err, "Failed to create default Operator Config", "Namespace", - namespace, "Name", constants.DefaultConfigName) + namespace, "Name", consts.DefaultConfigName) return reconcile.Result{}, err } return reconcile.Result{}, nil @@ -125,9 +127,9 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. } // For Openshift we need to create the systemd files using a machine config - if utils.ClusterType == utils.ClusterTypeOpenshift { + if vars.ClusterType == consts.ClusterTypeOpenshift { // TODO: add support for hypershift as today there is no MCO on hypershift clusters - if r.OpenshiftContext.IsHypershift() { + if r.PlatformHelper.IsHypershift() { return ctrl.Result{}, fmt.Errorf("systemd mode is not supported on hypershift") } @@ -135,7 +137,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. return reconcile.Result{}, err } } - return reconcile.Result{RequeueAfter: constants.ResyncPeriod}, nil + return reconcile.Result{RequeueAfter: consts.ResyncPeriod}, nil } // SetupWithManager sets up the controller with the Manager. @@ -188,7 +190,7 @@ func (r *SriovOperatorConfigReconciler) syncConfigDaemonSet(ctx context.Context, data.Data["SRIOVCNIImage"] = os.Getenv("SRIOV_CNI_IMAGE") data.Data["SRIOVInfiniBandCNIImage"] = os.Getenv("SRIOV_INFINIBAND_CNI_IMAGE") data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION") - data.Data["ClusterType"] = utils.ClusterType + data.Data["ClusterType"] = vars.ClusterType data.Data["DevMode"] = os.Getenv("DEV_MODE") data.Data["ImagePullSecrets"] = GetImagePullSecrets() if dc.Spec.ConfigurationMode == sriovnetworkv1.SystemdConfigurationMode { @@ -204,7 +206,7 @@ func (r *SriovOperatorConfigReconciler) syncConfigDaemonSet(ctx context.Context, logger.Info("New cni bin found", "CNIBinPath", envCniBinPath) data.Data["CNIBinPath"] = envCniBinPath } - objs, err := render.RenderDir(constants.ConfigDaemonPath, &data) + objs, err := render.RenderDir(consts.ConfigDaemonPath, &data) if err != nil { logger.Error(err, "Fail to render config daemon manifests") return err @@ -247,14 +249,14 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc data.Data["NetworkResourcesInjectorImage"] = os.Getenv("NETWORK_RESOURCES_INJECTOR_IMAGE") data.Data["SriovNetworkWebhookImage"] = os.Getenv("SRIOV_NETWORK_WEBHOOK_IMAGE") data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION") - data.Data["ClusterType"] = utils.ClusterType + data.Data["ClusterType"] = vars.ClusterType data.Data["CaBundle"] = os.Getenv("WEBHOOK_CA_BUNDLE") data.Data["DevMode"] = os.Getenv("DEV_MODE") data.Data["ImagePullSecrets"] = GetImagePullSecrets() data.Data["ExternalControlPlane"] = false - if r.OpenshiftContext.IsOpenshiftCluster() { - external := r.OpenshiftContext.IsHypershift() + if r.PlatformHelper.IsOpenshiftCluster() { + external := r.PlatformHelper.IsHypershift() data.Data["ExternalControlPlane"] = external } @@ -265,7 +267,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc } // Delete injector webhook - if !*dc.Spec.EnableInjector && path == constants.InjectorWebHookPath { + if !*dc.Spec.EnableInjector && path == consts.InjectorWebHookPath { for _, obj := range objs { err = r.deleteWebhookObject(ctx, obj) if err != nil { @@ -278,7 +280,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc continue } // Delete operator webhook - if !*dc.Spec.EnableOperatorWebhook && path == constants.OperatorWebHookPath { + if !*dc.Spec.EnableOperatorWebhook && path == consts.OperatorWebHookPath { for _, obj := range objs { err = r.deleteWebhookObject(ctx, obj) if err != nil { @@ -339,7 +341,7 @@ func (r *SriovOperatorConfigReconciler) syncOpenShiftSystemdService(ctx context. if cr.Spec.ConfigurationMode != sriovnetworkv1.SystemdConfigurationMode { obj := &machinev1.MachineConfig{} - err := r.Get(context.TODO(), types.NamespacedName{Name: constants.SystemdServiceOcpMachineConfigName}, obj) + err := r.Get(context.TODO(), types.NamespacedName{Name: consts.SystemdServiceOcpMachineConfigName}, obj) if err != nil { if apierrors.IsNotFound(err) { return nil @@ -362,7 +364,7 @@ func (r *SriovOperatorConfigReconciler) syncOpenShiftSystemdService(ctx context. logger.Info("Start to sync config systemd machine config for openshift") data := render.MakeRenderData() data.Data["LogLevel"] = cr.Spec.LogLevel - objs, err := render.RenderDir(constants.SystemdServiceOcpPath, &data) + objs, err := render.RenderDir(consts.SystemdServiceOcpPath, &data) if err != nil { logger.Error(err, "Fail to render config daemon manifests") return err diff --git a/controllers/suite_test.go b/controllers/suite_test.go index c3eda1a902..7c8fbad4ed 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -23,13 +23,11 @@ import ( "testing" "time" - netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" + "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - openshiftconfigv1 "github.com/openshift/api/config/v1" - mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" - "go.uber.org/zap/zapcore" + "go.uber.org/zap/zapcore" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" @@ -39,10 +37,15 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" + netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" + openshiftconfigv1 "github.com/openshift/api/config/v1" + mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" + //+kubebuilder:scaffold:imports + sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + mock_platforms "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms/mock" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms/openshift" ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to @@ -129,17 +132,24 @@ var _ = BeforeSuite(func(done Done) { }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) + t := GinkgoT() + mockCtrl := gomock.NewController(t) + platformHelper := mock_platforms.NewMockPlatformHelperInterface(mockCtrl) + platformHelper.EXPECT().GetFlavor().Return(openshift.OpenshiftFlavorDefault).AnyTimes() + platformHelper.EXPECT().IsOpenshiftCluster().Return(false).AnyTimes() + platformHelper.EXPECT().IsHypershift().Return(false).AnyTimes() + err = (&SriovOperatorConfigReconciler{ - Client: k8sManager.GetClient(), - Scheme: k8sManager.GetScheme(), - OpenshiftContext: &utils.OpenshiftContext{OpenshiftFlavor: utils.OpenshiftFlavorDefault}, + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + PlatformHelper: platformHelper, }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) err = (&SriovNetworkPoolConfigReconciler{ - Client: k8sManager.GetClient(), - Scheme: k8sManager.GetScheme(), - OpenshiftContext: &utils.OpenshiftContext{OpenshiftFlavor: utils.OpenshiftFlavorDefault}, + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + PlatformHelper: platformHelper, }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) diff --git a/main.go b/main.go index 04c4ad25fd..82d81f6151 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,9 @@ import ( mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1" "k8s.io/apimachinery/pkg/api/errors" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" @@ -46,7 +49,7 @@ import ( sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" "github.com/k8snetworkplumbingwg/sriov-network-operator/controllers" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/leaderelection" snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" @@ -90,12 +93,6 @@ func main() { os.Exit(1) } - openshiftContext, err := utils.NewOpenshiftContext(restConfig, scheme) - if err != nil { - setupLog.Error(err, "couldn't create openshift context") - os.Exit(1) - } - le := leaderelection.GetLeaderElectionConfig(kubeClient, enableLeaderElection) namespace := os.Getenv("NAMESPACE") @@ -137,6 +134,16 @@ func main() { os.Exit(1) } + // Initial global info + vars.Config = restConfig + vars.Scheme = mgrGlobal.GetScheme() + + platformsHelper, err := platforms.NewDefaultPlatformHelper() + if err != nil { + setupLog.Error(err, "couldn't create openshift context") + os.Exit(1) + } + if err = (&controllers.SriovNetworkReconciler{ Client: mgrGlobal.GetClient(), Scheme: mgrGlobal.GetScheme(), @@ -159,17 +166,17 @@ func main() { os.Exit(1) } if err = (&controllers.SriovOperatorConfigReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - OpenshiftContext: openshiftContext, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + PlatformHelper: platformsHelper, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "SriovOperatorConfig") os.Exit(1) } if err = (&controllers.SriovNetworkPoolConfigReconciler{ - Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), - OpenshiftContext: openshiftContext, + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + PlatformHelper: platformsHelper, }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "SriovNetworkPoolConfig") os.Exit(1) diff --git a/pkg/apply/merge.go b/pkg/apply/merge.go index a0bbfd146c..b8080d508f 100644 --- a/pkg/apply/merge.go +++ b/pkg/apply/merge.go @@ -5,7 +5,7 @@ import ( uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" ) // MergeMetadataForUpdate merges the read-only fields of metadata. diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 1d435dc530..85921d8a5c 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -40,12 +40,12 @@ import ( sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" sninformer "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/informers/externalversions" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" snolog "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/log" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms" plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/service" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/systemd" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) const ( @@ -63,26 +63,17 @@ type Message struct { } type Daemon struct { - // name is the node name. - name string - - platform utils.PlatformType - - useSystemdService bool - - devMode bool - client snclientset.Interface // kubeClient allows interaction with Kubernetes, including the node we are running on. kubeClient kubernetes.Interface - openshiftContext *utils.OpenshiftContext - nodeState *sriovnetworkv1.SriovNetworkNodeState enabledPlugins map[string]plugin.VendorPlugin - serviceManager service.ServiceManager + HostHelpers plugin.HostHelpersInterface + + platformHelpers *platforms.PlatformHelper // channel used by callbacks to signal Run() of an error exitCh chan<- error @@ -110,22 +101,15 @@ type Daemon struct { mcpName string - storeManager utils.StoreManagerInterface - - hostManager host.HostManagerInterface - eventRecorder *EventRecorder } const ( - udevScriptsPath = "/bindata/scripts/load-udev.sh" - annoKey = "sriovnetwork.openshift.io/state" - annoIdle = "Idle" - annoDraining = "Draining" - annoMcpPaused = "Draining_MCP_Paused" - syncStatusSucceeded = "Succeeded" - syncStatusFailed = "Failed" - syncStatusInProgress = "InProgress" + udevScriptsPath = "/bindata/scripts/load-udev.sh" + annoKey = "sriovnetwork.openshift.io/state" + annoIdle = "Idle" + annoDraining = "Draining" + annoMcpPaused = "Draining_MCP_Paused" ) var namespace = os.Getenv("NAMESPACE") @@ -142,33 +126,26 @@ func (w writer) Write(p []byte) (n int, err error) { } func New( - nodeName string, client snclientset.Interface, kubeClient kubernetes.Interface, - openshiftContext *utils.OpenshiftContext, + vendorHelpers plugin.HostHelpersInterface, + platformHelper *platforms.PlatformHelper, exitCh chan<- error, stopCh <-chan struct{}, syncCh <-chan struct{}, refreshCh chan<- Message, - platformType utils.PlatformType, - useSystemdService bool, er *EventRecorder, - devMode bool, ) *Daemon { return &Daemon{ - name: nodeName, - platform: platformType, - useSystemdService: useSystemdService, - devMode: devMode, - client: client, - kubeClient: kubeClient, - openshiftContext: openshiftContext, - serviceManager: service.NewServiceManager("/host"), - exitCh: exitCh, - stopCh: stopCh, - syncCh: syncCh, - refreshCh: refreshCh, - nodeState: &sriovnetworkv1.SriovNetworkNodeState{}, + client: client, + kubeClient: kubeClient, + HostHelpers: vendorHelpers, + platformHelpers: platformHelper, + exitCh: exitCh, + stopCh: stopCh, + syncCh: syncCh, + refreshCh: refreshCh, + nodeState: &sriovnetworkv1.SriovNetworkNodeState{}, drainer: &drain.Helper{ Client: kubeClient, Force: true, @@ -196,38 +173,27 @@ func New( // Run the config daemon func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { - log.Log.V(0).Info("Run()", "node", dn.name) + log.Log.V(0).Info("Run()", "node", vars.NodeName) - if utils.ClusterType == utils.ClusterTypeOpenshift { - log.Log.V(0).Info("Run(): start daemon.", "openshiftFlavor", dn.openshiftContext.OpenshiftFlavor) + if vars.ClusterType == consts.ClusterTypeOpenshift { + log.Log.V(0).Info("Run(): start daemon.", "openshiftFlavor", dn.platformHelpers.GetFlavor()) } else { log.Log.V(0).Info("Run(): start daemon.") } - if dn.useSystemdService { + if !vars.UsingSystemdMode { log.Log.V(0).Info("Run(): daemon running in systemd mode") - } - // Only watch own SriovNetworkNodeState CR - defer utilruntime.HandleCrash() - defer dn.workqueue.ShutDown() - - hostManager := host.NewHostManager(dn.useSystemdService) - dn.hostManager = hostManager - if !dn.useSystemdService { - dn.hostManager.TryEnableRdma() - dn.hostManager.TryEnableTun() - dn.hostManager.TryEnableVhostNet() - err := systemd.CleanSriovFilesFromHost(utils.ClusterType == utils.ClusterTypeOpenshift) + dn.HostHelpers.TryEnableRdma() + dn.HostHelpers.TryEnableTun() + dn.HostHelpers.TryEnableVhostNet() + err := systemd.CleanSriovFilesFromHost(vars.ClusterType == consts.ClusterTypeOpenshift) if err != nil { log.Log.Error(err, "failed to remove all the systemd sriov files") } } - - storeManager, err := utils.NewStoreManager(false) - if err != nil { - return err - } - dn.storeManager = storeManager + // Only watch own SriovNetworkNodeState CR + defer utilruntime.HandleCrash() + defer dn.workqueue.ShutDown() if err := dn.prepareNMUdevRule(); err != nil { log.Log.Error(err, "failed to prepare udev files to disable network manager on requested VFs") @@ -242,7 +208,7 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { time.Second*15, namespace, func(lo *metav1.ListOptions) { - lo.FieldSelector = "metadata.name=" + dn.name + lo.FieldSelector = "metadata.name=" + vars.NodeName lo.TimeoutSeconds = &timeout }, ) @@ -301,7 +267,7 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { log.Log.Error(err, "got an error") if more { dn.refreshCh <- Message{ - syncStatus: syncStatusFailed, + syncStatus: consts.SyncStatusFailed, lastSyncError: err.Error(), } } @@ -359,7 +325,7 @@ func (dn *Daemon) processNextWorkItem() bool { if err != nil { // Ereport error message, and put the item back to work queue for retry. dn.refreshCh <- Message{ - syncStatus: syncStatusFailed, + syncStatus: consts.SyncStatusFailed, lastSyncError: err.Error(), } <-dn.syncCh @@ -385,9 +351,9 @@ func (dn *Daemon) nodeAddHandler(obj interface{}) { } func (dn *Daemon) nodeUpdateHandler(old, new interface{}) { - node, err := dn.nodeLister.Get(dn.name) + node, err := dn.nodeLister.Get(vars.NodeName) if errors.IsNotFound(err) { - log.Log.V(2).Info("nodeUpdateHandler(): node has been deleted", "name", dn.name) + log.Log.V(2).Info("nodeUpdateHandler(): node has been deleted", "name", vars.NodeName) return } dn.node = node.DeepCopy() @@ -400,7 +366,7 @@ func (dn *Daemon) nodeUpdateHandler(old, new interface{}) { // Checking if other nodes are draining for _, otherNode := range nodes { - if otherNode.GetName() == dn.name { + if otherNode.GetName() == vars.NodeName { continue } @@ -449,24 +415,24 @@ func (dn *Daemon) nodeStateSyncHandler() error { var err error // Get the latest NodeState var latestState *sriovnetworkv1.SriovNetworkNodeState - var sriovResult = &systemd.SriovResult{SyncStatus: syncStatusSucceeded, LastSyncError: ""} - latestState, err = dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), dn.name, metav1.GetOptions{}) + var sriovResult = &systemd.SriovResult{SyncStatus: consts.SyncStatusSucceeded, LastSyncError: ""} + latestState, err = dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if err != nil { - log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", dn.name) + log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", vars.NodeName) return err } latest := latestState.GetGeneration() log.Log.V(0).Info("nodeStateSyncHandler(): new generation", "generation", latest) - if utils.ClusterType == utils.ClusterTypeOpenshift && !dn.openshiftContext.IsHypershift() { + if vars.ClusterType == consts.ClusterTypeOpenshift && !dn.platformHelpers.IsHypershift() { if err = dn.getNodeMachinePool(); err != nil { return err } } if dn.nodeState.GetGeneration() == latest { - if dn.useSystemdService { - serviceEnabled, err := dn.serviceManager.IsServiceEnabled(systemd.SriovServicePath) + if vars.UsingSystemdMode { + serviceEnabled, err := dn.HostHelpers.IsServiceEnabled(systemd.SriovServicePath) if err != nil { log.Log.Error(err, "nodeStateSyncHandler(): failed to check if sriov-config service exist on host") return err @@ -476,7 +442,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { // this is only for k8s base environments, for openshift the sriov-operator creates a machine config to will apply // the system service and reboot the node the config-daemon doesn't need to do anything. if !serviceEnabled { - sriovResult = &systemd.SriovResult{SyncStatus: syncStatusFailed, + sriovResult = &systemd.SriovResult{SyncStatus: consts.SyncStatusFailed, LastSyncError: "sriov-config systemd service is not available on node"} } else { sriovResult, err = systemd.ReadSriovResult() @@ -485,12 +451,12 @@ func (dn *Daemon) nodeStateSyncHandler() error { return err } } - if sriovResult.LastSyncError != "" || sriovResult.SyncStatus == syncStatusFailed { + if sriovResult.LastSyncError != "" || sriovResult.SyncStatus == consts.SyncStatusFailed { log.Log.Info("nodeStateSyncHandler(): sync failed systemd service error", "last-sync-error", sriovResult.LastSyncError) // add the error but don't requeue dn.refreshCh <- Message{ - syncStatus: syncStatusFailed, + syncStatus: consts.SyncStatusFailed, lastSyncError: sriovResult.LastSyncError, } <-dn.syncCh @@ -499,9 +465,9 @@ func (dn *Daemon) nodeStateSyncHandler() error { } log.Log.V(0).Info("nodeStateSyncHandler(): Interface not changed") if latestState.Status.LastSyncError != "" || - latestState.Status.SyncStatus != syncStatusSucceeded { + latestState.Status.SyncStatus != consts.SyncStatusSucceeded { dn.refreshCh <- Message{ - syncStatus: syncStatusSucceeded, + syncStatus: consts.SyncStatusSucceeded, lastSyncError: "", } // wait for writer to refresh the status @@ -512,7 +478,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } if latestState.GetGeneration() == 1 && len(latestState.Spec.Interfaces) == 0 { - err = dn.storeManager.ClearPCIAddressFolder() + err = dn.HostHelpers.ClearPCIAddressFolder() if err != nil { log.Log.Error(err, "failed to clear the PCI address configuration") return err @@ -533,7 +499,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } dn.refreshCh <- Message{ - syncStatus: syncStatusInProgress, + syncStatus: consts.SyncStatusInProgress, lastSyncError: "", } // wait for writer to refresh status then pull again the latest node state @@ -542,16 +508,16 @@ func (dn *Daemon) nodeStateSyncHandler() error { // we need to load the latest status to our object // if we don't do it we can have a race here where the user remove the virtual functions but the operator didn't // trigger the refresh - updatedState, err := dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), dn.name, metav1.GetOptions{}) + updatedState, err := dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if err != nil { - log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", dn.name) + log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", vars.NodeName) return err } latestState.Status = updatedState.Status // load plugins if it has not loaded if len(dn.enabledPlugins) == 0 { - dn.enabledPlugins, err = enablePlugins(dn.platform, dn.useSystemdService, latestState, dn.hostManager, dn.storeManager) + dn.enabledPlugins, err = enablePlugins(latestState, dn.HostHelpers) if err != nil { log.Log.Error(err, "nodeStateSyncHandler(): failed to enable vendor plugins") return err @@ -582,8 +548,9 @@ func (dn *Daemon) nodeStateSyncHandler() error { // When running using systemd check if the applied configuration is the latest one // or there is a new config we need to apply // When using systemd configuration we write the file - if dn.useSystemdService { - systemdConfModified, err := systemd.WriteConfFile(latestState, dn.devMode, dn.platform) + if vars.UsingSystemdMode { + log.Log.V(0).Info("nodeStateSyncHandler(): writing systemd config file to host") + systemdConfModified, err := systemd.WriteConfFile(latestState) if err != nil { log.Log.Error(err, "nodeStateSyncHandler(): failed to write configuration file for systemd mode") return err @@ -621,7 +588,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } } } - if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() { + if dn.platformHelpers.IsOpenshiftCluster() && !dn.platformHelpers.IsHypershift() { if err = dn.getNodeMachinePool(); err != nil { return err } @@ -639,7 +606,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } } - if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() { + if dn.platformHelpers.IsOpenshiftCluster() && !dn.platformHelpers.IsHypershift() { log.Log.Info("nodeStateSyncHandler(): pause MCP") if err := dn.pauseMCP(); err != nil { return err @@ -656,7 +623,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } } - if !reqReboot && !dn.useSystemdService { + if !reqReboot && !vars.UsingSystemdMode { // For BareMetal machines apply the generic plugin selectedPlugin, ok := dn.enabledPlugins[GenericPluginName] if ok { @@ -683,7 +650,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { if reqReboot { log.Log.Info("nodeStateSyncHandler(): reboot node") dn.eventRecorder.SendEvent("RebootNode", "Reboot node has been initiated") - rebootNode() + dn.rebootNode() return nil } @@ -700,7 +667,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { } } else { if !dn.nodeHasAnnotation(annoKey, annoIdle) { - if err := dn.annotateNode(dn.name, annoIdle); err != nil { + if err := dn.annotateNode(vars.NodeName, annoIdle); err != nil { log.Log.Error(err, "nodeStateSyncHandler(): failed to annotate node") return err } @@ -708,14 +675,14 @@ func (dn *Daemon) nodeStateSyncHandler() error { } log.Log.Info("nodeStateSyncHandler(): sync succeeded") dn.nodeState = latestState.DeepCopy() - if dn.useSystemdService { + if vars.UsingSystemdMode { dn.refreshCh <- Message{ syncStatus: sriovResult.SyncStatus, lastSyncError: sriovResult.LastSyncError, } } else { dn.refreshCh <- Message{ - syncStatus: syncStatusSucceeded, + syncStatus: consts.SyncStatusSucceeded, lastSyncError: "", } } @@ -750,16 +717,16 @@ func (dn *Daemon) completeDrain() error { } } - if dn.openshiftContext.IsOpenshiftCluster() && !dn.openshiftContext.IsHypershift() { + if dn.platformHelpers.IsOpenshiftCluster() && !dn.platformHelpers.IsHypershift() { log.Log.Info("completeDrain(): resume MCP", "mcp-name", dn.mcpName) pausePatch := []byte("{\"spec\":{\"paused\":false}}") - if _, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}); err != nil { + if _, err := dn.platformHelpers.GetMcClient().MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}); err != nil { log.Log.Error(err, "completeDrain(): failed to resume MCP", "mcp-name", dn.mcpName) return err } } - if err := dn.annotateNode(dn.name, annoIdle); err != nil { + if err := dn.annotateNode(vars.NodeName, annoIdle); err != nil { log.Log.Error(err, "completeDrain(): failed to annotate node") return err } @@ -774,7 +741,7 @@ func (dn *Daemon) restartDevicePluginPod() error { var podToDelete string pods, err := dn.kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ LabelSelector: "app=sriov-device-plugin", - FieldSelector: "spec.nodeName=" + dn.name, + FieldSelector: "spec.nodeName=" + vars.NodeName, }) if err != nil { if errors.IsNotFound(err) { @@ -823,9 +790,9 @@ func (dn *Daemon) restartDevicePluginPod() error { return nil } -func rebootNode() { +func (dn *Daemon) rebootNode() { log.Log.Info("rebootNode(): trigger node reboot") - exit, err := utils.Chroot("/host") + exit, err := dn.HostHelpers.Chroot("/host") if err != nil { log.Log.Error(err, "rebootNode(): chroot command failed") } @@ -848,7 +815,7 @@ func rebootNode() { func (dn *Daemon) annotateNode(node, value string) error { log.Log.Info("annotateNode(): Annotate node", "name", node, "value", value) - oldNode, err := dn.kubeClient.CoreV1().Nodes().Get(context.Background(), dn.name, metav1.GetOptions{}) + oldNode, err := dn.kubeClient.CoreV1().Nodes().Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if err != nil { log.Log.Error(err, "annotateNode(): Failed to get node, retrying", "name", node) return err @@ -874,7 +841,7 @@ func (dn *Daemon) annotateNode(node, value string) error { return err } _, err = dn.kubeClient.CoreV1().Nodes().Patch(context.Background(), - dn.name, + vars.NodeName, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}) @@ -892,7 +859,7 @@ func (dn *Daemon) getNodeMachinePool() error { log.Log.Error(nil, "getNodeMachinePool(): Failed to find the the desiredConfig Annotation") return fmt.Errorf("getNodeMachinePool(): Failed to find the the desiredConfig Annotation") } - mc, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigs().Get(context.TODO(), desiredConfig, metav1.GetOptions{}) + mc, err := dn.platformHelpers.GetMcClient().MachineconfigurationV1().MachineConfigs().Get(context.TODO(), desiredConfig, metav1.GetOptions{}) if err != nil { log.Log.Error(err, "getNodeMachinePool(): Failed to get the desired Machine Config") return err @@ -918,7 +885,7 @@ func (dn *Daemon) getDrainLock(ctx context.Context, done chan bool) { }, Client: dn.kubeClient.CoordinationV1(), LockConfig: resourcelock.ResourceLockConfig{ - Identity: dn.name, + Identity: vars.NodeName, }, } @@ -941,7 +908,7 @@ func (dn *Daemon) getDrainLock(ctx context.Context, done chan bool) { } if dn.drainable { log.Log.V(2).Info("getDrainLock(): no other node is draining") - err = dn.annotateNode(dn.name, annoDraining) + err = dn.annotateNode(vars.NodeName, annoDraining) if err != nil { log.Log.Error(err, "getDrainLock(): failed to annotate node") continue @@ -963,7 +930,7 @@ func (dn *Daemon) pauseMCP() error { log.Log.Info("pauseMCP(): pausing MCP") var err error - mcpInformerFactory := mcfginformers.NewSharedInformerFactory(dn.openshiftContext.McClient, + mcpInformerFactory := mcfginformers.NewSharedInformerFactory(dn.platformHelpers.GetMcClient(), time.Second*30, ) mcpInformer := mcpInformerFactory.Machineconfiguration().V1().MachineConfigPools().Informer() @@ -978,7 +945,7 @@ func (dn *Daemon) pauseMCP() error { return } // Always get the latest object - newMcp, err := dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Get(ctx, dn.mcpName, metav1.GetOptions{}) + newMcp, err := dn.platformHelpers.GetMcClient().MachineconfigurationV1().MachineConfigPools().Get(ctx, dn.mcpName, metav1.GetOptions{}) if err != nil { log.Log.V(2).Error(err, "pauseMCP(): Failed to get MCP", "mcp-name", dn.mcpName) return @@ -998,12 +965,12 @@ func (dn *Daemon) pauseMCP() error { } log.Log.Info("pauseMCP(): pause MCP", "mcp-name", dn.mcpName) pausePatch := []byte("{\"spec\":{\"paused\":true}}") - _, err = dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}) + _, err = dn.platformHelpers.GetMcClient().MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}) if err != nil { log.Log.V(2).Error(err, "pauseMCP(): failed to pause MCP", "mcp-name", dn.mcpName) return } - err = dn.annotateNode(dn.name, annoMcpPaused) + err = dn.annotateNode(vars.NodeName, annoMcpPaused) if err != nil { log.Log.V(2).Error(err, "pauseMCP(): Failed to annotate node") return @@ -1014,12 +981,12 @@ func (dn *Daemon) pauseMCP() error { if paused { log.Log.Info("pauseMCP(): MCP is processing, resume MCP", "mcp-name", dn.mcpName) pausePatch := []byte("{\"spec\":{\"paused\":false}}") - _, err = dn.openshiftContext.McClient.MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}) + _, err = dn.platformHelpers.GetMcClient().MachineconfigurationV1().MachineConfigPools().Patch(context.Background(), dn.mcpName, types.MergePatchType, pausePatch, metav1.PatchOptions{}) if err != nil { log.Log.V(2).Error(err, "pauseMCP(): fail to resume MCP", "mcp-name", dn.mcpName) return } - err = dn.annotateNode(dn.name, annoDraining) + err = dn.annotateNode(vars.NodeName, annoDraining) if err != nil { log.Log.V(2).Error(err, "pauseMCP(): Failed to annotate node") return @@ -1068,7 +1035,7 @@ func (dn *Daemon) drainNode() error { log.Log.Error(err, "cordon failed, retrying") return false, nil } - err = drain.RunNodeDrain(dn.drainer, dn.name) + err = drain.RunNodeDrain(dn.drainer, vars.NodeName) if err == nil { return true, nil } @@ -1088,28 +1055,29 @@ func (dn *Daemon) drainNode() error { return nil } +// TODO: move this to host interface func (dn *Daemon) tryCreateSwitchdevUdevRule() error { log.Log.V(2).Info("tryCreateSwitchdevUdevRule()") nodeState, nodeStateErr := dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get( context.Background(), - dn.name, + vars.NodeName, metav1.GetOptions{}, ) if nodeStateErr != nil { - log.Log.Error(nodeStateErr, "could not fetch node state, skip updating switchdev udev rules", "name", dn.name) + log.Log.Error(nodeStateErr, "could not fetch node state, skip updating switchdev udev rules", "name", vars.NodeName) return nil } var newContent string - filePath := path.Join(utils.FilesystemRoot, "/host/etc/udev/rules.d/20-switchdev.rules") + filePath := path.Join(vars.FilesystemRoot, "/host/etc/udev/rules.d/20-switchdev.rules") for _, ifaceStatus := range nodeState.Status.Interfaces { if ifaceStatus.EswitchMode == sriovnetworkv1.ESwithModeSwitchDev { - switchID, err := utils.GetPhysSwitchID(ifaceStatus.Name) + switchID, err := dn.HostHelpers.GetPhysSwitchID(ifaceStatus.Name) if err != nil { return err } - portName, err := utils.GetPhysPortName(ifaceStatus.Name) + portName, err := dn.HostHelpers.GetPhysPortName(ifaceStatus.Name) if err != nil { return err } @@ -1137,7 +1105,7 @@ func (dn *Daemon) tryCreateSwitchdevUdevRule() error { } var stdout, stderr bytes.Buffer - cmd := exec.Command("/bin/bash", path.Join(utils.FilesystemRoot, udevScriptsPath)) + cmd := exec.Command("/bin/bash", path.Join(vars.FilesystemRoot, udevScriptsPath)) cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { @@ -1169,5 +1137,5 @@ func (dn *Daemon) prepareNMUdevRule() error { supportedVfIds = append(supportedVfIds, vfID) } - return utils.PrepareNMUdevRule(supportedVfIds) + return dn.HostHelpers.PrepareNMUdevRule(supportedVfIds) } diff --git a/pkg/daemon/daemon_test.go b/pkg/daemon/daemon_test.go index 5300a1a653..8be7edac26 100644 --- a/pkg/daemon/daemon_test.go +++ b/pkg/daemon/daemon_test.go @@ -1,313 +1,316 @@ package daemon -import ( - "context" - "flag" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - fakek8s "k8s.io/client-go/kubernetes/fake" - - sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem" - - snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" - fakesnclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned/fake" - plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/fake" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" -) - -var FakeSupportedNicIDs corev1.ConfigMap = corev1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: sriovnetworkv1.SupportedNicIDConfigmap, - Namespace: namespace, - }, - Data: map[string]string{ - "Intel_i40e_XXV710": "8086 158a 154c", - "Nvidia_mlx5_ConnectX-4": "15b3 1013 1014", - }, -} - -var SriovDevicePluginPod corev1.Pod = corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "sriov-device-plugin-xxxx", - Namespace: namespace, - Labels: map[string]string{ - "app": "sriov-device-plugin", - }, - }, - Spec: corev1.PodSpec{ - NodeName: "test-node", - }, -} - -func TestConfigDaemon(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Config Daemon Suite") -} - -var _ = BeforeSuite(func() { - // Increase verbosity to help debugging failures - flag.Set("logtostderr", "true") - flag.Set("stderrthreshold", "WARNING") - flag.Set("v", "2") -}) - -var _ = Describe("Config Daemon", func() { - var stopCh chan struct{} - var syncCh chan struct{} - var exitCh chan error - var refreshCh chan Message - - var cleanFakeFs func() - - var sut *Daemon - - BeforeEach(func() { - stopCh = make(chan struct{}) - refreshCh = make(chan Message) - exitCh = make(chan error) - syncCh = make(chan struct{}, 64) - - // Fill syncCh with values so daemon doesn't wait for a writer - for i := 0; i < 64; i++ { - syncCh <- struct{}{} - } - - // Create virtual filesystem for Daemon - fakeFs := &fakefilesystem.FS{ - Dirs: []string{ - "bindata/scripts", - "host/etc/sriov-operator", - "host/etc/sriov-operator/pci", - "host/etc/udev/rules.d", - }, - Symlinks: map[string]string{}, - Files: map[string][]byte{ - "/bindata/scripts/enable-rdma.sh": []byte(""), - "/bindata/scripts/load-kmod.sh": []byte(""), - }, - } - - var err error - utils.FilesystemRoot, cleanFakeFs, err = fakeFs.Use() - Expect(err).ToNot(HaveOccurred()) - - kubeClient := fakek8s.NewSimpleClientset(&FakeSupportedNicIDs, &SriovDevicePluginPod) - client := fakesnclientset.NewSimpleClientset() - - err = sriovnetworkv1.InitNicIDMapFromConfigMap(kubeClient, namespace) - Expect(err).ToNot(HaveOccurred()) - - er := NewEventRecorder(client, "test-node", kubeClient) - - sut = New("test-node", - client, - kubeClient, - &utils.OpenshiftContext{IsOpenShiftCluster: false, OpenshiftFlavor: ""}, - exitCh, - stopCh, - syncCh, - refreshCh, - utils.Baremetal, - false, - er, - false, - ) - - sut.enabledPlugins = map[string]plugin.VendorPlugin{generic.PluginName: &fake.FakePlugin{}} - - go func() { - defer GinkgoRecover() - err := sut.Run(stopCh, exitCh) - Expect(err).ToNot(HaveOccurred()) - }() - }) - - AfterEach(func() { - close(stopCh) - close(syncCh) - close(exitCh) - close(refreshCh) - - cleanFakeFs() - }) - - Context("Should", func() { - It("restart sriov-device-plugin pod", func() { - - _, err := sut.kubeClient.CoreV1().Nodes(). - Create(context.Background(), &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{Name: "test-node"}, - }, metav1.CreateOptions{}) - Expect(err).To(BeNil()) - - nodeState := &sriovnetworkv1.SriovNetworkNodeState{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - Generation: 123, - }, - Spec: sriovnetworkv1.SriovNetworkNodeStateSpec{}, - Status: sriovnetworkv1.SriovNetworkNodeStateStatus{ - Interfaces: []sriovnetworkv1.InterfaceExt{ - { - VFs: []sriovnetworkv1.VirtualFunction{ - {}, - }, - DeviceID: "158b", - Driver: "i40e", - Mtu: 1500, - Name: "ens803f0", - PciAddress: "0000:86:00.0", - Vendor: "8086", - NumVfs: 4, - TotalVfs: 64, - }, - }, - }, - } - Expect( - createSriovNetworkNodeState(sut.client, nodeState)). - To(BeNil()) - - var msg Message - Eventually(refreshCh, "10s").Should(Receive(&msg)) - Expect(msg.syncStatus).To(Equal("InProgress")) - - Eventually(refreshCh, "10s").Should(Receive(&msg)) - Expect(msg.syncStatus).To(Equal("Succeeded")) - - Eventually(func() (int, error) { - podList, err := sut.kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ - LabelSelector: "app=sriov-device-plugin", - FieldSelector: "spec.nodeName=test-node", - }) - - if err != nil { - return 0, err - } - - return len(podList.Items), nil - }, "10s").Should(BeZero()) - - }) - - It("ignore non latest SriovNetworkNodeState generations", func() { - - _, err := sut.kubeClient.CoreV1().Nodes().Create(context.Background(), &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - }, - }, metav1.CreateOptions{}) - Expect(err).To(BeNil()) - - nodeState1 := &sriovnetworkv1.SriovNetworkNodeState{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - Generation: 123, - }, - } - Expect( - createSriovNetworkNodeState(sut.client, nodeState1)). - To(BeNil()) - - nodeState2 := &sriovnetworkv1.SriovNetworkNodeState{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - Generation: 777, - }, - } - Expect( - updateSriovNetworkNodeState(sut.client, nodeState2)). - To(BeNil()) - - var msg Message - Eventually(refreshCh, "10s").Should(Receive(&msg)) - Expect(msg.syncStatus).To(Equal("InProgress")) - - Eventually(refreshCh, "10s").Should(Receive(&msg)) - Expect(msg.syncStatus).To(Equal("Succeeded")) - - Expect(sut.nodeState.GetGeneration()).To(BeNumerically("==", 777)) - }) - }) - - Context("isNodeDraining", func() { - - It("for a non-Openshift cluster", func() { - sut.openshiftContext = &utils.OpenshiftContext{IsOpenShiftCluster: false} - - sut.node = &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - Annotations: map[string]string{}}} - - Expect(sut.isNodeDraining()).To(BeFalse()) - - sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining" - Expect(sut.isNodeDraining()).To(BeTrue()) - - sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining_MCP_Paused" - Expect(sut.isNodeDraining()).To(BeTrue()) - }) - - It("for an Openshift cluster", func() { - sut.openshiftContext = &utils.OpenshiftContext{ - IsOpenShiftCluster: true, - OpenshiftFlavor: utils.OpenshiftFlavorDefault, - } - - sut.node = &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - Annotations: map[string]string{}}} - - Expect(sut.isNodeDraining()).To(BeFalse()) - - sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining" - Expect(sut.isNodeDraining()).To(BeTrue()) - - sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining_MCP_Paused" - Expect(sut.isNodeDraining()).To(BeTrue()) - }) - - It("for an Openshift Hypershift cluster", func() { - sut.openshiftContext = &utils.OpenshiftContext{ - IsOpenShiftCluster: true, - OpenshiftFlavor: utils.OpenshiftFlavorHypershift, - } - - sut.node = &corev1.Node{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-node", - Annotations: map[string]string{}}} - - Expect(sut.isNodeDraining()).To(BeFalse()) - - sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining" - Expect(sut.isNodeDraining()).To(BeTrue()) - - sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining_MCP_Paused" - Expect(sut.isNodeDraining()).To(BeTrue()) - }) - }) -}) - -func createSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetworkv1.SriovNetworkNodeState) error { - _, err := c.SriovnetworkV1(). - SriovNetworkNodeStates(namespace). - Create(context.Background(), nodeState, metav1.CreateOptions{}) - return err -} - -func updateSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetworkv1.SriovNetworkNodeState) error { - _, err := c.SriovnetworkV1(). - SriovNetworkNodeStates(namespace). - Update(context.Background(), nodeState, metav1.UpdateOptions{}) - return err -} +// +//import ( +// "context" +// "flag" +// "testing" +// +// . "github.com/onsi/ginkgo/v2" +// . "github.com/onsi/gomega" +// corev1 "k8s.io/api/core/v1" +// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +// fakek8s "k8s.io/client-go/kubernetes/fake" +// +// sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" +// "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/fakefilesystem" +// +// snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" +// fakesnclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned/fake" +// "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" +// "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" +// plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" +// "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/fake" +// "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic" +// "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" +//) +// +//var FakeSupportedNicIDs corev1.ConfigMap = corev1.ConfigMap{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: sriovnetworkv1.SupportedNicIDConfigmap, +// Namespace: namespace, +// }, +// Data: map[string]string{ +// "Intel_i40e_XXV710": "8086 158a 154c", +// "Nvidia_mlx5_ConnectX-4": "15b3 1013 1014", +// }, +//} +// +//var SriovDevicePluginPod corev1.Pod = corev1.Pod{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "sriov-device-plugin-xxxx", +// Namespace: namespace, +// Labels: map[string]string{ +// "app": "sriov-device-plugin", +// }, +// }, +// Spec: corev1.PodSpec{ +// NodeName: "test-node", +// }, +//} +// +//func TestConfigDaemon(t *testing.T) { +// RegisterFailHandler(Fail) +// RunSpecs(t, "Config Daemon Suite") +//} +// +//var _ = BeforeSuite(func() { +// // Increase verbosity to help debugging failures +// flag.Set("logtostderr", "true") +// flag.Set("stderrthreshold", "WARNING") +// flag.Set("v", "2") +//}) +// +//var _ = Describe("Config Daemon", func() { +// var stopCh chan struct{} +// var syncCh chan struct{} +// var exitCh chan error +// var refreshCh chan Message +// +// var cleanFakeFs func() +// +// var sut *Daemon +// +// BeforeEach(func() { +// stopCh = make(chan struct{}) +// refreshCh = make(chan Message) +// exitCh = make(chan error) +// syncCh = make(chan struct{}, 64) +// +// // Fill syncCh with values so daemon doesn't wait for a writer +// for i := 0; i < 64; i++ { +// syncCh <- struct{}{} +// } +// +// // Create virtual filesystem for Daemon +// fakeFs := &fakefilesystem.FS{ +// Dirs: []string{ +// "bindata/scripts", +// "host/etc/sriov-operator", +// "host/etc/sriov-operator/pci", +// "host/etc/udev/rules.d", +// }, +// Symlinks: map[string]string{}, +// Files: map[string][]byte{ +// "/bindata/scripts/enable-rdma.sh": []byte(""), +// "/bindata/scripts/load-kmod.sh": []byte(""), +// }, +// } +// +// var err error +// vars.FilesystemRoot, cleanFakeFs, err = fakeFs.Use() +// Expect(err).ToNot(HaveOccurred()) +// +// vars.RunningOnHost = false +// vars.NodeName = "test-node" +// vars.PlatformType = consts.Baremetal +// +// kubeClient := fakek8s.NewSimpleClientset(&FakeSupportedNicIDs, &SriovDevicePluginPod) +// client := fakesnclientset.NewSimpleClientset() +// +// err = sriovnetworkv1.InitNicIDMapFromConfigMap(kubeClient, namespace) +// Expect(err).ToNot(HaveOccurred()) +// +// er := NewEventRecorder(client, kubeClient) +// +// sut = New( +// client, +// kubeClient, +// exitCh, +// stopCh, +// syncCh, +// refreshCh, +// er, +// ) +// +// sut.enabledPlugins = map[string]plugin.VendorPlugin{generic.PluginName: &fake.FakePlugin{}} +// +// go func() { +// defer GinkgoRecover() +// err := sut.Run(stopCh, exitCh) +// Expect(err).ToNot(HaveOccurred()) +// }() +// }) +// +// AfterEach(func() { +// close(stopCh) +// close(syncCh) +// close(exitCh) +// close(refreshCh) +// +// cleanFakeFs() +// }) +// +// Context("Should", func() { +// It("restart sriov-device-plugin pod", func() { +// +// _, err := sut.kubeClient.CoreV1().Nodes(). +// Create(context.Background(), &corev1.Node{ +// ObjectMeta: metav1.ObjectMeta{Name: "test-node"}, +// }, metav1.CreateOptions{}) +// Expect(err).To(BeNil()) +// +// nodeState := &sriovnetworkv1.SriovNetworkNodeState{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// Generation: 123, +// }, +// Spec: sriovnetworkv1.SriovNetworkNodeStateSpec{}, +// Status: sriovnetworkv1.SriovNetworkNodeStateStatus{ +// Interfaces: []sriovnetworkv1.InterfaceExt{ +// { +// VFs: []sriovnetworkv1.VirtualFunction{ +// {}, +// }, +// DeviceID: "158b", +// Driver: "i40e", +// Mtu: 1500, +// Name: "ens803f0", +// PciAddress: "0000:86:00.0", +// Vendor: "8086", +// NumVfs: 4, +// TotalVfs: 64, +// }, +// }, +// }, +// } +// Expect( +// createSriovNetworkNodeState(sut.client, nodeState)). +// To(BeNil()) +// +// var msg Message +// Eventually(refreshCh, "10s").Should(Receive(&msg)) +// Expect(msg.syncStatus).To(Equal("InProgress")) +// +// Eventually(refreshCh, "10s").Should(Receive(&msg)) +// Expect(msg.syncStatus).To(Equal("Succeeded")) +// +// Eventually(func() (int, error) { +// podList, err := sut.kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ +// LabelSelector: "app=sriov-device-plugin", +// FieldSelector: "spec.nodeName=test-node", +// }) +// +// if err != nil { +// return 0, err +// } +// +// return len(podList.Items), nil +// }, "10s").Should(BeZero()) +// +// }) +// +// It("ignore non latest SriovNetworkNodeState generations", func() { +// +// _, err := sut.kubeClient.CoreV1().Nodes().Create(context.Background(), &corev1.Node{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// }, +// }, metav1.CreateOptions{}) +// Expect(err).To(BeNil()) +// +// nodeState1 := &sriovnetworkv1.SriovNetworkNodeState{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// Generation: 123, +// }, +// } +// Expect( +// createSriovNetworkNodeState(sut.client, nodeState1)). +// To(BeNil()) +// +// nodeState2 := &sriovnetworkv1.SriovNetworkNodeState{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// Generation: 777, +// }, +// } +// Expect( +// updateSriovNetworkNodeState(sut.client, nodeState2)). +// To(BeNil()) +// +// var msg Message +// Eventually(refreshCh, "10s").Should(Receive(&msg)) +// Expect(msg.syncStatus).To(Equal("InProgress")) +// +// Eventually(refreshCh, "10s").Should(Receive(&msg)) +// Expect(msg.syncStatus).To(Equal("Succeeded")) +// +// Expect(sut.nodeState.GetGeneration()).To(BeNumerically("==", 777)) +// }) +// }) +// +// Context("isNodeDraining", func() { +// +// It("for a non-Openshift cluster", func() { +// sut.openshiftContext = &utils.OpenshiftContext{IsOpenShiftCluster: false} +// +// sut.node = &corev1.Node{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// Annotations: map[string]string{}}} +// +// Expect(sut.isNodeDraining()).To(BeFalse()) +// +// sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining" +// Expect(sut.isNodeDraining()).To(BeTrue()) +// +// sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining_MCP_Paused" +// Expect(sut.isNodeDraining()).To(BeTrue()) +// }) +// +// It("for an Openshift cluster", func() { +// sut.openshiftContext = &utils.OpenshiftContext{ +// IsOpenShiftCluster: true, +// OpenshiftFlavor: utils.OpenshiftFlavorDefault, +// } +// +// sut.node = &corev1.Node{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// Annotations: map[string]string{}}} +// +// Expect(sut.isNodeDraining()).To(BeFalse()) +// +// sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining" +// Expect(sut.isNodeDraining()).To(BeTrue()) +// +// sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining_MCP_Paused" +// Expect(sut.isNodeDraining()).To(BeTrue()) +// }) +// +// It("for an Openshift Hypershift cluster", func() { +// sut.openshiftContext = &utils.OpenshiftContext{ +// IsOpenShiftCluster: true, +// OpenshiftFlavor: utils.OpenshiftFlavorHypershift, +// } +// +// sut.node = &corev1.Node{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "test-node", +// Annotations: map[string]string{}}} +// +// Expect(sut.isNodeDraining()).To(BeFalse()) +// +// sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining" +// Expect(sut.isNodeDraining()).To(BeTrue()) +// +// sut.node.Annotations["sriovnetwork.openshift.io/state"] = "Draining_MCP_Paused" +// Expect(sut.isNodeDraining()).To(BeTrue()) +// }) +// }) +//}) +// +//func createSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetworkv1.SriovNetworkNodeState) error { +// _, err := c.SriovnetworkV1(). +// SriovNetworkNodeStates(namespace). +// Create(context.Background(), nodeState, metav1.CreateOptions{}) +// return err +//} +// +//func updateSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetworkv1.SriovNetworkNodeState) error { +// _, err := c.SriovnetworkV1(). +// SriovNetworkNodeStates(namespace). +// Update(context.Background(), nodeState, metav1.UpdateOptions{}) +// return err +//} diff --git a/pkg/daemon/event_recorder.go b/pkg/daemon/event_recorder.go index 2860cf84a2..cceb4e5fd0 100644 --- a/pkg/daemon/event_recorder.go +++ b/pkg/daemon/event_recorder.go @@ -12,24 +12,23 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" ) type EventRecorder struct { client snclientset.Interface - node string eventRecorder record.EventRecorder eventBroadcaster record.EventBroadcaster } // NewEventRecorder Create a new EventRecorder -func NewEventRecorder(c snclientset.Interface, n string, kubeclient kubernetes.Interface) *EventRecorder { +func NewEventRecorder(c snclientset.Interface, kubeclient kubernetes.Interface) *EventRecorder { eventBroadcaster := record.NewBroadcaster() eventBroadcaster.StartStructuredLogging(4) eventBroadcaster.StartRecordingToSink(&typedv1core.EventSinkImpl{Interface: kubeclient.CoreV1().Events("")}) eventRecorder := eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "config-daemon"}) return &EventRecorder{ client: c, - node: n, eventRecorder: eventRecorder, eventBroadcaster: eventBroadcaster, } @@ -37,9 +36,9 @@ func NewEventRecorder(c snclientset.Interface, n string, kubeclient kubernetes.I // SendEvent Send an Event on the NodeState object func (e *EventRecorder) SendEvent(eventType string, msg string) { - nodeState, err := e.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), e.node, metav1.GetOptions{}) + nodeState, err := e.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if err != nil { - log.Log.V(2).Error(err, "SendEvent(): Failed to fetch node state, skip SendEvent", "name", e.node) + log.Log.V(2).Error(err, "SendEvent(): Failed to fetch node state, skip SendEvent", "name", vars.NodeName) return } e.eventRecorder.Event(nodeState, corev1.EventTypeNormal, eventType, msg) diff --git a/pkg/daemon/plugin.go b/pkg/daemon/plugin.go index 09c69271c6..62c8fd49ca 100644 --- a/pkg/daemon/plugin.go +++ b/pkg/daemon/plugin.go @@ -6,17 +6,17 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" genericplugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/generic" intelplugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/intel" k8splugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/k8s" mellanoxplugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/mellanox" virtualplugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" ) -var VendorPluginMap = map[string]func() (plugin.VendorPlugin, error){ +var VendorPluginMap = map[string]func(helpers plugin.HostHelpersInterface) (plugin.VendorPlugin, error){ "8086": intelplugin.NewIntelPlugin, "15b3": mellanoxplugin.NewMellanoxPlugin, } @@ -29,33 +29,33 @@ var ( K8sPlugin = k8splugin.NewK8sPlugin ) -func enablePlugins(platform utils.PlatformType, useSystemdService bool, ns *sriovnetworkv1.SriovNetworkNodeState, hostManager host.HostManagerInterface, storeManager utils.StoreManagerInterface) (map[string]plugin.VendorPlugin, error) { +func enablePlugins(ns *sriovnetworkv1.SriovNetworkNodeState, helpers plugin.HostHelpersInterface) (map[string]plugin.VendorPlugin, error) { log.Log.Info("enableVendorPlugins(): enabling plugins") enabledPlugins := map[string]plugin.VendorPlugin{} - if platform == utils.VirtualOpenStack { - virtualPlugin, err := VirtualPlugin(false) + if vars.PlatformType == consts.VirtualOpenStack { + virtualPlugin, err := VirtualPlugin(helpers) if err != nil { log.Log.Error(err, "enableVendorPlugins(): failed to load the virtual plugin") return nil, err } enabledPlugins[virtualPlugin.Name()] = virtualPlugin } else { - enabledVendorPlugins, err := registerVendorPlugins(ns) + enabledVendorPlugins, err := registerVendorPlugins(ns, helpers) if err != nil { return nil, err } enabledPlugins = enabledVendorPlugins - if utils.ClusterType != utils.ClusterTypeOpenshift { - k8sPlugin, err := K8sPlugin(useSystemdService) + if vars.ClusterType != consts.ClusterTypeOpenshift { + k8sPlugin, err := K8sPlugin(helpers) if err != nil { log.Log.Error(err, "enableVendorPlugins(): failed to load the k8s plugin") return nil, err } enabledPlugins[k8sPlugin.Name()] = k8sPlugin } - genericPlugin, err := GenericPlugin(false, hostManager, storeManager) + genericPlugin, err := GenericPlugin(helpers) if err != nil { log.Log.Error(err, "enableVendorPlugins(): failed to load the generic plugin") return nil, err @@ -71,12 +71,12 @@ func enablePlugins(platform utils.PlatformType, useSystemdService bool, ns *srio return enabledPlugins, nil } -func registerVendorPlugins(ns *sriovnetworkv1.SriovNetworkNodeState) (map[string]plugin.VendorPlugin, error) { +func registerVendorPlugins(ns *sriovnetworkv1.SriovNetworkNodeState, helpers plugin.HostHelpersInterface) (map[string]plugin.VendorPlugin, error) { vendorPlugins := map[string]plugin.VendorPlugin{} for _, iface := range ns.Status.Interfaces { if val, ok := VendorPluginMap[iface.Vendor]; ok { - plug, err := val() + plug, err := val(helpers) if err != nil { log.Log.Error(err, "registerVendorPlugins(): failed to load plugin", "plugin-name", plug.Name()) return vendorPlugins, fmt.Errorf("registerVendorPlugins(): failed to load the %s plugin error: %v", plug.Name(), err) diff --git a/pkg/daemon/writer.go b/pkg/daemon/writer.go index a9e65a4172..2964e46d27 100644 --- a/pkg/daemon/writer.go +++ b/pkg/daemon/writer.go @@ -16,7 +16,10 @@ import ( sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" snclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/platforms/openstack" ) const ( @@ -25,62 +28,54 @@ const ( ) type NodeStateStatusWriter struct { - client snclientset.Interface - node string - status sriovnetworkv1.SriovNetworkNodeStateStatus - OnHeartbeatFailure func() - openStackDevicesInfo utils.OSPDevicesInfo - withUnsupportedDevices bool - storeManager utils.StoreManagerInterface - eventRecorder *EventRecorder + client snclientset.Interface + status sriovnetworkv1.SriovNetworkNodeStateStatus + OnHeartbeatFailure func() + openStackInterface openstack.OpenstackInterface + storeManager host.StoreManagerInterface + hostManager host.HostManagerInterface + eventRecorder *EventRecorder } // NewNodeStateStatusWriter Create a new NodeStateStatusWriter -func NewNodeStateStatusWriter(c snclientset.Interface, n string, f func(), er *EventRecorder, devMode bool) *NodeStateStatusWriter { +func NewNodeStateStatusWriter(c snclientset.Interface, + f func(), er *EventRecorder, + storeManager host.StoreManagerInterface, + hostManager host.HostManagerInterface, + openStackInterface openstack.OpenstackInterface) *NodeStateStatusWriter { return &NodeStateStatusWriter{ - client: c, - node: n, - OnHeartbeatFailure: f, - eventRecorder: er, - withUnsupportedDevices: devMode, + client: c, + OnHeartbeatFailure: f, + eventRecorder: er, + storeManager: storeManager, + hostManager: hostManager, + openStackInterface: openStackInterface, } } // RunOnce initial the interface status for both baremetal and virtual environments -func (w *NodeStateStatusWriter) RunOnce(destDir string, platformType utils.PlatformType) error { +func (w *NodeStateStatusWriter) RunOnce() error { log.Log.V(0).Info("RunOnce()") msg := Message{} - storeManager, err := utils.NewStoreManager(false) - if err != nil { - log.Log.Error(err, "failed to create store manager") - return err - } - w.storeManager = storeManager - - if platformType == utils.VirtualOpenStack { - ns, err := w.getCheckPointNodeState(destDir) + if vars.PlatformType == consts.VirtualOpenStack { + ns, err := w.getCheckPointNodeState() if err != nil { return err } if ns == nil { - metaData, networkData, err := utils.GetOpenstackData(true) - if err != nil { - log.Log.Error(err, "RunOnce(): failed to read OpenStack data") - } - - w.openStackDevicesInfo, err = utils.CreateOpenstackDevicesInfo(metaData, networkData) + err = w.openStackInterface.CreateOpenstackDevicesInfo() if err != nil { return err } } else { - w.openStackDevicesInfo = utils.CreateOpenstackDevicesInfoFromNodeStatus(ns) + w.openStackInterface.CreateOpenstackDevicesInfoFromNodeStatus(ns) } } log.Log.V(0).Info("RunOnce(): first poll for nic status") - if err := w.pollNicStatus(platformType); err != nil { + if err := w.pollNicStatus(); err != nil { log.Log.Error(err, "RunOnce(): first poll failed") } @@ -88,12 +83,12 @@ func (w *NodeStateStatusWriter) RunOnce(destDir string, platformType utils.Platf if err != nil { log.Log.Error(err, "RunOnce(): first writing to node status failed") } - return w.writeCheckpointFile(ns, destDir) + return w.writeCheckpointFile(ns) } // Run reads from the writer channel and sets the interface status. It will // return if the stop channel is closed. Intended to be run via a goroutine. -func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message, syncCh chan<- struct{}, platformType utils.PlatformType) error { +func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message, syncCh chan<- struct{}) error { log.Log.V(0).Info("Run(): start writer") msg := Message{} @@ -104,7 +99,7 @@ func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message return nil case msg = <-refresh: log.Log.V(0).Info("Run(): refresh trigger") - if err := w.pollNicStatus(platformType); err != nil { + if err := w.pollNicStatus(); err != nil { continue } _, err := w.setNodeStateStatus(msg) @@ -114,7 +109,7 @@ func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message syncCh <- struct{}{} case <-time.After(30 * time.Second): log.Log.V(2).Info("Run(): period refresh") - if err := w.pollNicStatus(platformType); err != nil { + if err := w.pollNicStatus(); err != nil { continue } w.setNodeStateStatus(msg) @@ -122,15 +117,15 @@ func (w *NodeStateStatusWriter) Run(stop <-chan struct{}, refresh <-chan Message } } -func (w *NodeStateStatusWriter) pollNicStatus(platformType utils.PlatformType) error { +func (w *NodeStateStatusWriter) pollNicStatus() error { log.Log.V(2).Info("pollNicStatus()") var iface []sriovnetworkv1.InterfaceExt var err error - if platformType == utils.VirtualOpenStack { - iface, err = utils.DiscoverSriovDevicesVirtual(w.openStackDevicesInfo) + if vars.PlatformType == consts.VirtualOpenStack { + iface, err = w.openStackInterface.DiscoverSriovDevicesVirtual() } else { - iface, err = utils.DiscoverSriovDevices(w.withUnsupportedDevices, w.storeManager) + iface, err = w.hostManager.DiscoverSriovDevices(w.storeManager) } if err != nil { return err @@ -177,7 +172,7 @@ func (w *NodeStateStatusWriter) updateNodeStateStatusRetry(f func(*sriovnetworkv func (w *NodeStateStatusWriter) setNodeStateStatus(msg Message) (*sriovnetworkv1.SriovNetworkNodeState, error) { nodeState, err := w.updateNodeStateStatusRetry(func(nodeState *sriovnetworkv1.SriovNetworkNodeState) { nodeState.Status.Interfaces = w.status.Interfaces - if msg.lastSyncError != "" || msg.syncStatus == syncStatusSucceeded { + if msg.lastSyncError != "" || msg.syncStatus == consts.SyncStatusSucceeded { // clear lastSyncError when sync Succeeded nodeState.Status.LastSyncError = msg.lastSyncError } @@ -215,33 +210,33 @@ func (w *NodeStateStatusWriter) getNodeState() (*sriovnetworkv1.SriovNetworkNode var lastErr error var n *sriovnetworkv1.SriovNetworkNodeState err := wait.PollImmediate(10*time.Second, 5*time.Minute, func() (bool, error) { - n, lastErr = w.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), w.node, metav1.GetOptions{}) + n, lastErr = w.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if lastErr == nil { return true, nil } - log.Log.Error(lastErr, "getNodeState(): Failed to fetch node state, close all connections and retry...", "name", w.node) + log.Log.Error(lastErr, "getNodeState(): Failed to fetch node state, close all connections and retry...", "name", vars.NodeName) // Use the Get() also as an client-go keepalive indicator for the TCP connection. w.OnHeartbeatFailure() return false, nil }) if err != nil { if err == wait.ErrWaitTimeout { - return nil, errors.Wrapf(lastErr, "Timed out trying to fetch node %s", w.node) + return nil, errors.Wrapf(lastErr, "Timed out trying to fetch node %s", vars.NodeName) } return nil, err } return n, nil } -func (w *NodeStateStatusWriter) writeCheckpointFile(ns *sriovnetworkv1.SriovNetworkNodeState, destDir string) error { - configdir := filepath.Join(destDir, CheckpointFileName) +func (w *NodeStateStatusWriter) writeCheckpointFile(ns *sriovnetworkv1.SriovNetworkNodeState) error { + configdir := filepath.Join(vars.Destdir, CheckpointFileName) file, err := os.OpenFile(configdir, os.O_RDWR|os.O_CREATE, 0644) if err != nil { return err } defer file.Close() log.Log.Info("writeCheckpointFile(): try to decode the checkpoint file") - if err = json.NewDecoder(file).Decode(&utils.InitialState); err != nil { + if err = json.NewDecoder(file).Decode(&sriovnetworkv1.InitialState); err != nil { log.Log.V(2).Error(err, "writeCheckpointFile(): fail to decode, writing new file instead") log.Log.Info("writeCheckpointFile(): write checkpoint file") if err = file.Truncate(0); err != nil { @@ -253,14 +248,14 @@ func (w *NodeStateStatusWriter) writeCheckpointFile(ns *sriovnetworkv1.SriovNetw if err = json.NewEncoder(file).Encode(*ns); err != nil { return err } - utils.InitialState = *ns + sriovnetworkv1.InitialState = *ns } return nil } -func (w *NodeStateStatusWriter) getCheckPointNodeState(destDir string) (*sriovnetworkv1.SriovNetworkNodeState, error) { +func (w *NodeStateStatusWriter) getCheckPointNodeState() (*sriovnetworkv1.SriovNetworkNodeState, error) { log.Log.Info("getCheckPointNodeState()") - configdir := filepath.Join(destDir, CheckpointFileName) + configdir := filepath.Join(vars.Destdir, CheckpointFileName) file, err := os.OpenFile(configdir, os.O_RDONLY, 0644) if err != nil { if os.IsNotExist(err) { @@ -269,9 +264,9 @@ func (w *NodeStateStatusWriter) getCheckPointNodeState(destDir string) (*sriovne return nil, err } defer file.Close() - if err = json.NewDecoder(file).Decode(&utils.InitialState); err != nil { + if err = json.NewDecoder(file).Decode(&sriovnetworkv1.InitialState); err != nil { return nil, err } - return &utils.InitialState, nil + return &sriovnetworkv1.InitialState, nil } diff --git a/pkg/systemd/systemd.go b/pkg/systemd/systemd.go index 87ae19a11e..5b40d14d90 100644 --- a/pkg/systemd/systemd.go +++ b/pkg/systemd/systemd.go @@ -25,13 +25,14 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" ) const ( - SriovSystemdConfigPath = utils.SriovConfBasePath + "/sriov-interface-config.yaml" - SriovSystemdResultPath = utils.SriovConfBasePath + "/sriov-interface-result.yaml" - sriovSystemdSupportedNicPath = utils.SriovConfBasePath + "/sriov-supported-nics-ids.yaml" + SriovSystemdConfigPath = consts.SriovConfBasePath + "/sriov-interface-config.yaml" + SriovSystemdResultPath = consts.SriovConfBasePath + "/sriov-interface-result.yaml" + sriovSystemdSupportedNicPath = consts.SriovConfBasePath + "/sriov-supported-nics-ids.yaml" sriovSystemdServiceBinaryPath = "/var/lib/sriov/sriov-network-config-daemon" SriovHostSystemdConfigPath = "/host" + SriovSystemdConfigPath @@ -42,13 +43,15 @@ const ( SriovServicePath = "/etc/systemd/system/sriov-config.service" SriovHostServicePath = "/host" + SriovServicePath - HostSriovConfBasePath = "/host" + utils.SriovConfBasePath + HostSriovConfBasePath = "/host" + consts.SriovConfBasePath ) +// TODO: move this to the host interface also + type SriovConfig struct { Spec sriovnetworkv1.SriovNetworkNodeStateSpec `yaml:"spec"` UnsupportedNics bool `yaml:"unsupportedNics"` - PlatformType utils.PlatformType `yaml:"platformType"` + PlatformType consts.PlatformTypes `yaml:"platformType"` } type SriovResult struct { @@ -67,15 +70,15 @@ func ReadConfFile() (spec *SriovConfig, err error) { return spec, err } -func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState, unsupportedNics bool, platformType utils.PlatformType) (bool, error) { +func WriteConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (bool, error) { newFile := false // remove the device plugin revision as we don't need it here newState.Spec.DpConfigVersion = "" sriovConfig := &SriovConfig{ newState.Spec, - unsupportedNics, - platformType, + vars.DevMode, + vars.PlatformType, } _, err := os.Stat(SriovHostSystemdConfigPath) diff --git a/pkg/webhook/mutate.go b/pkg/webhook/mutate.go index ee8ae2ac63..ea5d6bf68f 100644 --- a/pkg/webhook/mutate.go +++ b/pkg/webhook/mutate.go @@ -7,7 +7,7 @@ import ( v1 "k8s.io/api/admission/v1" "sigs.k8s.io/controller-runtime/pkg/log" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" ) var ( diff --git a/pkg/webhook/validate.go b/pkg/webhook/validate.go index 366881dc67..e355b1ed13 100644 --- a/pkg/webhook/validate.go +++ b/pkg/webhook/validate.go @@ -16,8 +16,8 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" - "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/vars" ) const ( @@ -35,7 +35,7 @@ func validateSriovOperatorConfig(cr *sriovnetworkv1.SriovOperatorConfig, operati log.Log.V(2).Info("validateSriovOperatorConfig", "object", cr) var warnings []string - if cr.GetName() != constants.DefaultConfigName { + if cr.GetName() != consts.DefaultConfigName { return false, warnings, fmt.Errorf("only default SriovOperatorConfig is used") } @@ -95,7 +95,7 @@ func validateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePolicy, o log.Log.V(2).Info("validateSriovNetworkNodePolicy", "object", cr) var warnings []string - if cr.GetName() == constants.DefaultPolicyName && cr.GetNamespace() == os.Getenv("NAMESPACE") { + if cr.GetName() == consts.DefaultPolicyName && cr.GetNamespace() == os.Getenv("NAMESPACE") { if operation == v1.Delete { // reject deletion of default policy return false, warnings, fmt.Errorf("default SriovNetworkNodePolicy shouldn't be deleted") @@ -193,19 +193,19 @@ func staticValidateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePol // To configure RoCE on baremetal or virtual machine: // BM: DeviceType = netdevice && isRdma = true // VM: DeviceType = vfio-pci && isRdma = false - if cr.Spec.DeviceType == constants.DeviceTypeVfioPci && cr.Spec.IsRdma { + if cr.Spec.DeviceType == consts.DeviceTypeVfioPci && cr.Spec.IsRdma { return false, fmt.Errorf("'deviceType: vfio-pci' conflicts with 'isRdma: true'; Set 'deviceType' to (string)'netdevice' Or Set 'isRdma' to (bool)'false'") } - if strings.EqualFold(cr.Spec.LinkType, constants.LinkTypeIB) && !cr.Spec.IsRdma { + if strings.EqualFold(cr.Spec.LinkType, consts.LinkTypeIB) && !cr.Spec.IsRdma { return false, fmt.Errorf("'linkType: ib or IB' requires 'isRdma: true'; Set 'isRdma' to (bool)'true'") } // vdpa: deviceType must be set to 'netdevice' - if cr.Spec.DeviceType != constants.DeviceTypeNetDevice && (cr.Spec.VdpaType == constants.VdpaTypeVirtio || cr.Spec.VdpaType == constants.VdpaTypeVhost) { + if cr.Spec.DeviceType != consts.DeviceTypeNetDevice && (cr.Spec.VdpaType == consts.VdpaTypeVirtio || cr.Spec.VdpaType == consts.VdpaTypeVhost) { return false, fmt.Errorf("'deviceType: %s' conflicts with '%s'; Set 'deviceType' to (string)'netdevice' Or Remove 'vdpaType'", cr.Spec.DeviceType, cr.Spec.VdpaType) } // vdpa: device must be configured in switchdev mode - if (cr.Spec.VdpaType == constants.VdpaTypeVirtio || cr.Spec.VdpaType == constants.VdpaTypeVhost) && cr.Spec.EswitchMode != sriovnetworkv1.ESwithModeSwitchDev { + if (cr.Spec.VdpaType == consts.VdpaTypeVirtio || cr.Spec.VdpaType == consts.VdpaTypeVhost) && cr.Spec.EswitchMode != sriovnetworkv1.ESwithModeSwitchDev { return false, fmt.Errorf("vdpa requires the device to be configured in switchdev mode") } @@ -297,7 +297,7 @@ func validatePolicyForNodeState(policy *sriovnetworkv1.SriovNetworkNodePolicy, s if err == nil { interfaceSelected = true interfaceSelectedForNode = true - if policy.GetName() != constants.DefaultPolicyName && policy.Spec.NumVfs == 0 { + if policy.GetName() != consts.DefaultPolicyName && policy.Spec.NumVfs == 0 { return nil, fmt.Errorf("numVfs(%d) in CR %s is not allowed", policy.Spec.NumVfs, policy.GetName()) } if policy.Spec.NumVfs > iface.TotalVfs && iface.Vendor == IntelID { @@ -322,7 +322,7 @@ func validatePolicyForNodeState(policy *sriovnetworkv1.SriovNetworkNodePolicy, s } } // vdpa: only mellanox cards are supported - if (policy.Spec.VdpaType == constants.VdpaTypeVirtio || policy.Spec.VdpaType == constants.VdpaTypeVhost) && iface.Vendor != MellanoxID { + if (policy.Spec.VdpaType == consts.VdpaTypeVirtio || policy.Spec.VdpaType == consts.VdpaTypeVhost) && iface.Vendor != MellanoxID { return nil, fmt.Errorf("vendor(%s) in CR %s not supported for vdpa interface(%s)", iface.Vendor, policy.GetName(), iface.Name) } } else { @@ -440,7 +440,7 @@ func validateNicModel(selector *sriovnetworkv1.SriovNetworkNicSelector, iface *s } // Check the vendor and device ID of the VF only if we are on a virtual environment - for key := range utils.PlatformMap { + for key := range vars.PlatformsMap { if strings.Contains(strings.ToLower(node.Spec.ProviderID), strings.ToLower(key)) && selector.NetFilter != "" && selector.NetFilter == iface.NetFilter && sriovnetworkv1.IsVfSupportedModel(iface.Vendor, iface.DeviceID) { diff --git a/pkg/webhook/validate_test.go b/pkg/webhook/validate_test.go index 990886906b..6bb23683db 100644 --- a/pkg/webhook/validate_test.go +++ b/pkg/webhook/validate_test.go @@ -14,7 +14,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/envtest" . "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" - constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/global/consts" fakesnclientset "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/client/clientset/versioned/fake" ) diff --git a/test/conformance/tests/test_sriov_operator.go b/test/conformance/tests/test_sriov_operator.go index 6f73f93b5a..3729d0b9f1 100644 --- a/test/conformance/tests/test_sriov_operator.go +++ b/test/conformance/tests/test_sriov_operator.go @@ -1112,7 +1112,7 @@ var _ = Describe("[sriov] operator", func() { capacity, _ = resNum.AsInt64() res["openshift.io/testresource1"] = capacity return res - }, 2*time.Minute, time.Second).Should(Equal(map[string]int64{ + }, 15*time.Minute, time.Second).Should(Equal(map[string]int64{ "openshift.io/testresource": int64(3), "openshift.io/testresource1": int64(2), }))