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

95 lines
2.6 KiB
Go
Raw Normal View History

2020-06-12 15:48:19 +05:30
package operator
import (
"fmt"
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/pkg/engine/context"
2020-06-12 15:48:19 +05:30
)
//NewNotInHandler returns handler to manage NotIn operations
func NewNotInHandler(log logr.Logger, ctx context.EvalInterface, subHandler VariableSubstitutionHandler) OperatorHandler {
return NotInHandler{
ctx: ctx,
subHandler: subHandler,
log: log,
}
}
//NotInHandler provides implementation to handle NotIn Operator
type NotInHandler struct {
ctx context.EvalInterface
subHandler VariableSubstitutionHandler
log logr.Logger
}
//Evaluate evaluates expression with NotIn Operator
2020-06-24 12:27:08 +05:30
func (nin NotInHandler) Evaluate(key, value interface{}) bool {
2020-06-12 15:48:19 +05:30
var err error
2020-11-28 23:38:44 -08:00
2020-06-12 15:48:19 +05:30
// substitute the variables
2020-06-24 12:27:08 +05:30
if key, err = nin.subHandler(nin.log, nin.ctx, key); err != nil {
nin.log.Error(err, "Failed to resolve variable", "variable", key)
2020-06-12 15:48:19 +05:30
return false
}
2020-11-28 23:38:44 -08:00
2020-06-24 12:27:08 +05:30
if value, err = nin.subHandler(nin.log, nin.ctx, value); err != nil {
nin.log.Error(err, "Failed to resolve variable", "variable", value)
2020-06-12 15:48:19 +05:30
return false
}
2020-06-26 12:33:49 +05:30
2020-06-12 15:48:19 +05:30
switch typedKey := key.(type) {
case string:
2020-11-28 23:38:44 -08:00
return nin.validateValueWithStringPattern(typedKey, value)
case []interface{}:
var stringSlice []string
for _, v := range typedKey {
stringSlice = append(stringSlice, v.(string))
}
return nin.validateValueWithStringSetPattern(stringSlice, value)
2020-06-12 15:48:19 +05:30
default:
2020-06-24 12:27:08 +05:30
nin.log.Info("Unsupported type", "value", typedKey, "type", fmt.Sprintf("%T", typedKey))
2020-06-12 15:48:19 +05:30
return false
}
}
2020-11-28 23:38:44 -08:00
func (nin NotInHandler) validateValueWithStringPattern(key string, value interface{}) bool {
invalidType, keyExists := keyExistsInArray(key, value, nin.log)
2020-06-24 12:27:08 +05:30
if invalidType {
nin.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
2020-11-28 23:38:44 -08:00
return !keyExists
2020-06-12 15:48:19 +05:30
}
func (nin NotInHandler) validateValueWithStringSetPattern(key []string, value interface{}) bool {
invalidType, isNotIn := setExistsInArray(key, value, nin.log, true)
if invalidType {
nin.log.Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
return false
}
return isNotIn
}
2020-11-28 23:38:44 -08:00
func (nin NotInHandler) validateValueWithBoolPattern(_ bool, _ interface{}) bool {
2020-06-12 15:48:19 +05:30
return false
}
2020-11-28 23:38:44 -08:00
func (nin NotInHandler) validateValueWithIntPattern(_ int64, _ interface{}) bool {
2020-06-12 15:48:19 +05:30
return false
}
2020-11-28 23:38:44 -08:00
func (nin NotInHandler) validateValueWithFloatPattern(_ float64, _ interface{}) bool {
2020-06-12 15:48:19 +05:30
return false
}
2020-11-28 23:38:44 -08:00
func (nin NotInHandler) validateValueWithMapPattern(_ map[string]interface{}, _ interface{}) bool {
2020-06-24 12:27:08 +05:30
return false
2020-06-12 15:48:19 +05:30
}
2020-11-28 23:38:44 -08:00
func (nin NotInHandler) validateValueWithSlicePattern(_ []interface{}, _ interface{}) bool {
2020-06-24 12:27:08 +05:30
return false
2020-06-12 15:48:19 +05:30
}