kubernetes-sigs / kubebuilder

Kubebuilder - SDK for building Kubernetes APIs using CRDs
http://book.kubebuilder.io
Apache License 2.0
7.83k stars 1.44k forks source link

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

Open kgoutham93 opened 11 months ago

kgoutham93 commented 11 months ago

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 commented 11 months ago

Working on this

/assign

camilamacedo86 commented 11 months ago

Please feel free to open a PR with you suggestion. Your help is more than welcome 🎉

k8s-triage-robot commented 8 months ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

kgoutham93 commented 8 months ago

/remove-lifecycle-stale

k8s-triage-robot commented 7 months ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

kgoutham93 commented 7 months ago

/remove-lifecycle rotten

k8s-triage-robot commented 4 months ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

kgoutham93 commented 4 months ago

/remove-lifecycle stale

k8s-triage-robot commented 1 month ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot commented 2 weeks ago

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten