-
Notifications
You must be signed in to change notification settings - Fork 126
/
etcd-cached.go
102 lines (88 loc) · 2.57 KB
/
etcd-cached.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) 2014 The SkyDNS Authors.
// Copyright (c) 2017 The containerdns Authors.
// Use of this source code is governed by The MIT License (MIT) that can be
// found in the LICENSE file.
package etcdCached
import (
"errors"
"encoding/json"
etcdv3 "github.com/coreos/etcd/clientv3"
dnsServer "github.com/tiglabs/containerdns/dns-server"
"golang.org/x/net/context"
"time"
"github.com/coreos/etcd/mvcc/mvccpb"
)
const ErrorCodeKeyNotFound = "key not found"
type Backend struct {
clientv3 etcdv3.Client
ctx context.Context
timeOut time.Duration
}
// NewBackend returns a new Backend for containerdns, backed by etcd.
func NewBackend(clientv3 etcdv3.Client, ctx context.Context,timeOut int , ttl uint32, priority uint16) *Backend {
backend := new(Backend)
backend.clientv3 = clientv3
backend.ctx = ctx
backend.timeOut = time.Duration(timeOut) * time.Second
return backend
}
func (g *Backend) Records(name string) ([]dnsServer.ServiceRecord, error) {
dnsServer.EtcdCachesLock.RLock()
defer dnsServer.EtcdCachesLock.RUnlock()
if val, ok := dnsServer.EtcdRecordCaches[name]; ok {
return val, nil
} else {
str := ErrorCodeKeyNotFound + name
return nil, errors.New(str)
}
}
func (g *Backend) Get(name string) ([]dnsServer.ServiceRecord, int64, error) {
path := dnsServer.DnsPath(name)
ctx, cancel := context.WithTimeout(g.ctx, g.timeOut )
defer cancel()
r, err := g.clientv3.Get(ctx, path, etcdv3.WithPrefix())
if err != nil {
return nil, 0, err
}
msgs, err := LoopNodes(r.Kvs)
return msgs, r.Header.Revision, nil
}
func (g *Backend) GetRaw(path string) (*etcdv3.GetResponse, error) {
ctx, cancel := context.WithTimeout(g.ctx, g.timeOut)
defer cancel()
return g.clientv3.Get(ctx, path, etcdv3.WithPrefix())
}
func (g *Backend) ReverseRecord(name string) (*dnsServer.ServiceRecord, error) {
return nil, errors.New(" cahce not support PTR")
}
func LoopNodes(kv []*mvccpb.KeyValue) (sx []dnsServer.ServiceRecord, err error) {
for _, item := range kv {
serv := new(dnsServer.ServiceRecord)
if err := json.Unmarshal(item.Value, serv); err != nil {
return nil, err
}
serv.Key = string(item.Key)
serv.DnsTtl = calculateTtl(item, serv)
if serv.DnsPriority == 0 {
serv.DnsPriority = int(10)
}
sx = append(sx, *serv)
}
return sx, nil
}
func calculateTtl(kv *mvccpb.KeyValue, serv *dnsServer.ServiceRecord) uint32 {
etcdTtl := uint32(kv.Lease)
if etcdTtl == 0 && serv.DnsTtl == 0 {
return 10
}
if etcdTtl == 0 {
return serv.DnsTtl
}
if serv.DnsTtl == 0 {
return etcdTtl
}
if etcdTtl < serv.DnsTtl {
return etcdTtl
}
return serv.DnsTtl
}