当我更新节点或使用 client-go 部署时,我偶尔会收到这样的错误“对象已被修改;请将您的更改应用到最新版本并重试”。我的目标是向其中添加一个 taint/toleration 和标签节点/部署。
有人说应该使用
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {})
,但似乎不起作用。
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
}