Skip to content

Commit 953fbac

Browse files
committed
support opaque kinds
1 parent 0a4e863 commit 953fbac

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

staging/src/k8s.io/apiserver/pkg/cel/cidr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type CIDR struct {
3333
}
3434

3535
var (
36-
CIDRType = cel.ObjectType("net.CIDR")
36+
CIDRType = cel.OpaqueType("net.CIDR")
3737
)
3838

3939
// ConvertToNative implements ref.Val.ConvertToNative.

staging/src/k8s.io/apiserver/pkg/cel/ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type IP struct {
3333
}
3434

3535
var (
36-
IPType = cel.ObjectType("net.IP")
36+
IPType = cel.OpaqueType("net.IP")
3737
)
3838

3939
// ConvertToNative implements ref.Val.ConvertToNative.

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -498,19 +498,27 @@ func (l *CostEstimator) EstimateCallCost(function, overloadId string, target *ch
498498
rhs := args[1]
499499
if lhs.Type().Equal(rhs.Type()) == types.True {
500500
t := lhs.Type()
501-
switch t {
502-
case cel.IPType, cel.CIDRType, cel.QuantityType: // O(1) cost equality checks
503-
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: 1}}
504-
case cel.FormatType:
505-
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: cel.MaxFormatSize}.MultiplyByCostFactor(common.StringTraversalCostFactor)}
506-
case cel.URLType:
507-
size := checker.SizeEstimate{Min: 1, Max: 1}
508-
rhSize := rhs.ComputedSize()
509-
lhSize := rhs.ComputedSize()
510-
if rhSize != nil && lhSize != nil {
511-
size = rhSize.Union(*lhSize)
501+
if t.Kind() == types.OpaqueKind {
502+
switch t.TypeName() {
503+
case cel.IPType.TypeName(), cel.CIDRType.TypeName():
504+
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: 1}}
505+
}
506+
}
507+
if t.Kind() == types.StructKind {
508+
switch t {
509+
case cel.QuantityType: // O(1) cost equality checks
510+
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: 1}}
511+
case cel.FormatType:
512+
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: cel.MaxFormatSize}.MultiplyByCostFactor(common.StringTraversalCostFactor)}
513+
case cel.URLType:
514+
size := checker.SizeEstimate{Min: 1, Max: 1}
515+
rhSize := rhs.ComputedSize()
516+
lhSize := rhs.ComputedSize()
517+
if rhSize != nil && lhSize != nil {
518+
size = rhSize.Union(*lhSize)
519+
}
520+
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: size.Max}.MultiplyByCostFactor(common.StringTraversalCostFactor)}
512521
}
513-
return &checker.CallEstimate{CostEstimate: checker.CostEstimate{Min: 1, Max: size.Max}.MultiplyByCostFactor(common.StringTraversalCostFactor)}
514522
}
515523
}
516524
}

0 commit comments

Comments
 (0)