From fe0f1121d5d0b64a34f721f2cc43309113a1e97e Mon Sep 17 00:00:00 2001 From: Prashanth Balasubramanian Date: Mon, 29 Feb 2016 11:38:30 -0800 Subject: [PATCH] Continue logging failure to contact metadata url, fix conn leak. --- pkg/kubelet/config/http.go | 11 ++++++++--- pkg/kubelet/config/http_test.go | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/kubelet/config/http.go b/pkg/kubelet/config/http.go index 11bed577278..0752e5fa302 100644 --- a/pkg/kubelet/config/http.go +++ b/pkg/kubelet/config/http.go @@ -38,6 +38,7 @@ type sourceURL struct { updates chan<- interface{} data []byte failureLogs int + client *http.Client } func NewSourceURL(url string, header http.Header, nodeName string, period time.Duration, updates chan<- interface{}) { @@ -47,6 +48,9 @@ func NewSourceURL(url string, header http.Header, nodeName string, period time.D nodeName: nodeName, updates: updates, data: nil, + // Timing out requests leads to retries. This client is only used to + // read the the manifest URL passed to kubelet. + client: &http.Client{Timeout: 10 * time.Second}, } glog.V(1).Infof("Watching URL %s", url) go wait.Until(config.run, period, wait.NeverStop) @@ -59,7 +63,9 @@ func (s *sourceURL) run() { if s.failureLogs < 3 { glog.Warningf("Failed to read pods from URL: %v", err) } else if s.failureLogs == 3 { - glog.Warningf("Failed to read pods from URL. Won't log this message anymore: %v", err) + glog.Warningf("Failed to read pods from URL. Dropping verbosity of this message to V(4): %v", err) + } else { + glog.V(4).Infof("Failed to read pods from URL: %v", err) } s.failureLogs++ } else { @@ -80,8 +86,7 @@ func (s *sourceURL) extractFromURL() error { return err } req.Header = s.header - client := &http.Client{} - resp, err := client.Do(req) + resp, err := s.client.Do(req) if err != nil { return err } diff --git a/pkg/kubelet/config/http_test.go b/pkg/kubelet/config/http_test.go index 14dab53176e..a2d9359bdd5 100644 --- a/pkg/kubelet/config/http_test.go +++ b/pkg/kubelet/config/http_test.go @@ -44,7 +44,7 @@ func TestURLErrorNotExistNoUpdate(t *testing.T) { func TestExtractFromHttpBadness(t *testing.T) { ch := make(chan interface{}, 1) - c := sourceURL{"http://localhost:49575/_not_found_", http.Header{}, "other", ch, nil, 0} + c := sourceURL{"http://localhost:49575/_not_found_", http.Header{}, "other", ch, nil, 0, http.DefaultClient} if err := c.extractFromURL(); err == nil { t.Errorf("Expected error") } @@ -114,7 +114,7 @@ func TestExtractInvalidPods(t *testing.T) { // TODO: Uncomment when fix #19254 // defer testServer.Close() ch := make(chan interface{}, 1) - c := sourceURL{testServer.URL, http.Header{}, "localhost", ch, nil, 0} + c := sourceURL{testServer.URL, http.Header{}, "localhost", ch, nil, 0, http.DefaultClient} if err := c.extractFromURL(); err == nil { t.Errorf("%s: Expected error", testCase.desc) } @@ -293,7 +293,7 @@ func TestExtractPodsFromHTTP(t *testing.T) { // TODO: Uncomment when fix #19254 // defer testServer.Close() ch := make(chan interface{}, 1) - c := sourceURL{testServer.URL, http.Header{}, hostname, ch, nil, 0} + c := sourceURL{testServer.URL, http.Header{}, hostname, ch, nil, 0, http.DefaultClient} if err := c.extractFromURL(); err != nil { t.Errorf("%s: Unexpected error: %v", testCase.desc, err) continue @@ -341,7 +341,7 @@ func TestURLWithHeader(t *testing.T) { ch := make(chan interface{}, 1) header := make(http.Header) header.Set("Metadata-Flavor", "Google") - c := sourceURL{testServer.URL, header, "localhost", ch, nil, 0} + c := sourceURL{testServer.URL, header, "localhost", ch, nil, 0, http.DefaultClient} if err := c.extractFromURL(); err != nil { t.Fatalf("Unexpected error extracting from URL: %v", err) }