From 2e771b8e745c4a3be0d5bae3a6dc94087284c73b Mon Sep 17 00:00:00 2001 From: "Julian V. Modesto" Date: Tue, 25 May 2021 16:01:38 -0400 Subject: [PATCH] Make a public ValidateAnnotationsSize --- .../pkg/api/validation/objectmeta.go | 17 +++++++++++++---- .../handlers/fieldmanager/lastappliedupdater.go | 13 +------------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta.go b/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta.go index 9530419db7e..4d9efe3f72b 100644 --- a/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta.go +++ b/staging/src/k8s.io/apimachinery/pkg/api/validation/objectmeta.go @@ -46,19 +46,28 @@ var ValidateClusterName = NameIsDNS1035Label // ValidateAnnotations validates that a set of annotations are correctly defined. func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} - var totalSize int64 - for k, v := range annotations { + for k := range annotations { for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) { allErrs = append(allErrs, field.Invalid(fldPath, k, msg)) } - totalSize += (int64)(len(k)) + (int64)(len(v)) } - if totalSize > (int64)(TotalAnnotationSizeLimitB) { + if err := ValidateAnnotationsSize(annotations); err != nil { allErrs = append(allErrs, field.TooLong(fldPath, "", TotalAnnotationSizeLimitB)) } return allErrs } +func ValidateAnnotationsSize(annotations map[string]string) error { + var totalSize int64 + for k, v := range annotations { + totalSize += (int64)(len(k)) + (int64)(len(v)) + } + if totalSize > (int64)(TotalAnnotationSizeLimitB) { + return fmt.Errorf("annotations size %d is larger than limit %d", totalSize, TotalAnnotationSizeLimitB) + } + return nil +} + func validateOwnerReference(ownerReference metav1.OwnerReference, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} gvk := schema.FromAPIVersionAndKind(ownerReference.APIVersion, ownerReference.Kind) diff --git a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/lastappliedupdater.go b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/lastappliedupdater.go index 551661a0610..7cd4eb1289b 100644 --- a/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/lastappliedupdater.go +++ b/staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/lastappliedupdater.go @@ -93,7 +93,7 @@ func setLastApplied(obj runtime.Object, value string) error { annotations = map[string]string{} } annotations[corev1.LastAppliedConfigAnnotation] = value - if isAnnotationsValid(annotations) != nil { + if err := apimachineryvalidation.ValidateAnnotationsSize(annotations); err != nil { delete(annotations, corev1.LastAppliedConfigAnnotation) } accessor.SetAnnotations(annotations) @@ -119,14 +119,3 @@ func buildLastApplied(obj runtime.Object) (string, error) { } return string(lastApplied), nil } - -func isAnnotationsValid(annotations map[string]string) error { - var totalSize int64 - for k, v := range annotations { - totalSize += (int64)(len(k)) + (int64)(len(v)) - } - if totalSize > (int64)(apimachineryvalidation.TotalAnnotationSizeLimitB) { - return fmt.Errorf("annotations size %d is larger than limit %d", totalSize, apimachineryvalidation.TotalAnnotationSizeLimitB) - } - return nil -}