1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/engine/variables/operator/notin.go

84 lines
2.3 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
//
// Deprecated: Use `NewAllNotInHandler` or `NewAnyNotInHandler` instead
func NewNotInHandler(log logr.Logger, ctx context.EvalInterface) OperatorHandler {
2020-06-12 15:48:19 +05:30
return NotInHandler{
ctx: ctx,
log: log,
2020-06-12 15:48:19 +05:30
}
}
// NotInHandler provides implementation to handle NotIn Operator
2020-06-12 15:48:19 +05:30
type NotInHandler struct {
ctx context.EvalInterface
log logr.Logger
2020-06-12 15:48:19 +05:30
}
// Evaluate evaluates expression with NotIn Operator
func (nin NotInHandler) Evaluate(key, value interface{}) bool {
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 int, int32, int64, float32, float64:
return nin.validateValueWithStringPattern(fmt.Sprint(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:
nin.log.V(2).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.V(2).Info("expected type []string", "value", value, "type", fmt.Sprintf("%T", value))
2020-06-24 12:27:08 +05:30
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.V(2).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
}