This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
forked from skynetservices/skydns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
62 lines (56 loc) · 1.54 KB
/
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
// Copyright (c) 2014 The SkyDNS Authors. All rights reserved.
// Use of this source code is governed by The MIT License (MIT) that can be
// found in the LICENSE file.
package main
import (
"log"
"net/url"
"strings"
"github.com/coreos/go-etcd/etcd"
)
func NewClient(machines []string) (client *etcd.Client) {
// set default if not specified in env
if len(machines) == 1 && machines[0] == "" {
machines[0] = "http://127.0.0.1:4001"
}
// override if we have a commandline flag as well
if machine != "" {
machines = strings.Split(machine, ",")
}
if strings.HasPrefix(machines[0], "https://") {
var err error
if client, err = etcd.NewTLSClient(machines, tlspem, tlskey, ""); err != nil {
log.Printf("failure to connect: %s\n", err)
}
} else {
client = etcd.NewClient(machines)
}
client.SyncCluster()
return client
}
// updateClient updates the client with the machines found in v2/_etcd/machines.
func (s *server) UpdateClient(resp *etcd.Response) {
machines := make([]string, 0, 3)
for _, m := range resp.Node.Nodes {
u, e := url.Parse(m.Value)
if e != nil {
continue
}
// etcd=bla&raft=bliep
// TODO(miek): surely there is a better way to do this
ms := strings.Split(u.String(), "&")
if len(ms) == 0 {
continue
}
if len(ms[0]) < 5 {
continue
}
machines = append(machines, ms[0][5:])
}
s.config.log.Infof("setting new etcd cluster to %v", machines)
c := NewClient(machines)
c.SyncCluster()
// This is our RCU, switch the pointer, old readers get the old
// one, new reader get the new one.
s.client = c
}