I occasionally get such an error "the object has been modified; please apply your changes to the latest version and try again" when I update node or deploy with client-go.My goal is that adding a taint/toleration and a label to one node/deployment.
some people said should use
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {})
,but it seems that does not work.
func AddFaultToleration(deploy *appsv1.Deployment, ns string, client kubernetes.Interface) (*appsv1.Deployment, error) {
updateDeploy, err := client.AppsV1().Deployments(ns).Get(deploy.Name, metav1.GetOptions{})
if updateDeploy == nil || err != nil {
return deploy, fmt.Errorf("Failed to get latest version of Deployment: %v", err)
}
effect := apiv1.TaintEffectNoExecute
updateDeploy.Spec.Template.Spec.Tolerations = append(updateDeploy.Spec.Template.Spec.Tolerations, apiv1.Toleration{
Key: ToBeFaultTaintKey,
Value: ToBeFaultTaintValue,
Effect: effect,
Operator: apiv1.TolerationOpEqual,
})
updatedDeployWithTolera, err := client.AppsV1().Deployments(ns).Update(updateDeploy)
if err != nil || updatedDeployWithTolera == nil {
return deploy, fmt.Errorf("failed to update deploy %v after adding toleration: %v", deploy.Name, err)
}
log.Info("Successfully added toleration on pod:", updatedDeployWithTolera.Name)
return updatedDeployWithTolera, nil
}
I have solved the problem. the reason is that the err of conflict is hided by
updatedDeployWithTolera, err := client.AppsV1().Deployments(ns).Update(updateDeploy) if err != nil || updatedDeployWithTolera == nil { return deploy, fmt.Errorf("failed to update deploy %v after adding toleration: %v", deploy.Name, err)}
,So the function of retry.RetryOnConflict doesn't work.