2020-01-07 15:13:57 -08:00
|
|
|
package variables
|
|
|
|
|
|
|
|
import (
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables/operator"
|
2020-01-07 15:13:57 -08:00
|
|
|
)
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// Evaluate evaluates the condition
|
2022-05-17 13:12:43 +02:00
|
|
|
func Evaluate(log logr.Logger, ctx context.EvalInterface, condition kyvernov1.Condition) bool {
|
2020-01-07 15:13:57 -08:00
|
|
|
// get handler for the operator
|
2021-07-28 19:54:50 +03:00
|
|
|
handle := operator.CreateOperatorHandler(log, ctx, condition.Operator)
|
2020-01-07 15:13:57 -08:00
|
|
|
if handle == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-03-06 20:07:51 +01:00
|
|
|
return handle.Evaluate(condition.GetKey(), condition.GetValue())
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// EvaluateConditions evaluates all the conditions present in a slice, in a backwards compatible way
|
2021-07-28 19:54:50 +03:00
|
|
|
func EvaluateConditions(log logr.Logger, ctx context.EvalInterface, conditions interface{}) bool {
|
2021-03-02 10:01:06 +05:30
|
|
|
switch typedConditions := conditions.(type) {
|
2022-05-17 13:12:43 +02:00
|
|
|
case kyvernov1.AnyAllConditions:
|
2021-07-28 19:54:50 +03:00
|
|
|
return evaluateAnyAllConditions(log, ctx, typedConditions)
|
2022-05-17 13:12:43 +02:00
|
|
|
case []kyvernov1.Condition: // backwards compatibility
|
2021-07-28 19:54:50 +03:00
|
|
|
return evaluateOldConditions(log, ctx, typedConditions)
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:12:43 +02:00
|
|
|
func EvaluateAnyAllConditions(log logr.Logger, ctx context.EvalInterface, conditions []kyvernov1.AnyAllConditions) bool {
|
2021-10-05 22:42:42 -07:00
|
|
|
for _, c := range conditions {
|
2022-04-28 14:30:23 +02:00
|
|
|
if !evaluateAnyAllConditions(log, ctx, c) {
|
2021-10-05 22:42:42 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// evaluateAnyAllConditions evaluates multiple conditions as a logical AND (all) or OR (any) operation depending on the conditions
|
2022-05-17 13:12:43 +02:00
|
|
|
func evaluateAnyAllConditions(log logr.Logger, ctx context.EvalInterface, conditions kyvernov1.AnyAllConditions) bool {
|
2021-03-02 10:01:06 +05:30
|
|
|
anyConditions, allConditions := conditions.AnyConditions, conditions.AllConditions
|
|
|
|
anyConditionsResult, allConditionsResult := true, true
|
|
|
|
|
|
|
|
// update the anyConditionsResult if they are present
|
|
|
|
if anyConditions != nil {
|
|
|
|
anyConditionsResult = false
|
|
|
|
for _, condition := range anyConditions {
|
2021-07-28 19:54:50 +03:00
|
|
|
if Evaluate(log, ctx, condition) {
|
2021-03-02 10:01:06 +05:30
|
|
|
anyConditionsResult = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 23:57:01 -07:00
|
|
|
|
2022-06-15 16:39:24 +01:00
|
|
|
if !anyConditionsResult {
|
2022-08-01 22:27:25 -07:00
|
|
|
log.V(3).Info("no condition passed for 'any' block", "any", anyConditions)
|
2022-06-15 16:39:24 +01:00
|
|
|
}
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// update the allConditionsResult if they are present
|
2021-10-14 01:00:41 +02:00
|
|
|
for _, condition := range allConditions {
|
|
|
|
if !Evaluate(log, ctx, condition) {
|
|
|
|
allConditionsResult = false
|
2022-08-01 22:27:25 -07:00
|
|
|
log.V(3).Info("a condition failed in 'all' block", "condition", condition)
|
2021-10-14 01:00:41 +02:00
|
|
|
break
|
2021-03-02 10:01:06 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finalResult := anyConditionsResult && allConditionsResult
|
|
|
|
return finalResult
|
|
|
|
}
|
|
|
|
|
2022-05-17 08:19:03 +02:00
|
|
|
// evaluateOldConditions evaluates multiple conditions when those conditions are provided in the old manner i.e. without 'any' or 'all'
|
2022-05-17 13:12:43 +02:00
|
|
|
func evaluateOldConditions(log logr.Logger, ctx context.EvalInterface, conditions []kyvernov1.Condition) bool {
|
2020-01-07 15:13:57 -08:00
|
|
|
for _, condition := range conditions {
|
2021-07-28 19:54:50 +03:00
|
|
|
if !Evaluate(log, ctx, condition) {
|
2020-01-07 15:13:57 -08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-12-23 15:10:07 -08:00
|
|
|
|
2020-01-07 15:13:57 -08:00
|
|
|
return true
|
|
|
|
}
|