Skip to content
Snippets Groups Projects
Commit a76897ce authored by ale's avatar ale
Browse files

etcd.NewClient() wants URLs

parent e3e5db7c
Branches
Tags
No related merge requests found
......@@ -11,7 +11,7 @@ import (
)
var (
etcdMachines = flag.String("etcd-servers", "localhost:7800", "Etcd servers (comma-separated list)")
etcdMachines = flag.String("etcd-servers", "localhost:4001", "Etcd servers (comma-separated list)")
etcdCertFile = flag.String("etcd-cert", "", "SSL certificate for etcd client")
etcdKeyFile = flag.String("etcd-key", "", "SSL private key for etcd client")
)
......@@ -24,7 +24,8 @@ func loadFile(path string) string {
return string(data)
}
func resolveAll(input []string) []string {
// Resolve a list of etcd host:port specs, returning URLs.
func resolveAll(input []string, proto string) []string {
result := make([]string, 0)
for _, hostport := range input {
host, port, err := net.SplitHostPort(hostport)
......@@ -36,19 +37,27 @@ func resolveAll(input []string) []string {
log.Fatal("Error resolving etcd server spec '%s': %s", hostport, err)
}
for _, a := range addrs {
result = append(result, net.JoinHostPort(a, port))
url := proto + "://" + net.JoinHostPort(a, port)
result = append(result, url)
}
}
return result
}
func NewEtcdClient() *etcd.Client {
machines := resolveAll(strings.Split(*etcdMachines, ","))
proto := "http"
if *etcdCertFile != "" && *etcdKeyFile != "" {
proto = "https"
}
machines := resolveAll(strings.Split(*etcdMachines, ","), proto)
if len(machines) == 0 {
log.Fatal("No etcd servers specified!")
}
log.Printf("etcd servers: %+v", machines)
c := etcd.NewClient(machines)
if *etcdCertFile != "" && *etcdKeyFile != "" {
if proto == "https" {
c.SetScheme(etcd.HTTPS)
if _, err := c.SetCertAndKey(loadFile(*etcdCertFile), loadFile(*etcdKeyFile)); err != nil {
log.Fatal("Error setting up SSL for etcd client: %s", err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment