From 755f41a185e828d9c64ae3ac37ce829e60592ad1 Mon Sep 17 00:00:00 2001 From: Jiahui Feng Date: Mon, 10 Oct 2022 14:42:24 -0700 Subject: [PATCH] use DefaultMaxRequestSizeBytes for maxRequestSizeBytes. --- .../pkg/apiserver/schema/cel/model/schemas.go | 15 +++++++++------ .../apiserver/schema/cel/model/schemas_test.go | 2 +- staging/src/k8s.io/apiserver/pkg/cel/limits.go | 5 ++--- staging/src/k8s.io/apiserver/pkg/server/config.go | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas.go index cd2770af557..b9b47caea50 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas.go @@ -27,6 +27,9 @@ import ( "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 // structural schema should not be exposed in CEL expressions. // 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 // 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 } @@ -229,7 +232,7 @@ func WithTypeAndObjectMeta(s *schema.Structural) *schema.Structural { // this function. func MaxCardinality(minSize int64) uint64 { 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) @@ -238,7 +241,7 @@ func MaxCardinality(minSize int64) uint64 { func estimateMaxStringLengthPerRequest(s *schema.Structural) int64 { if s.ValueValidation == nil || s.XIntOrString { // subtract 2 to account for "" - return apiservercel.MaxRequestSizeBytes - 2 + return maxRequestSizeBytes - 2 } switch s.ValueValidation.Format { case "duration": @@ -249,7 +252,7 @@ func estimateMaxStringLengthPerRequest(s *schema.Structural) int64 { return apiservercel.MaxDatetimeSizeJSON default: // 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. func estimateMaxArrayItemsFromMinSize(minSize int64) int64 { // 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 @@ -267,5 +270,5 @@ func estimateMaxAdditionalPropertiesFromMinSize(minSize int64) int64 { // will all vary in length keyValuePairSize := minSize + 6 // subtract 2 to account for { and } - return (apiservercel.MaxRequestSizeBytes - 2) / keyValuePairSize + return (maxRequestSizeBytes - 2) / keyValuePairSize } diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas_test.go index ee57293e86d..04ae3a1a415 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/model/schemas_test.go @@ -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) - ExpectedMaxElements: apiservercel.MaxRequestSizeBytes - 2, + ExpectedMaxElements: apiservercel.DefaultMaxRequestSizeBytes - 2, }, { Name: "objectDefaultFieldArray", diff --git a/staging/src/k8s.io/apiserver/pkg/cel/limits.go b/staging/src/k8s.io/apiserver/pkg/cel/limits.go index c38a47cea09..7bdb958d05c 100644 --- a/staging/src/k8s.io/apiserver/pkg/cel/limits.go +++ b/staging/src/k8s.io/apiserver/pkg/cel/limits.go @@ -17,9 +17,8 @@ limitations under the License. package cel const ( - // MaxRequestSizeBytes is the largest request that will be accepted is 3MB - // TODO(DangerOnTheRanger): wire in MaxRequestBodyBytes from apiserver/pkg/server/options/server_run_options.go to make this configurable - MaxRequestSizeBytes = int64(3 * 1024 * 1024) + // DefaultMaxRequestSizeBytes is the size of the largest request that will be accepted + DefaultMaxRequestSizeBytes = int64(3 * 1024 * 1024) // MaxDurationSizeJSON // OpenAPI duration strings follow RFC 3339, section 5.6 - see the comment on maxDatetimeSizeJSON diff --git a/staging/src/k8s.io/apiserver/pkg/server/config.go b/staging/src/k8s.io/apiserver/pkg/server/config.go index 0248fdd0a95..1d0753ea5df 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/config.go +++ b/staging/src/k8s.io/apiserver/pkg/server/config.go @@ -366,7 +366,7 @@ func NewConfig(codecs serializer.CodecFactory) *Config { // 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 // 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 // been wired together already somehow. MaxRequestBodyBytes: int64(3 * 1024 * 1024),