Skip to content

Commit 2243c36

Browse files
author
OpenShift Bot
committed
Merge pull request #9124 from csrwng/git_check_backoff
Merged by openshift-bot
2 parents dc90f22 + c469964 commit 2243c36

File tree

4 files changed

+38
-36
lines changed

4 files changed

+38
-36
lines changed

pkg/build/builder/docker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NewDockerBuilder(dockerClient DockerClient, buildsClient client.BuildInterf
4848
build: build,
4949
gitClient: gitClient,
5050
tar: tar.New(),
51-
urlTimeout: urlCheckTimeout,
51+
urlTimeout: initialURLCheckTimeout,
5252
client: buildsClient,
5353
cgLimits: cgLimits,
5454
}

pkg/build/builder/source.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import (
2121
)
2222

2323
const (
24-
// urlCheckTimeout is the timeout used to check the source URL
25-
// If fetching the URL exceeds the timeout, then the build will
26-
// not proceed further and stop
27-
urlCheckTimeout = 16 * time.Second
24+
// initialURLCheckTimeout is the initial timeout used to check the
25+
// source URL. If fetching the URL exceeds the timeout, then a longer
26+
// timeout will be tried until the fetch either succeeds or the build
27+
// itself times out.
28+
initialURLCheckTimeout = 16 * time.Second
29+
30+
// timeoutIncrementFactor is the factor to use when increasing
31+
// the timeout after each unsuccessful try
32+
timeoutIncrementFactor = 4
2833
)
2934

3035
type gitAuthError string
@@ -104,35 +109,42 @@ func fetchSource(dockerClient DockerClient, dir string, build *api.Build, urlTim
104109
// remote repository failed to authenticate.
105110
// Since this is calling the 'git' binary, the proxy settings should be
106111
// available for this command.
107-
func checkRemoteGit(gitClient GitClient, url string, timeout time.Duration) error {
108-
glog.V(4).Infof("git ls-remote --heads %s", url)
112+
func checkRemoteGit(gitClient GitClient, url string, initialTimeout time.Duration) error {
109113

110114
var (
111115
out string
112116
errOut string
113117
err error
114118
)
115119

116-
out, errOut, err = gitClient.TimedListRemote(timeout, url, "--heads")
117-
if _, ok := err.(*git.TimeoutError); err != nil && ok {
118-
return fmt.Errorf("timeout while waiting for remote repository %q", url)
119-
}
120-
121-
if len(out) != 0 {
122-
glog.V(4).Infof(out)
123-
}
124-
if len(errOut) != 0 {
125-
glog.V(4).Infof(errOut)
120+
timeout := initialTimeout
121+
for {
122+
glog.V(4).Infof("git ls-remote --heads %s", url)
123+
out, errOut, err = gitClient.TimedListRemote(timeout, url, "--heads")
124+
if len(out) != 0 {
125+
glog.V(4).Infof(out)
126+
}
127+
if len(errOut) != 0 {
128+
glog.V(4).Infof(errOut)
129+
}
130+
if err != nil {
131+
if _, ok := err.(*git.TimeoutError); ok {
132+
timeout = timeout * timeoutIncrementFactor
133+
glog.Infof("WARNING: timed out waiting for git server, will wait %s", timeout)
134+
continue
135+
}
136+
}
137+
break
126138
}
127-
128-
combinedOut := out + errOut
129-
switch {
130-
case strings.Contains(combinedOut, "Authentication failed"):
131-
return gitAuthError(url)
132-
case strings.Contains(combinedOut, "not found"):
133-
return gitNotFoundError(url)
139+
if err != nil {
140+
combinedOut := out + errOut
141+
switch {
142+
case strings.Contains(combinedOut, "Authentication failed"):
143+
return gitAuthError(url)
144+
case strings.Contains(combinedOut, "not found"):
145+
return gitNotFoundError(url)
146+
}
134147
}
135-
136148
return err
137149
}
138150

pkg/build/builder/source_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ func TestCheckRemoteGit(t *testing.T) {
3232
t.Errorf("expected gitAuthError, got %q", v)
3333
}
3434

35-
t0 := time.Now()
36-
err = checkRemoteGit(gitRepo, "https://254.254.254.254/foo/bar", 4*time.Second)
37-
t1 := time.Now()
38-
if err == nil || (err != nil && !strings.Contains(fmt.Sprintf("%s", err), "timeout")) {
39-
t.Errorf("expected timeout error, got %q", err)
40-
}
41-
if t1.Sub(t0) > 5*time.Second {
42-
t.Errorf("expected timeout in 4 seconds, it took %v", t1.Sub(t0))
43-
}
44-
4535
err = checkRemoteGit(gitRepo, "https://github.com/openshift/origin", 10*time.Second)
4636
if err != nil {
4737
t.Errorf("unexpected error %q", err)

pkg/build/builder/sti.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (s *S2IBuilder) Build() error {
115115
download := &downloader{
116116
s: s,
117117
in: os.Stdin,
118-
timeout: urlCheckTimeout,
118+
timeout: initialURLCheckTimeout,
119119

120120
dir: srcDir,
121121
contextDir: contextDir,

0 commit comments

Comments
 (0)