Skip to content

Commit f699574

Browse files
committed
Add basic panicOnUnknown support for kubernetes types
1 parent 953fbac commit f699574

File tree

1 file changed

+25
-0
lines changed
  • staging/src/k8s.io/apiserver/pkg/cel/library

1 file changed

+25
-0
lines changed

staging/src/k8s.io/apiserver/pkg/cel/library/cost.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package library
1919
import (
2020
"fmt"
2121
"math"
22+
"reflect"
2223

2324
"github.com/google/cel-go/checker"
2425
"github.com/google/cel-go/common"
@@ -27,6 +28,7 @@ import (
2728
"github.com/google/cel-go/common/types/ref"
2829
"github.com/google/cel-go/common/types/traits"
2930

31+
"k8s.io/apimachinery/pkg/util/sets"
3032
"k8s.io/apiserver/pkg/cel"
3133
)
3234

@@ -48,6 +50,22 @@ var knownUnhandledFunctions = map[string]bool{
4850
"strings.quote": true,
4951
}
5052

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+
5169
// CostEstimator implements CEL's interpretable.ActualCostEstimator and checker.CostEstimator.
5270
type CostEstimator struct {
5371
// 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
250268
return &unitCost
251269
case cel.URL: // TODO: Computing the actual cost is expensive, and changing this would be a breaking change
252270
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+
}
253275
}
254276
}
255277
}
@@ -520,6 +542,9 @@ func (l *CostEstimator) EstimateCallCost(function, overloadId string, target *ch
520542
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: size.Max}.MultiplyByCostFactor(common.StringTraversalCostFactor)}
521543
}
522544
}
545+
if panicOnUnknown && knownKubernetesCompilerTypes.Has(t) {
546+
panic(fmt.Errorf("EstimateCallCost: unhandled equality for Kubernetes type %v", t))
547+
}
523548
}
524549
}
525550
}

0 commit comments

Comments
 (0)