forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
etcd.go
60 lines (52 loc) · 1.68 KB
/
etcd.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
package etcd
import (
"fmt"
"time"
"github.com/coreos/etcd/clientv3"
"golang.org/x/net/context"
etcdutil "k8s.io/apiserver/pkg/storage/etcd/util"
restclient "k8s.io/client-go/rest"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
)
// MakeEtcdClientV3Config creates client configuration based on the configapi.
func MakeEtcdClientV3Config(etcdClientInfo configapi.EtcdConnectionInfo) (*clientv3.Config, error) {
tlsConfig, err := restclient.TLSConfigFor(&restclient.Config{
TLSClientConfig: restclient.TLSClientConfig{
CertFile: etcdClientInfo.ClientCert.CertFile,
KeyFile: etcdClientInfo.ClientCert.KeyFile,
CAFile: etcdClientInfo.CA,
},
})
if err != nil {
return nil, err
}
return &clientv3.Config{
Endpoints: etcdClientInfo.URLs,
DialTimeout: 30 * time.Second,
TLS: tlsConfig,
}, nil
}
// MakeEtcdClientV3 creates an etcd v3 client based on the provided config.
func MakeEtcdClientV3(etcdClientInfo configapi.EtcdConnectionInfo) (*clientv3.Client, error) {
cfg, err := MakeEtcdClientV3Config(etcdClientInfo)
if err != nil {
return nil, err
}
return clientv3.New(*cfg)
}
// TestEtcdClientV3 verifies a client is functional. It will attempt to
// connect to the etcd server and block until the server responds at least once, or return an
// error if the server never responded.
func TestEtcdClientV3(etcdClient *clientv3.Client) error {
for i := 0; ; i++ {
_, err := clientv3.NewKV(etcdClient).Get(context.Background(), "/", clientv3.WithLimit(1))
if err == nil || etcdutil.IsEtcdNotFound(err) {
break
}
if i > 100 {
return fmt.Errorf("could not reach etcd(v3): %v", err)
}
time.Sleep(50 * time.Millisecond)
}
return nil
}