mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
e9952fbaf2
Signed-off-by: Shuting Zhao <shutting06@gmail.com>
53 lines
1.6 KiB
Go
53 lines
1.6 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{"ConfigMap", "Deployment", "MutatingWebhookConfiguration", "ValidatingWebhookConfiguration"}
|
|
|
|
// 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
|
|
}
|