The pkg/api/validate package provides functions to validate fields and types in the Kubernetes API. Most public validator functions follow a standardized signature designed for automation and code generation. When implementing or calling these validators, you should expect the following pattern:
func <Name>(ctx context.Context, op operation.Operation, fldPath *field.Path, value, oldValue <ValueType>, <OtherArgs...>) field.ErrorList
Argument Details:
ctx: Standard Go context.Context.op: An operation.Operation providing context about the API operation (e.g., operation.Create, operation.Update).fldPath: A *field.Path representing the location of the field being validated; this is used to construct descriptive error messages.value: The current value being validated. This is always nilable.oldValue: The previous value (used primarily during UPDATE operations). For CREATE operations, this is always nil. This is also always nilable.<OtherArgs...>: Optional additional arguments required for specific validation logic (e.g., a maximum length constraint).
Return Value:
Validators always return a field.ErrorList.
- Success: A zero-length
field.ErrorList (not necessarily nil). - Failure: An
ErrorList containing one or more distinct validation failures.
import (
"context"
"k8s.io/apimachinery/pkg/api/operation"
"k8s.io/apimachinery/pkg/util/validation/field"
)
// Example signature pattern
func ValidateSomething(ctx context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *string) field.ErrorList