@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "math"
23
24
"reflect"
24
25
"strings"
25
26
"sync"
@@ -120,6 +121,10 @@ type Options struct {
120
121
121
122
// CostLimit allows overriding the default runtime cost limit [resourceapi.CELSelectorExpressionMaxCost].
122
123
CostLimit * uint64
124
+
125
+ // DisableCostEstimation can be set to skip estimating the worst-case CEL cost.
126
+ // If disabled or after an error, [CompilationResult.MaxCost] will be set to [math.Uint64].
127
+ DisableCostEstimation bool
123
128
}
124
129
125
130
// CompileCELExpression returns a compiled CEL expression. It evaluates to bool.
@@ -133,6 +138,7 @@ func (c compiler) CompileCELExpression(expression string, options Options) Compi
133
138
Detail : errorString ,
134
139
},
135
140
Expression : expression ,
141
+ MaxCost : math .MaxUint64 ,
136
142
}
137
143
}
138
144
@@ -141,10 +147,6 @@ func (c compiler) CompileCELExpression(expression string, options Options) Compi
141
147
return resultError (fmt .Sprintf ("unexpected error loading CEL environment: %v" , err ), apiservercel .ErrorTypeInternal )
142
148
}
143
149
144
- // We don't have a SizeEstimator. The potential size of the input (= a
145
- // device) is already declared in the definition of the environment.
146
- estimator := c .newCostEstimator ()
147
-
148
150
ast , issues := env .Compile (expression )
149
151
if issues != nil {
150
152
return resultError ("compilation failed: " + issues .String (), apiservercel .ErrorTypeInvalid )
@@ -176,15 +178,21 @@ func (c compiler) CompileCELExpression(expression string, options Options) Compi
176
178
OutputType : ast .OutputType (),
177
179
Environment : env ,
178
180
emptyMapVal : env .CELTypeAdapter ().NativeToValue (map [string ]any {}),
181
+ MaxCost : math .MaxUint64 ,
179
182
}
180
183
181
- costEst , err := env .EstimateCost (ast , estimator )
182
- if err != nil {
183
- compilationResult .Error = & apiservercel.Error {Type : apiservercel .ErrorTypeInternal , Detail : "cost estimation failed: " + err .Error ()}
184
- return compilationResult
184
+ if ! options .DisableCostEstimation {
185
+ // We don't have a SizeEstimator. The potential size of the input (= a
186
+ // device) is already declared in the definition of the environment.
187
+ estimator := c .newCostEstimator ()
188
+ costEst , err := env .EstimateCost (ast , estimator )
189
+ if err != nil {
190
+ compilationResult .Error = & apiservercel.Error {Type : apiservercel .ErrorTypeInternal , Detail : "cost estimation failed: " + err .Error ()}
191
+ return compilationResult
192
+ }
193
+ compilationResult .MaxCost = costEst .Max
185
194
}
186
195
187
- compilationResult .MaxCost = costEst .Max
188
196
return compilationResult
189
197
}
190
198
0 commit comments