diff --git a/pkg/apis/batch/validation/validation.go b/pkg/apis/batch/validation/validation.go index 0f83ac28d3c..86b80394cce 100644 --- a/pkg/apis/batch/validation/validation.go +++ b/pkg/apis/batch/validation/validation.go @@ -218,7 +218,7 @@ func validateJobSpec(spec *batch.JobSpec, fldPath *field.Path, opts apivalidatio if spec.ManagedBy != nil { allErrs = append(allErrs, apimachineryvalidation.IsDomainPrefixedPath(fldPath.Child("managedBy"), *spec.ManagedBy)...) if len(*spec.ManagedBy) > maxManagedByLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("managedBy"), *spec.ManagedBy, maxManagedByLength)) + allErrs = append(allErrs, field.TooLong(fldPath.Child("managedBy"), *spec.ManagedBy, maxManagedByLength)) } } if spec.CompletionMode != nil { diff --git a/pkg/apis/batch/validation/validation_test.go b/pkg/apis/batch/validation/validation_test.go index 64c50e50318..3f23cf0ae3c 100644 --- a/pkg/apis/batch/validation/validation_test.go +++ b/pkg/apis/batch/validation/validation_test.go @@ -436,7 +436,7 @@ func TestValidateJob(t *testing.T) { opts JobValidationOptions job batch.Job }{ - `spec.managedBy: Too long: may not be longer than 63`: { + `spec.managedBy: Too long: may not be more than 63 bytes`: { opts: JobValidationOptions{RequirePrefixedLabels: true}, job: batch.Job{ ObjectMeta: validJobObjectMeta, @@ -519,7 +519,7 @@ func TestValidateJob(t *testing.T) { }, opts: JobValidationOptions{RequirePrefixedLabels: true}, }, - `spec.successPolicy.rules[0].succeededIndexes: Too long: must have at most 65536 bytes`: { + `spec.successPolicy.rules[0].succeededIndexes: Too long: may not be more than 65536 bytes`: { job: batch.Job{ ObjectMeta: validJobObjectMeta, Spec: batch.JobSpec{ diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index 34e3002155f..9cb0a614c1b 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -4758,7 +4758,7 @@ func ValidateAppArmorProfileField(profile *core.AppArmorProfile, fldPath *field. const maxLocalhostProfileLength = 4095 // PATH_MAX - 1 if len(*profile.LocalhostProfile) > maxLocalhostProfileLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("localhostProfile"), *profile.LocalhostProfile, maxLocalhostProfileLength)) + allErrs = append(allErrs, field.TooLong(fldPath.Child("localhostProfile"), *profile.LocalhostProfile, maxLocalhostProfileLength)) } } diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index 0d24d54f0e1..24cfc3112a6 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -11926,7 +11926,7 @@ func TestValidatePod(t *testing.T) { ), }, "too long AppArmor localhost profile": { - expectedError: "Too long: may not be longer than 4095", + expectedError: "Too long: may not be more than 4095 bytes", spec: *podtest.MakePod("123", podtest.SetSecurityContext(&core.PodSecurityContext{ AppArmorProfile: &core.AppArmorProfile{ diff --git a/pkg/apis/resource/validation/validation.go b/pkg/apis/resource/validation/validation.go index d451f3912c8..022cbecc02f 100644 --- a/pkg/apis/resource/validation/validation.go +++ b/pkg/apis/resource/validation/validation.go @@ -52,7 +52,7 @@ func validatePoolName(name string, fldPath *field.Path) field.ErrorList { allErrs = append(allErrs, field.Required(fldPath, "")) } else { if len(name) > resource.PoolNameMaxLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath, name, resource.PoolNameMaxLength)) + allErrs = append(allErrs, field.TooLong(fldPath, name, resource.PoolNameMaxLength)) } parts := strings.Split(name, "/") for _, part := range parts { @@ -168,7 +168,7 @@ func validateCELSelector(celSelector resource.CELDeviceSelector, fldPath *field. envType = environment.StoredExpressions } if len(celSelector.Expression) > resource.CELSelectorExpressionMaxLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("expression"), "", resource.CELSelectorExpressionMaxLength)) + allErrs = append(allErrs, field.TooLong(fldPath.Child("expression"), "", resource.CELSelectorExpressionMaxLength)) // Don't bother compiling too long expressions. return allErrs } @@ -561,7 +561,7 @@ func validateDeviceAttribute(attribute resource.DeviceAttribute, fldPath *field. } if attribute.StringValue != nil { if len(*attribute.StringValue) > resource.DeviceAttributeMaxValueLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("string"), *attribute.StringValue, resource.DeviceAttributeMaxValueLength)) + allErrs = append(allErrs, field.TooLong(fldPath.Child("string"), *attribute.StringValue, resource.DeviceAttributeMaxValueLength)) } numFields++ } @@ -571,7 +571,7 @@ func validateDeviceAttribute(attribute resource.DeviceAttribute, fldPath *field. allErrs = append(allErrs, field.Invalid(fldPath.Child("version"), *attribute.VersionValue, "must be a string compatible with semver.org spec 2.0.0")) } if len(*attribute.VersionValue) > resource.DeviceAttributeMaxValueLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("version"), *attribute.VersionValue, resource.DeviceAttributeMaxValueLength)) + allErrs = append(allErrs, field.TooLong(fldPath.Child("version"), *attribute.VersionValue, resource.DeviceAttributeMaxValueLength)) } } @@ -628,7 +628,7 @@ func validateFullyQualifiedName(name resource.FullyQualifiedName, fldPath *field func validateCIdentifier(id string, fldPath *field.Path) field.ErrorList { var allErrs field.ErrorList if len(id) > resource.DeviceMaxIDLength { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath, id, resource.DeviceMaxIDLength)) + allErrs = append(allErrs, field.TooLong(fldPath, id, resource.DeviceMaxIDLength)) } for _, msg := range validation.IsCIdentifier(id) { allErrs = append(allErrs, field.TypeInvalid(fldPath, id, msg)) @@ -649,7 +649,7 @@ func validateSlice[T any](slice []T, maxSize int, validateItem func(T, *field.Pa // Dumping the entire field into the error message is likely to be too long, // in particular when it is already beyond the maximum size. Instead this // just shows the number of entries. - allErrs = append(allErrs, field.TooLongMaxLength(fldPath, len(slice), maxSize)) + allErrs = append(allErrs, field.TooMany(fldPath, len(slice), maxSize)) } return allErrs } @@ -685,7 +685,7 @@ func stringKey(item string) (string, string) { func validateMap[K ~string, T any](m map[K]T, maxSize int, validateKey func(K, *field.Path) field.ErrorList, validateItem func(T, *field.Path) field.ErrorList, fldPath *field.Path) field.ErrorList { var allErrs field.ErrorList if maxSize >= 0 && len(m) > maxSize { - allErrs = append(allErrs, field.TooLongMaxLength(fldPath, len(m), maxSize)) + allErrs = append(allErrs, field.TooMany(fldPath, len(m), maxSize)) } for key, item := range m { allErrs = append(allErrs, validateKey(key, fldPath)...) diff --git a/pkg/apis/resource/validation/validation_resourceclaim_test.go b/pkg/apis/resource/validation/validation_resourceclaim_test.go index 516fa6f9dfc..ad9a7a6cb11 100644 --- a/pkg/apis/resource/validation/validation_resourceclaim_test.go +++ b/pkg/apis/resource/validation/validation_resourceclaim_test.go @@ -322,7 +322,7 @@ func TestValidateClaim(t *testing.T) { }, "CEL-length": { wantFailures: field.ErrorList{ - field.TooLongMaxLength(field.NewPath("spec", "devices", "requests").Index(1).Child("selectors").Index(1).Child("cel", "expression"), "", resource.CELSelectorExpressionMaxLength), + field.TooLong(field.NewPath("spec", "devices", "requests").Index(1).Child("selectors").Index(1).Child("cel", "expression"), "", resource.CELSelectorExpressionMaxLength), }, claim: func() *resource.ResourceClaim { claim := testClaim(goodName, goodNS, validClaimSpec) @@ -552,7 +552,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) { }, }, "invalid-reserved-for-too-large": { - wantFailures: field.ErrorList{field.TooLongMaxLength(field.NewPath("status", "reservedFor"), resource.ResourceClaimReservedForMaxSize+1, resource.ResourceClaimReservedForMaxSize)}, + wantFailures: field.ErrorList{field.TooMany(field.NewPath("status", "reservedFor"), resource.ResourceClaimReservedForMaxSize+1, resource.ResourceClaimReservedForMaxSize)}, oldClaim: validAllocatedClaim, update: func(claim *resource.ResourceClaim) *resource.ResourceClaim { for i := 0; i < resource.ResourceClaimReservedForMaxSize+1; i++ { diff --git a/pkg/controlplane/controller/clusterauthenticationtrust/cluster_authentication_trust_controller_test.go b/pkg/controlplane/controller/clusterauthenticationtrust/cluster_authentication_trust_controller_test.go index d593799ee82..b9181104801 100644 --- a/pkg/controlplane/controller/clusterauthenticationtrust/cluster_authentication_trust_controller_test.go +++ b/pkg/controlplane/controller/clusterauthenticationtrust/cluster_authentication_trust_controller_test.go @@ -381,7 +381,7 @@ func TestWriteConfigMapDeleted(t *testing.T) { }) err := writeConfigMap(client.CoreV1(), cm) - if err == nil || err.Error() != `ConfigMap "extension-apiserver-authentication" is invalid: []: Too long: must have at most 1048576 bytes` { + if err == nil || err.Error() != `ConfigMap "extension-apiserver-authentication" is invalid: []: Too long: may not be more than 1048576 bytes` { t.Fatal(err) } if len(client.Actions()) != 2 { diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation.go index 7304018fb34..9c49f39841d 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation.go @@ -190,7 +190,7 @@ func kubeOpenAPIResultToFieldErrors(fldPath *field.Path, result *validate.Result if i, ok := err.Valid.(int64); ok { max = i } - allErrs = append(allErrs, field.TooLongMaxLength(errPath, value, int(max))) + allErrs = append(allErrs, field.TooLong(errPath, value, int(max))) case openapierrors.MaxItemsFailCode: actual := int64(-1) diff --git a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go index d1b9da2e5df..27a10f4dc05 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go +++ b/staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go @@ -594,7 +594,7 @@ func TestValidateCustomResource(t *testing.T) { }, failingObjects: []failingObject{ {object: map[string]interface{}{"fieldX": "abc"}, expectErrs: []string{ - `fieldX: Too long: may not be longer than 2`, + `fieldX: Too long: may not be more than 2 bytes`, }}, }, }, diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go index bc387d01163..3c2db9a32b4 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go @@ -223,25 +223,23 @@ func Forbidden(field *Path, detail string) *Error { // TooLong returns a *Error indicating "too long". This is used to // report that the given value is too long. This is similar to // Invalid, but the returned error will not include the too-long -// value. +// value. If maxLength is negative, it will be included in the message. func TooLong(field *Path, value interface{}, maxLength int) *Error { - return &Error{ErrorTypeTooLong, field.String(), value, fmt.Sprintf("must have at most %d bytes", maxLength)} -} - -// TooLongMaxLength returns a *Error indicating "too long". This is used to -// report that the given value is too long. This is similar to -// Invalid, but the returned error will not include the too-long -// value. If maxLength is negative, no max length will be included in the message. -func TooLongMaxLength(field *Path, value interface{}, maxLength int) *Error { var msg string if maxLength >= 0 { - msg = fmt.Sprintf("may not be longer than %d", maxLength) + msg = fmt.Sprintf("may not be more than %d bytes", maxLength) } else { msg = "value is too long" } return &Error{ErrorTypeTooLong, field.String(), value, msg} } +// TooLongMaxLength returns a *Error indicating "too long". +// Deprecated: Use TooLong instead. +func TooLongMaxLength(field *Path, value interface{}, maxLength int) *Error { + return TooLong(field, "", maxLength) +} + // TooMany returns a *Error indicating "too many". This is used to // report that a given list has too many items. This is similar to TooLong, // but the returned error indicates quantity instead of length. diff --git a/test/integration/controlplane/synthetic_controlplane_test.go b/test/integration/controlplane/synthetic_controlplane_test.go index c5f661df736..615b35e62ed 100644 --- a/test/integration/controlplane/synthetic_controlplane_test.go +++ b/test/integration/controlplane/synthetic_controlplane_test.go @@ -338,7 +338,7 @@ func TestObjectSizeResponses(t *testing.T) { expectedMsgFor1MB := `etcdserver: request is too large` expectedMsgFor2MB := `rpc error: code = ResourceExhausted desc = trying to send message larger than max` expectedMsgFor3MB := `Request entity too large: limit is 3145728` - expectedMsgForLargeAnnotation := `metadata.annotations: Too long: must have at most 262144 bytes` + expectedMsgForLargeAnnotation := `metadata.annotations: Too long: may not be more than 262144 bytes` deployment1 := constructBody("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", DeploymentMegabyteSize, "labels", t) // >1.5 MB file deployment2 := constructBody("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", DeploymentTwoMegabyteSize, "labels", t) // >2 MB file