1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-07 00:17:13 +00:00
kyverno/pkg/engine/variables/operator/operator.go
Max Goncharenko 903963c26d
add special variable substitution logic for preconditions (#1930)
* add special variable substitution logic for preconditions

Signed-off-by: Max Goncharenko <kacejot@fex.net>

* handle NotFoundVariable error in proper way

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>

* remove excess log; fix grammar

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>

* removed isPrecondition flag; added test case for empty deny string; fixed related issue

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>

* fix test case

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>

* fix go lint

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>

* fix tests

Signed-off-by: Maxim Goncharenko <goncharenko.maxim@apriorit.com>
2021-07-28 09:54:50 -07:00

59 lines
2 KiB
Go

package operator
import (
"strings"
"github.com/go-logr/logr"
kyverno "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/engine/context"
)
//OperatorHandler provides interface to manage types
type OperatorHandler interface {
Evaluate(key, value interface{}) bool
validateValueWithStringPattern(key string, value interface{}) bool
validateValueWithBoolPattern(key bool, value interface{}) bool
validateValueWithIntPattern(key int64, value interface{}) bool
validateValueWithFloatPattern(key float64, value interface{}) bool
validateValueWithMapPattern(key map[string]interface{}, value interface{}) bool
validateValueWithSlicePattern(key []interface{}, value interface{}) bool
}
//VariableSubstitutionHandler defines the handler function for variable substitution
type VariableSubstitutionHandler = func(log logr.Logger, ctx context.EvalInterface, pattern interface{}) (interface{}, error)
//CreateOperatorHandler returns the operator handler based on the operator used in condition
func CreateOperatorHandler(log logr.Logger, ctx context.EvalInterface, op kyverno.ConditionOperator) OperatorHandler {
str := strings.ToLower(string(op))
switch str {
case strings.ToLower(string(kyverno.Equal)):
return NewEqualHandler(log, ctx)
case strings.ToLower(string(kyverno.Equals)):
return NewEqualHandler(log, ctx)
case strings.ToLower(string(kyverno.NotEqual)):
return NewNotEqualHandler(log, ctx)
case strings.ToLower(string(kyverno.NotEquals)):
return NewNotEqualHandler(log, ctx)
case strings.ToLower(string(kyverno.In)):
return NewInHandler(log, ctx)
case strings.ToLower(string(kyverno.NotIn)):
return NewNotInHandler(log, ctx)
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, op)
default:
log.Info("operator not supported", "operator", str)
}
return nil
}