mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2024-12-14 11:57:51 +00:00
Merge pull request #1965 from ozhuraki/namespace-lister-check
nfd-master: check that namespace informer cache sync succeeded
This commit is contained in:
commit
1416072699
3 changed files with 22 additions and 5 deletions
|
@ -16,6 +16,7 @@ limitations under the License.
|
||||||
package nfdmaster
|
package nfdmaster
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
@ -23,6 +24,7 @@ import (
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
k8sclient "k8s.io/client-go/kubernetes"
|
k8sclient "k8s.io/client-go/kubernetes"
|
||||||
v1lister "k8s.io/client-go/listers/core/v1"
|
v1lister "k8s.io/client-go/listers/core/v1"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NamespaceLister lists kubernetes namespaces.
|
// NamespaceLister lists kubernetes namespaces.
|
||||||
|
@ -32,19 +34,27 @@ type NamespaceLister struct {
|
||||||
stopChan chan 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)
|
factory := informers.NewSharedInformerFactory(k8sClient, time.Hour)
|
||||||
namespaceLister := factory.Core().V1().Namespaces().Lister()
|
namespaceLister := factory.Core().V1().Namespaces().Lister()
|
||||||
|
|
||||||
stopChan := make(chan struct{})
|
stopChan := make(chan struct{})
|
||||||
factory.Start(stopChan) // runs in background
|
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{
|
return &NamespaceLister{
|
||||||
namespaceLister: namespaceLister,
|
namespaceLister: namespaceLister,
|
||||||
labelsSelector: labelsSelector,
|
labelsSelector: labelsSelector,
|
||||||
stopChan: stopChan,
|
stopChan: stopChan,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// list returns all kubernetes namespaces.
|
// list returns all kubernetes namespaces.
|
||||||
|
|
|
@ -78,7 +78,12 @@ func newNfdController(config *restclient.Config, nfdApiControllerOptions nfdApiC
|
||||||
klog.ErrorS(err, "failed to convert label selector to map", "selector", nfdApiControllerOptions.NodeFeatureNamespaceSelector)
|
klog.ErrorS(err, "failed to convert label selector to map", "selector", nfdApiControllerOptions.NodeFeatureNamespaceSelector)
|
||||||
return nil, err
|
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)
|
nfdClient := nfdclientset.NewForConfigOrDie(config)
|
||||||
|
|
|
@ -104,7 +104,9 @@ func TestIsNamespaceSelected(t *testing.T) {
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
labelMap, _ := metav1.LabelSelectorAsSelector(tc.nodeFeatureNamespaceSelector)
|
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)
|
res := c.isNamespaceSelected(tc.objectNamespace)
|
||||||
assert.Equal(t, res, tc.expectedResult)
|
assert.Equal(t, res, tc.expectedResult)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue