mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-28 10:28:36 +00:00
engineResponse to contain stats
This commit is contained in:
parent
61d7ea276a
commit
0b5cc03b2d
5 changed files with 45 additions and 27 deletions
|
@ -18,19 +18,20 @@ import (
|
|||
)
|
||||
|
||||
//Generate apply generation rules on a resource
|
||||
func Generate(client *client.Client, policy kyverno.Policy, ns unstructured.Unstructured) []info.RuleInfo {
|
||||
func Generate(client *client.Client, policy kyverno.Policy, ns unstructured.Unstructured) EngineResponse {
|
||||
var response EngineResponse
|
||||
var executionTime time.Duration
|
||||
var rulesAppliedCount int
|
||||
startTime := time.Now()
|
||||
glog.V(4).Infof("started applying generation rules of policy %q (%v)", policy.Name, startTime)
|
||||
defer func() {
|
||||
executionTime = time.Since(startTime)
|
||||
glog.V(4).Infof("Finished applying generation rules policy %q (%v)", policy.Name, executionTime)
|
||||
glog.V(4).Infof("Generation Rules appplied succesfully count %q for policy %q", rulesAppliedCount, policy.Name)
|
||||
response.ExecutionTime = time.Since(startTime)
|
||||
glog.V(4).Infof("Finished applying generation rules policy %q (%v)", policy.Name, response.ExecutionTime)
|
||||
glog.V(4).Infof("Mutation Rules appplied succesfully count %q for policy %q", response.RulesAppliedCount, policy.Name)
|
||||
}()
|
||||
succesfulRuleCount := func() {
|
||||
// rules applied succesfully count
|
||||
rulesAppliedCount++
|
||||
response.RulesAppliedCount++
|
||||
}
|
||||
|
||||
ris := []info.RuleInfo{}
|
||||
|
@ -52,7 +53,8 @@ func Generate(client *client.Client, policy kyverno.Policy, ns unstructured.Unst
|
|||
}
|
||||
ris = append(ris, ri)
|
||||
}
|
||||
return ris
|
||||
response.RuleInfos = ris
|
||||
return response
|
||||
}
|
||||
|
||||
func applyRuleGenerator(client *client.Client, ns unstructured.Unstructured, gen kyverno.Generation, policyCreationTime metav1.Time) error {
|
||||
|
|
|
@ -13,22 +13,23 @@ import (
|
|||
// Mutate performs mutation. Overlay first and then mutation patches
|
||||
|
||||
func Mutate(policy kyverno.Policy, resource unstructured.Unstructured) EngineResponse {
|
||||
var response EngineResponse
|
||||
var allPatches, rulePatches [][]byte
|
||||
var err error
|
||||
var errs []error
|
||||
ris := []info.RuleInfo{}
|
||||
var executionTime time.Duration
|
||||
var rulesAppliedCount int
|
||||
startTime := time.Now()
|
||||
glog.V(4).Infof("started applying mutation rules of policy %q (%v)", policy.Name, startTime)
|
||||
defer func() {
|
||||
executionTime = time.Since(startTime)
|
||||
glog.V(4).Infof("Finished applying mutation rules policy %q (%v)", policy.Name, executionTime)
|
||||
glog.V(4).Infof("Mutation Rules appplied succesfully count %q for policy %q", rulesAppliedCount, policy.Name)
|
||||
response.ExecutionTime = time.Since(startTime)
|
||||
glog.V(4).Infof("Finished applying mutation rules policy %q (%v)", policy.Name, response.ExecutionTime)
|
||||
glog.V(4).Infof("Mutation Rules appplied succesfully count %q for policy %q", response.RulesAppliedCount, policy.Name)
|
||||
}()
|
||||
succesfulRuleCount := func() {
|
||||
// rules applied succesfully count
|
||||
rulesAppliedCount++
|
||||
response.RulesAppliedCount++
|
||||
}
|
||||
|
||||
patchedDocument, err := resource.MarshalJSON()
|
||||
|
@ -38,7 +39,8 @@ func Mutate(policy kyverno.Policy, resource unstructured.Unstructured) EngineRes
|
|||
|
||||
if err != nil {
|
||||
glog.V(4).Infof("unable to marshal resource : %v", err)
|
||||
return EngineResponse{PatchedResource: resource}
|
||||
response.PatchedResource = resource
|
||||
return response
|
||||
}
|
||||
|
||||
for _, rule := range policy.Spec.Rules {
|
||||
|
@ -114,12 +116,12 @@ func Mutate(policy kyverno.Policy, resource unstructured.Unstructured) EngineRes
|
|||
patchedResource, err := ConvertToUnstructured(patchedDocument)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to convert patched resource to unstructuredtype, err%v\n:", err)
|
||||
return EngineResponse{PatchedResource: resource}
|
||||
response.PatchedResource = resource
|
||||
return response
|
||||
}
|
||||
|
||||
return EngineResponse{
|
||||
Patches: allPatches,
|
||||
PatchedResource: *patchedResource,
|
||||
RuleInfos: ris,
|
||||
}
|
||||
response.Patches = allPatches
|
||||
response.PatchedResource = *patchedResource
|
||||
response.RuleInfos = ris
|
||||
return response
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
|
@ -18,10 +19,20 @@ import (
|
|||
"k8s.io/apimachinery/pkg/labels"
|
||||
)
|
||||
|
||||
//EngineResponse provides the response to the application of a policy rule set on a resource
|
||||
type EngineResponse struct {
|
||||
Patches [][]byte
|
||||
PatchedResource unstructured.Unstructured
|
||||
RuleInfos []info.RuleInfo
|
||||
EngineStats
|
||||
}
|
||||
|
||||
//EngineStats stores in the statistics for a single application of resource
|
||||
type EngineStats struct {
|
||||
// average time required to process the policy rules on a resource
|
||||
ExecutionTime time.Duration
|
||||
// Count of rules that were applied succesfully
|
||||
RulesAppliedCount int
|
||||
}
|
||||
|
||||
// //ListResourcesThatApplyToPolicy returns list of resources that are filtered by policy rules
|
||||
|
|
|
@ -19,29 +19,32 @@ import (
|
|||
// Validate handles validating admission request
|
||||
// Checks the target resources for rules defined in the policy
|
||||
func Validate(policy kyverno.Policy, resource unstructured.Unstructured) EngineResponse {
|
||||
var response EngineResponse
|
||||
var executionTime time.Duration
|
||||
var rulesAppliedCount int
|
||||
startTime := time.Now()
|
||||
glog.V(4).Infof("started applying validation rules of policy %q (%v)", policy.Name, startTime)
|
||||
defer func() {
|
||||
executionTime = time.Since(startTime)
|
||||
glog.V(4).Infof("Finished applying validation rules policy %q (%v)", policy.Name, executionTime)
|
||||
glog.V(4).Infof("Validation Rules appplied succesfully count %q for policy %q", rulesAppliedCount, policy.Name)
|
||||
response.ExecutionTime = time.Since(startTime)
|
||||
glog.V(4).Infof("Finished applying mutation rules policy %q (%v)", policy.Name, response.ExecutionTime)
|
||||
glog.V(4).Infof("Mutation Rules appplied succesfully count %q for policy %q", response.RulesAppliedCount, policy.Name)
|
||||
}()
|
||||
succesfulRuleCount := func() {
|
||||
// rules applied succesfully count
|
||||
rulesAppliedCount++
|
||||
response.RulesAppliedCount++
|
||||
}
|
||||
resourceRaw, err := resource.MarshalJSON()
|
||||
if err != nil {
|
||||
glog.V(4).Infof("Skip processing validating rule, unable to marshal resource : %v\n", err)
|
||||
return EngineResponse{PatchedResource: resource}
|
||||
response.PatchedResource = resource
|
||||
return response
|
||||
}
|
||||
|
||||
var resourceInt interface{}
|
||||
if err := json.Unmarshal(resourceRaw, &resourceInt); err != nil {
|
||||
glog.V(4).Infof("unable to unmarshal resource : %v\n", err)
|
||||
return EngineResponse{PatchedResource: resource}
|
||||
response.PatchedResource = resource
|
||||
return response
|
||||
}
|
||||
|
||||
var ruleInfos []info.RuleInfo
|
||||
|
@ -73,8 +76,8 @@ func Validate(policy kyverno.Policy, resource unstructured.Unstructured) EngineR
|
|||
}
|
||||
ruleInfos = append(ruleInfos, ruleInfo)
|
||||
}
|
||||
|
||||
return EngineResponse{RuleInfos: ruleInfos}
|
||||
response.RuleInfos = ruleInfos
|
||||
return response
|
||||
}
|
||||
|
||||
// validateResourceWithPattern is a start of element-by-element validation process
|
||||
|
|
|
@ -148,8 +148,8 @@ func applyPolicy(client *client.Client, resource unstructured.Unstructured, poli
|
|||
glog.V(4).Infof("Finished applying %s on resource %s/%s/%s (%v)", policy.Name, resource.GetKind(), resource.GetNamespace(), resource.GetName(), time.Since(startTime))
|
||||
}()
|
||||
policyInfo := info.NewPolicyInfo(policy.Name, resource.GetKind(), resource.GetName(), resource.GetNamespace(), policy.Spec.ValidationFailureAction)
|
||||
ruleInfos := engine.Generate(client, policy, resource)
|
||||
policyInfo.AddRuleInfos(ruleInfos)
|
||||
engineResponse := engine.Generate(client, policy, resource)
|
||||
policyInfo.AddRuleInfos(engineResponse.RuleInfos)
|
||||
|
||||
return policyInfo
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue