Add httpHeaders to httpGet liveness probe

Also update existing documentation and try to steer users away from 'host'.
Add validation.
This commit is contained in:
Rudi Chiarito
2016-02-02 10:03:50 -05:00
parent faa0fc3d8c
commit a2d1bb7acf
27 changed files with 30025 additions and 28798 deletions

View File

@@ -36,7 +36,7 @@ func New() HTTPProber {
}
type HTTPProber interface {
Probe(url *url.URL, timeout time.Duration) (probe.Result, string, error)
Probe(url *url.URL, headers http.Header, timeout time.Duration) (probe.Result, string, error)
}
type httpProber struct {
@@ -44,20 +44,26 @@ type httpProber struct {
}
// Probe returns a ProbeRunner capable of running an http check.
func (pr httpProber) Probe(url *url.URL, timeout time.Duration) (probe.Result, string, error) {
return DoHTTPProbe(url, &http.Client{Timeout: timeout, Transport: pr.transport})
func (pr httpProber) Probe(url *url.URL, headers http.Header, timeout time.Duration) (probe.Result, string, error) {
return DoHTTPProbe(url, headers, &http.Client{Timeout: timeout, Transport: pr.transport})
}
type HTTPGetInterface interface {
Get(u string) (*http.Response, error)
Do(req *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, headers http.Header, client HTTPGetInterface) (probe.Result, string, error) {
req, err := http.NewRequest("GET", url.String(), nil)
if err != nil {
// Convert errors into failures to catch timeouts.
return probe.Failure, err.Error(), nil
}
req.Header = headers
res, err := client.Do(req)
if err != nil {
// Convert errors into failures to catch timeouts.
return probe.Failure, err.Error(), nil

View File

@@ -42,8 +42,8 @@ func containsAny(s string, substrs []string) bool {
}
func TestHTTPProbeChecker(t *testing.T) {
handleReq := func(s int, body string) func(w http.ResponseWriter) {
return func(w http.ResponseWriter) {
handleReq := func(s int, body string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(s)
w.Write([]byte(body))
}
@@ -51,8 +51,9 @@ func TestHTTPProbeChecker(t *testing.T) {
prober := New()
testCases := []struct {
handler func(w http.ResponseWriter)
health probe.Result
handler func(w http.ResponseWriter, r *http.Request)
reqHeaders http.Header
health probe.Result
// go1.5: error message changed for timeout, need to support
// both old and new
accBodies []string
@@ -60,18 +61,41 @@ func TestHTTPProbeChecker(t *testing.T) {
// The probe will be filled in below. This is primarily testing that an HTTP GET happens.
{
handleReq(http.StatusOK, "ok body"),
nil,
probe.Success,
[]string{"ok body"},
},
{
// Echo handler that returns the contents of request headers in the body
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
output := ""
for k, arr := range r.Header {
for _, v := range arr {
output += fmt.Sprintf("%s: %s\n", k, v)
}
}
w.Write([]byte(output))
},
http.Header{
"X-Muffins-Or-Cupcakes": {"muffins"},
},
probe.Success,
[]string{
"X-Muffins-Or-Cupcakes: muffins",
},
},
{
handleReq(FailureCode, "fail body"),
nil,
probe.Failure,
[]string{fmt.Sprintf("HTTP probe failed with statuscode: %d", FailureCode)},
},
{
func(w http.ResponseWriter) {
func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
},
nil,
probe.Failure,
[]string{
"use of closed network connection",
@@ -82,7 +106,7 @@ func TestHTTPProbeChecker(t *testing.T) {
for _, test := range testCases {
// TODO: Close() this when fix #19254
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
test.handler(w)
test.handler(w, r)
}))
u, err := url.Parse(server.URL)
if err != nil {
@@ -96,7 +120,7 @@ func TestHTTPProbeChecker(t *testing.T) {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
health, output, err := prober.Probe(u, 1*time.Second)
health, output, err := prober.Probe(u, test.reqHeaders, 1*time.Second)
if test.health == probe.Unknown && err == nil {
t.Errorf("Expected error")
}