Repository: kubernetes-sigs/node-feature-discovery
Pull request #1965 (2024-11-29 15:01:40)

nfd-master: check that namespace informer cache sync succeeded
Mitigate the namespace cache sync reported in issue #1936 

k8s-ci-robot: Hi @ozhuraki. Thanks for your PR.

I'm waiting for a [kubernetes-sigs](https://github.com/orgs/kubernetes-sigs/people) member to verify that this patch is reasonable to test. If it is, they should reply with `/ok-to-test` on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should [join the org](https://git.k8s.io/community/community-membership.md#member) to skip this step.

Once the patch is verified, the new status will be reflected by the `ok-to-test` label.

I understand the commands that are listed [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fnode-feature-discovery).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
</details>

k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/node-feature-discovery/pull/1965#" title="Author self-approved">ozhuraki</a>*
**Once this PR has been reviewed and has the lgtm label**, please assign [arangogutierrez](https://github.com/arangogutierrez) for approval. For more information see [the Kubernetes Code Review Process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process).

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fnode-feature-discovery).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/node-feature-discovery/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["arangogutierrez"]} -->
netlify[bot]: ### <span aria-hidden="true">👷</span> Deploy Preview for *kubernetes-sigs-nfd* processing.


|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | 6e62f620a3ee60d8820a8bd10d80a3f06d801c67 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/kubernetes-sigs-nfd/deploys/6749d7575c760900084cead0 |

Patch:
diff --git a/pkg/nfd-master/namespace-lister.go b/pkg/nfd-master/namespace-lister.go
index ea8b97a4c..1665a824d 100644
--- a/pkg/nfd-master/namespace-lister.go
+++ b/pkg/nfd-master/namespace-lister.go
@@ -16,6 +16,7 @@ limitations under the License.
 package nfdmaster
 
 import (
+	"fmt"
 	"time"
 
 	corev1 "k8s.io/api/core/v1"
@@ -23,6 +24,7 @@ import (
 	"k8s.io/client-go/informers"
 	k8sclient "k8s.io/client-go/kubernetes"
 	v1lister "k8s.io/client-go/listers/core/v1"
+	"k8s.io/klog/v2"
 )
 
 // NamespaceLister lists kubernetes namespaces.
@@ -32,19 +34,27 @@ type NamespaceLister struct {
 	stopChan        chan struct{}
 }
 
-func newNamespaceLister(k8sClient k8sclient.Interface, labelsSelector labels.Selector) *NamespaceLister {
+func newNamespaceLister(k8sClient k8sclient.Interface, labelsSelector labels.Selector) (*NamespaceLister, error) {
 	factory := informers.NewSharedInformerFactory(k8sClient, time.Hour)
 	namespaceLister := factory.Core().V1().Namespaces().Lister()
 
 	stopChan := make(chan struct{})
 	factory.Start(stopChan) // runs in background
-	factory.WaitForCacheSync(stopChan)
+
+	start := time.Now()
+	ret := factory.WaitForCacheSync(stopChan)
+	for res, ok := range ret {
+		if !ok {
+			return &NamespaceLister{}, fmt.Errorf("namespace informer cache failed to sync (%s)", res)
+		}
+	}
+	klog.InfoS("namespace informer cache synced", "duration", time.Since(start))
 
 	return &NamespaceLister{
 		namespaceLister: namespaceLister,
 		labelsSelector:  labelsSelector,
 		stopChan:        stopChan,
-	}
+	}, nil
 }
 
 // list returns all kubernetes namespaces.
diff --git a/pkg/nfd-master/nfd-api-controller.go b/pkg/nfd-master/nfd-api-controller.go
index 5ba38e588..a3870f489 100644
--- a/pkg/nfd-master/nfd-api-controller.go
+++ b/pkg/nfd-master/nfd-api-controller.go
@@ -78,7 +78,12 @@ func newNfdController(config *restclient.Config, nfdApiControllerOptions nfdApiC
 			klog.ErrorS(err, "failed to convert label selector to map", "selector", nfdApiControllerOptions.NodeFeatureNamespaceSelector)
 			return nil, err
 		}
-		c.namespaceLister = newNamespaceLister(nfdApiControllerOptions.K8sClient, labelMap)
+		c.namespaceLister, err = newNamespaceLister(nfdApiControllerOptions.K8sClient, labelMap)
+		if err != nil {
+			klog.ErrorS(err, "coudn't create namespace lister")
+			return nil, err
+		}
+
 	}
 
 	nfdClient := nfdclientset.NewForConfigOrDie(config)


Test patch:
diff --git a/pkg/nfd-master/nfd-api-controller_test.go b/pkg/nfd-master/nfd-api-controller_test.go
index a49c4939b..c7e217ec5 100644
--- a/pkg/nfd-master/nfd-api-controller_test.go
+++ b/pkg/nfd-master/nfd-api-controller_test.go
@@ -104,7 +104,9 @@ func TestIsNamespaceSelected(t *testing.T) {
 
 	for _, tc := range testcases {
 		labelMap, _ := metav1.LabelSelectorAsSelector(tc.nodeFeatureNamespaceSelector)
-		c.namespaceLister = newNamespaceLister(fakeCli, labelMap)
+		lister, err := newNamespaceLister(fakeCli, labelMap)
+		assert.Nil(t, err)
+		c.namespaceLister = lister
 		res := c.isNamespaceSelected(tc.objectNamespace)
 		assert.Equal(t, res, tc.expectedResult)
 	}