Retry reconcile and clone actions once

We have observed that the code at times outperforms GitHub mechanics,
resulting in not found errors that are only true for a millisecond.
Retrying those actions once with a 2 second delay should be more
friendly to users.

Signed-off-by: Hidde Beydals <hello@hidde.co>
pull/968/head
Hidde Beydals 4 years ago
parent 4ece12348b
commit 7481c6beb0

@ -182,3 +182,17 @@ func kustomizationReconciled(ctx context.Context, kube client.Client, objKey cli
return false, nil return false, nil
} }
} }
func retry(retries int, wait time.Duration, fn func() error) (err error) {
for i := 0; ; i++ {
err = fn()
if err == nil {
return
}
if i >= retries {
break
}
time.Sleep(wait)
}
return err
}

@ -104,8 +104,11 @@ func (b *PlainGitBootstrapper) ReconcileComponents(ctx context.Context, manifest
} }
b.logger.Actionf("cloning branch %q from Git repository %q", b.branch, b.url) b.logger.Actionf("cloning branch %q from Git repository %q", b.branch, b.url)
cloned, err := b.git.Clone(ctx, b.url, b.branch) var cloned bool
if err != nil { if err = retry(1, 2*time.Second, func() (err error) {
cloned, err = b.git.Clone(ctx, b.url, b.branch)
return
}); err != nil {
return fmt.Errorf("failed to clone repository: %w", err) return fmt.Errorf("failed to clone repository: %w", err)
} }
if cloned { if cloned {
@ -216,12 +219,15 @@ func (b *PlainGitBootstrapper) ReconcileSyncConfig(ctx context.Context, options
if _, err := b.git.Status(); err != nil { if _, err := b.git.Status(); err != nil {
if err == git.ErrNoGitRepository { if err == git.ErrNoGitRepository {
b.logger.Actionf("cloning branch %q from Git repository %q", b.branch, b.url) b.logger.Actionf("cloning branch %q from Git repository %q", b.branch, b.url)
cloned, err := b.git.Clone(ctx, b.url, b.branch) var cloned bool
if err != nil { if err = retry(1, 2*time.Second, func() (err error) {
cloned, err = b.git.Clone(ctx, b.url, b.branch)
return
}); err != nil {
return fmt.Errorf("failed to clone repository: %w", err) return fmt.Errorf("failed to clone repository: %w", err)
} }
if cloned { if cloned {
b.logger.Successf("cloned repository", b.url) b.logger.Successf("cloned repository")
} }
} }
return err return err

@ -278,11 +278,15 @@ func (b *GitProviderBootstrapper) reconcileOrgRepository(ctx context.Context) (g
} }
b.logger.Successf("repository %q created", repoRef.String()) b.logger.Successf("repository %q created", repoRef.String())
} }
// Set default branch before calling Reconcile due to bug described // Set default branch before calling Reconcile due to bug described
// above. // above.
repoInfo.DefaultBranch = repo.Get().DefaultBranch repoInfo.DefaultBranch = repo.Get().DefaultBranch
var changed bool var changed bool
if repo, changed, err = b.provider.OrgRepositories().Reconcile(ctx, repoRef, repoInfo); err != nil { if err = retry(1, 2*time.Second, func() (err error) {
repo, changed, err = b.provider.OrgRepositories().Reconcile(ctx, repoRef, repoInfo)
return
}); err != nil {
return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err) return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err)
} }
if changed { if changed {
@ -352,12 +356,16 @@ func (b *GitProviderBootstrapper) reconcileUserRepository(ctx context.Context) (
// above. // above.
repoInfo.DefaultBranch = repo.Get().DefaultBranch repoInfo.DefaultBranch = repo.Get().DefaultBranch
var changed bool var changed bool
if repo, changed, err = b.provider.UserRepositories().Reconcile(ctx, repoRef, repoInfo); err != nil { if err = retry(1, 2*time.Second, func() (err error) {
return nil, err repo, changed, err = b.provider.UserRepositories().Reconcile(ctx, repoRef, repoInfo)
return
}); err != nil {
return nil, fmt.Errorf("failed to reconcile Git repository %q: %w", repoRef.String(), err)
} }
if changed { if changed {
b.logger.Successf("repository %q reconciled", repoRef.String()) b.logger.Successf("repository %q reconciled", repoRef.String())
} }
return repo, nil return repo, nil
} }

Loading…
Cancel
Save