forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
health.go
77 lines (69 loc) · 2.07 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package metrics
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
"github.com/golang/glog"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/kubernetes/pkg/probe"
probehttp "k8s.io/kubernetes/pkg/probe/http"
)
var errBackend = fmt.Errorf("backend reported failure")
// HTTPBackendAvailable returns a healthz check that verifies a backend responds to a GET to
// the provided URL with 2xx or 3xx response.
func HTTPBackendAvailable(u *url.URL) healthz.HealthzChecker {
p := probehttp.New()
return healthz.NamedCheck("backend-http", func(r *http.Request) error {
result, _, err := p.Probe(u, nil, 2*time.Second)
if err != nil {
return err
}
if result != probe.Success {
return errBackend
}
return nil
})
}
// ProxyProtocolHTTPBackendAvailable returns a healthz check that verifies a backend supporting
// the HAProxy PROXY protocol responds to a GET to the provided URL with 2xx or 3xx response.
func ProxyProtocolHTTPBackendAvailable(u *url.URL) healthz.HealthzChecker {
dialer := &net.Dialer{
Timeout: 2 * time.Second,
DualStack: true,
}
return healthz.NamedCheck("backend-proxy-http", func(r *http.Request) error {
conn, err := dialer.Dial("tcp", u.Host)
if err != nil {
return err
}
conn.SetDeadline(time.Now().Add(2 * time.Second))
br := bufio.NewReader(conn)
if _, err := conn.Write([]byte("PROXY UNKNOWN\r\n")); err != nil {
return err
}
req := &http.Request{Method: "GET", URL: u, Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1}
if err := req.Write(conn); err != nil {
return err
}
res, err := http.ReadResponse(br, req)
if err != nil {
return err
}
// read full body
defer res.Body.Close()
if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
glog.V(4).Infof("Error discarding probe body contents: %v", err)
}
if res.StatusCode < http.StatusOK && res.StatusCode >= http.StatusBadRequest {
glog.V(4).Infof("Probe failed for %s, Response: %v", u.String(), res)
return errBackend
}
glog.V(4).Infof("Probe succeeded for %s, Response: %v", u.String(), res)
return nil
})
}