mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Make a public ValidateAnnotationsSize
This commit is contained in:
parent
55ff963017
commit
2e771b8e74
@ -46,19 +46,28 @@ var ValidateClusterName = NameIsDNS1035Label
|
|||||||
// ValidateAnnotations validates that a set of annotations are correctly defined.
|
// ValidateAnnotations validates that a set of annotations are correctly defined.
|
||||||
func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) field.ErrorList {
|
func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) field.ErrorList {
|
||||||
allErrs := field.ErrorList{}
|
allErrs := field.ErrorList{}
|
||||||
var totalSize int64
|
for k := range annotations {
|
||||||
for k, v := range annotations {
|
|
||||||
for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) {
|
for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) {
|
||||||
allErrs = append(allErrs, field.Invalid(fldPath, k, msg))
|
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))
|
allErrs = append(allErrs, field.TooLong(fldPath, "", TotalAnnotationSizeLimitB))
|
||||||
}
|
}
|
||||||
return allErrs
|
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 {
|
func validateOwnerReference(ownerReference metav1.OwnerReference, fldPath *field.Path) field.ErrorList {
|
||||||
allErrs := field.ErrorList{}
|
allErrs := field.ErrorList{}
|
||||||
gvk := schema.FromAPIVersionAndKind(ownerReference.APIVersion, ownerReference.Kind)
|
gvk := schema.FromAPIVersionAndKind(ownerReference.APIVersion, ownerReference.Kind)
|
||||||
|
@ -93,7 +93,7 @@ func setLastApplied(obj runtime.Object, value string) error {
|
|||||||
annotations = map[string]string{}
|
annotations = map[string]string{}
|
||||||
}
|
}
|
||||||
annotations[corev1.LastAppliedConfigAnnotation] = value
|
annotations[corev1.LastAppliedConfigAnnotation] = value
|
||||||
if isAnnotationsValid(annotations) != nil {
|
if err := apimachineryvalidation.ValidateAnnotationsSize(annotations); err != nil {
|
||||||
delete(annotations, corev1.LastAppliedConfigAnnotation)
|
delete(annotations, corev1.LastAppliedConfigAnnotation)
|
||||||
}
|
}
|
||||||
accessor.SetAnnotations(annotations)
|
accessor.SetAnnotations(annotations)
|
||||||
@ -119,14 +119,3 @@ func buildLastApplied(obj runtime.Object) (string, error) {
|
|||||||
}
|
}
|
||||||
return string(lastApplied), nil
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user