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/in.go

97 lines
2.5 KiB
Go
Raw Normal View History

2020-06-12 15:48:19 +05:30
package operator
import (
"fmt"
"reflect"
"github.com/go-logr/logr"
"github.com/nirmata/kyverno/pkg/engine/context"
)
//NewInHandler returns handler to manage In operations
func NewInHandler(log logr.Logger, ctx context.EvalInterface, subHandler VariableSubstitutionHandler) OperatorHandler {
return InHandler{
ctx: ctx,
subHandler: subHandler,
log: log,
}
}
2020-06-26 12:33:49 +05:30
//InHandler provides implementation to handle In Operator
2020-06-12 15:48:19 +05:30
type InHandler struct {
ctx context.EvalInterface
subHandler VariableSubstitutionHandler
log logr.Logger
}
//Evaluate evaluates expression with In Operator
2020-06-24 12:27:08 +05:30
func (in InHandler) Evaluate(key, value interface{}) bool {
2020-06-12 15:48:19 +05:30
var err error
// substitute the variables
2020-06-24 12:27:08 +05:30
if key, err = in.subHandler(in.log, in.ctx, key); err != nil {
in.log.Error(err, "Failed to resolve variable", "variable", key)
2020-06-12 15:48:19 +05:30
return false
}
2020-06-24 12:27:08 +05:30
if value, err = in.subHandler(in.log, in.ctx, value); err != nil {
in.log.Error(err, "Failed to resolve variable", "variable", value)
2020-06-12 15:48:19 +05:30
return false
}
switch typedKey := key.(type) {
case string:
2020-06-24 12:27:08 +05:30
return in.validateValuewithStringPattern(typedKey, value)
2020-06-12 15:48:19 +05:30
default:
2020-06-24 12:27:08 +05:30
in.log.Info("Unsupported type", "value", typedKey, "type", fmt.Sprintf("%T", typedKey))
2020-06-12 15:48:19 +05:30
return false
}
}
2020-06-24 12:27:08 +05:30
func (in InHandler) validateValuewithStringPattern(key string, value interface{}) (keyExists bool) {
invalidType, keyExists := ValidateStringPattern(key, value)
if invalidType {
in.log.Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
return false
2020-06-12 15:48:19 +05:30
}
2020-06-24 12:27:08 +05:30
return keyExists
2020-06-12 15:48:19 +05:30
}
2020-06-24 12:27:08 +05:30
func ValidateStringPattern(key string, value interface{}) (invalidType bool, keyExists bool) {
stringType := reflect.TypeOf("")
switch valuesAvaliable := value.(type) {
case []interface{}:
for _, val := range valuesAvaliable {
if reflect.TypeOf(val) != stringType {
return true, false
}
if key == val {
keyExists = true
}
}
default:
return true, false
2020-06-12 15:48:19 +05:30
}
2020-06-24 12:27:08 +05:30
return invalidType, keyExists
2020-06-12 15:48:19 +05:30
}
2020-06-24 12:27:08 +05:30
func (in InHandler) validateValuewithBoolPattern(key bool, value interface{}) bool {
return false
}
2020-06-12 15:48:19 +05:30
2020-06-24 12:27:08 +05:30
func (in InHandler) validateValuewithIntPattern(key int64, value interface{}) bool {
2020-06-12 15:48:19 +05:30
return false
}
2020-06-24 12:27:08 +05:30
func (in InHandler) validateValuewithFloatPattern(key float64, value interface{}) bool {
2020-06-12 15:48:19 +05:30
return false
}
2020-06-24 12:27:08 +05:30
func (in InHandler) validateValueWithMapPattern(key map[string]interface{}, value interface{}) bool {
return false
2020-06-12 15:48:19 +05:30
}
2020-06-24 12:27:08 +05:30
func (in InHandler) validateValueWithSlicePattern(key []interface{}, value interface{}) bool {
return false
2020-06-12 15:48:19 +05:30
}