Set timeout for accessing credential provider's URL

This changes sets the timeout and also adds the retry mechanism.
This commit is contained in:
Yu-Ju Hong
2016-03-01 13:47:03 -08:00
parent 361cd2cad3
commit 9d3806bceb
2 changed files with 22 additions and 3 deletions

View File

@@ -26,8 +26,10 @@ import (
"path/filepath"
"strings"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/kubernetes/pkg/util/wait"
)
// DockerConfigJson represents ~/.docker/config.json file info
@@ -48,6 +50,10 @@ type DockerConfigEntry struct {
Email string
}
const (
readURLTimeout = time.Second * 20
)
var (
preferredPathLock sync.Mutex
preferredPath = ""
@@ -138,6 +144,19 @@ func (he *HttpError) Error() string {
}
func ReadUrl(url string, client *http.Client, header *http.Header) (body []byte, err error) {
retryInterval := time.Second
wait.PollImmediate(retryInterval, readURLTimeout, func() (bool, error) {
body, err = readUrl(url, client, header)
if err != nil {
glog.V(4).Infof("Error reading %q: %v", url, err)
return false, nil
}
return true, nil
})
return body, err
}
func readUrl(url string, client *http.Client, header *http.Header) (body []byte, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err