1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/response/response.go

134 lines
3.4 KiB
Go
Raw Normal View History

package response
2019-08-23 18:34:23 -07:00
import (
"fmt"
"time"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
2019-10-08 10:57:24 -07:00
//EngineResponse engine response to the action
type EngineResponse struct {
2019-08-23 18:34:23 -07:00
// Resource patched with the engine action changes
PatchedResource unstructured.Unstructured
// Policy Response
PolicyResponse PolicyResponse
}
//PolicyResponse policy application response
type PolicyResponse struct {
// policy name
2019-08-29 11:44:50 -07:00
Policy string `json:"policy"`
2019-08-23 18:34:23 -07:00
// resource details
2019-08-29 11:44:50 -07:00
Resource ResourceSpec `json:"resource"`
2019-08-23 18:34:23 -07:00
// policy statistics
PolicyStats `json:",inline"`
2019-08-23 18:34:23 -07:00
// rule response
2019-08-29 11:44:50 -07:00
Rules []RuleResponse `json:"rules"`
// ValidationFailureAction: audit(default if not set),enforce
2019-08-23 18:34:23 -07:00
ValidationFailureAction string
}
//ResourceSpec resource action applied on
type ResourceSpec struct {
//TODO: support ApiVersion
2019-08-29 11:44:50 -07:00
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Namespace string `json:"namespace"`
Name string `json:"name"`
2019-08-23 18:34:23 -07:00
}
//GetKey returns the key
func (rs ResourceSpec) GetKey() string {
return rs.Kind + "/" + rs.Namespace + "/" + rs.Name
}
2019-08-23 18:34:23 -07:00
//PolicyStats stores statistics for the single policy application
type PolicyStats struct {
// time required to process the policy rules on a resource
2019-08-29 11:44:50 -07:00
ProcessingTime time.Duration `json:"processingTime"`
2019-08-23 18:34:23 -07:00
// Count of rules that were applied succesfully
2019-08-29 11:44:50 -07:00
RulesAppliedCount int `json:"rulesAppliedCount"`
2019-08-23 18:34:23 -07:00
}
//RuleResponse details for each rule applicatino
type RuleResponse struct {
// rule name specified in policy
2019-08-29 11:44:50 -07:00
Name string `json:"name"`
2019-08-23 18:34:23 -07:00
// rule type (Mutation,Generation,Validation) for Kyverno Policy
2019-08-29 11:44:50 -07:00
Type string `json:"type"`
2019-08-23 18:34:23 -07:00
// message response from the rule application
2019-08-29 11:44:50 -07:00
Message string `json:"message"`
2019-08-23 18:34:23 -07:00
// JSON patches, for mutation rules
2019-08-29 11:44:50 -07:00
Patches [][]byte `json:"patches,omitempty"`
2019-08-23 18:34:23 -07:00
// success/fail
2019-08-29 11:44:50 -07:00
Success bool `json:"success"`
2019-08-23 18:34:23 -07:00
// statistics
RuleStats `json:",inline"`
// PathNotPresent indicates whether referenced path in variable substitution exist
PathNotPresent bool `json:"pathNotPresent"`
2019-08-23 18:34:23 -07:00
}
//ToString ...
func (rr RuleResponse) ToString() string {
return fmt.Sprintf("rule %s (%s): %v", rr.Name, rr.Type, rr.Message)
}
//RuleStats stores the statisctis for the single rule application
type RuleStats struct {
// time required to appliy the rule on the resource
2019-08-29 11:44:50 -07:00
ProcessingTime time.Duration `json:"processingTime"`
2019-08-23 18:34:23 -07:00
}
//IsSuccesful checks if any rule has failed or not
2019-10-08 10:57:24 -07:00
func (er EngineResponse) IsSuccesful() bool {
2019-08-23 18:34:23 -07:00
for _, r := range er.PolicyResponse.Rules {
if !r.Success {
return false
}
}
return true
}
//GetPatches returns all the patches joined
2019-10-08 10:57:24 -07:00
func (er EngineResponse) GetPatches() [][]byte {
2019-08-23 18:34:23 -07:00
var patches [][]byte
for _, r := range er.PolicyResponse.Rules {
if r.Patches != nil {
patches = append(patches, r.Patches...)
}
}
// join patches
2019-08-26 16:10:19 -07:00
return patches
2019-08-23 18:34:23 -07:00
}
2019-08-26 13:34:42 -07:00
//GetFailedRules returns failed rules
2019-10-08 10:57:24 -07:00
func (er EngineResponse) GetFailedRules() []string {
2019-08-26 13:34:42 -07:00
return er.getRules(false)
}
//GetSuccessRules returns success rules
2019-10-08 10:57:24 -07:00
func (er EngineResponse) GetSuccessRules() []string {
2019-08-26 13:34:42 -07:00
return er.getRules(true)
}
2019-10-08 10:57:24 -07:00
func (er EngineResponse) getRules(success bool) []string {
2019-08-26 13:34:42 -07:00
var rules []string
for _, r := range er.PolicyResponse.Rules {
if r.Success == success {
rules = append(rules, r.Name)
}
}
return rules
}
// IsPathNotPresent checks if the referenced path(in variable substitution) exist
func (er EngineResponse) IsPathNotPresent() bool {
for _, r := range er.PolicyResponse.Rules {
if r.PathNotPresent {
return true
}
}
return false
}