-
Notifications
You must be signed in to change notification settings - Fork 130
/
health.go
73 lines (68 loc) · 2.19 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
package k8s
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/hyperledger/fabric-test/tools/operator/logger"
"github.com/hyperledger/fabric-test/tools/operator/networkspec"
"k8s.io/client-go/kubernetes"
)
func (k8s K8s) checkHealth(componentName string, config networkspec.Config, clientset *kubernetes.Clientset) error {
logger.INFO("Checking health for ", componentName)
portNumber, err := k8s.ServicePort(componentName, config.K8s.ServiceType, k8s.Config.K8s.Namespace, true, clientset)
if err != nil {
return fmt.Errorf("Failed to get the port for %v", componentName)
}
nodeIP, err := k8s.ExternalIP(config, componentName, clientset)
if err != nil {
return fmt.Errorf("Failed to get the IP address for %v", componentName)
}
url := fmt.Sprintf("http://%s:%s/healthz", nodeIP, portNumber)
for i := 0; i < 6; i++ {
logger.INFO("Querying /healthz URL: " + url)
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("Error while hitting the endpoint")
}
bytes, err := ioutil.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return err
}
if resp.StatusCode == http.StatusOK {
logger.INFO("Health check passed for ", componentName)
return nil
}
logger.INFO("Response status: ", strconv.Itoa(resp.StatusCode))
logger.INFO("Response body: ", string(bytes))
logger.INFO("Healthcheck failed, attempting again in 10 seconds")
time.Sleep(time.Second * 5)
}
return fmt.Errorf("Health check failed for %s", componentName)
}
//CheckComponentsHealth --
func (k8s K8s) CheckComponentsHealth(config networkspec.Config, clientset *kubernetes.Clientset) error {
for i := 0; i < len(config.OrdererOrganizations); i++ {
org := config.OrdererOrganizations[i]
for j := 0; j < org.NumOrderers; j++ {
ordererName := fmt.Sprintf("orderer%d-%s", j, org.Name)
err := k8s.checkHealth(ordererName, config, clientset)
if err != nil {
return err
}
}
}
for i := 0; i < len(config.PeerOrganizations); i++ {
org := config.PeerOrganizations[i]
for j := 0; j < org.NumPeers; j++ {
peerName := fmt.Sprintf("peer%d-%s", j, org.Name)
err := k8s.checkHealth(peerName, config, clientset)
if err != nil {
return err
}
}
}
return nil
}