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
Mohan B E 51ac382c6c
Feature/configmaps var 724 (#1118)
* added configmap data substitution for foreground mutate and validate

* added configmap data substitution for foreground mutate and validate fmt

* added configmap lookup for background

* added comments to resource cache

* added configmap data lookup in preConditions

* added parse strings in In operator and configmap lookup docs

* added configmap lookup docs

* modified configmap lookup docs
2020-09-22 14:11:49 -07:00

105 lines
2.7 KiB
Go

package operator
import (
"fmt"
"reflect"
"strings"
"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,
}
}
//InHandler provides implementation to handle In Operator
type InHandler struct {
ctx context.EvalInterface
subHandler VariableSubstitutionHandler
log logr.Logger
}
//Evaluate evaluates expression with In Operator
func (in InHandler) Evaluate(key, value interface{}) bool {
var err error
// substitute the variables
if key, err = in.subHandler(in.log, in.ctx, key); err != nil {
in.log.Error(err, "Failed to resolve variable", "variable", key)
return false
}
if value, err = in.subHandler(in.log, in.ctx, value); err != nil {
in.log.Error(err, "Failed to resolve variable", "variable", value)
return false
}
switch typedKey := key.(type) {
case string:
return in.validateValuewithStringPattern(typedKey, value)
default:
in.log.Info("Unsupported type", "value", typedKey, "type", fmt.Sprintf("%T", typedKey))
return false
}
}
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
}
return keyExists
}
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
}
}
case string:
valuesAvaliable = strings.TrimSpace(valuesAvaliable)
vars := strings.Split(valuesAvaliable, ",")
for _, val := range vars {
if key == val {
keyExists = true
}
}
default:
return true, false
}
return invalidType, keyExists
}
func (in InHandler) validateValuewithBoolPattern(key bool, value interface{}) bool {
return false
}
func (in InHandler) validateValuewithIntPattern(key int64, value interface{}) bool {
return false
}
func (in InHandler) validateValuewithFloatPattern(key float64, value interface{}) bool {
return false
}
func (in InHandler) validateValueWithMapPattern(key map[string]interface{}, value interface{}) bool {
return false
}
func (in InHandler) validateValueWithSlicePattern(key []interface{}, value interface{}) bool {
return false
}