From c9b2f861f0ce4e5ab44c7463461b9a479903bedd Mon Sep 17 00:00:00 2001 From: Daniil Fedotov Date: Thu, 14 Dec 2023 16:53:29 -0500 Subject: [PATCH] Retry on updating already existing CRD definitions (#2540) When updating existing CRD definition we can run into conflict error. This happens often in the test cases. Added `RetryOnConflict` from `client-go` with default parameters for update operation. This should be good enough to reduce test flakes and should not impact the live installations. --- pkg/customresource/customresource.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/customresource/customresource.go b/pkg/customresource/customresource.go index e6584955a7..992d05155a 100644 --- a/pkg/customresource/customresource.go +++ b/pkg/customresource/customresource.go @@ -30,6 +30,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/serializer" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/util/retry" // importing go check to bypass the testing flags _ "gopkg.in/check.v1" @@ -130,16 +131,22 @@ func createCRD(context Context, resource CustomResource) error { return errors.Errorf("Failed to create %s CRD. %+v", resource.Name, err) } - // if CRD already exists, get the resource version and create the CRD with that resource version - c, err := context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crd.Name, metav1.GetOptions{}) - if err != nil { - return fmt.Errorf("Failed to get CRD to get resource version: %s\n", err.Error()) - } + err := retry.RetryOnConflict(retry.DefaultRetry, func() error { + // if CRD already exists, get the resource version and create the CRD with that resource version + c, err := context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crd.Name, metav1.GetOptions{}) + if err != nil { + return err + } - crd.ResourceVersion = c.ResourceVersion - _, err = context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, crd, metav1.UpdateOptions{}) + crd.ResourceVersion = c.ResourceVersion + _, err = context.APIExtensionClientset.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, crd, metav1.UpdateOptions{}) + if err != nil { + return err + } + return nil + }) if err != nil { - return fmt.Errorf("Failed to delete already present CRD: %s\n", err.Error()) + return fmt.Errorf("Failed to update existing CRD: %s\n", err.Error()) } } return nil