-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
56 lines (45 loc) · 1.2 KB
/
utils.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
package etcd
import (
"strings"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/pkg/transport"
"github.com/withlin/macvlan-ipam-etcd/backend/allocator"
)
func connectStore(etcdConfig *allocator.EtcdConfig) (*clientv3.Client, error) {
var etcdClient *clientv3.Client
var err error
if strings.HasPrefix(etcdConfig.EtcdURL, "https") {
etcdClient, err = connectWithTLS(etcdConfig.EtcdURL, etcdConfig.EtcdCertFile, etcdConfig.EtcdKeyFile, etcdConfig.EtcdTrustedCAFileFile)
} else {
etcdClient, err = connectWithoutTLS(etcdConfig.EtcdURL)
}
return etcdClient, err
}
/*
ETCD Related
*/
func connectWithoutTLS(url string) (*clientv3.Client, error) {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{url},
DialTimeout: 5 * time.Second,
})
return cli, err
}
func connectWithTLS(url, cert, key, trusted string) (*clientv3.Client, error) {
tlsInfo := transport.TLSInfo{
CertFile: cert,
KeyFile: key,
TrustedCAFile: trusted,
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
return nil, err
}
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{url},
DialTimeout: 5 * time.Second,
TLS: tlsConfig,
})
return cli, err
}