mirror of https://github.com/k3s-io/k3s
add Probers to Probe pkgs.
parent
a298402bd4
commit
6eb0b89cbd
|
@ -32,23 +32,29 @@ import (
|
||||||
"github.com/golang/glog"
|
"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) {
|
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 {
|
if p.Exec != nil {
|
||||||
return execprobe.Probe(kl.newExecInContainer(podFullName, podUID, container))
|
return execprober.Probe(kl.newExecInContainer(podFullName, podUID, container))
|
||||||
}
|
}
|
||||||
if p.HTTPGet != nil {
|
if p.HTTPGet != nil {
|
||||||
port, err := extractPort(p.HTTPGet.Port, container)
|
port, err := extractPort(p.HTTPGet.Port, container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return probe.Unknown, err
|
return probe.Unknown, err
|
||||||
}
|
}
|
||||||
return httprobe.Probe(extractGetParams(p.HTTPGet, status, port))
|
return httprober.Probe(extractGetParams(p.HTTPGet, status, port))
|
||||||
}
|
}
|
||||||
if p.TCPSocket != nil {
|
if p.TCPSocket != nil {
|
||||||
port, err := extractPort(p.TCPSocket.Port, container)
|
port, err := extractPort(p.TCPSocket.Port, container)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return probe.Unknown, err
|
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)
|
glog.Warningf("Failed to find probe builder for %s %+v", container.Name, container.LivenessProbe)
|
||||||
return probe.Unknown, nil
|
return probe.Unknown, nil
|
||||||
|
|
|
@ -27,7 +27,13 @@ import (
|
||||||
|
|
||||||
const defaultHealthyOutput = "ok"
|
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()
|
data, err := e.CombinedOutput()
|
||||||
glog.V(4).Infof("health check response: %s", string(data))
|
glog.V(4).Infof("health check response: %s", string(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -42,6 +42,7 @@ type healthCheckTest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExec(t *testing.T) {
|
func TestExec(t *testing.T) {
|
||||||
|
prober := New()
|
||||||
fake := FakeCmd{}
|
fake := FakeCmd{}
|
||||||
tests := []healthCheckTest{
|
tests := []healthCheckTest{
|
||||||
// Ok
|
// Ok
|
||||||
|
@ -54,7 +55,7 @@ func TestExec(t *testing.T) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
fake.out = test.output
|
fake.out = test.output
|
||||||
fake.err = test.err
|
fake.err = test.err
|
||||||
status, err := Probe(&fake)
|
status, err := prober.Probe(&fake)
|
||||||
if status != test.expectedStatus {
|
if status != test.expectedStatus {
|
||||||
t.Errorf("expected %v, got %v", test.expectedStatus, status)
|
t.Errorf("expected %v, got %v", test.expectedStatus, status)
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,11 +27,17 @@ import (
|
||||||
"github.com/golang/glog"
|
"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.
|
// Probe returns a ProbeRunner capable of running an http check.
|
||||||
func Probe(host string, port int, path string) (probe.Status, error) {
|
func (pr *HTTPProber) Probe(host string, port int, path string) (probe.Status, error) {
|
||||||
return DoHTTPProbe(formatURL(host, port, path), client)
|
return DoHTTPProbe(formatURL(host, port, path), pr.client)
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPGetInterface interface {
|
type HTTPGetInterface interface {
|
||||||
|
|
|
@ -46,6 +46,7 @@ func TestFormatURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPProbeChecker(t *testing.T) {
|
func TestHTTPProbeChecker(t *testing.T) {
|
||||||
|
prober := New()
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
status int
|
status int
|
||||||
health probe.Status
|
health probe.Status
|
||||||
|
@ -70,7 +71,7 @@ func TestHTTPProbeChecker(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error: %v", err)
|
t.Errorf("Unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
health, err := Probe(host, p, "")
|
health, err := prober.Probe(host, p, "")
|
||||||
if test.health == probe.Unknown && err == nil {
|
if test.health == probe.Unknown && err == nil {
|
||||||
t.Errorf("Expected error")
|
t.Errorf("Expected error")
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,13 @@ import (
|
||||||
"github.com/golang/glog"
|
"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)))
|
return DoTCPProbe(net.JoinHostPort(host, strconv.Itoa(port)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTcpHealthChecker(t *testing.T) {
|
func TestTcpHealthChecker(t *testing.T) {
|
||||||
|
prober := New()
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
expectedStatus probe.Status
|
expectedStatus probe.Status
|
||||||
usePort bool
|
usePort bool
|
||||||
|
@ -58,7 +59,7 @@ func TestTcpHealthChecker(t *testing.T) {
|
||||||
if !test.usePort {
|
if !test.usePort {
|
||||||
p = -1
|
p = -1
|
||||||
}
|
}
|
||||||
status, err := Probe(host, p)
|
status, err := prober.Probe(host, p)
|
||||||
if status != test.expectedStatus {
|
if status != test.expectedStatus {
|
||||||
t.Errorf("expected: %v, got: %v", test.expectedStatus, status)
|
t.Errorf("expected: %v, got: %v", test.expectedStatus, status)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue