mirror of
https://github.com/kyverno/kyverno.git
synced 2025-04-16 01:07:14 +00:00
refactor
This commit is contained in:
parent
5cee543755
commit
4f830acb65
4 changed files with 5 additions and 87 deletions
|
@ -13,9 +13,9 @@ import (
|
||||||
"github.com/nirmata/kyverno/pkg/engine/operator"
|
"github.com/nirmata/kyverno/pkg/engine/operator"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidateResourceWithPattern1 is a start of element-by-element validation process
|
// ValidateResourceWithPattern is a start of element-by-element validation process
|
||||||
// It assumes that validation is started from root, so "/" is passed
|
// It assumes that validation is started from root, so "/" is passed
|
||||||
func ValidateResourceWithPattern1(resource, pattern interface{}) (string, error) {
|
func ValidateResourceWithPattern(resource, pattern interface{}) (string, error) {
|
||||||
path, err := validateResourceElement(resource, pattern, pattern, "/")
|
path, err := validateResourceElement(resource, pattern, pattern, "/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return path, err
|
return path, err
|
||||||
|
|
|
@ -192,7 +192,7 @@ func validatePatterns(ctx context.EvalInterface, resource unstructured.Unstructu
|
||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
if path, err := validate.ValidateResourceWithPattern1(resource.Object, pattern); err != nil {
|
if path, err := validate.ValidateResourceWithPattern(resource.Object, pattern); err != nil {
|
||||||
// validation failed
|
// validation failed
|
||||||
resp.Success = false
|
resp.Success = false
|
||||||
resp.Message = fmt.Sprintf("Validation error: %s; Validation rule '%s' failed at path '%s'",
|
resp.Message = fmt.Sprintf("Validation error: %s; Validation rule '%s' failed at path '%s'",
|
||||||
|
@ -216,7 +216,7 @@ func validatePatterns(ctx context.EvalInterface, resource unstructured.Unstructu
|
||||||
failedSubstitutionsErrors = append(failedSubstitutionsErrors, err)
|
failedSubstitutionsErrors = append(failedSubstitutionsErrors, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, err := validate.ValidateResourceWithPattern1(resource.Object, pattern)
|
_, err := validate.ValidateResourceWithPattern(resource.Object, pattern)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
resp.Success = true
|
resp.Success = true
|
||||||
resp.Message = fmt.Sprintf("Validation rule '%s' anyPattern[%d] succeeded.", rule.Name, idx)
|
resp.Message = fmt.Sprintf("Validation rule '%s' anyPattern[%d] succeeded.", rule.Name, idx)
|
||||||
|
|
|
@ -1,82 +0,0 @@
|
||||||
package variables
|
|
||||||
|
|
||||||
// // ValidateVariables validates if referenced path is present
|
|
||||||
// // return empty string if all paths are valid, otherwise return invalid path
|
|
||||||
// func ValidateVariables(ctx context.EvalInterface, pattern interface{}) string {
|
|
||||||
// var pathsNotPresent []string
|
|
||||||
// variableList := extractVariables(pattern)
|
|
||||||
// for _, variable := range variableList {
|
|
||||||
// if len(variable) == 2 {
|
|
||||||
// varName := variable[0]
|
|
||||||
// varValue := variable[1]
|
|
||||||
// glog.V(3).Infof("validating variable %s", varName)
|
|
||||||
// val, err := ctx.Query(varValue)
|
|
||||||
// if err == nil && val == nil {
|
|
||||||
// // path is not present, returns nil interface
|
|
||||||
// pathsNotPresent = append(pathsNotPresent, varValue)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if len(pathsNotPresent) != 0 {
|
|
||||||
// return strings.Join(pathsNotPresent, ";")
|
|
||||||
// }
|
|
||||||
// return ""
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // extractVariables extracts variables in the pattern
|
|
||||||
// func extractVariables(pattern interface{}) [][]string {
|
|
||||||
// switch typedPattern := pattern.(type) {
|
|
||||||
// case map[string]interface{}:
|
|
||||||
// return extractMap(typedPattern)
|
|
||||||
// case []interface{}:
|
|
||||||
// return extractArray(typedPattern)
|
|
||||||
// case string:
|
|
||||||
// return extractValue(typedPattern)
|
|
||||||
// default:
|
|
||||||
// fmt.Printf("variable type %T", typedPattern)
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func extractMap(patternMap map[string]interface{}) [][]string {
|
|
||||||
// var variableList [][]string
|
|
||||||
|
|
||||||
// for _, patternElement := range patternMap {
|
|
||||||
// if vars := extractVariables(patternElement); vars != nil {
|
|
||||||
// variableList = append(variableList, vars...)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return variableList
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func extractArray(patternList []interface{}) [][]string {
|
|
||||||
// var variableList [][]string
|
|
||||||
|
|
||||||
// for _, patternElement := range patternList {
|
|
||||||
// if vars := extractVariables(patternElement); vars != nil {
|
|
||||||
// variableList = append(variableList, vars...)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return variableList
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func extractValue(valuePattern string) [][]string {
|
|
||||||
// operatorVariable := getOperator(valuePattern)
|
|
||||||
// variable := valuePattern[len(operatorVariable):]
|
|
||||||
// return extractValueVariable(variable)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // returns multiple variable match groups
|
|
||||||
// func extractValueVariable(valuePattern string) [][]string {
|
|
||||||
// variableRegex := regexp.MustCompile(variableRegex)
|
|
||||||
// groups := variableRegex.FindAllStringSubmatch(valuePattern, -1)
|
|
||||||
// if len(groups) == 0 {
|
|
||||||
// // no variables
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
// // group[*] <- all the matches
|
|
||||||
// // group[*][0] <- group match
|
|
||||||
// // group[*][1] <- group capture value
|
|
||||||
// return groups
|
|
||||||
// }
|
|
|
@ -300,7 +300,7 @@ const (
|
||||||
|
|
||||||
func checkResource(newResourceSpec interface{}, resource *unstructured.Unstructured) error {
|
func checkResource(newResourceSpec interface{}, resource *unstructured.Unstructured) error {
|
||||||
// check if the resource spec if a subset of the resource
|
// check if the resource spec if a subset of the resource
|
||||||
if path, err := validate.ValidateResourceWithPattern1(resource.Object, newResourceSpec); err != nil {
|
if path, err := validate.ValidateResourceWithPattern(resource.Object, newResourceSpec); err != nil {
|
||||||
glog.V(4).Infof("Failed to match the resource at path %s: err %v", path, err)
|
glog.V(4).Infof("Failed to match the resource at path %s: err %v", path, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue