forked from rancher/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
external-dns.go
216 lines (187 loc) · 6.27 KB
/
external-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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"strings"
"github.com/Sirupsen/logrus"
"github.com/rancher/external-dns/config"
"github.com/rancher/external-dns/utils"
)
func UpdateProviderDnsRecords(metadataRecs map[string]utils.MetadataDnsRecord) ([]utils.MetadataDnsRecord, error) {
var updated []utils.MetadataDnsRecord
ourRecords, allRecords, err := getProviderDnsRecords()
if err != nil {
return nil, fmt.Errorf("Provider error reading dns entries: %v", err)
}
logrus.Debugf("DNS records from provider: %v", ourRecords)
removeExtraRecords(metadataRecs, ourRecords)
updated = append(updated, addMissingRecords(metadataRecs, allRecords)...)
updated = append(updated, updateExistingRecords(metadataRecs, allRecords)...)
return updated, nil
}
func addMissingRecords(metadataRecs map[string]utils.MetadataDnsRecord, providerRecs map[string]utils.DnsRecord) []utils.MetadataDnsRecord {
var toAdd []utils.MetadataDnsRecord
for key := range metadataRecs {
if _, ok := providerRecs[key]; !ok {
toAdd = append(toAdd, metadataRecs[key])
}
}
if len(toAdd) == 0 {
logrus.Debug("No DNS records to add")
} else {
logrus.Debugf("DNS records to add: %v", toAdd)
}
return updateRecords(toAdd, &Add)
}
func updateRecords(toChange []utils.MetadataDnsRecord, op *Op) []utils.MetadataDnsRecord {
var changed []utils.MetadataDnsRecord
for _, value := range toChange {
switch *op {
case Add:
logrus.Infof("Adding dns record: %v", value)
if err := provider.AddRecord(value.DnsRecord); err != nil {
logrus.Errorf("Failed to add DNS record to provider %v: %v", value, err)
} else {
changed = append(changed, value)
}
case Remove:
logrus.Infof("Removing dns record: %v", value)
if err := provider.RemoveRecord(value.DnsRecord); err != nil {
logrus.Errorf("Failed to remove DNS record from provider %v: %v", value, err)
}
case Update:
logrus.Infof("Updating dns record: %v", value)
if err := provider.UpdateRecord(value.DnsRecord); err != nil {
logrus.Errorf("Failed to update DNS record to provider %v: %v", value, err)
} else {
changed = append(changed, value)
}
}
}
return changed
}
func updateExistingRecords(metadataRecs map[string]utils.MetadataDnsRecord, providerRecs map[string]utils.DnsRecord) []utils.MetadataDnsRecord {
var toUpdate []utils.MetadataDnsRecord
for key := range metadataRecs {
if _, ok := providerRecs[key]; ok {
metadataR := make(map[string]struct{}, len(metadataRecs[key].DnsRecord.Records))
for _, s := range metadataRecs[key].DnsRecord.Records {
metadataR[s] = struct{}{}
}
providerR := make(map[string]struct{}, len(providerRecs[key].Records))
for _, s := range providerRecs[key].Records {
providerR[s] = struct{}{}
}
var update bool
if len(metadataR) != len(providerR) {
update = true
} else {
for key := range metadataR {
if _, ok := providerR[key]; !ok {
update = true
}
}
for key := range providerR {
if _, ok := metadataR[key]; !ok {
update = true
}
}
}
if update {
toUpdate = append(toUpdate, metadataRecs[key])
}
}
}
if len(toUpdate) == 0 {
logrus.Debug("No DNS records to update")
} else {
logrus.Debugf("DNS records to update: %v", toUpdate)
}
return updateRecords(toUpdate, &Update)
}
func removeExtraRecords(metadataRecs map[string]utils.MetadataDnsRecord, providerRecs map[string]utils.DnsRecord) []utils.MetadataDnsRecord {
var toRemove []utils.MetadataDnsRecord
for key := range providerRecs {
if _, ok := metadataRecs[key]; !ok {
toRemove = append(toRemove, utils.MetadataDnsRecord{"", "", providerRecs[key]})
}
}
if len(toRemove) == 0 {
logrus.Debug("No DNS records to remove")
} else {
logrus.Debugf("DNS records to remove: %v", toRemove)
}
return updateRecords(toRemove, &Remove)
}
func getProviderDnsRecords() (map[string]utils.DnsRecord, map[string]utils.DnsRecord, error) {
providerRecords, err := provider.GetRecords()
if err != nil {
return nil, nil, err
}
allRecords := make(map[string]utils.DnsRecord)
ourRecords := make(map[string]utils.DnsRecord)
if len(providerRecords) == 0 {
return ourRecords, allRecords, nil
}
stateFqdn := utils.StateFqdn(m.EnvironmentUUID, config.RootDomainName)
ourFqdns := make(map[string]struct{})
// Get the FQDNs that were created by us from the state RRSet
for _, rec := range providerRecords {
if rec.Fqdn == stateFqdn && rec.Type == "TXT" {
logrus.Debugf("FQDNs from state RRSet: %v", rec.Records)
for _, value := range rec.Records {
ourFqdns[value] = struct{}{}
}
ourRecords[stateFqdn] = rec
break
}
}
for _, rec := range providerRecords {
if rec.Type == "A" {
allRecords[rec.Fqdn] = rec
if _, ok := ourFqdns[rec.Fqdn]; ok {
ourRecords[rec.Fqdn] = rec
}
}
}
if stateRec, ok := ourRecords[stateFqdn]; ok {
allRecords[stateFqdn] = stateRec
}
return ourRecords, allRecords, nil
}
// upgrade path from previous versions of external-dns.
// checks for any pre-existing A records with names matching the legacy
// suffix and TTLs matching the value of config.TTL. If any are found,
// a state RRSet is created in the zone using the FQDNs of the records
// as values.
func EnsureUpgradeToStateRRSet() error {
allRecords, err := provider.GetRecords()
if err != nil {
return err
}
stateFqdn := utils.StateFqdn(m.EnvironmentUUID, config.RootDomainName)
logrus.Debugf("Checking for state RRSet %s", stateFqdn)
for _, rec := range allRecords {
if rec.Fqdn == stateFqdn && rec.Type == "TXT" {
logrus.Debugf("Found state RRSet with %d records", len(rec.Records))
return nil
}
}
logrus.Debug("State RRSet not found")
ourFqdns := make(map[string]struct{})
// records created by previous versions will match this suffix
joins := []string{m.EnvironmentName, config.RootDomainName}
suffix := "." + strings.ToLower(strings.Join(joins, "."))
for _, rec := range allRecords {
if rec.Type == "A" && strings.HasSuffix(rec.Fqdn, suffix) && rec.TTL == config.TTL {
ourFqdns[rec.Fqdn] = struct{}{}
}
}
if len(ourFqdns) > 0 {
logrus.Infof("Creating RRSet '%s TXT' for %d pre-existing records", stateFqdn, len(ourFqdns))
stateRec := utils.StateRecord(stateFqdn, config.TTL, ourFqdns)
if err := provider.AddRecord(stateRec); err != nil {
return fmt.Errorf("Failed to add RRSet to provider %v: %v", stateRec, err)
}
}
return nil
}