开发者

There comes "the object has been modified" after patch

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜