Skip to content

Commit

Permalink
Retry on updating already existing CRD definitions (#2540)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hairyhum committed Dec 14, 2023
1 parent 67b70e0 commit c9b2f86
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions pkg/customresource/customresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c9b2f86

Please sign in to comment.