use DefaultMaxRequestSizeBytes for maxRequestSizeBytes.

This commit is contained in:
Jiahui Feng 2022-10-10 14:42:24 -07:00
parent 5b8a5b37d5
commit 755f41a185
4 changed files with 13 additions and 11 deletions

View File

@ -27,6 +27,9 @@ import (
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema" "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
) )
// TODO(DangerOnTheRanger): wire in MaxRequestBodyBytes from apiserver/pkg/server/options/server_run_options.go to make this configurable
const maxRequestSizeBytes = apiservercel.DefaultMaxRequestSizeBytes
// SchemaDeclType converts the structural schema to a CEL declaration, or returns nil if the // SchemaDeclType converts the structural schema to a CEL declaration, or returns nil if the
// structural schema should not be exposed in CEL expressions. // structural schema should not be exposed in CEL expressions.
// Set isResourceRoot to true for the root of a custom resource or embedded resource. // Set isResourceRoot to true for the root of a custom resource or embedded resource.
@ -55,7 +58,7 @@ func SchemaDeclType(s *schema.Structural, isResourceRoot bool) *apiservercel.Dec
// //
dyn := apiservercel.NewSimpleTypeWithMinSize("dyn", cel.DynType, nil, 1) // smallest value for a serialized x-kubernetes-int-or-string is 0 dyn := apiservercel.NewSimpleTypeWithMinSize("dyn", cel.DynType, nil, 1) // smallest value for a serialized x-kubernetes-int-or-string is 0
// handle x-kubernetes-int-or-string by returning the max length/min serialized size of the largest possible string // handle x-kubernetes-int-or-string by returning the max length/min serialized size of the largest possible string
dyn.MaxElements = apiservercel.MaxRequestSizeBytes - 2 dyn.MaxElements = maxRequestSizeBytes - 2
return dyn return dyn
} }
@ -229,7 +232,7 @@ func WithTypeAndObjectMeta(s *schema.Structural) *schema.Structural {
// this function. // this function.
func MaxCardinality(minSize int64) uint64 { func MaxCardinality(minSize int64) uint64 {
sz := minSize + 1 // assume at least one comma between elements sz := minSize + 1 // assume at least one comma between elements
return uint64(apiservercel.MaxRequestSizeBytes / sz) return uint64(maxRequestSizeBytes / sz)
} }
// estimateMaxStringLengthPerRequest estimates the maximum string length (in characters) // estimateMaxStringLengthPerRequest estimates the maximum string length (in characters)
@ -238,7 +241,7 @@ func MaxCardinality(minSize int64) uint64 {
func estimateMaxStringLengthPerRequest(s *schema.Structural) int64 { func estimateMaxStringLengthPerRequest(s *schema.Structural) int64 {
if s.ValueValidation == nil || s.XIntOrString { if s.ValueValidation == nil || s.XIntOrString {
// subtract 2 to account for "" // subtract 2 to account for ""
return apiservercel.MaxRequestSizeBytes - 2 return maxRequestSizeBytes - 2
} }
switch s.ValueValidation.Format { switch s.ValueValidation.Format {
case "duration": case "duration":
@ -249,7 +252,7 @@ func estimateMaxStringLengthPerRequest(s *schema.Structural) int64 {
return apiservercel.MaxDatetimeSizeJSON return apiservercel.MaxDatetimeSizeJSON
default: default:
// subtract 2 to account for "" // subtract 2 to account for ""
return apiservercel.MaxRequestSizeBytes - 2 return maxRequestSizeBytes - 2
} }
} }
@ -257,7 +260,7 @@ func estimateMaxStringLengthPerRequest(s *schema.Structural) int64 {
// the provided minimum serialized size that can fit into a single request. // the provided minimum serialized size that can fit into a single request.
func estimateMaxArrayItemsFromMinSize(minSize int64) int64 { func estimateMaxArrayItemsFromMinSize(minSize int64) int64 {
// subtract 2 to account for [ and ] // subtract 2 to account for [ and ]
return (apiservercel.MaxRequestSizeBytes - 2) / (minSize + 1) return (maxRequestSizeBytes - 2) / (minSize + 1)
} }
// estimateMaxAdditionalPropertiesPerRequest estimates the maximum number of additional properties // estimateMaxAdditionalPropertiesPerRequest estimates the maximum number of additional properties
@ -267,5 +270,5 @@ func estimateMaxAdditionalPropertiesFromMinSize(minSize int64) int64 {
// will all vary in length // will all vary in length
keyValuePairSize := minSize + 6 keyValuePairSize := minSize + 6
// subtract 2 to account for { and } // subtract 2 to account for { and }
return (apiservercel.MaxRequestSizeBytes - 2) / keyValuePairSize return (maxRequestSizeBytes - 2) / keyValuePairSize
} }

View File

@ -443,7 +443,7 @@ func TestEstimateMaxLengthJSON(t *testing.T) {
}, },
}, },
// should be exactly equal to maxRequestSizeBytes - 2 (to allow for quotes in the case of a string) // should be exactly equal to maxRequestSizeBytes - 2 (to allow for quotes in the case of a string)
ExpectedMaxElements: apiservercel.MaxRequestSizeBytes - 2, ExpectedMaxElements: apiservercel.DefaultMaxRequestSizeBytes - 2,
}, },
{ {
Name: "objectDefaultFieldArray", Name: "objectDefaultFieldArray",

View File

@ -17,9 +17,8 @@ limitations under the License.
package cel package cel
const ( const (
// MaxRequestSizeBytes is the largest request that will be accepted is 3MB // DefaultMaxRequestSizeBytes is the size of the largest request that will be accepted
// TODO(DangerOnTheRanger): wire in MaxRequestBodyBytes from apiserver/pkg/server/options/server_run_options.go to make this configurable DefaultMaxRequestSizeBytes = int64(3 * 1024 * 1024)
MaxRequestSizeBytes = int64(3 * 1024 * 1024)
// MaxDurationSizeJSON // MaxDurationSizeJSON
// OpenAPI duration strings follow RFC 3339, section 5.6 - see the comment on maxDatetimeSizeJSON // OpenAPI duration strings follow RFC 3339, section 5.6 - see the comment on maxDatetimeSizeJSON

View File

@ -366,7 +366,7 @@ func NewConfig(codecs serializer.CodecFactory) *Config {
// A request body might be encoded in json, and is converted to // A request body might be encoded in json, and is converted to
// proto when persisted in etcd, so we allow 2x as the largest request // proto when persisted in etcd, so we allow 2x as the largest request
// body size to be accepted and decoded in a write request. // body size to be accepted and decoded in a write request.
// If this constant is changed, maxRequestSizeBytes in apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas.go // If this constant is changed, DefaultMaxRequestSizeBytes in k8s.io/apiserver/pkg/cel/limits.go
// should be changed to reflect the new value, if the two haven't // should be changed to reflect the new value, if the two haven't
// been wired together already somehow. // been wired together already somehow.
MaxRequestBodyBytes: int64(3 * 1024 * 1024), MaxRequestBodyBytes: int64(3 * 1024 * 1024),