forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
192 lines (163 loc) · 3.91 KB
/
dns.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package plugin
import (
"fmt"
"net"
"os/exec"
"strings"
"sync"
"time"
"github.com/golang/glog"
kerrors "k8s.io/apimachinery/pkg/util/errors"
kexec "k8s.io/kubernetes/pkg/util/exec"
)
const (
DIG = "dig"
defaultTTL = 30 * time.Minute
)
type dnsValue struct {
// All IPv4 addresses for a given domain name
ips []net.IP
// Time-to-live value from non-authoritative/cached name server for the domain
ttl time.Duration
// Holds (last dns lookup time + ttl), tells when to refresh IPs next time
nextQueryTime time.Time
}
type DNS struct {
// Runs shell commands
execer kexec.Interface
// Protects dnsMap operations
lock sync.Mutex
// Holds dns name and its corresponding information
dnsMap map[string]dnsValue
}
func CheckDNSResolver() error {
if _, err := exec.LookPath(DIG); err != nil {
return fmt.Errorf("%s is not installed", DIG)
}
return nil
}
func NewDNS(execer kexec.Interface) *DNS {
return &DNS{
execer: execer,
dnsMap: map[string]dnsValue{},
}
}
func (d *DNS) Size() int {
d.lock.Lock()
defer d.lock.Unlock()
return len(d.dnsMap)
}
func (d *DNS) Get(dns string) dnsValue {
d.lock.Lock()
defer d.lock.Unlock()
data := dnsValue{}
if res, ok := d.dnsMap[dns]; ok {
data.ips = make([]net.IP, len(res.ips))
copy(data.ips, res.ips)
data.ttl = res.ttl
data.nextQueryTime = res.nextQueryTime
}
return data
}
func (d *DNS) Add(dns string) error {
d.lock.Lock()
defer d.lock.Unlock()
d.dnsMap[dns] = dnsValue{}
err, _ := d.updateOne(dns)
if err != nil {
delete(d.dnsMap, dns)
}
return err
}
func (d *DNS) Update() (error, bool) {
d.lock.Lock()
defer d.lock.Unlock()
errList := []error{}
changed := false
for dns := range d.dnsMap {
err, updated := d.updateOne(dns)
if err != nil {
errList = append(errList, err)
continue
}
if updated {
changed = true
}
}
return kerrors.NewAggregate(errList), changed
}
func (d *DNS) updateOne(dns string) (error, bool) {
// Due to lack of any go bindings for dns resolver that actually provides TTL value, we are relying on 'dig' shell command.
// Output Format:
// <domain-name>. <<ttl from authoritative ns> IN A <IP addr>
out, err := d.execer.Command(DIG, "+nocmd", "+noall", "+answer", "+ttlid", "a", dns).CombinedOutput()
if err != nil || len(out) == 0 {
return fmt.Errorf("Failed to fetch IP addr and TTL value for domain: %q, err: %v", dns, err), false
}
outStr := strings.Trim(string(out[:]), "\n")
res, ok := d.dnsMap[dns]
if !ok {
// Should not happen, all operations on dnsMap are synchronized by d.lock
return fmt.Errorf("DNS value not found in dnsMap for domain: %q", dns), false
}
var minTTL time.Duration
var ips []net.IP
for _, line := range strings.Split(outStr, "\n") {
fields := strings.Fields(line)
if len(fields) != 5 {
continue
}
ttl, err := time.ParseDuration(fmt.Sprintf("%ss", fields[1]))
if err != nil {
glog.Errorf("Invalid TTL value for domain: %q, err: %v, defaulting ttl=%s", dns, err, defaultTTL.String())
ttl = defaultTTL
}
if (minTTL.Seconds() == 0) || (minTTL.Seconds() > ttl.Seconds()) {
minTTL = ttl
}
ip := net.ParseIP(fields[4])
if ip != nil {
ips = append(ips, ip)
}
}
changed := false
if !ipsEqual(res.ips, ips) {
changed = true
}
res.ips = ips
res.ttl = minTTL
res.nextQueryTime = time.Now().Add(res.ttl)
d.dnsMap[dns] = res
return nil, changed
}
func (d *DNS) GetMinQueryTime() (time.Time, bool) {
d.lock.Lock()
defer d.lock.Unlock()
timeSet := false
var minTime time.Time
for _, res := range d.dnsMap {
if (timeSet == false) || res.nextQueryTime.Before(minTime) {
timeSet = true
minTime = res.nextQueryTime
}
}
return minTime, timeSet
}
func ipsEqual(oldips, newips []net.IP) bool {
if len(oldips) != len(newips) {
return false
}
for _, oldip := range oldips {
found := false
for _, newip := range newips {
if oldip.Equal(newip) {
found = true
break
}
}
if !found {
return false
}
}
return true
}