1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-01-20 18:52:16 +00:00
kyverno/pkg/resourcecache/main.go
Kumar Mallikarjuna e39489f838
SharedInformers for WebhookConfigurations (#3007)
* SharedInformers for WebhookConfigurations

Signed-off-by: Kumar Mallikarjuna <kumar@nirmata.com>

* Add GVK to typed resources

Signed-off-by: Kumar Mallikarjuna <kumar@nirmata.com>

* Remove ToUnstructured()

Signed-off-by: Kumar Mallikarjuna <kumar@nirmata.com>

* Remove default informers from Resource Cache

Signed-off-by: Kumar Mallikarjuna <kumar@nirmata.com>

* Formatted files

Signed-off-by: Kumar Mallikarjuna <kumar@nirmata.com>
2022-01-19 15:57:32 +00:00

53 lines
1.5 KiB
Go

package resourcecache
import (
"fmt"
"github.com/go-logr/logr"
dclient "github.com/kyverno/kyverno/pkg/dclient"
cmap "github.com/orcaman/concurrent-map"
"k8s.io/client-go/dynamic/dynamicinformer"
)
// ResourceCache - allows the creation, deletion and saving the resource informers as a cache
// the resource cache can be registered by gvks as follows:
// - group/version/kind
// - group/kind
// - kind
type ResourceCache interface {
CreateInformers(gvks ...string) []error
CreateGVKInformer(gvk string) (GenericCache, error)
StopResourceInformer(gvk string)
GetGVRCache(gvk string) (GenericCache, bool)
}
type resourceCache struct {
dclient *dclient.Client
dinformer dynamicinformer.DynamicSharedInformerFactory
// gvrCache - stores the manipulate factory for a resource
// it uses resource name as key (i.e., namespaces for Namespace, pods for Pod, clusterpolicies for ClusterPolicy, etc)
gvrCache cmap.ConcurrentMap
log logr.Logger
}
var KyvernoDefaultInformer = []string{}
// NewResourceCache - initializes the ResourceCache
func NewResourceCache(dclient *dclient.Client, dInformer dynamicinformer.DynamicSharedInformerFactory, logger logr.Logger) (ResourceCache, error) {
rCache := &resourceCache{
dclient: dclient,
gvrCache: cmap.New(),
dinformer: dInformer,
log: logger,
}
errs := rCache.CreateInformers(KyvernoDefaultInformer...)
if len(errs) != 0 {
return rCache, fmt.Errorf("failed to register default informers %v", errs)
}
return rCache, nil
}