-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathetcd.go
More file actions
34 lines (27 loc) · 743 Bytes
/
etcd.go
File metadata and controls
34 lines (27 loc) · 743 Bytes
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
package main
import (
"fmt"
"github.com/coreos/go-etcd/etcd"
"log"
"strings"
)
type Backend struct {
Name string
Ip string
Port string
}
func GetBackends(client *etcd.Client, service, backendName string) ([]Backend, error) {
resp, err := client.Get(service, false, true)
if err != nil {
log.Println("Error when reading etcd: ", err)
return nil, err
} else {
backends := make([]Backend, len(resp.Node.Nodes))
for index, element := range resp.Node.Nodes {
key := (*element).Key // key format is: /service/IP:PORT
service := strings.Split(key[strings.LastIndex(key, "/")+1:], ":")
backends[index] = Backend{Name: fmt.Sprintf("back-%v", index), Ip: service[0], Port: service[1]}
}
return backends, nil
}
}