Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nfd-master: check that namespace informer cache sync succeeded #1965

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/nfd-master/namespace-lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ limitations under the License.
package nfdmaster

import (
"fmt"
"time"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"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.
Expand All @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion pkg/nfd-master/nfd-api-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/nfd-master/nfd-api-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down