Skip to content

Latest commit

 

History

History
44 lines (32 loc) · 942 Bytes

adding_defaulting.md

File metadata and controls

44 lines (32 loc) · 942 Bytes

Adding a value defaulting to a resources schema

To add server side field value defaulting for your resource override the function func (<group>.<Kind>SchemeFns) DefaultingFunction(o interface{}) in the group package.

Important: The validation logic lives in the version package not the group package.

Example:

File: pkg/apis/<group>/<version>/types_bar.go

func (BarSchemeFns) DefaultingFunction(o interface{}) {
	obj := o.(*Bar)
	if obj.Spec.Field == nil {
		f := "value"
		obj.Spec.Field = &f
	}
}

Anatomy of defaulting

A default <group>.<Kind>SchemeFns is generated for each resource with an embedded empty defaulting function. To specify custom defaulting logic, override the embedded implementation.

Cast the object type to your resource Kind

bar := obj.(*Bar)

Update set values for fields with nil values.

	if obj.Spec.Field == nil {
		f := "value"
		obj.Spec.Field = &f
	}