2020-01-07 15:13:57 -08:00
|
|
|
package variables
|
|
|
|
|
|
|
|
import (
|
2020-03-17 11:05:20 -07:00
|
|
|
"github.com/go-logr/logr"
|
2020-10-07 11:12:31 -07:00
|
|
|
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables/operator"
|
2020-01-07 15:13:57 -08:00
|
|
|
)
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//Evaluate evaluates the condition
|
2021-07-28 19:54:50 +03:00
|
|
|
func Evaluate(log logr.Logger, ctx context.EvalInterface, condition kyverno.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
|
|
|
|
}
|
2021-07-28 19:54:50 +03:00
|
|
|
return handle.Evaluate(condition.Key, condition.Value)
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
|
|
|
|
2021-10-05 22:42:42 -07: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) {
|
|
|
|
case kyverno.AnyAllConditions:
|
2021-07-28 19:54:50 +03:00
|
|
|
return evaluateAnyAllConditions(log, ctx, typedConditions)
|
2021-03-02 10:01:06 +05:30
|
|
|
case []kyverno.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
|
|
|
|
}
|
|
|
|
|
2021-10-05 22:42:42 -07:00
|
|
|
func EvaluateAnyAllConditions(log logr.Logger, ctx context.EvalInterface, conditions []*kyverno.AnyAllConditions) bool {
|
|
|
|
for _, c := range conditions {
|
|
|
|
if !evaluateAnyAllConditions(log, ctx, *c) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:01:06 +05:30
|
|
|
//evaluateAnyAllConditions evaluates multiple conditions as a logical AND (all) or OR (any) operation depending on the conditions
|
2021-07-28 19:54:50 +03:00
|
|
|
func evaluateAnyAllConditions(log logr.Logger, ctx context.EvalInterface, conditions kyverno.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the allConditionsResult if they are present
|
|
|
|
if allConditions != nil {
|
|
|
|
for _, condition := range allConditions {
|
2021-07-28 19:54:50 +03:00
|
|
|
if !Evaluate(log, ctx, condition) {
|
2021-03-02 10:01:06 +05:30
|
|
|
allConditionsResult = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finalResult := anyConditionsResult && allConditionsResult
|
|
|
|
return finalResult
|
|
|
|
}
|
|
|
|
|
|
|
|
//evaluateOldConditions evaluates multiple conditions when those conditions are provided in the old manner i.e. without 'any' or 'all'
|
2021-07-28 19:54:50 +03:00
|
|
|
func evaluateOldConditions(log logr.Logger, ctx context.EvalInterface, conditions []kyverno.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
|
|
|
|
}
|