Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retry logic for pushing git repos #1067

Merged
merged 13 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions src/internal/packager/git/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"path/filepath"

"github.com/defenseunicorns/zarf/src/internal/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/go-git/go-git/v5"
goConfig "github.com/go-git/go-git/v5/config"
Expand All @@ -18,15 +17,6 @@ import (
)

func (g *Git) PushRepo(localPath string) error {
// If this is a serviceURL, create a port-forward tunnel to that resource
if tunnel, err := cluster.NewTunnelFromServiceURL(g.Server.Address); err != nil {
return err
} else {
tunnel.Connect("", false)
defer tunnel.Close()
g.Server.Address = fmt.Sprintf("http://%s", tunnel.Endpoint())
}

spinner := message.NewProgressSpinner("Processing git repo at %s", localPath)
defer spinner.Stop()

Expand Down Expand Up @@ -90,6 +80,9 @@ func (g *Git) prepRepoForPush() (*git.Repository, error) {
return nil, fmt.Errorf("unable to transform the git url: %w", err)
}

// Remove any preexisting offlineRemotes (happens when a retry is triggered)
_ = repo.DeleteRemote(offlineRemoteName)

_, err = repo.CreateRemote(&goConfig.RemoteConfig{
Name: offlineRemoteName,
URLs: []string{targetUrl},
Expand Down Expand Up @@ -135,7 +128,7 @@ func (g *Git) push(repo *git.Repository, spinner *message.Spinner) error {
} else if errors.Is(err, git.NoErrAlreadyUpToDate) {
message.Debugf("Repo already up-to-date, skipping fetch...")
} else if err != nil {
message.Warnf("unable to fetch remote cleanly prior to push: %w", err)
return fmt.Errorf("unable to fetch the git repo prior to push: %w", err)
}

// Push all heads and tags to the offline remote
Expand Down
33 changes: 23 additions & 10 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,32 @@ func (p *Packager) pushImagesToRegistry(componentImages []string, noImgChecksum

// Push all of the components git repos to the configured git server
func (p *Packager) pushReposToRepository(reposPath string, repos []string) error {
// Try repo push up to 3 times
for _, repoURL := range repos {
gitClient := git.New(p.cfg.State.GitServer)
repoPath, err := gitClient.TransformURLtoRepoName(repoURL)
if err != nil {
return fmt.Errorf("unable to get the repo name from the URL %s: %w", repoURL, err)

// Create an anonymous function to push the repo to the Zarf git server
YrrepNoj marked this conversation as resolved.
Show resolved Hide resolved
tryPush := func() error {
gitClient := git.New(p.cfg.State.GitServer)

// If this is a serviceURL, create a port-forward tunnel to that resource
if tunnel, err := cluster.NewTunnelFromServiceURL(gitClient.Server.Address); err != nil {
jeff-mccoy marked this conversation as resolved.
Show resolved Hide resolved
return err
} else {
tunnel.Connect("", false)
defer tunnel.Close()
gitClient.Server.Address = fmt.Sprintf("http://%s", tunnel.Endpoint())
}

// Convert the repo URL to a Zarf-formatted repo name
if repoPath, err := gitClient.TransformURLtoRepoName(repoURL); err != nil {
return fmt.Errorf("unable to get the repo name from the URL %s: %w", repoURL, err)
} else {
return gitClient.PushRepo(filepath.Join(reposPath, repoPath))
}
}

err = utils.Retry(func() error {
return gitClient.PushRepo(filepath.Join(reposPath, repoPath))
}, 3, 5*time.Second)
if err != nil {
return fmt.Errorf("unable to push repo %s to the Git Server: %w", repoPath, err)
// Try repo push up to 3 times
if err := utils.Retry(tryPush, 3, 5*time.Second); err != nil {
return fmt.Errorf("unable to push repo %s to the Git Server: %w", repoURL, err)
}
}

Expand Down