1

当我更新节点或使用 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
}

4

1 回答 1

1

我已经解决了这个问题。原因是冲突的错误被隐藏了

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)}

,所以retry.RetryOnConflict的功能不起作用。

于 2019-07-21T05:01:59.847 回答