forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnscache.go
91 lines (73 loc) · 2.39 KB
/
dnscache.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
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dns
import (
"fmt"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
)
// dnsCache is a wrapper around the DNS provider, adding some caching
type dnsCache struct {
// zonesProviders is a slice of configured DNS providers
zonesProviders []dnsprovider.Zones
// mutex protects the following mutable state
mutex sync.Mutex
cachedZones []dnsprovider.Zone
cachedZonesTimestamp int64
}
func newDNSCache(providers []dnsprovider.Interface) (*dnsCache, error) {
var zonesProviders []dnsprovider.Zones
for _, provider := range providers {
zonesProvider, ok := provider.Zones()
if !ok {
return nil, fmt.Errorf("DNS provider does not support zones")
}
zonesProviders = append(zonesProviders, zonesProvider)
}
return &dnsCache{
zonesProviders: zonesProviders,
}, nil
}
// nanoTime is a stand-in until we get a monotonic clock
func nanoTime() int64 {
return time.Now().UnixNano()
}
// ListZones returns the zones, using a cached copy if validity has not yet expired.
// This is not a cheap call with a large number of hosted zones, hence the caching.
func (d *dnsCache) ListZones(validity time.Duration) ([]dnsprovider.Zone, error) {
d.mutex.Lock()
defer d.mutex.Unlock()
now := nanoTime()
if d.cachedZones != nil {
if (d.cachedZonesTimestamp + validity.Nanoseconds()) > now {
return d.cachedZones, nil
} else {
glog.V(2).Infof("querying all DNS zones (cache expired)")
}
} else {
glog.V(2).Infof("querying all DNS zones (no cached results)")
}
var allZones []dnsprovider.Zone
for _, zonesProvider := range d.zonesProviders {
zones, err := zonesProvider.List()
if err != nil {
return nil, fmt.Errorf("error querying for DNS zones: %v", err)
}
allZones = append(allZones, zones...)
}
d.cachedZones = allZones
d.cachedZonesTimestamp = now
return allZones, nil
}