Skip to content

Commit

Permalink
Set User-Agent for HTTP prober requests.
Browse files Browse the repository at this point in the history
Signed-off-by: Rodrigo Chacon <rochacon@gmail.com>
  • Loading branch information
rochacon committed Dec 10, 2015
1 parent f1f250c commit 5be24e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/probe/http/http.go
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"k8s.io/kubernetes/pkg/probe"
"k8s.io/kubernetes/pkg/version"

"github.com/golang/glog"
)
Expand All @@ -48,16 +49,21 @@ func (pr httpProber) Probe(url *url.URL, timeout time.Duration) (probe.Result, s
return DoHTTPProbe(url, &http.Client{Timeout: timeout, Transport: pr.transport})
}

type HTTPGetInterface interface {
Get(u string) (*http.Response, error)
type HTTPDoInterface interface {
Do(r *http.Request) (*http.Response, error)
}

// DoHTTPProbe checks if a GET request to the url succeeds.
// If the HTTP response code is successful (i.e. 400 > code >= 200), it returns Success.
// If the HTTP response code is unsuccessful or HTTP communication fails, it returns Failure.
// This is exported because some other packages may want to do direct HTTP probes.
func DoHTTPProbe(url *url.URL, client HTTPGetInterface) (probe.Result, string, error) {
res, err := client.Get(url.String())
func DoHTTPProbe(url *url.URL, client HTTPDoInterface) (probe.Result, string, error) {
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
return probe.Failure, err.Error(), nil
}
req.Header.Set("User-Agent", fmt.Sprintf("Kubernetes/%s HTTP-Prober", version.Get()))
res, err := client.Do(req)
if err != nil {
// Convert errors into failures to catch timeouts.
return probe.Failure, err.Error(), nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/probe/http/http_test.go
Expand Up @@ -28,6 +28,7 @@ import (
"time"

"k8s.io/kubernetes/pkg/probe"
"k8s.io/kubernetes/pkg/version"
)

const FailureCode int = -1
Expand Down Expand Up @@ -110,3 +111,22 @@ func TestHTTPProbeChecker(t *testing.T) {
}
}
}

func TestHTTPProbeCheckerUserAgent(t *testing.T) {
expected := fmt.Sprintf("Kubernetes/%s HTTP-Prober", version.Get())
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got := r.UserAgent()
if got != expected {
t.Errorf("User Agent mismatch, expected: %q got: %q", expected, got)
}
}))
u, err := url.Parse(server.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
prober := New()
_, _, err = prober.Probe(u, 1*time.Second)
if err != nil {
t.Errorf("Expected error: %v", err)
}
}

1 comment on commit 5be24e1

@k8s-teamcity-mesosphere

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity OSS :: Kubernetes Mesos :: 4 - Smoke Tests Build 8899 outcome was SUCCESS
Summary: Tests passed: 1, ignored: 205 Build time: 00:10:09

Please sign in to comment.