kubernetes-sigs / kubebuilder-declarative-pattern

A toolkit for building declarative operators with kubebuilder
Apache License 2.0
256 stars 85 forks source link

make status.condition patchable #319

Closed yuwenma closed 1 year ago

yuwenma commented 1 year ago

This change improves the status.conditions for the following 3 scenarios:

Case 1. No status.condition (backward compatible)

Current behavior

If users update the k-d-p in their operator, they have to regenerate the CRD because by default the api schema contains the status.conditions field in the default commonstatus. users don't need to change their api schema.

New behavior

If users do not want to add the patchable conditions, after they update the k-d-p they can keep their existing behavior and the CR object will not have .status.conditions field.

Case 2: Default status.conditions

users can use the new status.conditions simply by adding the PatchableConditions to their CR obejcts' Status api (see below). They should re-generate the CRD and DeepCopy objects to contain new status.conditions, but they don't need to implement the addonsv1alpha1.ConditionGetterSetter interface.

type MyObjStatus struct {
    addonv1alpha1.CommonStatus        `json:",inline"`
    addonv1alpha1.PatchableConditions `json:",inline"` # NEW
}

type MyObj struct {
    metav1.TypeMeta   `json:",inline"`
    metav1.ObjectMeta `json:"metadata,omitempty"`

    Spec   MyObjSpec   `json:"spec,omitempty"`
    Status MyObjStatus `json:"status,omitempty"`
}

Case 3: Customize the status.conditions

If users add the patchable conditions and want to implement their own condition setter/getter (addonsv1alpha1.ConditionGetterSetter) to their declarative object, they should expect the customize getter/setter being used.


type MyObjStatus struct {
    addonv1alpha1.CommonStatus        `json:",inline"`
    addonv1alpha1.PatchableConditions `json:",inline"` # NEW
}

var _ addonsv1alpha1.ConditionGetterSetter = &MyObjStatus{}
func (o *MyObjStatus) SetConditions(addonv1alpha1.PatchableConditions) {...}
func (o *MyObjStatus) GetConditions() addonv1alpha1.PatchableConditions {...}

TODO: Add the patchableConditions to guestbook example.

yuwenma commented 1 year ago

/assign @justinsb

yuwenma commented 1 year ago

/test pull-declarative-test

justinsb commented 1 year ago

A few nits/thoughts but nothing blocking - thanks for doing this.

/label tide/merge-method-squash

/approve /lgtm

k8s-ci-robot commented 1 year ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb, yuwenma

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/blob/master/OWNERS)~~ [justinsb] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment