diff --git a/controllers/helper.go b/controllers/helper.go index 0b0ac6c27..f0380f671 100644 --- a/controllers/helper.go +++ b/controllers/helper.go @@ -39,6 +39,7 @@ import ( "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/apply" constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" ) var webhooks = map[string](string){ @@ -55,8 +56,6 @@ const ( trueString = "true" ) -var namespace = os.Getenv("NAMESPACE") - func GetImagePullSecrets() []string { imagePullSecrets := os.Getenv("IMAGE_PULL_SECRETS") if imagePullSecrets != "" { @@ -85,7 +84,7 @@ func syncPluginDaemonObjs(ctx context.Context, client k8sclient.Client, scheme * // render plugin manifests data := render.MakeRenderData() - data.Data["Namespace"] = namespace + data.Data["Namespace"] = vars.Namespace data.Data["SRIOVDevicePluginImage"] = os.Getenv("SRIOV_DEVICE_PLUGIN_IMAGE") data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION") data.Data["ResourcePrefix"] = os.Getenv("RESOURCE_PREFIX") @@ -94,7 +93,7 @@ func syncPluginDaemonObjs(ctx context.Context, client k8sclient.Client, scheme * defaultConfig := &sriovnetworkv1.SriovOperatorConfig{} err := client.Get(ctx, types.NamespacedName{ - Name: constants.DefaultConfigName, Namespace: namespace}, defaultConfig) + Name: constants.DefaultConfigName, Namespace: vars.Namespace}, defaultConfig) if err != nil { return err } diff --git a/controllers/sriovibnetwork_controller.go b/controllers/sriovibnetwork_controller.go index 0ffe5fba0..65b66f167 100644 --- a/controllers/sriovibnetwork_controller.go +++ b/controllers/sriovibnetwork_controller.go @@ -35,6 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" ) // SriovIBNetworkReconciler reconciles a SriovIBNetwork object @@ -58,7 +59,7 @@ type SriovIBNetworkReconciler struct { // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { // The SriovNetwork CR shall only be defined in operator namespace. - req.Namespace = namespace + req.Namespace = vars.Namespace reqLogger := log.FromContext(ctx).WithValues("sriovnetwork", req.NamespacedName) reqLogger.Info("Reconciling SriovIBNetwork") var err error diff --git a/controllers/sriovnetwork_controller.go b/controllers/sriovnetwork_controller.go index f89d524c1..7ef991e93 100644 --- a/controllers/sriovnetwork_controller.go +++ b/controllers/sriovnetwork_controller.go @@ -35,6 +35,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" ) // SriovNetworkReconciler reconciles a SriovNetwork object @@ -57,7 +58,7 @@ type SriovNetworkReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile func (r *SriovNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - req.Namespace = namespace + req.Namespace = vars.Namespace reqLogger := log.FromContext(ctx).WithValues("sriovnetwork", req.NamespacedName) reqLogger.Info("Reconciling SriovNetwork") diff --git a/controllers/sriovnetworknodepolicy_controller.go b/controllers/sriovnetworknodepolicy_controller.go index 14acc9a8d..fa67a54aa 100644 --- a/controllers/sriovnetworknodepolicy_controller.go +++ b/controllers/sriovnetworknodepolicy_controller.go @@ -43,6 +43,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" utils "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils" + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" dptypes "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types" @@ -82,11 +83,11 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct reqLogger.Info("Reconciling") defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{} - err := r.Get(ctx, types.NamespacedName{Name: constants.DefaultPolicyName, Namespace: namespace}, defaultPolicy) + err := r.Get(ctx, types.NamespacedName{Name: constants.DefaultPolicyName, Namespace: vars.Namespace}, defaultPolicy) if err != nil { if errors.IsNotFound(err) { // Default policy object not found, create it. - defaultPolicy.SetNamespace(namespace) + defaultPolicy.SetNamespace(vars.Namespace) defaultPolicy.SetName(constants.DefaultPolicyName) defaultPolicy.Spec = sriovnetworkv1.SriovNetworkNodePolicySpec{ NumVfs: 0, @@ -95,7 +96,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct } err = r.Create(ctx, defaultPolicy) if err != nil { - reqLogger.Error(err, "Failed to create default Policy", "Namespace", namespace, "Name", constants.DefaultPolicyName) + reqLogger.Error(err, "Failed to create default Policy", "Namespace", vars.Namespace, "Name", constants.DefaultPolicyName) return reconcile.Result{}, err } reqLogger.Info("Default policy created") @@ -125,7 +126,7 @@ func (r *SriovNetworkNodePolicyReconciler) Reconcile(ctx context.Context, req ct "kubernetes.io/os": "linux", } defaultOpConf := &sriovnetworkv1.SriovOperatorConfig{} - if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: constants.DefaultConfigName}, defaultOpConf); err != nil { + if err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: constants.DefaultConfigName}, defaultOpConf); err != nil { return reconcile.Result{}, err } if len(defaultOpConf.Spec.ConfigDaemonNodeSelector) > 0 { @@ -216,7 +217,7 @@ func (r *SriovNetworkNodePolicyReconciler) syncDevicePluginConfigMap(ctx context }, ObjectMeta: metav1.ObjectMeta{ Name: constants.ConfigMapName, - Namespace: namespace, + Namespace: vars.Namespace, }, Data: configData, } @@ -246,14 +247,14 @@ func (r *SriovNetworkNodePolicyReconciler) syncAllSriovNetworkNodeStates(ctx con logger := log.Log.WithName("syncAllSriovNetworkNodeStates") logger.V(1).Info("Start to sync all SriovNetworkNodeState custom resource") found := &corev1.ConfigMap{} - if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: constants.ConfigMapName}, found); err != nil { + if err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: constants.ConfigMapName}, found); err != nil { logger.V(1).Info("Fail to get", "ConfigMap", constants.ConfigMapName) } for _, node := range nl.Items { logger.V(1).Info("Sync SriovNetworkNodeState CR", "name", node.Name) ns := &sriovnetworkv1.SriovNetworkNodeState{} ns.Name = node.Name - ns.Namespace = namespace + ns.Namespace = vars.Namespace j, _ := json.Marshal(ns) logger.V(2).Info("SriovNetworkNodeState CR", "content", j) if err := r.syncSriovNetworkNodeState(ctx, np, npl, ns, &node, utils.HashConfigMap(found)); err != nil { @@ -428,7 +429,7 @@ func (r *SriovNetworkNodePolicyReconciler) renderDevicePluginConfigData(ctx cont } nodeState := &sriovnetworkv1.SriovNetworkNodeState{} - err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: node.Name}, nodeState) + err := r.Get(ctx, types.NamespacedName{Namespace: vars.Namespace, Name: node.Name}, nodeState) if err != nil { return rcl, err } diff --git a/controllers/sriovnetworknodepolicy_controller_test.go b/controllers/sriovnetworknodepolicy_controller_test.go index bd4579d78..3894f5a93 100644 --- a/controllers/sriovnetworknodepolicy_controller_test.go +++ b/controllers/sriovnetworknodepolicy_controller_test.go @@ -18,6 +18,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/vars" ) func TestNodeSelectorMerge(t *testing.T) { @@ -213,7 +214,7 @@ func TestRenderDevicePluginConfigData(t *testing.T) { reconciler := SriovNetworkNodePolicyReconciler{} node := corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "node1"}} - nodeState := sriovnetworkv1.SriovNetworkNodeState{ObjectMeta: metav1.ObjectMeta{Name: node.Name, Namespace: namespace}} + nodeState := sriovnetworkv1.SriovNetworkNodeState{ObjectMeta: metav1.ObjectMeta{Name: node.Name, Namespace: vars.Namespace}} scheme := runtime.NewScheme() utilruntime.Must(sriovnetworkv1.AddToScheme(scheme)) diff --git a/controllers/sriovoperatorconfig_controller.go b/controllers/sriovoperatorconfig_controller.go index d6fe28e78..d75371581 100644 --- a/controllers/sriovoperatorconfig_controller.go +++ b/controllers/sriovoperatorconfig_controller.go @@ -77,7 +77,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. } defaultConfig := &sriovnetworkv1.SriovOperatorConfig{} err := r.Get(ctx, types.NamespacedName{ - Name: consts.DefaultConfigName, Namespace: namespace}, defaultConfig) + Name: consts.DefaultConfigName, Namespace: vars.Namespace}, defaultConfig) if err != nil { if apierrors.IsNotFound(err) { singleNode, err := utils.IsSingleNodeCluster(r.Client) @@ -86,7 +86,7 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. } // Default Config object not found, create it. - defaultConfig.SetNamespace(namespace) + defaultConfig.SetNamespace(vars.Namespace) defaultConfig.SetName(consts.DefaultConfigName) defaultConfig.Spec = sriovnetworkv1.SriovOperatorConfigSpec{ EnableInjector: func() *bool { b := vars.EnableAdmissionController; return &b }(), @@ -100,7 +100,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", consts.DefaultConfigName) + vars.Namespace, "Name", consts.DefaultConfigName) return reconcile.Result{}, err } return reconcile.Result{}, nil @@ -118,13 +118,13 @@ func (r *SriovOperatorConfigReconciler) Reconcile(ctx context.Context, req ctrl. } defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{} - err = r.Get(ctx, types.NamespacedName{Name: consts.DefaultPolicyName, Namespace: namespace}, defaultPolicy) + err = r.Get(ctx, types.NamespacedName{Name: consts.DefaultPolicyName, Namespace: vars.Namespace}, defaultPolicy) if err != nil { // Error reading the object - requeue the request. return reconcile.Result{}, err } - if req.Namespace != namespace { + if req.Namespace != vars.Namespace { return reconcile.Result{}, nil } @@ -173,7 +173,7 @@ func (r *SriovOperatorConfigReconciler) syncConfigDaemonSet(ctx context.Context, data := render.MakeRenderData() data.Data["Image"] = os.Getenv("SRIOV_NETWORK_CONFIG_DAEMON_IMAGE") - data.Data["Namespace"] = namespace + data.Data["Namespace"] = vars.Namespace data.Data["SRIOVCNIImage"] = os.Getenv("SRIOV_CNI_IMAGE") data.Data["SRIOVInfiniBandCNIImage"] = os.Getenv("SRIOV_INFINIBAND_CNI_IMAGE") data.Data["ReleaseVersion"] = os.Getenv("RELEASEVERSION") @@ -231,7 +231,7 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(ctx context.Context, dc for name, path := range webhooks { // Render Webhook manifests data := render.MakeRenderData() - data.Data["Namespace"] = namespace + data.Data["Namespace"] = vars.Namespace data.Data["SRIOVMutatingWebhookName"] = name data.Data["NetworkResourcesInjectorImage"] = os.Getenv("NETWORK_RESOURCES_INJECTOR_IMAGE") data.Data["SriovNetworkWebhookImage"] = os.Getenv("SRIOV_NETWORK_WEBHOOK_IMAGE") diff --git a/controllers/sriovoperatorconfig_controller_test.go b/controllers/sriovoperatorconfig_controller_test.go index c36b2a91f..a871f69a1 100644 --- a/controllers/sriovoperatorconfig_controller_test.go +++ b/controllers/sriovoperatorconfig_controller_test.go @@ -12,6 +12,7 @@ import ( 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/vars" util "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util" ) @@ -19,7 +20,7 @@ var _ = Describe("Operator", func() { var config *sriovnetworkv1.SriovOperatorConfig BeforeEach(func() { defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{} - defaultPolicy.SetNamespace(namespace) + defaultPolicy.SetNamespace(vars.Namespace) defaultPolicy.SetName(constants.DefaultPolicyName) defaultPolicy.Spec = sriovnetworkv1.SriovNetworkNodePolicySpec{ NumVfs: 0, @@ -30,7 +31,7 @@ var _ = Describe("Operator", func() { }) AfterEach(func() { defaultPolicy := &sriovnetworkv1.SriovNetworkNodePolicy{} - defaultPolicy.SetNamespace(namespace) + defaultPolicy.SetNamespace(vars.Namespace) defaultPolicy.SetName(constants.DefaultPolicyName) Expect(k8sClient.Delete(goctx.TODO(), defaultPolicy)).Should(Succeed()) }) diff --git a/pkg/daemon/daemon.go b/pkg/daemon/daemon.go index 9d4a43e94..c6c845cb0 100644 --- a/pkg/daemon/daemon.go +++ b/pkg/daemon/daemon.go @@ -112,8 +112,6 @@ const ( annoMcpPaused = "Draining_MCP_Paused" ) -var namespace = os.Getenv("NAMESPACE") - // writer implements io.Writer interface as a pass-through for log.Log. type writer struct { logFunc func(msg string, keysAndValues ...interface{}) @@ -210,7 +208,7 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { dn.mu = &sync.Mutex{} informerFactory := sninformer.NewFilteredSharedInformerFactory(dn.client, time.Second*15, - namespace, + vars.Namespace, func(lo *metav1.ListOptions) { lo.FieldSelector = metadataKey + "=" + vars.NodeName lo.TimeoutSeconds = &timeout @@ -227,7 +225,7 @@ func (dn *Daemon) Run(stopCh <-chan struct{}, exitCh <-chan error) error { cfgInformerFactory := sninformer.NewFilteredSharedInformerFactory(dn.client, time.Second*30, - namespace, + vars.Namespace, func(lo *metav1.ListOptions) { lo.FieldSelector = metadataKey + "=" + "default" }, @@ -410,7 +408,7 @@ func (dn *Daemon) nodeStateSyncHandler() error { // Get the latest NodeState var latestState *sriovnetworkv1.SriovNetworkNodeState var sriovResult = &systemd.SriovResult{SyncStatus: consts.SyncStatusSucceeded, LastSyncError: ""} - latestState, err = dn.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) + latestState, err = dn.client.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if err != nil { log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", vars.NodeName) return err @@ -502,7 +500,7 @@ 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(), vars.NodeName, metav1.GetOptions{}) + updatedState, err := dn.client.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if err != nil { log.Log.Error(err, "nodeStateSyncHandler(): Failed to fetch node state", "name", vars.NodeName) return err @@ -733,7 +731,7 @@ func (dn *Daemon) restartDevicePluginPod() error { log.Log.V(2).Info("restartDevicePluginPod(): try to restart device plugin pod") var podToDelete string - pods, err := dn.kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ + pods, err := dn.kubeClient.CoreV1().Pods(vars.Namespace).List(context.Background(), metav1.ListOptions{ LabelSelector: "app=sriov-device-plugin", FieldSelector: "spec.nodeName=" + vars.NodeName, }) @@ -753,7 +751,7 @@ func (dn *Daemon) restartDevicePluginPod() error { podToDelete = pods.Items[0].Name log.Log.V(2).Info("restartDevicePluginPod(): Found device plugin pod, deleting it", "pod-name", podToDelete) - err = dn.kubeClient.CoreV1().Pods(namespace).Delete(context.Background(), podToDelete, metav1.DeleteOptions{}) + err = dn.kubeClient.CoreV1().Pods(vars.Namespace).Delete(context.Background(), podToDelete, metav1.DeleteOptions{}) if errors.IsNotFound(err) { log.Log.Info("restartDevicePluginPod(): pod to delete not found") return nil @@ -764,7 +762,7 @@ func (dn *Daemon) restartDevicePluginPod() error { } if err := wait.PollImmediateUntil(3*time.Second, func() (bool, error) { - _, err := dn.kubeClient.CoreV1().Pods(namespace).Get(context.Background(), podToDelete, metav1.GetOptions{}) + _, err := dn.kubeClient.CoreV1().Pods(vars.Namespace).Get(context.Background(), podToDelete, metav1.GetOptions{}) if errors.IsNotFound(err) { log.Log.Info("restartDevicePluginPod(): device plugin pod exited") return true, nil @@ -875,7 +873,7 @@ func (dn *Daemon) getDrainLock(ctx context.Context, done chan bool) { lock := &resourcelock.LeaseLock{ LeaseMeta: metav1.ObjectMeta{ Name: "config-daemon-draining-lock", - Namespace: namespace, + Namespace: vars.Namespace, }, Client: dn.kubeClient.CoordinationV1(), LockConfig: resourcelock.ResourceLockConfig{ @@ -1052,7 +1050,7 @@ func (dn *Daemon) drainNode() error { // 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( + nodeState, nodeStateErr := dn.client.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).Get( context.Background(), vars.NodeName, metav1.GetOptions{}, diff --git a/pkg/daemon/daemon_test.go b/pkg/daemon/daemon_test.go index b804888fa..e81f7b677 100644 --- a/pkg/daemon/daemon_test.go +++ b/pkg/daemon/daemon_test.go @@ -30,7 +30,7 @@ import ( var FakeSupportedNicIDs corev1.ConfigMap = corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: sriovnetworkv1.SupportedNicIDConfigmap, - Namespace: namespace, + Namespace: vars.Namespace, }, Data: map[string]string{ "Intel_i40e_XXV710": "8086 158a 154c", @@ -41,7 +41,7 @@ var FakeSupportedNicIDs corev1.ConfigMap = corev1.ConfigMap{ var SriovDevicePluginPod corev1.Pod = corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "sriov-device-plugin-xxxx", - Namespace: namespace, + Namespace: vars.Namespace, Labels: map[string]string{ "app": "sriov-device-plugin", }, @@ -110,7 +110,7 @@ var _ = Describe("Config Daemon", func() { kubeClient := fakek8s.NewSimpleClientset(&FakeSupportedNicIDs, &SriovDevicePluginPod) client := fakesnclientset.NewSimpleClientset() - err = sriovnetworkv1.InitNicIDMapFromConfigMap(kubeClient, namespace) + err = sriovnetworkv1.InitNicIDMapFromConfigMap(kubeClient, vars.Namespace) Expect(err).ToNot(HaveOccurred()) er := NewEventRecorder(client, kubeClient) @@ -203,7 +203,7 @@ var _ = Describe("Config Daemon", func() { Expect(msg.syncStatus).To(Equal("Succeeded")) Eventually(func() (int, error) { - podList, err := sut.kubeClient.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ + podList, err := sut.kubeClient.CoreV1().Pods(vars.Namespace).List(context.Background(), metav1.ListOptions{ LabelSelector: "app=sriov-device-plugin", FieldSelector: "spec.nodeName=test-node", }) @@ -260,14 +260,14 @@ var _ = Describe("Config Daemon", func() { func createSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetworkv1.SriovNetworkNodeState) error { _, err := c.SriovnetworkV1(). - SriovNetworkNodeStates(namespace). + SriovNetworkNodeStates(vars.Namespace). Create(context.Background(), nodeState, metav1.CreateOptions{}) return err } func updateSriovNetworkNodeState(c snclientset.Interface, nodeState *sriovnetworkv1.SriovNetworkNodeState) error { _, err := c.SriovnetworkV1(). - SriovNetworkNodeStates(namespace). + SriovNetworkNodeStates(vars.Namespace). Update(context.Background(), nodeState, metav1.UpdateOptions{}) return err } diff --git a/pkg/daemon/event_recorder.go b/pkg/daemon/event_recorder.go index ed9b34ee7..25b2d2351 100644 --- a/pkg/daemon/event_recorder.go +++ b/pkg/daemon/event_recorder.go @@ -36,7 +36,7 @@ func NewEventRecorder(c snclientset.Interface, kubeclient kubernetes.Interface) // 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(), vars.NodeName, metav1.GetOptions{}) + nodeState, err := e.client.SriovnetworkV1().SriovNetworkNodeStates(vars.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", vars.NodeName) return diff --git a/pkg/daemon/writer.go b/pkg/daemon/writer.go index 1a83c77d8..c796546ef 100644 --- a/pkg/daemon/writer.go +++ b/pkg/daemon/writer.go @@ -150,7 +150,7 @@ func (w *NodeStateStatusWriter) updateNodeStateStatusRetry(f func(*sriovnetworkv lastError = n.Status.LastSyncError var err error - nodeState, err = w.client.SriovnetworkV1().SriovNetworkNodeStates(namespace).UpdateStatus(context.Background(), n, metav1.UpdateOptions{}) + nodeState, err = w.client.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).UpdateStatus(context.Background(), n, metav1.UpdateOptions{}) if err != nil { log.Log.V(0).Error(err, "updateNodeStateStatusRetry(): fail to update the node status") } @@ -207,7 +207,7 @@ 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(), vars.NodeName, metav1.GetOptions{}) + n, lastErr = w.client.SriovnetworkV1().SriovNetworkNodeStates(vars.Namespace).Get(context.Background(), vars.NodeName, metav1.GetOptions{}) if lastErr == nil { return true, nil } diff --git a/pkg/vars/vars.go b/pkg/vars/vars.go index a34cab8af..ebc49fa25 100644 --- a/pkg/vars/vars.go +++ b/pkg/vars/vars.go @@ -57,6 +57,9 @@ var ( // PfPhysPortNameRe regex to find switchdev devices on the host PfPhysPortNameRe = regexp.MustCompile(`p\d+`) + + // Namespace contains k8s namespace + Namespace = "" ) func init() { @@ -79,4 +82,6 @@ func init() { if enableAdmissionController == "True" { EnableAdmissionController = true } + + Namespace = os.Getenv("NAMESPACE") }