@@ -2,7 +2,6 @@ package reconcile
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
76 "math/rand/v2"
87 "reflect"
@@ -192,72 +191,40 @@ type StatusWithMetadata[T any] interface {
192191 GetStatusMetadata () * vmv1beta1.StatusMetadata
193192}
194193
195- // UpdateStatus reconcile provided object status with given actualStatus status
196- func UpdateObjectStatus [T client.Object , ST StatusWithMetadata [STC ], STC any ](ctx context.Context , rclient client.Client , object ObjectWithDeepCopyAndStatus [T , ST , STC ], actualStatus vmv1beta1.UpdateStatus , maybeErr error ) error {
197- currentStatus := object .GetStatus ( )
198- prevStatus := currentStatus . DeepCopy ()
199- currMeta := currentStatus . GetStatusMetadata ()
200- newUpdateStatus := actualStatus
194+ // UpdateStatus reconcile provided object status with given newUpdateStatus status
195+ func UpdateObjectStatus [T client.Object , ST StatusWithMetadata [STC ], STC any ](ctx context.Context , rclient client.Client , object ObjectWithDeepCopyAndStatus [T , ST , STC ], newUpdateStatus vmv1beta1.UpdateStatus , maybeErr error ) error {
196+ patch := client . MergeFrom ( object .DeepCopy () )
197+ newObjectStatus := object . GetStatus ()
198+ prevObjectStatus := newObjectStatus . DeepCopy ()
199+ newObjectMeta := newObjectStatus . GetStatusMetadata ()
201200
202- switch actualStatus {
201+ switch newUpdateStatus {
203202 case vmv1beta1 .UpdateStatusExpanding , vmv1beta1 .UpdateStatusOperational :
204- currMeta .Reason = ""
203+ newObjectMeta .Reason = ""
205204 case vmv1beta1 .UpdateStatusPaused :
206205 case vmv1beta1 .UpdateStatusFailed :
207206 if maybeErr != nil {
208- currMeta .Reason = maybeErr .Error ()
207+ newObjectMeta .Reason = maybeErr .Error ()
209208 }
210209 default :
211- panic (fmt .Sprintf ("BUG: not expected status=%q" , actualStatus ))
210+ panic (fmt .Sprintf ("BUG: not expected status=%q" , newUpdateStatus ))
212211 }
213212
214- currMeta .ObservedGeneration = object .GetGeneration ()
215- object .DefaultStatusFields (currentStatus )
216- // if opts.mutateCurrentBeforeCompare != nil {
217- // opts.mutateCurrentBeforeCompare(opts.crStatus.(ST))
218- // }
213+ object .DefaultStatusFields (newObjectStatus )
214+ newObjectMeta .ObservedGeneration = object .GetGeneration ()
215+ newObjectMeta .UpdateStatus = newUpdateStatus
219216 // compare before send update request
220217 // it reduces load at kubernetes api-server
221- if equality .Semantic .DeepEqual (currentStatus , prevStatus ) && currMeta . UpdateStatus == actualStatus {
218+ if equality .Semantic .DeepEqual (newObjectStatus , prevObjectStatus ) {
222219 return nil
223220 }
224- currMeta .UpdateStatus = newUpdateStatus
225221
226- // make a deep copy before passing object to Patch function
227- // it reload state of the object from API server
228- // which is not desired behaviour
229- objecToUpdate := object .DeepCopy ()
230- pr , err := buildStatusPatch (currentStatus )
231- if err != nil {
232- return err
233- }
234- if err := rclient .Status ().Patch (ctx , objecToUpdate , pr ); err != nil {
222+ objectToUpdate := object .DeepCopy ()
223+ if err := rclient .Status ().Patch (ctx , objectToUpdate , patch ); err != nil {
235224 return fmt .Errorf ("cannot update resource status with patch: %w" , err )
236225 }
237226 // Update ResourceVersion in order to resolve future conflicts
238- object .SetResourceVersion (objecToUpdate .GetResourceVersion ())
227+ object .SetResourceVersion (objectToUpdate .GetResourceVersion ())
239228
240229 return nil
241230}
242-
243- func buildStatusPatch (currentStatus any ) (client.Patch , error ) {
244- type patch struct {
245- OP string `json:"op"`
246- Path string `json:"path"`
247- Value any `json:"value"`
248- }
249- ops := []patch {
250- {
251- OP : "replace" ,
252- Path : "/status" ,
253- Value : currentStatus ,
254- },
255- }
256- data , err := json .Marshal (ops )
257- if err != nil {
258- return nil , fmt .Errorf ("possible bug, cannot serialize patch specification as json :%w" , err )
259- }
260-
261- return client .RawPatch (types .JSONPatchType , data ), nil
262-
263- }
0 commit comments