Make validation totalAnnotationSizeLimitB public.

Replace the forked totalAnnotationSizeLimitB with
apimachineryvalidation.TotalAnnotationSizeLimitB.
This commit is contained in:
Julian V. Modesto 2021-05-18 17:28:11 -04:00
parent 851d37ed40
commit 55ff963017
3 changed files with 12 additions and 13 deletions

View File

@ -33,7 +33,7 @@ import (
// FieldImmutableErrorMsg is a error message for field is immutable. // FieldImmutableErrorMsg is a error message for field is immutable.
const FieldImmutableErrorMsg string = `field is immutable` const FieldImmutableErrorMsg string = `field is immutable`
const totalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB const TotalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB
// BannedOwners is a black list of object that are not allowed to be owners. // BannedOwners is a black list of object that are not allowed to be owners.
var BannedOwners = map[schema.GroupVersionKind]struct{}{ var BannedOwners = map[schema.GroupVersionKind]struct{}{
@ -53,8 +53,8 @@ func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) fie
} }
totalSize += (int64)(len(k)) + (int64)(len(v)) totalSize += (int64)(len(k)) + (int64)(len(v))
} }
if totalSize > (int64)(totalAnnotationSizeLimitB) { if totalSize > (int64)(TotalAnnotationSizeLimitB) {
allErrs = append(allErrs, field.TooLong(fldPath, "", totalAnnotationSizeLimitB)) allErrs = append(allErrs, field.TooLong(fldPath, "", TotalAnnotationSizeLimitB))
} }
return allErrs return allErrs
} }

View File

@ -452,10 +452,10 @@ func TestValidateAnnotations(t *testing.T) {
{"1234/5678": "bar"}, {"1234/5678": "bar"},
{"1.2.3.4/5678": "bar"}, {"1.2.3.4/5678": "bar"},
{"UpperCase123": "bar"}, {"UpperCase123": "bar"},
{"a": strings.Repeat("b", totalAnnotationSizeLimitB-1)}, {"a": strings.Repeat("b", TotalAnnotationSizeLimitB-1)},
{ {
"a": strings.Repeat("b", totalAnnotationSizeLimitB/2-1), "a": strings.Repeat("b", TotalAnnotationSizeLimitB/2-1),
"c": strings.Repeat("d", totalAnnotationSizeLimitB/2-1), "c": strings.Repeat("d", TotalAnnotationSizeLimitB/2-1),
}, },
} }
for i := range successCases { for i := range successCases {
@ -485,10 +485,10 @@ func TestValidateAnnotations(t *testing.T) {
} }
} }
totalSizeErrorCases := []map[string]string{ totalSizeErrorCases := []map[string]string{
{"a": strings.Repeat("b", totalAnnotationSizeLimitB)}, {"a": strings.Repeat("b", TotalAnnotationSizeLimitB)},
{ {
"a": strings.Repeat("b", totalAnnotationSizeLimitB/2), "a": strings.Repeat("b", TotalAnnotationSizeLimitB/2),
"c": strings.Repeat("d", totalAnnotationSizeLimitB/2), "c": strings.Repeat("d", TotalAnnotationSizeLimitB/2),
}, },
} }
for i := range totalSizeErrorCases { for i := range totalSizeErrorCases {

View File

@ -21,12 +21,11 @@ import (
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
) )
const totalAnnotationSizeLimitB int64 = 256 * (1 << 10) // 256 kB
type lastAppliedUpdater struct { type lastAppliedUpdater struct {
fieldManager Manager fieldManager Manager
} }
@ -126,8 +125,8 @@ func isAnnotationsValid(annotations map[string]string) error {
for k, v := range annotations { for k, v := range annotations {
totalSize += (int64)(len(k)) + (int64)(len(v)) totalSize += (int64)(len(k)) + (int64)(len(v))
} }
if totalSize > (int64)(totalAnnotationSizeLimitB) { if totalSize > (int64)(apimachineryvalidation.TotalAnnotationSizeLimitB) {
return fmt.Errorf("annotations size %d is larger than limit %d", totalSize, totalAnnotationSizeLimitB) return fmt.Errorf("annotations size %d is larger than limit %d", totalSize, apimachineryvalidation.TotalAnnotationSizeLimitB)
} }
return nil return nil
} }