-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioner.go
178 lines (151 loc) · 4.83 KB
/
provisioner.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
// Copyright 2017 Qubit Ltd.
//
// 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 dubber
import (
"fmt"
"regexp"
"sort"
"github.com/miekg/dns"
klog "k8s.io/klog/v2"
)
// A Provisioner can manage a zone. RemoteZone should include exactly 1 SOA
// record. It is assumed that Zones do not change without that Serial Number
// being changed. In the event that records must be added/removed from the
// Zone retuned by RemoteZone, UpdateZone will be called with the relevant
// changes, plus an update to the SOA record. It is assumed that an update
// will fail if the SOA serial from the remote list does not match the
// SOA of the current remote zone state.
type Provisioner interface {
RemoteZone() (Zone, error)
UpdateZone(wanted, unwanted, desired, remote Zone) error
GroupFlags() []string
OwnerFlags() (map[string]*regexp.Regexp, error)
}
// ReconcileZone attempts to ensure that the set of records in the desired
// zone are present in the Provisioner's zone.
// - Records are grouped by Name.
// - Records from the provisioner that are not listed in the desired set
// are ignored.
// - Records of a given "Name, Type , Class" combination that are in the
// remote zone, but not in the desired zone are removed.
// - Records of a given "Name, Type , Class" combination that are in the
// desired zone, but not in the remote zone are added.
func (srv *Server) ReconcileZone(p Provisioner, desired Zone) error {
remz, err := p.RemoteZone()
if err != nil {
return err
}
var soarr *Record
for _, rr := range remz {
if rr.RR.Header().Rrtype != dns.TypeSOA {
continue
}
if soarr != nil {
return fmt.Errorf("multiple SOA records found")
}
soarr = rr
}
if soarr == nil {
return fmt.Errorf("no SOA records found")
}
// generate a new SOA record.
soa, ok := soarr.RR.(*dns.SOA)
if !ok {
return fmt.Errorf("unable to cast dns.RR %q to SOA record", soa)
}
if srv != nil {
srv.MetricDiscoveredZoneSerial.WithLabelValues(soa.Header().Name).Set(float64(soa.Serial))
}
dgroups := desired.Group(p.GroupFlags())
rgroups := remz.Group(p.GroupFlags())
var allWanted, allUnwanted Zone
for dgroupKey, dgroup := range dgroups {
rgroup, ok := rgroups[dgroupKey]
if !ok {
rgroup = make(Zone, 0)
}
sort.Sort(ByRR(dgroup))
sort.Sort(ByRR(rgroup))
dgroup = Zone(ByRR(dgroup).Dedupe())
rgroup = Zone(ByRR(rgroup).Dedupe())
wanted, _, unwanted := dgroup.Diff(rgroup)
allUnwanted = append(allUnwanted, unwanted...)
allWanted = append(allWanted, wanted...)
}
// unused remote groups
var unusedGroups []RecordSetKey
for rgroupKey := range rgroups {
_, ok := dgroups[rgroupKey]
if ok {
continue
}
// We can ignore the error here because this string
// was produced by us and so should always be valid
fs, _ := ParseRecordFlags(rgroupKey.GroupFlags)
matches := 0
// We can ignore the error here because we've already
// parsed these from config
oflags, _ := p.OwnerFlags()
if len(oflags) == 0 {
continue
}
for k, rx := range oflags {
for fk, fv := range fs {
if k != fk {
continue
}
if rx.MatchString(fv) {
matches += 1
}
}
}
if matches != 0 && matches == len(oflags) {
unusedGroups = append(unusedGroups, rgroupKey)
}
}
for _, unusedGroupKey := range unusedGroups {
rgroup := Zone(ByRR(rgroups[unusedGroupKey]).Dedupe())
allUnwanted = append(allUnwanted, rgroup...)
}
if len(allWanted) == 0 && len(allUnwanted) == 0 {
klog.V(1).Info("nothing to do")
return nil
}
newsoa := *soa
newsoa.Serial++
allWanted = append(allWanted, &Record{RR: &newsoa})
allUnwanted = append(allUnwanted, soarr)
err = p.UpdateZone(allWanted, allUnwanted, desired, remz)
if err == nil && srv != nil {
srv.MetricProvisionedZoneSerial.WithLabelValues(soa.Header().Name).Set(float64(soa.Serial))
}
return err
}
type dryRunProvisioner struct {
real Provisioner
}
func (p dryRunProvisioner) GroupFlags() []string {
return p.real.GroupFlags()
}
func (p dryRunProvisioner) OwnerFlags() (map[string]*regexp.Regexp, error) {
return p.real.OwnerFlags()
}
func (p dryRunProvisioner) RemoteZone() (Zone, error) {
return p.real.RemoteZone()
}
func (p dryRunProvisioner) UpdateZone(allWanted, allUnwanted, desired, remote Zone) error {
klog.V(0).Info("Unwanted records to be removed:\n", allUnwanted)
klog.V(0).Info("Wanted records to be added:\n", allWanted)
return nil
}