From 57fa2d9abe6b25c3ce696d81b9cb0bef8ded7da2 Mon Sep 17 00:00:00 2001 From: Elena Gershkovich Date: Thu, 22 Jun 2023 18:16:14 +0300 Subject: [PATCH] Adjustments Signed-off-by: Elena Gershkovich --- controllers/drcluster_controller_test.go | 23 +++++++++++----------- controllers/util/mw_util.go | 25 ++++++++++++------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/controllers/drcluster_controller_test.go b/controllers/drcluster_controller_test.go index 5c509f48ab..ada947f326 100644 --- a/controllers/drcluster_controller_test.go +++ b/controllers/drcluster_controller_test.go @@ -22,6 +22,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/util/retry" ctrl "sigs.k8s.io/controller-runtime" ) @@ -219,25 +220,23 @@ func updateDRClusterManifestWorkStatus(clusterNamespace string) { }, }, } - localRetries := 0 - var err error + retryErr := retry.RetryOnConflict(retry.DefaultBackoff, func() error { + var err error + + err = apiReader.Get(context.TODO(), manifestLookupKey, mw) + if err != nil { + return err + } - for localRetries < updateRetries { mw.Status = DRClusterStatusConditions err = k8sClient.Status().Update(context.TODO(), mw) - if errors.IsConflict(err) { - Expect(apiReader.Get(context.TODO(), manifestLookupKey, mw)).To(Succeed()) - localRetries++ - - continue - } - break - } + return err + }) - Expect(err).NotTo(HaveOccurred()) + Expect(retryErr).NotTo(HaveOccurred()) } var _ = Describe("DRClusterController", func() { diff --git a/controllers/util/mw_util.go b/controllers/util/mw_util.go index 1d2b79fdf2..de2ca8982e 100644 --- a/controllers/util/mw_util.go +++ b/controllers/util/mw_util.go @@ -20,6 +20,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/util/retry" "sigs.k8s.io/controller-runtime/pkg/client" dto "github.com/prometheus/client_model/go" @@ -504,25 +505,25 @@ func (mwu *MWUtil) createOrUpdateManifestWork( if !reflect.DeepEqual(foundMW.Spec, mw.Spec) { mwu.Log.Info("ManifestWork exists.", "name", mw.Name, "namespace", foundMW.Namespace) - for { - mw.Spec.DeepCopyInto(&foundMW.Spec) - - err = mwu.Client.Update(mwu.Ctx, foundMW) - if err == nil { - break - } - - if !errors.IsConflict(err) { - return err - } + retryErr := retry.RetryOnConflict(retry.DefaultBackoff, func() error { + var err error err = mwu.Client.Get(mwu.Ctx, types.NamespacedName{Name: mw.Name, Namespace: managedClusternamespace}, foundMW) - if err != nil { return err } + + mw.Spec.DeepCopyInto(&foundMW.Spec) + + err = mwu.Client.Update(mwu.Ctx, foundMW) + + return err + }) + + if retryErr != nil { + return retryErr } }