My CR has been set with:
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
and My reconcile like this:
func (r *ReconcileTest) Reconcile(ctx context.Context, req ctrl.Request) (reconcileRes ctrl.Result, errRes error) {
...
defer func() {
if err := r.Client.Status().Update(ctx, CRObject); err != nil {
r.Logger.Errorf("failed to update status of CR %s err: %v", CRObject.Name, err)
reconcileRes = ctrl.Result{}
errRes = err
}
if err := r.Client.Update(ctx, CRObject); err != nil {
r.Logger.Errorf("failed to update CR %s err: %v", CRObject.Name, err)
reconcileRes = ctrl.Result{}
errRes = err
}
}()
...
// change status
apimeta.SetStatusCondition(&CRObject.Status.Conditions, metav1.Condition{
......
})
// change object properties
if CRObject.Annotations == nil {
CRObject.Annotations = make(map[string]string)
}
CRObject.Annotations["Test"] = "OK"
....
return ctrl.Result{Requeue: true, RequeueAfter: RequeueTime}, nil
}
I thought that the ‘status’ and ‘object’ can be update at sametime. but there is only ‘status’ can be changed. the ‘object’ doesn’t be modified.
If I change the update order, for example, update ‘object’ at first and then update ‘status’, It’s the opposite. the ‘object’ can be update, but ‘status’ isn’t going to be changed.
How can I update ‘object’ and ‘status’ in one ‘reconcile’? Thanks!:)