Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
realshuting committed Nov 30, 2020
1 parent 2344b2c commit 6e1be1c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
8 changes: 3 additions & 5 deletions cmd/kyverno/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,12 @@ func main() {
os.Exit(1)
}

// ======================= resource cache ====================
rCache, err := resourcecache.NewResourceCache(log.Log, clientConfig, client, []string{"configmaps"}, []string{})
if err != nil {
setupLog.Error(err, "Failed to create resource cache")
os.Exit(1)
setupLog.Error(err, "ConfigMap lookup disabled: failed to create resource cache")
} else {
rCache.RunAllInformers(log.Log)
}
rCache.RunAllInformers(log.Log)
// ===========================================================

// CRD CHECK
// - verify if Kyverno CRDs are available
Expand Down
1 change: 1 addition & 0 deletions pkg/policy/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func applyPolicy(policy kyverno.ClusterPolicy, resource unstructured.Unstructure
//TODO: GENERATION
return engineResponses
}

func mutation(policy kyverno.ClusterPolicy, resource unstructured.Unstructured, ctx context.EvalInterface, log logr.Logger, resCache resourcecache.ResourceCacheIface, jsonContext *context.Context) (response.EngineResponse, error) {

engineResponse := engine.Mutate(engine.PolicyContext{Policy: policy, NewResource: resource, Context: ctx, ResourceCache: resCache, JSONContext: jsonContext})
Expand Down
32 changes: 26 additions & 6 deletions pkg/resourcecache/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package resourcecache

import (
// "fmt"
// "time"
"github.com/go-logr/logr"
dclient "github.com/kyverno/kyverno/pkg/dclient"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -47,19 +45,40 @@ func NewResourceCache(log logr.Logger, config *rest.Config, dclient *dclient.Cli

resCache := &ResourceCache{GVRCacheData: cacheData, dinformer: dInformer, match: match, exclude: exclude}

err := udateGVRCache(logger, resCache, discoveryIface)
if err != nil {
logger.Error(err, "error in udateGVRCache function")
return nil, err
if resCache.matchGVRKey("configmaps") {
_, ok := resCache.GVRCacheData["configmaps"]
if !ok {
updateGVRCacheForConfigMap(resCache)
}
} else {
err := udateGVRCache(logger, resCache, discoveryIface)
if err != nil {
logger.Error(err, "error in udateGVRCache function")
return nil, err
}
}

return resCache, nil
}

func updateGVRCacheForConfigMap(resc *ResourceCache) {
gvrc := &GVRCache{
GVR: schema.GroupVersionResource{
Version: "v1",
Resource: "configmaps",
},
Namespaced: true,
}

resc.GVRCacheData["configmaps"] = gvrc
}

func udateGVRCache(log logr.Logger, resc *ResourceCache, discoveryIface discovery.CachedDiscoveryInterface) error {
serverResources, err := discoveryIface.ServerPreferredResources()
if err != nil {
return err
}

for _, serverResource := range serverResources {
groupVersion := serverResource.GroupVersion
for _, resource := range serverResource.APIResources {
Expand All @@ -82,5 +101,6 @@ func udateGVRCache(log logr.Logger, resc *ResourceCache, discoveryIface discover
}
}
}

return nil
}

0 comments on commit 6e1be1c

Please sign in to comment.