From c996f6bd5b3b6e6e1372a4208c0bf72baae852b1 Mon Sep 17 00:00:00 2001 From: pigletfly Date: Wed, 9 Feb 2022 17:00:56 +0800 Subject: [PATCH] Add retry on CreateOrUpdateWork Signed-off-by: pigletfly --- pkg/util/helper/work.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/util/helper/work.go b/pkg/util/helper/work.go index 92592a9db..fca85c158 100644 --- a/pkg/util/helper/work.go +++ b/pkg/util/helper/work.go @@ -7,6 +7,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/util/retry" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -39,10 +40,17 @@ func CreateOrUpdateWork(client client.Client, workMeta metav1.ObjectMeta, resour } runtimeObject := work.DeepCopy() - operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error { - runtimeObject.Spec = work.Spec - runtimeObject.Labels = work.Labels - runtimeObject.Annotations = work.Annotations + var operationResult controllerutil.OperationResult + err = retry.RetryOnConflict(retry.DefaultRetry, func() (err error) { + operationResult, err = controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error { + runtimeObject.Spec = work.Spec + runtimeObject.Labels = work.Labels + runtimeObject.Annotations = work.Annotations + return nil + }) + if err != nil { + return err + } return nil }) if err != nil {