Changed HTTPGetAction to allow user-defined schemes
This commit is contained in:
@@ -17,11 +17,10 @@ limitations under the License.
|
||||
package http
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
@@ -30,12 +29,13 @@ import (
|
||||
)
|
||||
|
||||
func New() HTTPProber {
|
||||
transport := &http.Transport{}
|
||||
tlsConfig := &tls.Config{InsecureSkipVerify: true}
|
||||
transport := &http.Transport{TLSClientConfig: tlsConfig}
|
||||
return httpProber{transport}
|
||||
}
|
||||
|
||||
type HTTPProber interface {
|
||||
Probe(host string, port int, path string, timeout time.Duration) (probe.Result, string, error)
|
||||
Probe(url *url.URL, timeout time.Duration) (probe.Result, string, error)
|
||||
}
|
||||
|
||||
type httpProber struct {
|
||||
@@ -43,8 +43,8 @@ type httpProber struct {
|
||||
}
|
||||
|
||||
// Probe returns a ProbeRunner capable of running an http check.
|
||||
func (pr httpProber) Probe(host string, port int, path string, timeout time.Duration) (probe.Result, string, error) {
|
||||
return DoHTTPProbe(formatURL(host, port, path), &http.Client{Timeout: timeout, Transport: pr.transport})
|
||||
func (pr httpProber) Probe(url *url.URL, timeout time.Duration) (probe.Result, string, error) {
|
||||
return DoHTTPProbe(url, &http.Client{Timeout: timeout, Transport: pr.transport})
|
||||
}
|
||||
|
||||
type HTTPGetInterface interface {
|
||||
@@ -55,8 +55,8 @@ type HTTPGetInterface interface {
|
||||
// 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 string, client HTTPGetInterface) (probe.Result, string, error) {
|
||||
res, err := client.Get(url)
|
||||
func DoHTTPProbe(url *url.URL, client HTTPGetInterface) (probe.Result, string, error) {
|
||||
res, err := client.Get(url.String())
|
||||
if err != nil {
|
||||
// Convert errors into failures to catch timeouts.
|
||||
return probe.Failure, err.Error(), nil
|
||||
@@ -68,18 +68,9 @@ func DoHTTPProbe(url string, client HTTPGetInterface) (probe.Result, string, err
|
||||
}
|
||||
body := string(b)
|
||||
if res.StatusCode >= http.StatusOK && res.StatusCode < http.StatusBadRequest {
|
||||
glog.V(4).Infof("Probe succeeded for %s, Response: %v", url.String(), *res)
|
||||
return probe.Success, body, nil
|
||||
}
|
||||
glog.V(4).Infof("Probe failed for %s, Response: %v", url, *res)
|
||||
glog.V(4).Infof("Probe failed for %s, Response: %v", url.String(), *res)
|
||||
return probe.Failure, body, nil
|
||||
}
|
||||
|
||||
// formatURL formats a URL from args. For testability.
|
||||
func formatURL(host string, port int, path string) string {
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: net.JoinHostPort(host, strconv.Itoa(port)),
|
||||
Path: path,
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
@@ -29,24 +29,6 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/probe"
|
||||
)
|
||||
|
||||
func TestFormatURL(t *testing.T) {
|
||||
testCases := []struct {
|
||||
host string
|
||||
port int
|
||||
path string
|
||||
result string
|
||||
}{
|
||||
{"localhost", 93, "", "http://localhost:93"},
|
||||
{"localhost", 93, "/path", "http://localhost:93/path"},
|
||||
}
|
||||
for _, test := range testCases {
|
||||
url := formatURL(test.host, test.port, test.path)
|
||||
if url != test.result {
|
||||
t.Errorf("Expected %s, got %s", test.result, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPProbeChecker(t *testing.T) {
|
||||
handleReq := func(s int, body string) func(w http.ResponseWriter) {
|
||||
return func(w http.ResponseWriter) {
|
||||
@@ -74,15 +56,15 @@ func TestHTTPProbeChecker(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
host, port, err := net.SplitHostPort(u.Host)
|
||||
_, port, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
p, err := strconv.Atoi(port)
|
||||
_, err = strconv.Atoi(port)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
health, output, err := prober.Probe(host, p, "", 1*time.Second)
|
||||
health, output, err := prober.Probe(u, 1*time.Second)
|
||||
if test.health == probe.Unknown && err == nil {
|
||||
t.Errorf("Expected error")
|
||||
}
|
||||
|
Reference in New Issue
Block a user