✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes API Defaulting Model

Kubernetes API Defaulting Model automatically fills in missing fields when creating resources, ensuring consistency and reducing manual configuration efforts.

Kubernetes API Defaulting Model is the mechanism by which the API server fills in values for fields a client left unspecified, applying either built-in default logic for core types or schema-declared defaults for CustomResourceDefinitions, so that the objects actually persisted to the cluster are always fully populated according to consistent, predictable rules rather than left partially blank in ways every downstream consumer would otherwise need to handle defensively. Defaulting happens early in the admission pipeline, before validation, ensuring that what gets validated and eventually stored already reflects the complete, defaulted form of the object.


Why Defaulting Exists

Reducing Required Boilerplate for Clients

Without defaulting, every client would need to explicitly specify every field a type defines, even fields where a sensible default almost always applies, such as a Pod's restartPolicy or a container's imagePullPolicy; defaulting allows manifests to stay concise, specifying only what genuinely differs from the common case, while the API server fills in the rest consistently.

Consistency Across API Versions

Defaulting also plays a role in version conversion: when an object is read or written in an older API version that lacks a field introduced later, defaulting logic ensures that field still receives a sensible value when the object is converted to its internal storage version, rather than being left as an unset zero value that might have different meaning than an intentionally-unset field.


Defaulting for Built-In Types

Go-Based Defaulter Functions

Built-in types carry defaulting logic implemented directly in the API server's Go code, registered per type and per version, executed automatically as part of the admission pipeline immediately after decoding an incoming request and before validation runs; this defaulting is not configurable by cluster operators, since it is compiled directly into the API server binary matching the cluster's Kubernetes version.

Version-Specific Defaulting Behavior

Because defaulting functions are registered per API version, the same field can receive different default values, or defaulting behavior can be introduced or changed, across different served versions of the same type, though within the bounds of the API deprecation policy governing how such changes may be introduced for already-stable versions.


Defaulting for Custom Resources

Schema-Declared Defaults

CustomResourceDefinitions express defaults declaratively within their OpenAPI schema, using a default keyword attached to individual schema properties, rather than through custom Go code; the API server applies these declared defaults automatically whenever a request omits the corresponding field, using the exact same admission-time mechanism as built-in type defaulting.

Limits of Schema-Based Defaulting

Because CRD defaulting is purely declarative and tied to individual fields, it cannot express conditional logic such as "default field B based on the value of field A"; use cases requiring that kind of cross-field defaulting logic must instead be implemented through a mutating admission webhook, which can inspect the full object and apply arbitrary computed defaults.


Mutating Webhooks as Extended Defaulting

Beyond What Schema Defaults Can Express

Mutating admission webhooks provide a general-purpose mechanism for injecting default or computed values into any object, built-in or custom, evaluated during the mutating admission phase alongside (and typically after) the API server's own built-in defaulting, which is how features such as automatic sidecar container injection or default resource requests based on namespace policy are commonly implemented.

Ordering Relative to Validation

Because mutation, whether from built-in defaulting or webhooks, always completes before validation runs, any values a defaulting mechanism injects are themselves subject to the same schema and semantic validation as explicitly client-supplied values, ensuring a defaulted object cannot bypass the same correctness guarantees an explicitly authored object must satisfy.


Client-Visible Effects of Defaulting

Diffing Defaulted Fields

Because the object returned after creation includes every defaulted field populated with its concrete value, clients performing declarative updates (such as kubectl apply) must account for the difference between what they submitted and what the API server actually stored, which is part of why tools built on Server-Side Apply track field ownership explicitly rather than relying on a naive full-object diff.