forked from TrueCloudLab/distribution
Add headers parameter for HTTP checker
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
parent
e8f088fea6
commit
b67aab2f60
5 changed files with 36 additions and 6 deletions
|
@ -202,6 +202,8 @@ type HTTPChecker struct {
|
||||||
Interval time.Duration `yaml:"interval,omitempty"`
|
Interval time.Duration `yaml:"interval,omitempty"`
|
||||||
// URI is the HTTP URI to check
|
// URI is the HTTP URI to check
|
||||||
URI string `yaml:"uri,omitempty"`
|
URI string `yaml:"uri,omitempty"`
|
||||||
|
// Headers lists static headers that should be added to all requests
|
||||||
|
Headers http.Header `yaml:"headers"`
|
||||||
// Threshold is the number of times a check must fail to trigger an
|
// Threshold is the number of times a check must fail to trigger an
|
||||||
// unhealthy state
|
// unhealthy state
|
||||||
Threshold int `yaml:"threshold,omitempty"`
|
Threshold int `yaml:"threshold,omitempty"`
|
||||||
|
|
|
@ -205,6 +205,8 @@ information about each option that appears later in this page.
|
||||||
interval: 10s
|
interval: 10s
|
||||||
http:
|
http:
|
||||||
- uri: http://server.to.check/must/return/200
|
- uri: http://server.to.check/must/return/200
|
||||||
|
headers:
|
||||||
|
Authorization: [Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==]
|
||||||
statuscode: 200
|
statuscode: 200
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
interval: 10s
|
interval: 10s
|
||||||
|
@ -1400,7 +1402,9 @@ The URL to which events should be published.
|
||||||
yes
|
yes
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
Static headers to add to each request.
|
Static headers to add to each request. Each header's name should be a key
|
||||||
|
underneath headers, and each value is a list of payloads for that
|
||||||
|
header name. Note that values must always be lists.
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -1619,6 +1623,8 @@ Configure the behavior of the Redis connection pool.
|
||||||
interval: 10s
|
interval: 10s
|
||||||
http:
|
http:
|
||||||
- uri: http://server.to.check/must/return/200
|
- uri: http://server.to.check/must/return/200
|
||||||
|
headers:
|
||||||
|
Authorization: [Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==]
|
||||||
statuscode: 200
|
statuscode: 200
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
interval: 10s
|
interval: 10s
|
||||||
|
@ -1766,6 +1772,19 @@ health check will fail.
|
||||||
<td>
|
<td>
|
||||||
The URI to check.
|
The URI to check.
|
||||||
</td>
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<code>headers</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
no
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Static headers to add to each request. Each header's name should be a key
|
||||||
|
underneath headers, and each value is a list of payloads for that
|
||||||
|
header name. Note that values must always be lists.
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
|
|
|
@ -24,12 +24,21 @@ func FileChecker(f string) health.Checker {
|
||||||
|
|
||||||
// HTTPChecker does a HEAD request and verifies that the HTTP status code
|
// HTTPChecker does a HEAD request and verifies that the HTTP status code
|
||||||
// returned matches statusCode.
|
// returned matches statusCode.
|
||||||
func HTTPChecker(r string, statusCode int, timeout time.Duration) health.Checker {
|
func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker {
|
||||||
return health.CheckFunc(func() error {
|
return health.CheckFunc(func() error {
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
}
|
}
|
||||||
response, err := client.Head(r)
|
req, err := http.NewRequest("HEAD", r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("error creating request: " + r)
|
||||||
|
}
|
||||||
|
for headerName, headerValues := range headers {
|
||||||
|
for _, headerValue := range headerValues {
|
||||||
|
req.Header.Add(headerName, headerValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("error while checking: " + r)
|
return errors.New("error while checking: " + r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,11 @@ func TestFileChecker(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHTTPChecker(t *testing.T) {
|
func TestHTTPChecker(t *testing.T) {
|
||||||
if err := HTTPChecker("https://www.google.cybertron", 200, 0).Check(); err == nil {
|
if err := HTTPChecker("https://www.google.cybertron", 200, 0, nil).Check(); err == nil {
|
||||||
t.Errorf("Google on Cybertron was expected as not exists")
|
t.Errorf("Google on Cybertron was expected as not exists")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := HTTPChecker("https://www.google.pt", 200, 0).Check(); err != nil {
|
if err := HTTPChecker("https://www.google.pt", 200, 0, nil).Check(); err != nil {
|
||||||
t.Errorf("Google at Portugal was expected as exists, error:%v", err)
|
t.Errorf("Google at Portugal was expected as exists, error:%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -281,7 +281,7 @@ func (app *App) RegisterHealthChecks(healthRegistries ...*health.Registry) {
|
||||||
statusCode = 200
|
statusCode = 200
|
||||||
}
|
}
|
||||||
|
|
||||||
checker := checks.HTTPChecker(httpChecker.URI, statusCode, httpChecker.Timeout)
|
checker := checks.HTTPChecker(httpChecker.URI, statusCode, httpChecker.Timeout, httpChecker.Headers)
|
||||||
|
|
||||||
if httpChecker.Threshold != 0 {
|
if httpChecker.Threshold != 0 {
|
||||||
ctxu.GetLogger(app).Infof("configuring HTTP health check uri=%s, interval=%d, threshold=%d", httpChecker.URI, interval/time.Second, httpChecker.Threshold)
|
ctxu.GetLogger(app).Infof("configuring HTTP health check uri=%s, interval=%d, threshold=%d", httpChecker.URI, interval/time.Second, httpChecker.Threshold)
|
||||||
|
|
Loading…
Reference in a new issue