Skip to content

Commit

Permalink
Merge pull request #2752 from fabriziopandini/clusterctl-retry-create…
Browse files Browse the repository at this point in the history
…-inventory

🐛clusterctl: retry create inventory
  • Loading branch information
k8s-ci-robot authored Mar 23, 2020
2 parents ec24629 + 23ac302 commit 35f28e9
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions cmd/clusterctl/client/cluster/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,36 +189,41 @@ func (p *inventoryClient) createObj(o unstructured.Unstructured) error {
}

func (p *inventoryClient) Create(m clusterctlv1.Provider) error {
cl, err := p.proxy.NewClient()
if err != nil {
return err
}
// Create the Kubernetes object.
// Nb. The operation is wrapped in a retry loop to make Create more resilient to unexpected conditions.
createInventoryObjectBackoff := newBackoff()
return retryWithExponentialBackoff(createInventoryObjectBackoff, func() error {
cl, err := p.proxy.NewClient()
if err != nil {
return err
}

currentProvider := &clusterctlv1.Provider{}
key := client.ObjectKey{
Namespace: m.Namespace,
Name: m.Name,
}
if err := cl.Get(ctx, key, currentProvider); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to get current provider object")
currentProvider := &clusterctlv1.Provider{}
key := client.ObjectKey{
Namespace: m.Namespace,
Name: m.Name,
}
if err := cl.Get(ctx, key, currentProvider); err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "failed to get current provider object")
}

//if it does not exists, create the provider object
if err := cl.Create(ctx, &m); err != nil {
return errors.Wrapf(err, "failed to create provider object")
//if it does not exists, create the provider object
if err := cl.Create(ctx, &m); err != nil {
return errors.Wrapf(err, "failed to create provider object")
}
return nil
}
return nil
}

// otherwise patch the provider object
// NB. we are using client.Merge PatchOption so the new objects gets compared with the current one server side
m.SetResourceVersion(currentProvider.GetResourceVersion())
if err := cl.Patch(ctx, &m, client.Merge); err != nil {
return errors.Wrapf(err, "failed to patch provider object")
}
// otherwise patch the provider object
// NB. we are using client.Merge PatchOption so the new objects gets compared with the current one server side
m.SetResourceVersion(currentProvider.GetResourceVersion())
if err := cl.Patch(ctx, &m, client.Merge); err != nil {
return errors.Wrapf(err, "failed to patch provider object")
}

return nil
return nil
})
}

func (p *inventoryClient) List() (*clusterctlv1.ProviderList, error) {
Expand Down

0 comments on commit 35f28e9

Please sign in to comment.