mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
fb9c66f455
* feat(perf): add new linter prealloc to enforce slice declarations best practice Signed-off-by: ShutingZhao <shuting@nirmata.com> * fix(linter): prealloac slices Signed-off-by: ShutingZhao <shuting@nirmata.com> --------- Signed-off-by: ShutingZhao <shuting@nirmata.com>
33 lines
890 B
Go
33 lines
890 B
Go
package informers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-logr/logr"
|
|
"k8s.io/client-go/tools/cache"
|
|
)
|
|
|
|
type informer interface {
|
|
Informer() cache.SharedIndexInformer
|
|
}
|
|
|
|
func StartInformers(ctx context.Context, informers ...informer) {
|
|
for i := range informers {
|
|
go func(informer cache.SharedIndexInformer) {
|
|
informer.Run(ctx.Done())
|
|
}(informers[i].Informer())
|
|
}
|
|
}
|
|
|
|
func WaitForCacheSync(ctx context.Context, logger logr.Logger, informers ...informer) bool {
|
|
cacheSyncs := make([]cache.InformerSynced, 0, len(informers))
|
|
for i := range informers {
|
|
cacheSyncs = append(cacheSyncs, informers[i].Informer().HasSynced)
|
|
}
|
|
return cache.WaitForCacheSync(ctx.Done(), cacheSyncs...)
|
|
}
|
|
|
|
func StartInformersAndWaitForCacheSync(ctx context.Context, logger logr.Logger, informers ...informer) bool {
|
|
StartInformers(ctx, informers...)
|
|
return WaitForCacheSync(ctx, logger, informers...)
|
|
}
|