vmware-archive / operator-builder

A Kubebuilder plugin to accelerate the development of Kubernetes operators
MIT License
41 stars 6 forks source link

bug: zero values are set when fields are omitted #272

Closed scottd018 closed 2 years ago

scottd018 commented 2 years ago

Consider the following API (this is how we generate the API in the current version):

// MyApp is the Schema for the myapps API.
type MyApp struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty"`
    Spec              MyAppSpec   `json:"spec,omitempty"`
    Status            MyAppStatus `json:"status,omitempty"`
}

// MyAppSpec defines the desired state of MyApp.
type MyAppSpec struct {
    // +kubebuilder:validation:Optional
    Celery MyAppSpecCelery `json:"celery"`
}

//+kubebuilder:validation:Optional
type MyAppSpecCelery struct {
    // +kubebuilder:validation:Optional
    Beat MyAppSpecCeleryBeat `json:"beat,omitempty"`
}

type MyAppSpecCeleryBeat struct {
    // +kubebuilder:default=1
    // +kubebuilder:validation:Optional
    // (Default: 1)
    //  Minimum replicas for the celery beat job scheduler.
    MinReplicas int `json:"minReplicas"`
}

Consider the manifest when submitted to the Kubernetes API:

apiVersion: myapp.acme.com/v1alpha1
kind: MyApp
metadata:
  name: myapp-sample
spec:
  #celery: {}
    # beat:
    #   minReplicas: 1

According to the CRD, we should be defaulting the value to 1 when left omitted. However, when submitting the above manifest to the Kubernetes API, we see this:

      beat:
        minReplicas: 0

This shows that the zero value is being used and not the default value.