2020-01-07 15:13:57 -08:00
|
|
|
package operator
|
|
|
|
|
|
|
|
import (
|
2021-02-06 09:19:23 +05:30
|
|
|
"strings"
|
|
|
|
|
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"
|
2020-01-07 15:13:57 -08:00
|
|
|
)
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//OperatorHandler provides interface to manage types
|
2020-01-07 15:13:57 -08:00
|
|
|
type OperatorHandler interface {
|
2021-07-07 17:37:44 +05:30
|
|
|
Evaluate(key, value interface{}, isPreCondition bool) bool
|
2020-11-28 23:29:15 -08:00
|
|
|
validateValueWithStringPattern(key string, value interface{}) bool
|
|
|
|
validateValueWithBoolPattern(key bool, value interface{}) bool
|
|
|
|
validateValueWithIntPattern(key int64, value interface{}) bool
|
|
|
|
validateValueWithFloatPattern(key float64, value interface{}) bool
|
2020-01-07 15:13:57 -08:00
|
|
|
validateValueWithMapPattern(key map[string]interface{}, value interface{}) bool
|
|
|
|
validateValueWithSlicePattern(key []interface{}, value interface{}) bool
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//VariableSubstitutionHandler defines the handler function for variable substitution
|
2020-03-17 11:05:20 -07:00
|
|
|
type VariableSubstitutionHandler = func(log logr.Logger, ctx context.EvalInterface, pattern interface{}) (interface{}, error)
|
2020-01-07 15:13:57 -08:00
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//CreateOperatorHandler returns the operator handler based on the operator used in condition
|
2020-03-17 11:05:20 -07:00
|
|
|
func CreateOperatorHandler(log logr.Logger, ctx context.EvalInterface, op kyverno.ConditionOperator, subHandler VariableSubstitutionHandler) OperatorHandler {
|
2020-10-29 15:00:22 -07:00
|
|
|
str := strings.ToLower(string(op))
|
|
|
|
switch str {
|
|
|
|
|
|
|
|
case strings.ToLower(string(kyverno.Equal)):
|
2020-03-17 11:05:20 -07:00
|
|
|
return NewEqualHandler(log, ctx, subHandler)
|
2020-10-29 15:00:22 -07:00
|
|
|
|
|
|
|
case strings.ToLower(string(kyverno.Equals)):
|
2020-05-06 22:43:17 +05:30
|
|
|
return NewEqualHandler(log, ctx, subHandler)
|
2020-10-29 15:00:22 -07:00
|
|
|
|
|
|
|
case strings.ToLower(string(kyverno.NotEqual)):
|
|
|
|
return NewNotEqualHandler(log, ctx, subHandler)
|
|
|
|
|
|
|
|
case strings.ToLower(string(kyverno.NotEquals)):
|
2020-05-06 22:43:17 +05:30
|
|
|
return NewNotEqualHandler(log, ctx, subHandler)
|
2020-10-29 15:00:22 -07:00
|
|
|
|
|
|
|
case strings.ToLower(string(kyverno.In)):
|
2020-06-12 15:48:19 +05:30
|
|
|
return NewInHandler(log, ctx, subHandler)
|
2020-10-29 15:00:22 -07:00
|
|
|
|
|
|
|
case strings.ToLower(string(kyverno.NotIn)):
|
2020-06-12 15:48:19 +05:30
|
|
|
return NewNotInHandler(log, ctx, subHandler)
|
2020-10-29 15:00:22 -07:00
|
|
|
|
2021-02-06 09:19:23 +05:30
|
|
|
case strings.ToLower(string(kyverno.GreaterThanOrEquals)),
|
|
|
|
strings.ToLower(string(kyverno.GreaterThan)),
|
|
|
|
strings.ToLower(string(kyverno.LessThanOrEquals)),
|
|
|
|
strings.ToLower(string(kyverno.LessThan)):
|
|
|
|
return NewNumericOperatorHandler(log, ctx, subHandler, op)
|
|
|
|
|
2020-01-07 15:13:57 -08:00
|
|
|
default:
|
2020-10-29 15:00:22 -07:00
|
|
|
log.Info("operator not supported", "operator", str)
|
2020-01-07 15:13:57 -08:00
|
|
|
}
|
2020-10-29 15:00:22 -07:00
|
|
|
|
2020-01-07 15:13:57 -08:00
|
|
|
return nil
|
|
|
|
}
|