What is the syntax of annotation values in Kubernetes?

7/6/2021

According to Kubernetes documentation

The metadata in an annotation can be small or large, structured or unstructured, and can include characters not permitted by labels.

Annotations, like labels, are key/value maps

Then there is a detailed explanation on the syntax of the annotation keys. But it says nothing about the value part.

Where can I find more about the allowed length and character set for the value of an annotation in Kubernetes?

-- Thomas
kubernetes

1 Answer

7/6/2021

Here you can find the code that validates annotations in current master:

func ValidateAnnotations(annotations map[string]string, fldPath *field.Path) field.ErrorList {
	allErrs := field.ErrorList{}
	for k := range annotations {
		for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) {
			allErrs = append(allErrs, field.Invalid(fldPath, k, msg))
		}
	}
	if err := ValidateAnnotationsSize(annotations); err != nil {
		allErrs = append(allErrs, field.TooLong(fldPath, "", TotalAnnotationSizeLimitB))
	}
	return allErrs
}

The keys are validated according to the rules that you mentioned. The only validation applied to the values is the total length of all annotations (size of keys + size of values for all annotations) that can't be longer than 256Kb.

const TotalAnnotationSizeLimitB int = 256 * (1 << 10) // 256 kB

...

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
}
-- whites11
Source: StackOverflow