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

add git client timeouts #206

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all 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
32 changes: 29 additions & 3 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
gitclient "github.com/go-git/go-git/v5/plumbing/transport/client"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -33,8 +35,16 @@ const (
requeueTime = time.Second * 30
gitCommitAuthorName = "git-reconciler"
gitCommitAuthorEmail = "[email protected]"

gitTCPTimeout = 5 * time.Second
// timeout value for a git operation through http. clone, push, etc.
gitHTTPTimeout = 30 * time.Second
)

func init() {
configureGitClient()
}

type GiteaClientFunc func(url string, options ...gitea.ClientOption) (GiteaClient, error)

func NewGiteaClient(url string, options ...gitea.ClientOption) (GiteaClient, error) {
Expand Down Expand Up @@ -179,13 +189,13 @@ func (r *RepositoryReconciler) reconcileRepoContent(ctx context.Context, repo *v
NoCheckout: true,
InsecureSkipTLS: true,
}
clonedRepo, err := git.PlainClone(tempDir, false, cloneOptions)
clonedRepo, err := git.PlainCloneContext(ctx, tempDir, false, cloneOptions)
if err != nil {
// if we cannot clone with gitea's configured url, then we fallback to using the url provided in spec.
logger.V(1).Info("failed cloning with returned clone URL. Falling back to default url.", "err", err)

cloneOptions.URL = fmt.Sprintf("%s/%s.git", repo.Spec.GitURL, giteaRepo.FullName)
c, retErr := git.PlainClone(tempDir, false, cloneOptions)
c, retErr := git.PlainCloneContext(ctx, tempDir, false, cloneOptions)
if retErr != nil {
return fmt.Errorf("cloning repo with fall back url: %w", retErr)
}
Expand Down Expand Up @@ -251,7 +261,7 @@ func (r *RepositoryReconciler) reconcileRepoContent(ctx context.Context, repo *v
func reconcileRepo(giteaClient GiteaClient, repo *v1alpha1.GitRepository) (*gitea.Repository, error) {
resp, repoResp, err := giteaClient.GetRepo(getOrganizationName(*repo), getRepositoryName(*repo))
if err != nil {
if repoResp.StatusCode == 404 {
if repoResp != nil && repoResp.StatusCode == http.StatusNotFound {
createResp, _, CErr := giteaClient.CreateRepo(gitea.CreateRepoOption{
Name: getRepositoryName(*repo),
Description: fmt.Sprintf("created by Git Repository controller for %s in %s namespace", repo.Name, repo.Namespace),
Expand Down Expand Up @@ -312,3 +322,19 @@ func (r *RepositoryReconciler) writeRepoContents(repo *v1alpha1.GitRepository, d
func getRepositoryURL(namespace, name, baseUrl string) string {
return fmt.Sprintf("%s/giteaAdmin/%s-%s.git", baseUrl, namespace, name)
}

func configureGitClient() {
tr := http.DefaultTransport.(*http.Transport).Clone()

tr.DialContext = (&net.Dialer{
Timeout: gitTCPTimeout,
KeepAlive: 30 * time.Second, // from http.DefaultTransport
}).DialContext

customClient := &http.Client{
Transport: tr,
Timeout: gitHTTPTimeout,
}
gitclient.InstallProtocol("https", githttp.NewClient(customClient))
gitclient.InstallProtocol("http", githttp.NewClient(customClient))
}
Loading