operator icon indicating copy to clipboard operation
operator copied to clipboard

retry Update requests

Open f41gh7 opened this issue 1 year ago • 0 comments
trafficstars

Currently, operator may produce a conflict error if object was already modified by some kubernetes controller or any other external controller.

This error can be retried with the following code:

err := retry.RetryOnConflict(retry.DefaultRetry, func() error {    var res apiv1.MyResource    err := r.Get(ctx, types.NamespacedName{        Name:      resourceName,        Namespace: resourceNamespace,    }, &res)    if err != nil {        return err    }    res.Status.Replias = readyReplicas        return r.Status().Update(ctx, &res)})if err != nil {    return fmt.Errorf("failed to update resource status: %w", err)}

https://alenkacz.medium.com/kubernetes-operators-best-practices-understanding-conflict-errors-d05353dff421 https://pkg.go.dev/k8s.io/client-go/util/retry#RetryOnConflict

f41gh7 avatar Mar 12 '24 11:03 f41gh7