forked from rancher/rke
/
etcd_util.go
152 lines (138 loc) · 4.16 KB
/
etcd_util.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
package services
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"strings"
"time"
etcdclient "github.com/coreos/etcd/client"
"github.com/rancher/rke/hosts"
"github.com/sirupsen/logrus"
)
func getEtcdClient(ctx context.Context, etcdHost *hosts.Host, localConnDialerFactory hosts.DialerFactory, cert, key []byte) (etcdclient.Client, error) {
dialer, err := getEtcdDialer(localConnDialerFactory, etcdHost)
if err != nil {
return nil, fmt.Errorf("Failed to create a dialer for host [%s]: %v", etcdHost.Address, err)
}
tlsConfig, err := getEtcdTLSConfig(cert, key)
if err != nil {
return nil, err
}
var DefaultEtcdTransport etcdclient.CancelableTransport = &http.Transport{
Dial: dialer,
TLSClientConfig: tlsConfig,
TLSHandshakeTimeout: 10 * time.Second,
}
cfg := etcdclient.Config{
Endpoints: []string{"https://" + etcdHost.InternalAddress + ":2379"},
Transport: DefaultEtcdTransport,
}
return etcdclient.New(cfg)
}
func isEtcdHealthy(ctx context.Context, localConnDialerFactory hosts.DialerFactory, host *hosts.Host, cert, key []byte, url string) bool {
logrus.Debugf("[etcd] Check etcd cluster health")
for i := 0; i < 3; i++ {
dialer, err := getEtcdDialer(localConnDialerFactory, host)
if err != nil {
return false
}
tlsConfig, err := getEtcdTLSConfig(cert, key)
if err != nil {
logrus.Debugf("[etcd] Failed to create etcd tls config for host [%s]: %v", host.Address, err)
return false
}
hc := http.Client{
Transport: &http.Transport{
Dial: dialer,
TLSClientConfig: tlsConfig,
TLSHandshakeTimeout: 10 * time.Second,
},
}
healthy, err := getHealthEtcd(hc, host, url)
if err != nil {
logrus.Debug(err)
time.Sleep(5 * time.Second)
continue
}
if healthy == "true" {
logrus.Debugf("[etcd] etcd cluster is healthy")
return true
}
}
return false
}
func getHealthEtcd(hc http.Client, host *hosts.Host, url string) (string, error) {
healthy := struct{ Health string }{}
resp, err := hc.Get(url)
if err != nil {
return healthy.Health, fmt.Errorf("Failed to get /health for host [%s]: %v", host.Address, err)
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return healthy.Health, fmt.Errorf("Failed to read response of /health for host [%s]: %v", host.Address, err)
}
resp.Body.Close()
if err := json.Unmarshal(bytes, &healthy); err != nil {
return healthy.Health, fmt.Errorf("Failed to unmarshal response of /health for host [%s]: %v", host.Address, err)
}
return healthy.Health, nil
}
func GetEtcdInitialCluster(hosts []*hosts.Host) string {
initialCluster := ""
for i, host := range hosts {
initialCluster += fmt.Sprintf("etcd-%s=https://%s:2380", host.HostnameOverride, host.InternalAddress)
if i < (len(hosts) - 1) {
initialCluster += ","
}
}
return initialCluster
}
func getEtcdDialer(localConnDialerFactory hosts.DialerFactory, etcdHost *hosts.Host) (func(network, address string) (net.Conn, error), error) {
etcdHost.LocalConnPort = 2379
var etcdFactory hosts.DialerFactory
if localConnDialerFactory == nil {
etcdFactory = hosts.LocalConnFactory
} else {
etcdFactory = localConnDialerFactory
}
return etcdFactory(etcdHost)
}
func GetEtcdConnString(hosts []*hosts.Host, hostAddress string) string {
connHosts := []string{}
containsHostAddress := false
for _, host := range hosts {
if host.InternalAddress == hostAddress {
containsHostAddress = true
continue
}
connHosts = append(connHosts, "https://"+host.InternalAddress+":2379")
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(connHosts), func(i, j int) {
connHosts[i], connHosts[j] = connHosts[j], connHosts[i]
})
if containsHostAddress {
connHosts = append([]string{"https://" + hostAddress + ":2379"}, connHosts...)
}
return strings.Join(connHosts, ",")
}
func getEtcdTLSConfig(certificate, key []byte) (*tls.Config, error) {
// get tls config
x509Pair, err := tls.X509KeyPair([]byte(certificate), []byte(key))
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{x509Pair},
}
if err != nil {
return nil, err
}
return tlsConfig, nil
}