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

171 lines
4.2 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 details
Policy PolicySpec `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"`
Feature/cosign (#2078) * add image verification * inline policy list Signed-off-by: Jim Bugwadia <jim@nirmata.com> * cosign version and dependencies updates Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add registry initialization Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add build tag to exclude k8schain for cloud providers Signed-off-by: Jim Bugwadia <jim@nirmata.com> * generate deep copy and other fixtures Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix deep copy issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * mutate images to add digest Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add certificates to Kyverno container for HTTPS lookups Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align flag syntax Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update docs Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update dependencies Signed-off-by: Jim Bugwadia <jim@nirmata.com> * patch image with digest and fix checks Signed-off-by: Jim Bugwadia <jim@nirmata.com> * hardcode image for demos Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add default registry (docker.io) before calling reference.Parse Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix definition Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase webhook timeout Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix args Signed-off-by: Jim Bugwadia <jim@nirmata.com> * run gofmt Signed-off-by: Jim Bugwadia <jim@nirmata.com> * rename for clarity Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix HasImageVerify check Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * align make test commands Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix linter error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle API conflict and retry Signed-off-by: Jim Bugwadia <jim@nirmata.com> * format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix reviewdog issues Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix make for unit tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * improve error message Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix durations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * handle errors in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * print policy name Signed-off-by: Jim Bugwadia <jim@nirmata.com> * update tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add retries and duration to error log Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix time check in tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * round creation times in test Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix retry loop Signed-off-by: Jim Bugwadia <jim@nirmata.com> * remove timing check for policy creation Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix e2e error - policy not found Signed-off-by: Shuting Zhao <shutting06@gmail.com> * update string comparison method Signed-off-by: Shuting Zhao <shutting06@gmail.com> * fix test Generate_Namespace_Label_Actions Signed-off-by: Shuting Zhao <shutting06@gmail.com> * add debug info for e2e tests Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix error Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix generate bug Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix format Signed-off-by: Jim Bugwadia <jim@nirmata.com> * add check for update operations Signed-off-by: Jim Bugwadia <jim@nirmata.com> * increase time for deleteing a resource Signed-off-by: Jim Bugwadia <jim@nirmata.com> * fix check Signed-off-by: Jim Bugwadia <jim@nirmata.com> Co-authored-by: Shuting Zhao <shutting06@gmail.com>
2021-07-09 18:01:46 -07:00
// ValidationFailureAction: audit (default) or enforce
2019-08-23 18:34:23 -07:00
ValidationFailureAction string
}
//PolicySpec policy
type PolicySpec struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
}
2019-08-23 18:34:23 -07:00
//ResourceSpec resource action applied on
type ResourceSpec struct {
2019-08-29 11:44:50 -07:00
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Namespace string `json:"namespace"`
Name string `json:"name"`
// UID is not used to build the unique identifier
// optional
UID string `json:"uid"`
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 {
2019-08-23 18:34:23 -07:00
// time required to process the policy rules on a resource
2019-08-29 11:44:50 -07:00
ProcessingTime time.Duration `json:"processingTime"`
// Count of rules that were applied successfully
2019-08-29 11:44:50 -07:00
RulesAppliedCount int `json:"rulesAppliedCount"`
// Count of rules that with execution errors
RulesErrorCount int `json:"rulesErrorCount"`
// Timestamp of the instant the Policy was triggered
PolicyExecutionTimestamp int64 `json:"policyExecutionTimestamp"`
2019-08-23 18:34:23 -07:00
}
//RuleResponse details for each rule application
2019-08-23 18:34:23 -07:00
type RuleResponse struct {
2019-08-23 18:34:23 -07:00
// 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"`
// rule status
Status RuleStatus `json:"status"`
2019-08-23 18:34:23 -07:00
// statistics
RuleStats `json:",inline"`
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 statistics for the single rule application
2019-08-23 18:34:23 -07:00
type RuleStats struct {
// time required to apply the rule on the resource
2019-08-29 11:44:50 -07:00
ProcessingTime time.Duration `json:"processingTime"`
// Timestamp of the instant the rule got triggered
RuleExecutionTimestamp int64 `json:"ruleExecutionTimestamp"`
2019-08-23 18:34:23 -07:00
}
//IsSuccessful checks if any rule has failed or not
func (er EngineResponse) IsSuccessful() bool {
2019-08-23 18:34:23 -07:00
for _, r := range er.PolicyResponse.Rules {
if r.Status == RuleStatusFail || r.Status == RuleStatusError {
2019-08-23 18:34:23 -07:00
return false
}
}
2019-08-23 18:34:23 -07:00
return true
}
//IsFailed checks if any rule has succeeded or not
func (er EngineResponse) IsFailed() bool {
for _, r := range er.PolicyResponse.Rules {
if r.Status == RuleStatusFail {
return true
}
}
return false
}
2019-08-23 18:34:23 -07:00
//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...)
}
}
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 {
return er.getRules(RuleStatusFail)
2019-08-26 13:34:42 -07:00
}
//GetSuccessRules returns success rules
2019-10-08 10:57:24 -07:00
func (er EngineResponse) GetSuccessRules() []string {
return er.getRules(RuleStatusPass)
2019-08-26 13:34:42 -07:00
}
// GetResourceSpec returns resourceSpec of er
func (er EngineResponse) GetResourceSpec() ResourceSpec {
return ResourceSpec{
Kind: er.PatchedResource.GetKind(),
APIVersion: er.PatchedResource.GetAPIVersion(),
Namespace: er.PatchedResource.GetNamespace(),
Name: er.PatchedResource.GetName(),
UID: string(er.PatchedResource.GetUID()),
}
}
func (er EngineResponse) getRules(status RuleStatus) []string {
2019-08-26 13:34:42 -07:00
var rules []string
for _, r := range er.PolicyResponse.Rules {
if r.Status == status {
2019-08-26 13:34:42 -07:00
rules = append(rules, r.Name)
}
}
2019-08-26 13:34:42 -07:00
return rules
}