Kill TooLongMaxLength() in favor of TooLong()

This commit is contained in:
Tim Hockin 2024-11-04 15:16:29 -08:00
parent 50998de605
commit 4d0e1c8fd4
No known key found for this signature in database
11 changed files with 26 additions and 28 deletions

View File

@ -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 {

View File

@ -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{

View File

@ -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))
}
}

View File

@ -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{

View File

@ -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"), "<value omitted>", resource.CELSelectorExpressionMaxLength))
allErrs = append(allErrs, field.TooLong(fldPath.Child("expression"), "<value omitted>", 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)...)

View File

@ -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"), "<value omitted>", resource.CELSelectorExpressionMaxLength),
field.TooLong(field.NewPath("spec", "devices", "requests").Index(1).Child("selectors").Index(1).Child("cel", "expression"), "<value omitted>", 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++ {

View File

@ -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 {

View File

@ -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)

View File

@ -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`,
}},
},
},

View File

@ -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.

View File

@ -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