mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
688b4fb8e3
* add package logger in files Signed-off-by: damilola olayinka <holayinkajr@gmail.com> * add package logger to initContainer and other files Signed-off-by: damilola olayinka <holayinkajr@gmail.com> * helm docs Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> * helm default values Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> * release notes Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Signed-off-by: damilola olayinka <holayinkajr@gmail.com> Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com> Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package engine
|
|
|
|
import (
|
|
"time"
|
|
|
|
kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1"
|
|
"github.com/kyverno/kyverno/pkg/autogen"
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
"github.com/kyverno/kyverno/pkg/logging"
|
|
"k8s.io/client-go/tools/cache"
|
|
)
|
|
|
|
// GenerateResponse checks for validity of generate rule on the resource
|
|
func GenerateResponse(policyContext *PolicyContext, gr kyvernov1beta1.UpdateRequest) (resp *response.EngineResponse) {
|
|
policyStartTime := time.Now()
|
|
return filterGenerateRules(policyContext, gr.Spec.Policy, policyStartTime)
|
|
}
|
|
|
|
func filterGenerateRules(policyContext *PolicyContext, policyNameKey string, startTime time.Time) *response.EngineResponse {
|
|
kind := policyContext.NewResource.GetKind()
|
|
name := policyContext.NewResource.GetName()
|
|
namespace := policyContext.NewResource.GetNamespace()
|
|
apiVersion := policyContext.NewResource.GetAPIVersion()
|
|
pNamespace, pName, err := cache.SplitMetaNamespaceKey(policyNameKey)
|
|
if err != nil {
|
|
logging.Error(err, "failed to spilt name and namespace", policyNameKey)
|
|
}
|
|
|
|
resp := &response.EngineResponse{
|
|
PolicyResponse: response.PolicyResponse{
|
|
Policy: response.PolicySpec{
|
|
Name: pName,
|
|
Namespace: pNamespace,
|
|
},
|
|
PolicyStats: response.PolicyStats{
|
|
PolicyExecutionTimestamp: startTime.Unix(),
|
|
},
|
|
Resource: response.ResourceSpec{
|
|
Kind: kind,
|
|
Name: name,
|
|
Namespace: namespace,
|
|
APIVersion: apiVersion,
|
|
},
|
|
},
|
|
}
|
|
|
|
if policyContext.ExcludeResourceFunc(kind, namespace, name) {
|
|
logging.WithName("Generate").Info("resource excluded", "kind", kind, "namespace", namespace, "name", name)
|
|
return resp
|
|
}
|
|
|
|
for _, rule := range autogen.ComputeRules(policyContext.Policy) {
|
|
if ruleResp := filterRule(rule, policyContext); ruleResp != nil {
|
|
resp.PolicyResponse.Rules = append(resp.PolicyResponse.Rules, *ruleResp)
|
|
}
|
|
}
|
|
|
|
return resp
|
|
}
|