forked from gliderlabs/registrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskydns2.go
51 lines (41 loc) · 1.27 KB
/
skydns2.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
package main
import (
"net/url"
"strconv"
"strings"
"github.com/coreos/go-etcd/etcd"
)
type Skydns2Registry struct {
client *etcd.Client
path string
}
func NewSkydns2Registry(uri *url.URL) ServiceRegistry {
urls := make([]string, 0)
if uri.Host != "" {
urls = append(urls, "http://"+uri.Host)
}
return &Skydns2Registry{client: etcd.NewClient(urls), path: domainPath(uri.Path[1:])}
}
func (r *Skydns2Registry) Register(service *Service) error {
port := strconv.Itoa(service.Port)
record := `{"host":"` + service.IP + `","port":` + port + `}`
_, err := r.client.Set(r.servicePath(service), record, uint64(service.TTL))
return err
}
func (r *Skydns2Registry) Deregister(service *Service) error {
_, err := r.client.Delete(r.servicePath(service), false)
return err
}
func (r *Skydns2Registry) Refresh(service *Service) error {
return r.Register(service)
}
func (r *Skydns2Registry) servicePath(service *Service) string {
return r.path + "/" + service.Name + "/" + strings.Replace(service.ID, ":", "-", -1)
}
func domainPath(domain string) string {
components := strings.Split(domain, ".")
for i, j := 0, len(components)-1; i < j; i, j = i+1, j-1 {
components[i], components[j] = components[j], components[i]
}
return "/skydns/" + strings.Join(components, "/")
}