2017-12-19 22:18:27 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-12-19 22:18:27 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-12-19 22:18:27 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
HealthzAddress = "localhost"
|
|
|
|
HealthzEndpoint = "/healthz"
|
|
|
|
HTTPProtoPrefix = "http://"
|
|
|
|
HTTPSProtoPrefix = "https://"
|
|
|
|
)
|
|
|
|
|
2018-01-11 01:00:14 +00:00
|
|
|
func runHealthcheck(ctx context.Context, host *hosts.Host, port int, useTLS bool, serviceName string, localConnDialerFactory hosts.DialerFactory) error {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[healthcheck] Start Healthcheck on service [%s] on host [%s]", serviceName, host.Address)
|
2018-01-11 01:00:14 +00:00
|
|
|
client, err := getHealthCheckHTTPClient(host, port, localConnDialerFactory)
|
2017-12-19 22:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to initiate new HTTP client for service [%s] for host [%s]", serviceName, host.Address)
|
|
|
|
}
|
2018-01-09 21:09:41 +00:00
|
|
|
for retries := 0; retries < 10; retries++ {
|
2018-02-03 01:04:53 +00:00
|
|
|
if err = getHealthz(client, useTLS, serviceName, host.Address, port); err != nil {
|
2017-12-19 22:18:27 +00:00
|
|
|
logrus.Debugf("[healthcheck] %v", err)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[healthcheck] service [%s] on host [%s] is healthy", serviceName, host.Address)
|
2017-12-19 22:18:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Failed to verify healthcheck: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:00:14 +00:00
|
|
|
func getHealthCheckHTTPClient(host *hosts.Host, port int, localConnDialerFactory hosts.DialerFactory) (*http.Client, error) {
|
|
|
|
host.LocalConnPort = port
|
2017-12-19 22:18:27 +00:00
|
|
|
var factory hosts.DialerFactory
|
2018-01-11 01:00:14 +00:00
|
|
|
if localConnDialerFactory == nil {
|
|
|
|
factory = hosts.LocalConnFactory
|
2017-12-19 22:18:27 +00:00
|
|
|
} else {
|
2018-01-11 01:00:14 +00:00
|
|
|
factory = localConnDialerFactory
|
2017-12-19 22:18:27 +00:00
|
|
|
}
|
|
|
|
dialer, err := factory(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to create a dialer for host [%s]: %v", host.Address, err)
|
|
|
|
}
|
|
|
|
return &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: dialer,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-02-03 01:04:53 +00:00
|
|
|
func getHealthz(client *http.Client, useTLS bool, serviceName, hostAddress string, port int) error {
|
2017-12-19 22:18:27 +00:00
|
|
|
proto := HTTPProtoPrefix
|
|
|
|
if useTLS {
|
|
|
|
proto = HTTPSProtoPrefix
|
|
|
|
}
|
2018-02-03 01:04:53 +00:00
|
|
|
resp, err := client.Get(fmt.Sprintf("%s%s:%d%s", proto, HealthzAddress, port, HealthzEndpoint))
|
2017-12-19 22:18:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to check %s for service [%s] on host [%s]: %v", HealthzEndpoint, serviceName, hostAddress, err)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
statusBody, _ := ioutil.ReadAll(resp.Body)
|
|
|
|
return fmt.Errorf("service [%s] is not healthy response code: [%d], response body: %s", serviceName, resp.StatusCode, statusBody)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|