Skip to content

Commit 4d0e1c8

Browse files
committed
Kill TooLongMaxLength() in favor of TooLong()
1 parent 50998de commit 4d0e1c8

File tree

11 files changed

+26
-28
lines changed

11 files changed

+26
-28
lines changed

pkg/apis/batch/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func validateJobSpec(spec *batch.JobSpec, fldPath *field.Path, opts apivalidatio
218218
if spec.ManagedBy != nil {
219219
allErrs = append(allErrs, apimachineryvalidation.IsDomainPrefixedPath(fldPath.Child("managedBy"), *spec.ManagedBy)...)
220220
if len(*spec.ManagedBy) > maxManagedByLength {
221-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("managedBy"), *spec.ManagedBy, maxManagedByLength))
221+
allErrs = append(allErrs, field.TooLong(fldPath.Child("managedBy"), *spec.ManagedBy, maxManagedByLength))
222222
}
223223
}
224224
if spec.CompletionMode != nil {

pkg/apis/batch/validation/validation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func TestValidateJob(t *testing.T) {
436436
opts JobValidationOptions
437437
job batch.Job
438438
}{
439-
`spec.managedBy: Too long: may not be longer than 63`: {
439+
`spec.managedBy: Too long: may not be more than 63 bytes`: {
440440
opts: JobValidationOptions{RequirePrefixedLabels: true},
441441
job: batch.Job{
442442
ObjectMeta: validJobObjectMeta,
@@ -519,7 +519,7 @@ func TestValidateJob(t *testing.T) {
519519
},
520520
opts: JobValidationOptions{RequirePrefixedLabels: true},
521521
},
522-
`spec.successPolicy.rules[0].succeededIndexes: Too long: must have at most 65536 bytes`: {
522+
`spec.successPolicy.rules[0].succeededIndexes: Too long: may not be more than 65536 bytes`: {
523523
job: batch.Job{
524524
ObjectMeta: validJobObjectMeta,
525525
Spec: batch.JobSpec{

pkg/apis/core/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4758,7 +4758,7 @@ func ValidateAppArmorProfileField(profile *core.AppArmorProfile, fldPath *field.
47584758

47594759
const maxLocalhostProfileLength = 4095 // PATH_MAX - 1
47604760
if len(*profile.LocalhostProfile) > maxLocalhostProfileLength {
4761-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("localhostProfile"), *profile.LocalhostProfile, maxLocalhostProfileLength))
4761+
allErrs = append(allErrs, field.TooLong(fldPath.Child("localhostProfile"), *profile.LocalhostProfile, maxLocalhostProfileLength))
47624762
}
47634763
}
47644764

pkg/apis/core/validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11926,7 +11926,7 @@ func TestValidatePod(t *testing.T) {
1192611926
),
1192711927
},
1192811928
"too long AppArmor localhost profile": {
11929-
expectedError: "Too long: may not be longer than 4095",
11929+
expectedError: "Too long: may not be more than 4095 bytes",
1193011930
spec: *podtest.MakePod("123",
1193111931
podtest.SetSecurityContext(&core.PodSecurityContext{
1193211932
AppArmorProfile: &core.AppArmorProfile{

pkg/apis/resource/validation/validation.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func validatePoolName(name string, fldPath *field.Path) field.ErrorList {
5252
allErrs = append(allErrs, field.Required(fldPath, ""))
5353
} else {
5454
if len(name) > resource.PoolNameMaxLength {
55-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath, name, resource.PoolNameMaxLength))
55+
allErrs = append(allErrs, field.TooLong(fldPath, name, resource.PoolNameMaxLength))
5656
}
5757
parts := strings.Split(name, "/")
5858
for _, part := range parts {
@@ -168,7 +168,7 @@ func validateCELSelector(celSelector resource.CELDeviceSelector, fldPath *field.
168168
envType = environment.StoredExpressions
169169
}
170170
if len(celSelector.Expression) > resource.CELSelectorExpressionMaxLength {
171-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("expression"), "<value omitted>", resource.CELSelectorExpressionMaxLength))
171+
allErrs = append(allErrs, field.TooLong(fldPath.Child("expression"), "<value omitted>", resource.CELSelectorExpressionMaxLength))
172172
// Don't bother compiling too long expressions.
173173
return allErrs
174174
}
@@ -561,7 +561,7 @@ func validateDeviceAttribute(attribute resource.DeviceAttribute, fldPath *field.
561561
}
562562
if attribute.StringValue != nil {
563563
if len(*attribute.StringValue) > resource.DeviceAttributeMaxValueLength {
564-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("string"), *attribute.StringValue, resource.DeviceAttributeMaxValueLength))
564+
allErrs = append(allErrs, field.TooLong(fldPath.Child("string"), *attribute.StringValue, resource.DeviceAttributeMaxValueLength))
565565
}
566566
numFields++
567567
}
@@ -571,7 +571,7 @@ func validateDeviceAttribute(attribute resource.DeviceAttribute, fldPath *field.
571571
allErrs = append(allErrs, field.Invalid(fldPath.Child("version"), *attribute.VersionValue, "must be a string compatible with semver.org spec 2.0.0"))
572572
}
573573
if len(*attribute.VersionValue) > resource.DeviceAttributeMaxValueLength {
574-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath.Child("version"), *attribute.VersionValue, resource.DeviceAttributeMaxValueLength))
574+
allErrs = append(allErrs, field.TooLong(fldPath.Child("version"), *attribute.VersionValue, resource.DeviceAttributeMaxValueLength))
575575
}
576576
}
577577

@@ -628,7 +628,7 @@ func validateFullyQualifiedName(name resource.FullyQualifiedName, fldPath *field
628628
func validateCIdentifier(id string, fldPath *field.Path) field.ErrorList {
629629
var allErrs field.ErrorList
630630
if len(id) > resource.DeviceMaxIDLength {
631-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath, id, resource.DeviceMaxIDLength))
631+
allErrs = append(allErrs, field.TooLong(fldPath, id, resource.DeviceMaxIDLength))
632632
}
633633
for _, msg := range validation.IsCIdentifier(id) {
634634
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
649649
// Dumping the entire field into the error message is likely to be too long,
650650
// in particular when it is already beyond the maximum size. Instead this
651651
// just shows the number of entries.
652-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath, len(slice), maxSize))
652+
allErrs = append(allErrs, field.TooMany(fldPath, len(slice), maxSize))
653653
}
654654
return allErrs
655655
}
@@ -685,7 +685,7 @@ func stringKey(item string) (string, string) {
685685
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 {
686686
var allErrs field.ErrorList
687687
if maxSize >= 0 && len(m) > maxSize {
688-
allErrs = append(allErrs, field.TooLongMaxLength(fldPath, len(m), maxSize))
688+
allErrs = append(allErrs, field.TooMany(fldPath, len(m), maxSize))
689689
}
690690
for key, item := range m {
691691
allErrs = append(allErrs, validateKey(key, fldPath)...)

pkg/apis/resource/validation/validation_resourceclaim_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func TestValidateClaim(t *testing.T) {
322322
},
323323
"CEL-length": {
324324
wantFailures: field.ErrorList{
325-
field.TooLongMaxLength(field.NewPath("spec", "devices", "requests").Index(1).Child("selectors").Index(1).Child("cel", "expression"), "<value omitted>", resource.CELSelectorExpressionMaxLength),
325+
field.TooLong(field.NewPath("spec", "devices", "requests").Index(1).Child("selectors").Index(1).Child("cel", "expression"), "<value omitted>", resource.CELSelectorExpressionMaxLength),
326326
},
327327
claim: func() *resource.ResourceClaim {
328328
claim := testClaim(goodName, goodNS, validClaimSpec)
@@ -552,7 +552,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
552552
},
553553
},
554554
"invalid-reserved-for-too-large": {
555-
wantFailures: field.ErrorList{field.TooLongMaxLength(field.NewPath("status", "reservedFor"), resource.ResourceClaimReservedForMaxSize+1, resource.ResourceClaimReservedForMaxSize)},
555+
wantFailures: field.ErrorList{field.TooMany(field.NewPath("status", "reservedFor"), resource.ResourceClaimReservedForMaxSize+1, resource.ResourceClaimReservedForMaxSize)},
556556
oldClaim: validAllocatedClaim,
557557
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
558558
for i := 0; i < resource.ResourceClaimReservedForMaxSize+1; i++ {

pkg/controlplane/controller/clusterauthenticationtrust/cluster_authentication_trust_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func TestWriteConfigMapDeleted(t *testing.T) {
381381
})
382382

383383
err := writeConfigMap(client.CoreV1(), cm)
384-
if err == nil || err.Error() != `ConfigMap "extension-apiserver-authentication" is invalid: []: Too long: must have at most 1048576 bytes` {
384+
if err == nil || err.Error() != `ConfigMap "extension-apiserver-authentication" is invalid: []: Too long: may not be more than 1048576 bytes` {
385385
t.Fatal(err)
386386
}
387387
if len(client.Actions()) != 2 {

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func kubeOpenAPIResultToFieldErrors(fldPath *field.Path, result *validate.Result
190190
if i, ok := err.Valid.(int64); ok {
191191
max = i
192192
}
193-
allErrs = append(allErrs, field.TooLongMaxLength(errPath, value, int(max)))
193+
allErrs = append(allErrs, field.TooLong(errPath, value, int(max)))
194194

195195
case openapierrors.MaxItemsFailCode:
196196
actual := int64(-1)

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/validation/validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func TestValidateCustomResource(t *testing.T) {
594594
},
595595
failingObjects: []failingObject{
596596
{object: map[string]interface{}{"fieldX": "abc"}, expectErrs: []string{
597-
`fieldX: Too long: may not be longer than 2`,
597+
`fieldX: Too long: may not be more than 2 bytes`,
598598
}},
599599
},
600600
},

staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,25 +223,23 @@ func Forbidden(field *Path, detail string) *Error {
223223
// TooLong returns a *Error indicating "too long". This is used to
224224
// report that the given value is too long. This is similar to
225225
// Invalid, but the returned error will not include the too-long
226-
// value.
226+
// value. If maxLength is negative, it will be included in the message.
227227
func TooLong(field *Path, value interface{}, maxLength int) *Error {
228-
return &Error{ErrorTypeTooLong, field.String(), value, fmt.Sprintf("must have at most %d bytes", maxLength)}
229-
}
230-
231-
// TooLongMaxLength returns a *Error indicating "too long". This is used to
232-
// report that the given value is too long. This is similar to
233-
// Invalid, but the returned error will not include the too-long
234-
// value. If maxLength is negative, no max length will be included in the message.
235-
func TooLongMaxLength(field *Path, value interface{}, maxLength int) *Error {
236228
var msg string
237229
if maxLength >= 0 {
238-
msg = fmt.Sprintf("may not be longer than %d", maxLength)
230+
msg = fmt.Sprintf("may not be more than %d bytes", maxLength)
239231
} else {
240232
msg = "value is too long"
241233
}
242234
return &Error{ErrorTypeTooLong, field.String(), value, msg}
243235
}
244236

237+
// TooLongMaxLength returns a *Error indicating "too long".
238+
// Deprecated: Use TooLong instead.
239+
func TooLongMaxLength(field *Path, value interface{}, maxLength int) *Error {
240+
return TooLong(field, "", maxLength)
241+
}
242+
245243
// TooMany returns a *Error indicating "too many". This is used to
246244
// report that a given list has too many items. This is similar to TooLong,
247245
// but the returned error indicates quantity instead of length.

0 commit comments

Comments
 (0)