/
http.go
170 lines (147 loc) · 4.02 KB
/
http.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package healthzhttp
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"time"
"github.com/jasonhancock/healthz"
"github.com/pkg/errors"
)
type options struct {
client *http.Client
method string
matchContent *regexp.Regexp
body []byte
username string
password string
allowedStatusCodes map[int]struct{}
}
// Option is used to customize the summarizer
type Option func(*options) error
// WithHTTPClient sets the HTTP client to use for the checks
func WithHTTPClient(client *http.Client) Option {
return func(o *options) error {
o.client = client
return nil
}
}
// WithMethod sets the HTTP method to use for the checks
func WithMethod(method string) Option {
return func(o *options) error {
o.method = method
return nil
}
}
// WithBody sets the body to be sent along with each request
func WithBody(body []byte) Option {
return func(o *options) error {
o.body = body
return nil
}
}
// WithBasicAuth sets basic auth credentials to be used for each request
func WithBasicAuth(username, password string) Option {
return func(o *options) error {
o.username = username
o.password = password
return nil
}
}
// WithAllowedStatusCode adds a status code that won't trigger an error
func WithAllowedStatusCode(status int) Option {
return func(o *options) error {
o.allowedStatusCodes[status] = struct{}{}
return nil
}
}
// WithoutAllowedStatusCode removes a status code from the list of allowed codes.
// This is useful to remove the default 200 status code
func WithoutAllowedStatusCode(status int) Option {
return func(o *options) error {
if _, ok := o.allowedStatusCodes[status]; ok {
delete(o.allowedStatusCodes, status)
}
return nil
}
}
// WithRegexp performs a content check on the body of the HTTP response.
func WithRegexp(expr string) Option {
return func(o *options) error {
regex, err := regexp.Compile(expr)
if err != nil {
return err
}
o.matchContent = regex
return nil
}
}
// CheckHTTP is an HTTP healthz check
type CheckHTTP struct {
url *url.URL
client *http.Client
method string
body []byte
matchContent *regexp.Regexp
username string
password string
allowedStatusCodes map[int]struct{}
}
// NewCheck creates a new CheckHTTP.
func NewCheck(endpoint string, opts ...Option) (*CheckHTTP, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, errors.Wrap(err, "CheckHTTP parsing endpoint")
}
opt := &options{
client: &http.Client{Timeout: 10 * time.Second},
method: http.MethodGet,
allowedStatusCodes: map[int]struct{}{
http.StatusOK: struct{}{},
},
}
for _, o := range opts {
err := o(opt)
if err != nil {
return nil, errors.Wrap(err, "CheckHTTP evaluating options")
}
}
c := &CheckHTTP{
client: opt.client,
url: u,
method: opt.method,
body: opt.body,
allowedStatusCodes: opt.allowedStatusCodes,
matchContent: opt.matchContent,
}
return c, nil
}
// Check performs the check
func (c CheckHTTP) Check(ctx context.Context) *healthz.Response {
body := bytes.NewReader(c.body)
req, err := http.NewRequest(c.method, c.url.String(), body)
if err != nil {
return &healthz.Response{Error: err}
}
if c.username != "" && c.password != "" {
req.SetBasicAuth(c.username, c.password)
}
resp, err := c.client.Do(req)
if err != nil {
return &healthz.Response{Error: err}
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &healthz.Response{Error: errors.Wrap(err, "reading response body")}
}
if _, ok := c.allowedStatusCodes[resp.StatusCode]; !ok {
return &healthz.Response{Error: errors.Errorf("Unexpected http status code: %d", resp.StatusCode)}
}
if c.matchContent != nil && !c.matchContent.Match(respBody) {
return &healthz.Response{Error: errors.Errorf("the response body did not match the supplied regex: %s", c.matchContent.String())}
}
return &healthz.Response{}
}