From 728f6761f1183fc7c4d0e74fc8c9ecd39b499c6b Mon Sep 17 00:00:00 2001 From: Myasnikov Daniil Date: Fri, 19 Jun 2026 14:48:22 +0500 Subject: [PATCH] fix(api): retry HelmRelease update on resourceVersion conflict The aggregated API server translates app CR updates (Etcd, Postgres, ...) into a read-convert-update of the backing HelmRelease. Flux's helm-controller continuously writes the HelmRelease status, which shares the object's resourceVersion, so a CR update that lands while a prior reconcile is still in flight reads a resourceVersion that goes stale before the Update and the request fails with a 409 Conflict ("the object has been modified; please apply your changes to the latest version and try again"). The HelmRelease spec is fully derived from the Application the caller just applied, so a stale-resourceVersion conflict is never a real spec conflict here. Wrap the Update in retry.RetryOnConflict, refreshing the live resourceVersion on each conflict before retrying. The no-conflict path is unchanged, and this mirrors the existing RetryOnConflict usage in workloadmonitor_controller.go. Surfaced by the etcd.bats e2e test, which applies the same singleton Etcd resource three times back-to-back and intermittently failed the third apply with the 409. Assisted-By: Claude Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Myasnikov Daniil (cherry picked from commit 76410599de960a6391e553160e92d1cf1004b55d) --- pkg/registry/apps/application/rest.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/registry/apps/application/rest.go b/pkg/registry/apps/application/rest.go index 24a87c060..080e57ada 100644 --- a/pkg/registry/apps/application/rest.go +++ b/pkg/registry/apps/application/rest.go @@ -41,6 +41,7 @@ import ( "k8s.io/apimachinery/pkg/watch" "k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/registry/rest" + "k8s.io/client-go/util/retry" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" @@ -566,8 +567,27 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje klog.V(6).Infof("Updating HelmRelease %s in namespace %s", helmRelease.Name, helmRelease.Namespace) - // Update the HelmRelease in Kubernetes - err = r.c.Update(ctx, helmRelease, &client.UpdateOptions{Raw: &metav1.UpdateOptions{}}) + // Update the HelmRelease in Kubernetes. + // + // Flux's helm-controller (and other controllers) continuously write the + // HelmRelease's status, which shares the object's resourceVersion. When a + // caller updates an app CR while a prior reconcile is still in flight, the + // resourceVersion read above goes stale and the Update is rejected with a + // 409 Conflict. The HelmRelease spec is fully derived from the Application + // the caller just applied, so a stale-resourceVersion conflict is never a + // real spec conflict here: refresh the resourceVersion from the live object + // and retry. + err = retry.RetryOnConflict(retry.DefaultBackoff, func() error { + updateErr := r.c.Update(ctx, helmRelease, &client.UpdateOptions{Raw: &metav1.UpdateOptions{}}) + if apierrors.IsConflict(updateErr) { + cur := &helmv2.HelmRelease{} + if getErr := r.c.Get(ctx, client.ObjectKey{Namespace: helmRelease.Namespace, Name: helmRelease.Name}, cur, &client.GetOptions{Raw: &metav1.GetOptions{}}); getErr != nil { + return getErr + } + helmRelease.SetResourceVersion(cur.GetResourceVersion()) + } + return updateErr + }) if err != nil { klog.Errorf("Failed to update HelmRelease %s: %v", helmRelease.Name, err) return nil, false, fmt.Errorf("failed to update HelmRelease: %v", err)