kubebuilder icon indicating copy to clipboard operation
kubebuilder copied to clipboard

Document CRD design for the usecase of having mandatory fields within an optional struct field in the CRD spec.

Open kgoutham93 opened this issue 2 years ago • 10 comments
trafficstars

What do you want to happen?

Currently, Kubebuilder docs does not capture the common scenario of having mandatory (required) fields within an optional struct field in the CRD spec.

Consider the following silly example, I want ShippingAddress to be an optional spec field, but if user provides a ShippingAddress then I want them to mandatorily speicify AddressType.

type CustomerSpec struct {
	// +kubebuilder:validation:Required
       CustomerId string `json:"customerId"`

	// +kubebuilder:validation:Optional
	ShippingAddress Address `json:"shippingAddress,omitempty"`
}

type Address struct {
	// +kubebuilder:validation:Optional
	AddressLine string `json:"address,omitempty"`

	// +kubebuilder:validation:Required
	// +kubebuilder:validation:Enum=HOME;OFFICE
	AddressType AddressType `json:"addressType"`
}

type AddressType string

const (
	AddressTypeHome   AddressType = "HOME"
	AddressTypeOffice AddressType = "OFFICE"
)

Futher discussion on Slack

As it turns out, we can achieve the above requirement by changing ShippingAddress to a pointer type.

Not so silly example

VirtualHost is optional in HttpProxySpec in Coutour project. But Fqdn (a field in VirtualHost) is mandatory and should adhere to a specific regex pattern.

Extra Labels

/kind documentation

kgoutham93 avatar Oct 25 '23 14:10 kgoutham93