Reputation: 21
I use the patch function to devoid version check:
svcNow := &v1.Service{}
if err := s.Get(context.TODO(), event.NamespacedName, svcNow); err != nil {
return err
}
updateSvc := svcNow.DeepCopy()
updateSvc.Annotations = newAnno
if err := s.Patch(context.TODO(), updateSvc, client.MergeFrom(svcNow)); err != nil {
log.Info("Patch status to annotations failed", "patch", string(jsonStatus))
return err
}
But sometimes I would get the error the object has been modified; please apply your changes to the latest version and try again
still.
Why patch return the error? I thought patch will not check the version.
How can I solve this problem?
Thanks a lot!
I have get some new information!
If the object in server has been add some finalizer, or its status has been modified, could meet the error the object has been modified; please apply your changes to the latest version and try again
when patch.
Bad news :(
Status or Finalizer have been modified is not the reason why failed to patch.
Upvotes: 1
Views: 3311
Reputation: 84
The error " the object has been modified; please apply your changes to the latest version and try again"
usually happens because we fetched the resource and stored it in a variable in our code. Then after a while, we try to update this resource using the data stored in the variable. However, between the time we used the client to receive the data and the time we attempted to update it, the state of the resource on the cluster had changed.
A possible solution can be to ensure that you re-fetch the resource before any update or patch.
Upvotes: 0