1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-31 03:45:17 +00:00

Add AND logical operator support (#1539)

Signed-off-by: Max Goncharenko <kacejot@fex.net>
This commit is contained in:
Max Goncharenko 2021-02-06 03:52:31 +02:00 committed by GitHub
parent b91022d438
commit 536f364724
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View file

@ -154,10 +154,10 @@ func validateValueWithNilPattern(log logr.Logger, value interface{}) bool {
// Handler for pattern values during validation process
func validateValueWithStringPatterns(log logr.Logger, value interface{}, pattern string) bool {
statements := strings.Split(pattern, "|")
for _, statement := range statements {
statement = strings.Trim(statement, " ")
if validateValueWithStringPattern(log, value, statement) {
conditions := strings.Split(pattern, "|")
for _, condition := range conditions {
condition = strings.Trim(condition, " ")
if checkForAndConditionsAndValidate(log, value, condition) {
return true
}
}
@ -165,6 +165,18 @@ func validateValueWithStringPatterns(log logr.Logger, value interface{}, pattern
return false
}
func checkForAndConditionsAndValidate(log logr.Logger, value interface{}, pattern string) bool {
conditions := strings.Split(pattern, "&")
for _, condition := range conditions {
condition = strings.Trim(condition, " ")
if !validateValueWithStringPattern(log, value, condition) {
return false
}
}
return true
}
// Handler for single pattern value during validation process
// Detects if pattern has a number
func validateValueWithStringPattern(log logr.Logger, value interface{}, pattern string) bool {

View file

@ -196,6 +196,22 @@ func TestValidateValueWithPattern_StringsLogicalOr(t *testing.T) {
assert.Assert(t, ValidateValueWithPattern(log.Log, value, pattern))
}
func TestValidateValueWithPattern_StringsLogicalAnd(t *testing.T) {
pattern := ">1 & <20"
value := "10"
assert.Assert(t, ValidateValueWithPattern(log.Log, value, pattern))
}
func TestValidateValueWithPattern_StringsAllLogicalOperators(t *testing.T) {
pattern := ">1 & <20 | >31 & <33"
value := "10"
assert.Assert(t, ValidateValueWithPattern(log.Log, value, pattern))
value = "32"
assert.Assert(t, ValidateValueWithPattern(log.Log, value, pattern))
value = "21"
assert.Assert(t, !ValidateValueWithPattern(log.Log, value, pattern))
}
func TestValidateValueWithPattern_EqualTwoFloats(t *testing.T) {
assert.Assert(t, ValidateValueWithPattern(log.Log, 7.0, 7.000))
}