@@ -19,6 +19,7 @@ package library
19
19
import (
20
20
"fmt"
21
21
"math"
22
+ "reflect"
22
23
23
24
"github.com/google/cel-go/checker"
24
25
"github.com/google/cel-go/common"
@@ -27,6 +28,7 @@ import (
27
28
"github.com/google/cel-go/common/types/ref"
28
29
"github.com/google/cel-go/common/types/traits"
29
30
31
+ "k8s.io/apimachinery/pkg/util/sets"
30
32
"k8s.io/apiserver/pkg/cel"
31
33
)
32
34
@@ -48,6 +50,22 @@ var knownUnhandledFunctions = map[string]bool{
48
50
"strings.quote" : true ,
49
51
}
50
52
53
+ // TODO: Replace this with a utility that extracts types from libraries.
54
+ var knownKubernetesRuntimeTypes = sets .New [reflect.Type ](
55
+ reflect .ValueOf (cel.URL {}).Type (),
56
+ reflect .ValueOf (cel.IP {}).Type (),
57
+ reflect .ValueOf (cel.CIDR {}).Type (),
58
+ reflect .ValueOf (& cel.Format {}).Type (),
59
+ reflect .ValueOf (cel.Quantity {}).Type (),
60
+ )
61
+ var knownKubernetesCompilerTypes = sets .New [ref.Type ](
62
+ cel .CIDRType ,
63
+ cel .IPType ,
64
+ cel .FormatType ,
65
+ cel .QuantityType ,
66
+ cel .URLType ,
67
+ )
68
+
51
69
// CostEstimator implements CEL's interpretable.ActualCostEstimator and checker.CostEstimator.
52
70
type CostEstimator struct {
53
71
// SizeEstimator provides a CostEstimator.EstimateSize that this CostEstimator will delegate size estimation
@@ -250,6 +268,10 @@ func (l *CostEstimator) CallCost(function, overloadId string, args []ref.Val, re
250
268
return & unitCost
251
269
case cel.URL : // TODO: Computing the actual cost is expensive, and changing this would be a breaking change
252
270
return & unitCost
271
+ default :
272
+ if panicOnUnknown && knownKubernetesRuntimeTypes .Has (reflect .ValueOf (lhs ).Type ()) {
273
+ panic (fmt .Errorf ("CallCost: unhandled equality for Kubernetes type %T" , lhs ))
274
+ }
253
275
}
254
276
}
255
277
}
@@ -520,6 +542,9 @@ func (l *CostEstimator) EstimateCallCost(function, overloadId string, target *ch
520
542
return & checker.CallEstimate {CostEstimate : checker.CostEstimate {Min : 1 , Max : size .Max }.MultiplyByCostFactor (common .StringTraversalCostFactor )}
521
543
}
522
544
}
545
+ if panicOnUnknown && knownKubernetesCompilerTypes .Has (t ) {
546
+ panic (fmt .Errorf ("EstimateCallCost: unhandled equality for Kubernetes type %v" , t ))
547
+ }
523
548
}
524
549
}
525
550
}
0 commit comments