2020-09-23 02:41:49 +05:30
|
|
|
package resourcecache
|
|
|
|
|
|
|
|
import (
|
2021-02-05 09:58:10 -08:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-23 02:41:49 +05:30
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
dclient "github.com/kyverno/kyverno/pkg/dclient"
|
2021-01-29 17:38:23 -08:00
|
|
|
cmap "github.com/orcaman/concurrent-map"
|
2020-09-23 02:41:49 +05:30
|
|
|
"k8s.io/client-go/dynamic/dynamicinformer"
|
|
|
|
)
|
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
// ResourceCache - allows the creation, deletion and saving the resource informers as a cache
|
|
|
|
type ResourceCache interface {
|
|
|
|
CreateInformers(resources ...string) []error
|
2021-03-05 06:15:52 +05:30
|
|
|
CreateGVKInformer(kind string) (GenericCache, error)
|
2021-01-29 17:38:23 -08:00
|
|
|
StopResourceInformer(resource string)
|
|
|
|
GetGVRCache(resource string) (GenericCache, bool)
|
2020-09-23 02:41:49 +05:30
|
|
|
}
|
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
type resourceCache struct {
|
|
|
|
dclient *dclient.Client
|
2020-09-23 02:41:49 +05:30
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
dinformer dynamicinformer.DynamicSharedInformerFactory
|
2020-09-23 02:41:49 +05:30
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
// 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
|
2020-11-30 12:54:48 -08:00
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
log logr.Logger
|
2020-11-30 12:54:48 -08:00
|
|
|
}
|
|
|
|
|
2021-02-05 09:58:10 -08:00
|
|
|
var KyvernoDefaultInformer = []string{"ConfigMap", "Secret", "Deployment", "MutatingWebhookConfiguration", "ValidatingWebhookConfiguration"}
|
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
// 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,
|
2020-09-23 02:41:49 +05:30
|
|
|
}
|
2020-11-30 12:54:48 -08:00
|
|
|
|
2021-02-05 09:58:10 -08:00
|
|
|
errs := rCache.CreateInformers(KyvernoDefaultInformer...)
|
|
|
|
if len(errs) != 0 {
|
|
|
|
return rCache, fmt.Errorf("failed to register default informers %v", errs)
|
2020-09-23 02:41:49 +05:30
|
|
|
}
|
2020-11-30 12:54:48 -08:00
|
|
|
|
2021-01-29 17:38:23 -08:00
|
|
|
return rCache, nil
|
2020-09-23 02:41:49 +05:30
|
|
|
}
|