From 4bed5460b3291f194e2c85dff80876f7cbb47df1 Mon Sep 17 00:00:00 2001 From: Elena Gershkovich Date: Tue, 13 Jun 2023 18:45:38 +0300 Subject: [PATCH] Flaky unit test - fails to update DRCluster manifest work Signed-off-by: Elena Gershkovich --- controllers/drcluster_controller_test.go | 20 +++++++++++++------- controllers/util/mw_util.go | 24 +++++++++++++++++++++--- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/controllers/drcluster_controller_test.go b/controllers/drcluster_controller_test.go index b9b2a0039..ada947f32 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" ) @@ -220,17 +221,22 @@ func updateDRClusterManifestWorkStatus(clusterNamespace string) { }, } - mw.Status = DRClusterStatusConditions + retryErr := retry.RetryOnConflict(retry.DefaultBackoff, func() error { + var err error + + err = apiReader.Get(context.TODO(), manifestLookupKey, mw) + if err != nil { + return err + } - err := k8sClient.Status().Update(context.TODO(), mw) - if err != nil { - // try again - Expect(apiReader.Get(context.TODO(), manifestLookupKey, mw)).NotTo(HaveOccurred()) mw.Status = DRClusterStatusConditions + err = k8sClient.Status().Update(context.TODO(), mw) - } - Expect(err).NotTo(HaveOccurred()) + return err + }) + + Expect(retryErr).NotTo(HaveOccurred()) } var _ = Describe("DRClusterController", func() { diff --git a/controllers/util/mw_util.go b/controllers/util/mw_util.go index 2559d410c..de2ca8982 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" @@ -502,11 +503,28 @@ func (mwu *MWUtil) createOrUpdateManifestWork( } if !reflect.DeepEqual(foundMW.Spec, mw.Spec) { - mw.Spec.DeepCopyInto(&foundMW.Spec) - mwu.Log.Info("ManifestWork exists.", "name", mw.Name, "namespace", foundMW.Namespace) - return mwu.Client.Update(mwu.Ctx, foundMW) + 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 + } } return nil