1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00
kyverno/pkg/metrics/parsers.go
Charles-Edouard Brétéché d2658a1bc8
refactor: support Audit and Enforce validation failure actions (#5152)
* feat: remove policy mutation code

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* refactor: support Audit and Enforce failure actions

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* codegen

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* fix

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* typo

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* update changelog

Signed-off-by: ShutingZhao <shuting@nirmata.com>

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
Signed-off-by: ShutingZhao <shuting@nirmata.com>
Co-authored-by: shuting <shuting@nirmata.com>
Co-authored-by: Vyankatesh Kudtarkar <vyankateshkd@gmail.com>
2022-11-01 09:56:52 +00:00

77 lines
2.1 KiB
Go

package metrics
import (
"fmt"
"reflect"
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/engine/response"
)
func ParsePolicyValidationMode(validationFailureAction kyvernov1.ValidationFailureAction) (PolicyValidationMode, error) {
if validationFailureAction.Enforce() {
return Enforce, nil
}
return Audit, nil
}
func ParsePolicyBackgroundMode(policy kyvernov1.PolicyInterface) PolicyBackgroundMode {
if policy.BackgroundProcessingEnabled() {
return BackgroundTrue
}
return BackgroundFalse
}
func ParseRuleType(rule kyvernov1.Rule) RuleType {
if !reflect.DeepEqual(rule.Validation, kyvernov1.Validation{}) {
return Validate
}
if !reflect.DeepEqual(rule.Mutation, kyvernov1.Mutation{}) {
return Mutate
}
if !reflect.DeepEqual(rule.Generation, kyvernov1.Generation{}) {
return Generate
}
return EmptyRuleType
}
func ParseResourceRequestOperation(requestOperationStr string) (ResourceRequestOperation, error) {
switch requestOperationStr {
case "CREATE":
return ResourceCreated, nil
case "UPDATE":
return ResourceUpdated, nil
case "DELETE":
return ResourceDeleted, nil
case "CONNECT":
return ResourceConnected, nil
default:
return "", fmt.Errorf("unknown request operation made by resource: %s. Allowed requests: 'CREATE', 'UPDATE', 'DELETE', 'CONNECT'", requestOperationStr)
}
}
func ParseRuleTypeFromEngineRuleResponse(rule response.RuleResponse) RuleType {
switch rule.Type {
case "Validation":
return Validate
case "Mutation":
return Mutate
case "Generation":
return Generate
default:
return EmptyRuleType
}
}
func GetPolicyInfos(policy kyvernov1.PolicyInterface) (string, string, PolicyType, PolicyBackgroundMode, PolicyValidationMode, error) {
name := policy.GetName()
namespace := ""
policyType := Cluster
if policy.IsNamespaced() {
namespace = policy.GetNamespace()
policyType = Namespaced
}
backgroundMode := ParsePolicyBackgroundMode(policy)
validationMode, err := ParsePolicyValidationMode(policy.GetSpec().ValidationFailureAction)
return name, namespace, policyType, backgroundMode, validationMode, err
}