Documentation
¶
Overview ¶
Package crdvalidate validates custom resources the way the Kubernetes API server validates a create request, in process, against the CustomResourceDefinitions it is given and nothing else.
A Validator holds one definition per group and kind. For an object it runs the server's own routines in the server's own order, as k8s.io/apiextensions-apiserver v0.37.1 runs them for a create:
- the decode-time coercion, with strict field validation: metadata is decoded, unknown fields are pruned and reported, non-nullable nulls without a default are dropped, and embedded metadata is coerced (pkg/apiserver/customresource_handler.go, unstructuredSchemaCoercer);
- defaulting from the schema (unstructuredDefaulter);
- the name is generated from generateName when none is set (k8s.io/apiserver/pkg/registry/generic/registry, Store.create);
- the request namespace rule (k8s.io/apiserver/pkg/registry/rest BeforeCreate) — see Validator.Validate for the one place this package is stricter than the server;
- PrepareForCreate: status is dropped when the definition serves a status subresource, and the generation is set (pkg/registry/customresource/strategy.go);
- Validate: the schema, ObjectMeta, scale paths, embedded ObjectMeta, list set and map invariants, and the x-kubernetes-validations CEL rules unless a blocking schema error already stands (strategy.go, validator.go).
Before any of that, a definition is checked once as the server checks a CustomResourceDefinition create — PrepareForCreate, then ValidateCustomResourceDefinition (pkg/registry/customresourcedefinition/ strategy.go, pkg/apis/apiextensions/validation) — which compiles every CEL rule among everything else. A definition the server would refuse is one this package cannot answer for, and says so instead of reporting the definition's defects as the object's. Each served version is then compiled on first use as the server compiles it when it starts serving the definition: converted to the internal schema, made structural, defaults pruned, an OpenAPI validator and a CEL validator built from it.
What this cannot know: admission webhooks, the apiserver's per-kind Go validation for built-in kinds, and versions the definition does not serve.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateObjectMeta ¶
func ValidateObjectMeta(ctx context.Context, obj *unstructured.Unstructured, namespaced bool, name validation.ValidateNameFunc) field.ErrorList
ValidateObjectMeta validates obj's metadata as the server validates it on create, for a kind this package holds no definition for. namespaced says whether the kind takes a namespace; the request namespace rule of Validator.Validate applies. name is the kind's own name rule, the one the server's per-kind validation passes to ValidateObjectMeta (a DNS label for a Namespace, a path segment for a Role); nil is the rule every custom resource gets, a DNS subdomain.
Types ¶
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator validates objects against a fixed set of definitions.
func New ¶
func New(defs []*apiextensionsv1.CustomResourceDefinition) (*Validator, error)
New indexes the definitions by group and kind, each defaulted on a copy as the server defaults a definition it decodes (singular and list kind names, the conversion strategy; pkg/apis/apiextensions/v1/defaults.go). Two definitions for one kind are an error, not a merge: the caller decides which one is canonical.
func (*Validator) Compile ¶
Compile prepares the version that objects of gvk are validated against and returns what stops it: no definition for the kind, a version the definition does not serve, or a schema the server would not serve either.
func (*Validator) Definition ¶
func (v *Validator) Definition(gk schema.GroupKind) *apiextensionsv1.CustomResourceDefinition
Definition returns the definition held for a kind, or nil.
func (*Validator) Validate ¶
func (v *Validator) Validate(ctx context.Context, obj *unstructured.Unstructured) (field.ErrorList, error)
Validate validates obj as the server validates a create request for it, on a copy; obj is not changed. The list holds the object's errors. The error says the validator cannot answer for the object at all: no definition for its kind, or one whose schema the server would not serve.
obj must be decoded as the server decodes a request body — through unstructured.UnstructuredJSONScheme or k8s.io/apimachinery/pkg/util/json, which read whole numbers as int64. An integer held as float64 (the encoding/json default) is a shape the server never sees, and the scale paths and CEL rules report it as a type error.
A namespaced object with no metadata.namespace is validated in the request namespace, as kubectl submits it: the server fills "default" in before validating. A namespace on a cluster-scoped object is reported as forbidden; the server would drop it without a word, and an object that carries one is a defect in whatever produced it.