forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetcd_client.go
73 lines (66 loc) · 2.07 KB
/
etcd_client.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 main
import (
"errors"
"github.com/coreos/go-etcd/etcd"
"path/filepath"
"strings"
)
var replacer = strings.NewReplacer("/", "_")
// newEtcdClient returns an *etcd.Client with a connection to named machines.
// It returns an error if a connection to the cluster cannot be made.
func newEtcdClient(machines []string, cert, key string) (*etcd.Client, error) {
c := etcd.NewClient(machines)
if cert != "" && key != "" {
_, err := c.SetCertAndKey(cert, key)
if err != nil {
return c, err
}
}
success := c.SetCluster(machines)
if !success {
return c, errors.New("cannot connect to etcd cluster: " + strings.Join(machines, ","))
}
return c, nil
}
type EtcdClient interface {
Get(key string) ([]*etcd.Response, error)
}
// getValues queries etcd for keys prefixed by prefix.
// Etcd paths (keys) are translated into names more suitable for use in
// templates. For example if prefix where set to '/production' and one of the
// keys where '/nginx/port'; the prefixed '/production/nginx/port' key would
// be quired for. If the value for the prefixed key where 80, the returned map
// would contain the entry vars["nginx_port"] = "80".
func getValues(c EtcdClient, prefix string, keys []string) (map[string]interface{}, error) {
vars := make(map[string]interface{})
for _, key := range keys {
err := etcdWalk(c, filepath.Join(prefix, key), prefix, vars)
if err != nil {
return vars, err
}
}
return vars, nil
}
// etcdWalk recursively descends etcd paths, updating vars.
func etcdWalk(c EtcdClient, key string, prefix string, vars map[string]interface{}) error {
values, err := c.Get(key)
if err != nil {
return err
}
for _, v := range values {
if !v.Dir {
key := pathToKey(v.Key, prefix)
vars[key] = v.Value
} else {
etcdWalk(c, v.Key, prefix, vars)
}
}
return nil
}
// pathToKey translates etcd key paths into something more suitable for use
// in Golang templates. Turn /prefix/key/subkey into key_subkey.
func pathToKey(key, prefix string) string {
key = strings.TrimPrefix(key, prefix)
key = strings.TrimPrefix(key, "/")
return replacer.Replace(key)
}