diff --git a/pkg/kubelet/probe.go b/pkg/kubelet/probe.go index 185c2b3bab..6cd41186a2 100644 --- a/pkg/kubelet/probe.go +++ b/pkg/kubelet/probe.go @@ -32,23 +32,29 @@ import ( "github.com/golang/glog" ) +var ( + execprober = execprobe.New() + httprober = httprobe.New() + tcprober = tcprobe.New() +) + func (kl *Kubelet) probeContainer(p *api.LivenessProbe, podFullName string, podUID types.UID, status api.PodStatus, container api.Container) (probe.Status, error) { if p.Exec != nil { - return execprobe.Probe(kl.newExecInContainer(podFullName, podUID, container)) + return execprober.Probe(kl.newExecInContainer(podFullName, podUID, container)) } if p.HTTPGet != nil { port, err := extractPort(p.HTTPGet.Port, container) if err != nil { return probe.Unknown, err } - return httprobe.Probe(extractGetParams(p.HTTPGet, status, port)) + return httprober.Probe(extractGetParams(p.HTTPGet, status, port)) } if p.TCPSocket != nil { port, err := extractPort(p.TCPSocket.Port, container) if err != nil { return probe.Unknown, err } - return tcprobe.Probe(status.PodIP, port) + return tcprober.Probe(status.PodIP, port) } glog.Warningf("Failed to find probe builder for %s %+v", container.Name, container.LivenessProbe) return probe.Unknown, nil diff --git a/pkg/probe/exec/exec.go b/pkg/probe/exec/exec.go index 73d7283302..5633416168 100644 --- a/pkg/probe/exec/exec.go +++ b/pkg/probe/exec/exec.go @@ -27,7 +27,13 @@ import ( const defaultHealthyOutput = "ok" -func Probe(e uexec.Cmd) (probe.Status, error) { +func New() ExecProber { + return ExecProber{} +} + +type ExecProber struct{} + +func (pr ExecProber) Probe(e uexec.Cmd) (probe.Status, error) { data, err := e.CombinedOutput() glog.V(4).Infof("health check response: %s", string(data)) if err != nil { diff --git a/pkg/probe/exec/exec_test.go b/pkg/probe/exec/exec_test.go index 78c1a76be0..a3e710a862 100644 --- a/pkg/probe/exec/exec_test.go +++ b/pkg/probe/exec/exec_test.go @@ -42,6 +42,7 @@ type healthCheckTest struct { } func TestExec(t *testing.T) { + prober := New() fake := FakeCmd{} tests := []healthCheckTest{ // Ok @@ -54,7 +55,7 @@ func TestExec(t *testing.T) { for _, test := range tests { fake.out = test.output fake.err = test.err - status, err := Probe(&fake) + status, err := prober.Probe(&fake) if status != test.expectedStatus { t.Errorf("expected %v, got %v", test.expectedStatus, status) } diff --git a/pkg/probe/http/http.go b/pkg/probe/http/http.go index b9c406aa16..c8a8f05afa 100644 --- a/pkg/probe/http/http.go +++ b/pkg/probe/http/http.go @@ -27,11 +27,17 @@ import ( "github.com/golang/glog" ) -var client = &http.Client{} +func New() HTTPProber { + return HTTPProber{&http.Client{}} +} + +type HTTPProber struct { + client HTTPGetInterface +} // Probe returns a ProbeRunner capable of running an http check. -func Probe(host string, port int, path string) (probe.Status, error) { - return DoHTTPProbe(formatURL(host, port, path), client) +func (pr *HTTPProber) Probe(host string, port int, path string) (probe.Status, error) { + return DoHTTPProbe(formatURL(host, port, path), pr.client) } type HTTPGetInterface interface { diff --git a/pkg/probe/http/http_test.go b/pkg/probe/http/http_test.go index 146bf21b5c..b1bea1ee38 100644 --- a/pkg/probe/http/http_test.go +++ b/pkg/probe/http/http_test.go @@ -46,6 +46,7 @@ func TestFormatURL(t *testing.T) { } func TestHTTPProbeChecker(t *testing.T) { + prober := New() testCases := []struct { status int health probe.Status @@ -70,7 +71,7 @@ func TestHTTPProbeChecker(t *testing.T) { if err != nil { t.Errorf("Unexpected error: %v", err) } - health, err := Probe(host, p, "") + health, err := prober.Probe(host, p, "") if test.health == probe.Unknown && err == nil { t.Errorf("Expected error") } diff --git a/pkg/probe/tcp/tcp.go b/pkg/probe/tcp/tcp.go index 0dc2e343e7..c4b0da523b 100644 --- a/pkg/probe/tcp/tcp.go +++ b/pkg/probe/tcp/tcp.go @@ -25,7 +25,13 @@ import ( "github.com/golang/glog" ) -func Probe(host string, port int) (probe.Status, error) { +func New() TCPProber { + return TCPProber{} +} + +type TCPProber struct{} + +func (pr TCPProber) Probe(host string, port int) (probe.Status, error) { return DoTCPProbe(net.JoinHostPort(host, strconv.Itoa(port))) } diff --git a/pkg/probe/tcp/tcp_test.go b/pkg/probe/tcp/tcp_test.go index 2673ee3bcc..fd717e5d49 100644 --- a/pkg/probe/tcp/tcp_test.go +++ b/pkg/probe/tcp/tcp_test.go @@ -28,6 +28,7 @@ import ( ) func TestTcpHealthChecker(t *testing.T) { + prober := New() tests := []struct { expectedStatus probe.Status usePort bool @@ -58,7 +59,7 @@ func TestTcpHealthChecker(t *testing.T) { if !test.usePort { p = -1 } - status, err := Probe(host, p) + status, err := prober.Probe(host, p) if status != test.expectedStatus { t.Errorf("expected: %v, got: %v", test.expectedStatus, status) }